logo

utils-std

Collection of commonly available Unix tools
commit: 5e5ade7c0ae60211fde92f2ed4aad2e5461b5441
parent 5e09cd798fa4bb05eeb642a1788b0c678e3c0363
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date:   Thu, 28 Mar 2024 18:47:17 +0100

cmd/chmod: assert(errno == 0)

Diffstat:

Mcmd/chmod.c17++++++++++++++++-
1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/cmd/chmod.c b/cmd/chmod.c @@ -11,6 +11,7 @@ #include "../lib/mode.h" +#include <assert.h> #include <dirent.h> // fdopendir, readdir, closedir #include <errno.h> #include <fcntl.h> // AT_FDCWD @@ -29,9 +30,11 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive) struct stat stats; int err = 0; + assert(errno == 0); if(fstatat(fd, name, &stats, AT_SYMLINK_NOFOLLOW) != 0) { fprintf(stderr, "chmod: Failed getting status for '%s': %s\n", acc_path, strerror(errno)); + errno = 0; return 1; } @@ -48,6 +51,7 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive) if(mode != stats.st_mode) { + assert(errno == 0); if(fchmodat(fd, name, mode, 0) != 0) { fprintf(stderr, @@ -55,6 +59,7 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive) mode, acc_path, strerror(errno)); + errno = 0; return 1; } @@ -79,36 +84,43 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive) if(recursive && S_ISDIR(stats.st_mode)) { + assert(errno == 0); int dir = openat(fd, name, O_RDONLY | O_DIRECTORY | O_CLOEXEC); if(dir == -1) { fprintf(stderr, "chmod: Couldn't open '%s' as directory: %s\n", acc_path, strerror(errno)); + errno = 0; return 1; } + assert(errno == 0); DIR *dirp = fdopendir(dir); if(dirp == NULL) { fprintf( stderr, "chmod: Couldn't get DIR entry for opened '%s': %s\n", acc_path, strerror(errno)); + errno = 0; return 1; } while(true) { + assert(errno == 0); struct dirent *dp = readdir(dirp); if(dp == NULL) { if(errno == 0) break; fprintf(stderr, "chmod: Failed reading directory '%s': %s\n", acc_path, strerror(errno)); - closedir(dirp); + closedir(dirp); // FIXME: unhandled error + errno = 0; return 1; } if(strcmp(dp->d_name, ".") == 0) continue; if(strcmp(dp->d_name, "..") == 0) continue; + assert(errno == 0); char new_path[PATH_MAX] = ""; if(snprintf(new_path, PATH_MAX, "%s/%s", acc_path, dp->d_name) < 0) { @@ -118,6 +130,7 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive) acc_path, strerror(errno)); err++; + errno = 0; continue; } @@ -127,12 +140,14 @@ do_fchmodat(int fd, char *mode_arg, char *name, char *acc_path, bool recursive) } // fdopendir allocates memory for DIR, needs closedir + assert(errno == 0); if(closedir(dirp) != 0) { fprintf(stderr, "chmod: Deallocating directory entry for '%s' failed: %s\n", acc_path, strerror(errno)); + errno = 0; return 1; } }