On Mon, Jun 22, 2020 at 2:15 AM Arvind Raghavan <raghavan.arvind@xxxxxxxxx> wrote: > > Refactoring changes needed for recursive traversal and single file > input. Makes fstat and readlink use the 'at' alternatives, and creates a > helper function for opening files. > > Signed-off-by: Arvind Raghavan <raghavan.arvind@xxxxxxxxx> > Signed-off-by: Jayashree Mohan <jaya@xxxxxxxxxxxxx> > Signed-off-by: Vijay Chidambaram <vijay@xxxxxxxxxxxxx> Reviewed-by: Amir Goldstein <amir73il@xxxxxxxxx> > --- > src/fssum.c | 22 ++++++++++++---------- > 1 file changed, 12 insertions(+), 10 deletions(-) > > diff --git a/src/fssum.c b/src/fssum.c > index 30f456c2..135dd60f 100644 > --- a/src/fssum.c > +++ b/src/fssum.c > @@ -503,6 +503,13 @@ malformed: > excess_file(fn); > } > > +int open_one(int dirfd, const char *name) > +{ > + if (!name || !*name) > + return dup(dirfd); > + return openat(dirfd, name, 0); > +} > + > void > sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) > { > @@ -563,12 +570,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) > goto next; > } > > - ret = fchdir(dirfd); > - if (ret == -1) { > - perror("fchdir"); > - exit(-1); > - } > - ret = lstat64(namelist[i], &st); > + ret = fstatat64(dirfd, namelist[i], &st, AT_SYMLINK_NOFOLLOW); > if (ret) { > fprintf(stderr, "stat failed for %s/%s: %s\n", > path_prefix, path, strerror(errno)); > @@ -597,7 +599,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) > sum_add_time(&meta, st.st_ctime); > if (flags[FLAG_XATTRS] && > (S_ISDIR(st.st_mode) || S_ISREG(st.st_mode))) { > - fd = openat(dirfd, namelist[i], 0); > + fd = open_one(dirfd, namelist[i]); > if (fd == -1 && flags[FLAG_OPEN_ERROR]) { > sum_add_u64(&meta, errno); > } else if (fd == -1) { > @@ -618,7 +620,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) > } > } > if (S_ISDIR(st.st_mode)) { > - fd = openat(dirfd, namelist[i], 0); > + fd = open_one(dirfd, namelist[i]); > if (fd == -1 && flags[FLAG_OPEN_ERROR]) { > sum_add_u64(&meta, errno); > } else if (fd == -1) { > @@ -635,7 +637,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) > if (verbose) > fprintf(stderr, "file %s\n", > namelist[i]); > - fd = openat(dirfd, namelist[i], 0); > + fd = open_one(dirfd, namelist[i]); > if (fd == -1 && flags[FLAG_OPEN_ERROR]) { > sum_add_u64(&meta, errno); > } else if (fd == -1) { > @@ -659,7 +661,7 @@ sum(int dirfd, int level, sum_t *dircs, char *path_prefix, char *path_in) > } > } > } else if (S_ISLNK(st.st_mode)) { > - ret = readlink(namelist[i], buf, sizeof(buf)); > + ret = readlinkat(dirfd, namelist[i], buf, sizeof(buf)); > if (ret == -1) { > perror("readlink"); > exit(-1); > -- > 2.20.1 >