diff options
Diffstat (limited to 'man2/open_by_handle_at.2')
-rw-r--r-- | man2/open_by_handle_at.2 | 88 |
1 files changed, 44 insertions, 44 deletions
diff --git a/man2/open_by_handle_at.2 b/man2/open_by_handle_at.2 index bbdafb76d..4061faea9 100644 --- a/man2/open_by_handle_at.2 +++ b/man2/open_by_handle_at.2 @@ -533,31 +533,31 @@ open_by_handle_at: Stale NFS file handle #include <fcntl.h> #include <stdio.h> #include <stdlib.h> - +\& int main(int argc, char *argv[]) { int mount_id, fhsize, flags, dirfd; char *pathname; struct file_handle *fhp; - +\& if (argc != 2) { fprintf(stderr, "Usage: %s pathname\en", argv[0]); exit(EXIT_FAILURE); } - +\& pathname = argv[1]; - +\& /* Allocate file_handle structure. */ - +\& fhsize = sizeof(*fhp); fhp = malloc(fhsize); if (fhp == NULL) err(EXIT_FAILURE, "malloc"); - +\& /* Make an initial call to name_to_handle_at() to discover the size required for file handle. */ - +\& dirfd = AT_FDCWD; /* For name_to_handle_at() calls */ flags = 0; /* For name_to_handle_at() calls */ fhp\->handle_bytes = 0; @@ -568,28 +568,28 @@ main(int argc, char *argv[]) fprintf(stderr, "Unexpected result from name_to_handle_at()\en"); exit(EXIT_FAILURE); } - +\& /* Reallocate file_handle structure with correct size. */ - +\& fhsize = sizeof(*fhp) + fhp\->handle_bytes; fhp = realloc(fhp, fhsize); /* Copies fhp\->handle_bytes */ if (fhp == NULL) err(EXIT_FAILURE, "realloc"); - +\& /* Get file handle from pathname supplied on command line. */ - +\& if (name_to_handle_at(dirfd, pathname, fhp, &mount_id, flags) == \-1) err(EXIT_FAILURE, "name_to_handle_at"); - +\& /* Write mount ID, file handle size, and file handle to stdout, for later reuse by t_open_by_handle_at.c. */ - +\& printf("%d\en", mount_id); printf("%u %d ", fhp\->handle_bytes, fhp\->handle_type); for (size_t j = 0; j < fhp\->handle_bytes; j++) printf(" %02x", fhp\->f_handle[j]); printf("\en"); - +\& exit(EXIT_SUCCESS); } .EE @@ -606,13 +606,13 @@ main(int argc, char *argv[]) #include <stdlib.h> #include <string.h> #include <unistd.h> - +\& /* Scan /proc/self/mountinfo to find the line whose mount ID matches \[aq]mount_id\[aq]. (An easier way to do this is to install and use the \[aq]libmount\[aq] library provided by the \[aq]util\-linux\[aq] project.) Open the corresponding mount path and return the resulting file descriptor. */ - +\& static int open_mount_path_by_id(int mount_id) { @@ -622,40 +622,40 @@ open_mount_path_by_id(int mount_id) FILE *fp; size_t lsize; ssize_t nread; - +\& fp = fopen("/proc/self/mountinfo", "r"); if (fp == NULL) err(EXIT_FAILURE, "fopen"); - +\& found = 0; linep = NULL; while (!found) { nread = getline(&linep, &lsize, fp); if (nread == \-1) break; - +\& nread = sscanf(linep, "%d %*d %*s %*s %s", &mi_mount_id, mount_path); if (nread != 2) { fprintf(stderr, "Bad sscanf()\en"); exit(EXIT_FAILURE); } - +\& if (mi_mount_id == mount_id) found = 1; } free(linep); - +\& fclose(fp); - +\& if (!found) { fprintf(stderr, "Could not find mount point\en"); exit(EXIT_FAILURE); } - +\& return open(mount_path, O_RDONLY); } - +\& int main(int argc, char *argv[]) { @@ -666,69 +666,69 @@ main(int argc, char *argv[]) char *nextp; ssize_t nread; struct file_handle *fhp; - +\& if ((argc > 1 && strcmp(argv[1], "\-\-help") == 0) || argc > 2) { fprintf(stderr, "Usage: %s [mount\-path]\en", argv[0]); exit(EXIT_FAILURE); } - +\& /* Standard input contains mount ID and file handle information: - +\& Line 1: <mount_id> Line 2: <handle_bytes> <handle_type> <bytes of handle in hex> */ - +\& if (fgets(line1, sizeof(line1), stdin) == NULL || fgets(line2, sizeof(line2), stdin) == NULL) { fprintf(stderr, "Missing mount_id / file handle\en"); exit(EXIT_FAILURE); } - +\& mount_id = atoi(line1); - +\& handle_bytes = strtoul(line2, &nextp, 0); - +\& /* Given handle_bytes, we can now allocate file_handle structure. */ - +\& fhp = malloc(sizeof(*fhp) + handle_bytes); if (fhp == NULL) err(EXIT_FAILURE, "malloc"); - +\& fhp\->handle_bytes = handle_bytes; - +\& fhp\->handle_type = strtoul(nextp, &nextp, 0); - +\& for (size_t j = 0; j < fhp\->handle_bytes; j++) fhp\->f_handle[j] = strtoul(nextp, &nextp, 16); - +\& /* Obtain file descriptor for mount point, either by opening the pathname specified on the command line, or by scanning /proc/self/mounts to find a mount that matches the \[aq]mount_id\[aq] that we received from stdin. */ - +\& if (argc > 1) mount_fd = open(argv[1], O_RDONLY); else mount_fd = open_mount_path_by_id(mount_id); - +\& if (mount_fd == \-1) err(EXIT_FAILURE, "opening mount fd"); - +\& /* Open file using handle and mount point. */ - +\& fd = open_by_handle_at(mount_fd, fhp, O_RDONLY); if (fd == \-1) err(EXIT_FAILURE, "open_by_handle_at"); - +\& /* Try reading a few bytes from the file. */ - +\& nread = read(fd, buf, sizeof(buf)); if (nread == \-1) err(EXIT_FAILURE, "read"); - +\& printf("Read %zd bytes\en", nread); - +\& exit(EXIT_SUCCESS); } .EE |