/* Given a path, find the corresponding Lustre fsname */
int llapi_search_fsname(const char *pathname, char *fsname)
{
- char *path = (char*)pathname, buf[PATH_MAX + 1];
+ char *path;
+ int rc;
- if (pathname[0] != '/') { /* Need a absolute path */
- memset(buf, '\0', sizeof(buf));
- if (realpath(pathname, buf) == NULL) {
- llapi_err(LLAPI_MSG_ERROR, "pathname '%s' cannot expand",
- pathname);
- return -EINVAL;
+ path = realpath(pathname, NULL);
+ if (path == NULL) {
+ char buf[PATH_MAX + 1], *ptr;
+
+ buf[0] = 0;
+ if (pathname[0] != '/') {
+ /* Need an absolute path, but realpath() only works for
+ * pathnames that actually exist. We go through the
+ * extra hurdle of dirname(getcwd() + pathname) in
+ * case the relative pathname contains ".." in it. */
+ if (getcwd(buf, sizeof(buf) - 1) == NULL)
+ return -errno;
+ strcat(buf, "/");
+ }
+ strncat(buf, pathname, sizeof(buf) - strlen(buf));
+ path = realpath(buf, NULL);
+ if (path == NULL) {
+ ptr = strrchr(buf, '/');
+ if (ptr == NULL)
+ return -ENOENT;
+ *ptr = '\0';
+ path = realpath(buf, NULL);
+ if (path == NULL) {
+ llapi_err(LLAPI_MSG_ERROR,
+ "pathname '%s' cannot expand",
+ pathname);
+ return -errno;
+ }
}
- path = buf;
}
- return get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL,
- path, -1);
+ rc = get_root_path(WANT_FSNAME | WANT_ERROR, fsname, NULL, path, -1);
+ free(path);
+ return rc;
}
/* return the first file matching this pattern */
*mdtidxp = index;
rc = ioctl(fd, opc, data);
+ if (rc == -1)
+ rc = -errno;
+ else
+ rc = 0;
if (rc && want_error)
llapi_err(LLAPI_MSG_ERROR, "ioctl %d err %d", opc, rc);
/* Take path or fsname */
rc = root_ioctl(device, OBD_IOC_FID2PATH, gf, NULL, 0);
if (rc) {
- llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
+ if (rc != -ENOENT)
+ llapi_err(LLAPI_MSG_ERROR, "ioctl err %d", rc);
} else {
memcpy(buf, gf->gf_path, gf->gf_pathlen);
*recno = gf->gf_recno;