- char *dname, *rpath = NULL;
- char path[PATH_MAX], cwd[PATH_MAX];
- FILE *fp;
- struct mntent *mnt;
- int i;
-
- if (argc < 2)
- return -1;
-
- /* FIXME the premise is there is no sub-mounted filesystems under this
- * mounted lustre tree. */
- strncpy(fname, argv[argc - 1], PATH_MAX);
-
- /* get path prefix */
- dname = dirname(fname);
-
- /* try to resolve the pathname into relative to the root of the mounted
- * lustre filesystem.
- */
- if (getcwd(cwd, sizeof(cwd)) == NULL) {
- fprintf(stderr, "getcwd %s failed: %s\n", cwd, strerror(errno));
- return -1;
- }
-
- if (chdir(dname) == -1) {
- fprintf(stderr, "chdir to %s failed: %s\n",
- dname, strerror(errno));
- return -1;
- }
-
- if (getcwd(path, sizeof(path)) == NULL) {
- fprintf(stderr, "getcwd %s: %s\n", path, strerror(errno));
- return -1;
- }
-
- if (chdir(cwd) == -1) {
- fprintf(stderr, "chdir back to %s: %s\n",
- cwd, strerror(errno));
- return -1;
- }
-
- strncat(path, "/", PATH_MAX);
- strncpy(fname, argv[argc - 1], PATH_MAX);
- strncat(path, basename(fname), PATH_MAX);
-
- fp = setmntent(MOUNTED, "r");
- if (fp == NULL) {
- fprintf(stderr, "setmntent %s failed: %s\n",
- MOUNTED, strerror(errno));
- return -1;
- }
-
- while (1) {
- mnt = getmntent(fp);
- if (!mnt)
- break;
-
- if (!llapi_is_lustre_mnttype(mnt->mnt_type))
- continue;
-
- if (!strncmp(mnt->mnt_dir, path, strlen(mnt->mnt_dir))) {
- rpath = path + strlen(mnt->mnt_dir);
- break;
- }
- }
- endmntent(fp);
-
- /* remove char '/' from rpath to be a relative path */
- while (rpath && *rpath == '/') rpath++;
-
- if (!rpath) {
- fprintf(stderr,
- "%s: file %s doesn't belong to a lustre file system!\n",
- argv[0], argv[argc - 1]);
- return -1;
- }
-
- for (i = 0; i < argc - 1; i++) {
- strncat(cmd, argv[i], PATH_MAX);
- strncat(cmd, " ", PATH_MAX);
- }
- strncat(cmd, *rpath ? rpath : ".", PATH_MAX);
- strncpy(fname, argv[argc - 1], sizeof(fname));
-
- return 0;