1 /* vi: set sw=4 ts=4: */
3 * Mini chown implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 /* BB_AUDIT SUSv3 defects - none? */
11 /* BB_AUDIT GNU defects - unsupported long options. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
16 #define OPT_STR ("Rh" USE_DESKTOP("vcfLHP"))
18 #define OPT_RECURSE (option_mask32 & 1)
19 #define OPT_NODEREF (option_mask32 & 2)
20 #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0))
21 #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0))
22 #define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
24 * -L traverse every symbolic link to a directory encountered
25 * -H if a command line argument is a symbolic link to a directory, traverse it
26 * -P do not traverse any symbolic links (default)
27 * We do not conform to the following:
28 * "Specifying more than one of -H, -L, and -P is not an error.
29 * The last option specified shall determine the behavior of the utility." */
31 #define BIT_TRAVERSE 0x20
32 #define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
34 #define BIT_TRAVERSE_TOP (0x20|0x40)
35 #define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
37 typedef int (*chown_fptr)(const char *, uid_t, gid_t);
39 static struct bb_uidgid_t ugid = { -1, -1 };
41 static int fileAction(const char *fileName, struct stat *statbuf,
44 uid_t u = (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid;
45 gid_t g = (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid;
47 if (!((chown_fptr)cf)(fileName, u, g)) {
49 || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
51 printf("changed ownership of '%s' to %u:%u\n",
52 fileName, (unsigned)u, (unsigned)g);
57 bb_perror_msg("%s", fileName); /* A filename can have % in it... */
61 int chown_main(int argc, char **argv);
62 int chown_main(int argc, char **argv)
64 int retval = EXIT_SUCCESS;
65 chown_fptr chown_func;
67 opt_complementary = "-2";
68 getopt32(argc, argv, OPT_STR);
71 /* This matches coreutils behavior (almost - see below) */
74 /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
75 USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
80 parse_chown_usergroup_or_die(&ugid, argv[0]);
82 /* Ok, ready to do the deed now */
87 if (OPT_TRAVERSE_TOP) {
88 /* resolves symlink (even recursive) */
89 arg = xmalloc_realpath(arg);
94 if (!recursive_action(arg,
95 (OPT_RECURSE ? action_recurse : 0 | /* recurse */
96 OPT_TRAVERSE ? action_followLinks : 0),/* follow links if -L */
97 fileAction, /* file action */
98 fileAction, /* dir action */
99 chown_func, /* user data */
102 retval = EXIT_FAILURE;
105 if (OPT_TRAVERSE_TOP)
113 Testcase. Run in empty directory.
116 t1="/tmp/busybox chown"
129 ln -s ../up dir/linkup
130 ln -s ../dir2 dir/linkupdir2
139 (cd test1; ls -lnR) >out1
140 (cd test2; ls -lnR) >out2
141 echo "chown $1" >out.diff
142 if ! diff -u out1 out2 >>out.diff; then exit 1; fi
149 tst "$1 1:1 linkfile"
151 echo "If script produced 'out.diff' file, then at least one testcase failed"
152 # These match coreutils 6.8:
161 # Below: with "chown linkdir" coreutils 6.8 will chown linkdir _target_,
162 # we lchown _the link_. I believe we are "more correct".