123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232 |
- #include "libbb.h"
- #define OPT_STR "Rh" IF_DESKTOP("vcfLHP")
- #define BIT_RECURSE 1
- #define OPT_RECURSE (opt & 1)
- #define OPT_NODEREF (opt & 2)
- #define OPT_VERBOSE (IF_DESKTOP(opt & 0x04) IF_NOT_DESKTOP(0))
- #define OPT_CHANGED (IF_DESKTOP(opt & 0x08) IF_NOT_DESKTOP(0))
- #define OPT_QUIET (IF_DESKTOP(opt & 0x10) IF_NOT_DESKTOP(0))
- #define BIT_TRAVERSE 0x20
- #define OPT_TRAVERSE (IF_DESKTOP(opt & BIT_TRAVERSE) IF_NOT_DESKTOP(0))
- #define BIT_TRAVERSE_TOP (0x20|0x40)
- #define OPT_TRAVERSE_TOP (IF_DESKTOP(opt & BIT_TRAVERSE_TOP) IF_NOT_DESKTOP(0))
- #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
- static const char chown_longopts[] ALIGN1 =
- "recursive\0" No_argument "R"
- "dereference\0" No_argument "\xff"
- "no-dereference\0" No_argument "h"
- # if ENABLE_DESKTOP
- "changes\0" No_argument "c"
- "silent\0" No_argument "f"
- "quiet\0" No_argument "f"
- "verbose\0" No_argument "v"
- # endif
- ;
- #endif
- typedef int (*chown_fptr)(const char *, uid_t, gid_t);
- struct param_t {
- struct bb_uidgid_t ugid;
- chown_fptr chown_func;
- };
- static int FAST_FUNC fileAction(const char *fileName, struct stat *statbuf,
- void *vparam, int depth UNUSED_PARAM)
- {
- #define param (*(struct param_t*)vparam)
- #define opt option_mask32
- uid_t u = (param.ugid.uid == (uid_t)-1L) ? statbuf->st_uid : param.ugid.uid;
- gid_t g = (param.ugid.gid == (gid_t)-1L) ? statbuf->st_gid : param.ugid.gid;
- if (param.chown_func(fileName, u, g) == 0) {
- if (OPT_VERBOSE
- || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
- ) {
- printf("changed ownership of '%s' to %u:%u\n",
- fileName, (unsigned)u, (unsigned)g);
- }
- return TRUE;
- }
- if (!OPT_QUIET)
- bb_simple_perror_msg(fileName);
- return FALSE;
- #undef opt
- #undef param
- }
- int chown_main(int argc UNUSED_PARAM, char **argv)
- {
- int retval = EXIT_SUCCESS;
- int opt, flags;
- struct param_t param;
- #if ENABLE_FEATURE_CHOWN_LONG_OPTIONS
- opt = getopt32long(argv, "^" OPT_STR "\0" "-2", chown_longopts);
- #else
- opt = getopt32(argv, "^" OPT_STR "\0" "-2");
- #endif
- argv += optind;
-
- param.chown_func = chown;
- if (OPT_NODEREF
-
- IF_DESKTOP( || (opt & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
- ) {
- param.chown_func = lchown;
- }
- flags = ACTION_DEPTHFIRST;
- if (OPT_RECURSE)
- flags |= ACTION_RECURSE;
- if (OPT_TRAVERSE_TOP)
- flags |= ACTION_FOLLOWLINKS_L0;
- if (OPT_TRAVERSE)
- flags |= ACTION_FOLLOWLINKS;
- parse_chown_usergroup_or_die(¶m.ugid, argv[0]);
-
- while (*++argv) {
- if (!recursive_action(*argv,
- flags,
- fileAction,
- fileAction,
- ¶m,
- 0)
- ) {
- retval = EXIT_FAILURE;
- }
- }
- return retval;
- }
|