From 51b4c92f80dc232b9d34985f99a4479393644433 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Fri, 27 Oct 2006 16:07:20 +0000 Subject: chown: add -vcf support if CONFIG_DESKTOP chmod: stop following symlinks --- coreutils/chmod.c | 31 ++++++++++++++++++++++++----- coreutils/chown.c | 58 ++++++++++++++++++++++++++++++++++++------------------- 2 files changed, 64 insertions(+), 25 deletions(-) (limited to 'coreutils') diff --git a/coreutils/chmod.c b/coreutils/chmod.c index 18334b8bb..c4f8fa0b2 100644 --- a/coreutils/chmod.c +++ b/coreutils/chmod.c @@ -22,9 +22,23 @@ #define OPT_QUIET (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0)) #define OPT_STR ("-R" USE_DESKTOP("vcf")) +/* TODO: + * chmod never changes the permissions of symbolic links; the chmod + * system call cannot change their permissions. This is not a problem + * since the permissions of symbolic links are never used. + * However, for each symbolic link listed on the command line, chmod changes + * the permissions of the pointed-to file. In contrast, chmod ignores + * symbolic links encountered during recursive directory traversals. + */ + static int fileAction(const char *fileName, struct stat *statbuf, void* junk) { mode_t newmode = statbuf->st_mode; + + // TODO: match GNU behavior: + // if (depth > 0 && S_ISLNK(statbuf->st_mode)) return TRUE; + // if (depth == 0) follow link + if (!bb_parse_mode((char *)junk, &newmode)) bb_error_msg_and_die("invalid mode: %s", (char *)junk); @@ -33,7 +47,7 @@ static int fileAction(const char *fileName, struct stat *statbuf, void* junk) || (OPT_CHANGED && statbuf->st_mode != newmode) ) { printf("mode of '%s' changed to %04o (%s)\n", fileName, - newmode & 7777, bb_mode_string(newmode)+1); + newmode & 07777, bb_mode_string(newmode)+1); } return TRUE; } @@ -48,10 +62,11 @@ int chmod_main(int argc, char **argv) char *arg, **argp; char *smode; - /* Convert first encountered -r into a-r, etc */ + /* Convert first encountered -r into a-r, -w into a-w etc */ argp = argv + 1; while ((arg = *argp)) { - /* Protect against mishandling e.g. "chmod 644 -r" */ + /* Mode spec must be the first arg (sans -R etc) */ + /* (protect against mishandling e.g. "chmod 644 -r") */ if (arg[0] != '-') break; /* An option. Not a -- or valid option? */ @@ -72,8 +87,14 @@ int chmod_main(int argc, char **argv) /* Ok, ready to do the deed now */ do { - if (!recursive_action(*argv, OPT_RECURSE, TRUE, FALSE, - fileAction, fileAction, smode)) { + if (!recursive_action(*argv, + OPT_RECURSE, // recurse + FALSE, // follow links: GNU doesn't + FALSE, // depth first + fileAction, // file action + fileAction, // dir action + smode) // user data + ) { retval = EXIT_FAILURE; } } while (*++argv); diff --git a/coreutils/chown.c b/coreutils/chown.c index 42f973f32..bef89ce86 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c @@ -7,14 +7,10 @@ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -/* BB_AUDIT SUSv3 defects - unsupported options -h, -H, -L, and -P. */ -/* BB_AUDIT GNU defects - unsupported options -h, -c, -f, -v, and long options. */ -/* BB_AUDIT Note: gnu chown does not support -H, -L, or -P. */ +/* BB_AUDIT SUSv3 defects - unsupported options -H, -L, and -P. */ +/* BB_AUDIT GNU defects - unsupported long options. */ /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */ -#include -#include -#include #include "busybox.h" static uid_t uid = -1; @@ -22,39 +18,55 @@ static gid_t gid = -1; static int (*chown_func)(const char *, uid_t, gid_t) = chown; +#define OPT_RECURSE (option_mask32 & 1) +#define OPT_NODEREF (option_mask32 & 2) +#define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0)) +#define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0)) +#define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0)) +#define OPT_STR ("Rh" USE_DESKTOP("vcf")) + +/* TODO: + * -H if a command line argument is a symbolic link to a directory, traverse it + * -L traverse every symbolic link to a directory encountered + * -P do not traverse any symbolic links (default) + */ + static int fileAction(const char *fileName, struct stat *statbuf, void ATTRIBUTE_UNUSED *junk) { + // TODO: -H/-L/-P + // if (depth ... && S_ISLNK(statbuf->st_mode)) .... + if (!chown_func(fileName, (uid == (uid_t)-1) ? statbuf->st_uid : uid, (gid == (gid_t)-1) ? statbuf->st_gid : gid)) { + if (OPT_VERBOSE + || (OPT_CHANGED && (statbuf->st_uid != uid || statbuf->st_gid != gid)) + ) { + printf("changed ownership of '%s' to %u:%u\n", fileName, uid, gid); + } return TRUE; } - bb_perror_msg("%s", fileName); /* A filename can have % in it... */ + if (!OPT_QUIET) + bb_perror_msg("%s", fileName); /* A filename can have % in it... */ return FALSE; } -#define FLAG_R 1 -#define FLAG_h 2 - int chown_main(int argc, char **argv) { - int flags; int retval = EXIT_SUCCESS; char *groupName; - flags = getopt32(argc, argv, "Rh"); + opt_complementary = "-2"; + getopt32(argc, argv, OPT_STR); - if (flags & FLAG_h) chown_func = lchown; - - if (argc - optind < 2) { - bb_show_usage(); - } + if (OPT_NODEREF) chown_func = lchown; argv += optind; /* First, check if there is a group name here */ - if ((groupName = strchr(*argv, '.')) == NULL) { + groupName = strchr(*argv, '.'); + if (!groupName) { groupName = strchr(*argv, ':'); } @@ -68,8 +80,14 @@ int chown_main(int argc, char **argv) /* Ok, ready to do the deed now */ do { - if (! recursive_action (*argv, (flags & FLAG_R), FALSE, FALSE, - fileAction, fileAction, NULL)) { + if (!recursive_action(*argv, + OPT_RECURSE, // recurse + FALSE, // follow links: TODO: -H/-L/-P + FALSE, // depth first + fileAction, // file action + fileAction, // dir action + NULL) // user data + ) { retval = EXIT_FAILURE; } } while (*++argv); -- cgit v1.2.3