Allow regular links and symlinks to be passed as input to fssum. Signed-off-by: Arvind Raghavan <raghavan.arvind@xxxxxxxxx> Signed-off-by: Jayashree Mohan <jaya@xxxxxxxxxxxxx> Signed-off-by: Vijay Chidambaram <vijay@xxxxxxxxxxxxx> --- src/fssum.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/fssum.c b/src/fssum.c index ece0f556..2d1624ca 100644 --- a/src/fssum.c +++ b/src/fssum.c @@ -29,6 +29,7 @@ #include <inttypes.h> #include <assert.h> #include <endian.h> +#include <libgen.h> #define CS_SIZE 16 #define CHUNKS 128 @@ -884,8 +885,40 @@ main(int argc, char *argv[]) if (gen_manifest) fprintf(out_fp, "Flags: %s\n", flagstring); + struct stat64 path_st; + if (fstat64(fd, &path_st)) { + perror("fstat"); + exit(-1); + } + sum_init(&cs); - sum(fd, 1, &cs, path, ""); + + if (S_ISDIR(path_st.st_mode)) { + sum(fd, 1, &cs, path, ""); + } else if (S_ISREG(path_st.st_mode) || S_ISLNK(path_st.st_mode)) { + // Copy because dirname may modify path + char* path_copy = alloc(strlen(path)); + strcpy(path_copy, path); + + char* dir_path = dirname(path); + char* name = basename(path_copy); + + int dirfd = open(dir_path, O_RDONLY); + if (fd == -1) { + fprintf(stderr, "failed to open %s: %s\n", dir_path, + strerror(errno)); + exit(-1); + } + + sum_one(dirfd, 1, &cs, dir_path, "", name); + + free(path_copy); + close(dirfd); + } else { + fprintf(stderr, "path must be file or dir: %s", path); + exit(-1); + } + sum_fini(&cs); close(fd); -- 2.20.1