e64a39c3e8ce6f9dc1c8a8610cd817fbfc14ecc2
[oweals/busybox.git] / coreutils / chown.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini chown implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 /* BB_AUDIT SUSv3 defects - none? */
11 /* BB_AUDIT GNU defects - unsupported long options. */
12 /* http://www.opengroup.org/onlinepubs/007904975/utilities/chown.html */
13
14 #include "busybox.h"
15
16 #define OPT_STR     ("Rh" USE_DESKTOP("vcfLHP"))
17 #define BIT_RECURSE 1
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))
23 /* POSIX options
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." */
30 /* -L */
31 #define BIT_TRAVERSE 0x20
32 #define OPT_TRAVERSE (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
33 /* -H or -L */
34 #define BIT_TRAVERSE_TOP (0x20|0x40)
35 #define OPT_TRAVERSE_TOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSE_TOP) SKIP_DESKTOP(0))
36
37 typedef int (*chown_fptr)(const char *, uid_t, gid_t);
38
39 static struct bb_uidgid_t ugid = { -1, -1 };
40
41 static int fileAction(const char *fileName, struct stat *statbuf,
42                 void *cf, int depth)
43 {
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;
46
47         if (!((chown_fptr)cf)(fileName, u, g)) {
48                 if (OPT_VERBOSE
49                  || (OPT_CHANGED && (statbuf->st_uid != u || statbuf->st_gid != g))
50                 ) {
51                         printf("changed ownership of '%s' to %u:%u\n",
52                                         fileName, (unsigned)u, (unsigned)g);
53                 }
54                 return TRUE;
55         }
56         if (!OPT_QUIET)
57                 bb_perror_msg("%s", fileName);  /* A filename can have % in it... */
58         return FALSE;
59 }
60
61 int chown_main(int argc, char **argv);
62 int chown_main(int argc, char **argv)
63 {
64         int retval = EXIT_SUCCESS;
65         chown_fptr chown_func;
66
67         opt_complementary = "-2";
68         getopt32(argc, argv, OPT_STR);
69         argv += optind;
70
71         /* This matches coreutils behavior (almost - see below) */
72         chown_func = chown;
73         if (OPT_NODEREF
74             /* || (OPT_RECURSE && !OPT_TRAVERSE_TOP): */
75             USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSE_TOP)) == BIT_RECURSE)
76         ) {
77                 chown_func = lchown;
78         }
79
80         parse_chown_usergroup_or_die(&ugid, argv[0]);
81
82         /* Ok, ready to do the deed now */
83         argv++;
84         do {
85                 char *arg = *argv;
86
87                 if (OPT_TRAVERSE_TOP) {
88                         /* resolves symlink (even recursive) */
89                         arg = xmalloc_realpath(arg);
90                         if (!arg)
91                                 continue;
92                 }
93
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 */
100                                 0)              /* depth */
101                 ) {
102                         retval = EXIT_FAILURE;
103                 }
104
105                 if (OPT_TRAVERSE_TOP)
106                         free(arg);
107         } while (*++argv);
108
109         return retval;
110 }
111
112 /*
113 Testcase. Run in empty directory.
114
115 #!/bin/sh
116 t1="/tmp/busybox chown"
117 t2="/usr/bin/chown"
118 create() {
119     rm -rf $1; mkdir $1
120     (
121     cd $1 || exit 1
122     mkdir dir dir2
123     >up
124     >file
125     >dir/file
126     >dir2/file
127     ln -s dir linkdir
128     ln -s file linkfile
129     ln -s ../up dir/linkup
130     ln -s ../dir2 dir/linkupdir2
131     )
132     chown -R 0:0 $1
133 }
134 tst() {
135     create test1
136     create test2
137     (cd test1; $t1 $1)
138     (cd test2; $t2 $1)
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
143     rm out.diff
144 }
145 tst_for_each() {
146     tst "$1 1:1 file"
147     tst "$1 1:1 dir"
148     tst "$1 1:1 linkdir"
149     tst "$1 1:1 linkfile"
150 }
151 echo "If script produced 'out.diff' file, then at least one testcase failed"
152 # These match coreutils 6.8:
153 tst_for_each ""
154 tst_for_each "-R"
155 tst_for_each "-RP"
156 tst_for_each "-RL"
157 tst_for_each "-RH"
158 tst_for_each "-h"
159 tst_for_each "-hR"
160 tst_for_each "-hRP"
161 # Below: with "chown linkdir" coreutils 6.8 will chown linkdir _target_,
162 # we lchown _the link_. I believe we are "more correct".
163 #tst_for_each "-hRL"
164 #tst_for_each "-hRH"
165
166 */