439b62ac808f8675d7486a7c57cd01726cbec800
[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 - unsupported options -H, -L, and -P. */
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 static struct bb_uidgid_t ugid = { -1, -1 };
17
18 static int (*chown_func)(const char *, uid_t, gid_t) = chown;
19
20 #define OPT_STR     ("Rh" USE_DESKTOP("vcfLHP"))
21 #define BIT_RECURSE 1
22 #define BIT_NODEREF 2
23 #define BIT_TRAVERSE 0x20
24 #define BIT_TRAVERSETOP (0x20|0x40)
25 #define OPT_RECURSE (option_mask32 & 1)
26 #define OPT_NODEREF (option_mask32 & 2)
27 #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0))
28 #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0))
29 #define OPT_QUIET   (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0))
30 /* POSIX options
31  * -L traverse every symbolic link to a directory encountered
32  * -H if a command line argument is a symbolic link to a directory, traverse it
33  * -P do not traverse any symbolic links (default)
34  * We do not conform to the following:
35  * "Specifying more than one of -H, -L, and -P is not an error.
36  * The last option specified shall determine the behavior of the utility." */
37 /* -L */
38 #define OPT_TRAVERSE    (USE_DESKTOP(option_mask32 & BIT_TRAVERSE) SKIP_DESKTOP(0))
39 /* -H or -L */
40 #define OPT_TRAVERSETOP (USE_DESKTOP(option_mask32 & BIT_TRAVERSETOP) SKIP_DESKTOP(0))
41
42 static int fileAction(const char *fileName, struct stat *statbuf,
43                 void ATTRIBUTE_UNUSED *junk, int depth)
44 {
45         if (!chown_func(fileName,
46                         (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid,
47                         (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid)
48         ) {
49                 if (OPT_VERBOSE
50                  || (OPT_CHANGED && (statbuf->st_uid != ugid.uid || statbuf->st_gid != ugid.gid))
51                 ) {
52                         printf("changed ownership of '%s' to %u:%u\n",
53                                         fileName, ugid.uid, ugid.gid);
54                 }
55                 return TRUE;
56         }
57         if (!OPT_QUIET)
58                 bb_perror_msg("%s", fileName);  /* A filename can have % in it... */
59         return FALSE;
60 }
61
62 int chown_main(int argc, char **argv);
63 int chown_main(int argc, char **argv)
64 {
65         int retval = EXIT_SUCCESS;
66
67         opt_complementary = "-2";
68         getopt32(argc, argv, OPT_STR);
69         argv += optind;
70
71         /* This matches coreutils behavior (almost - see below) */
72         if (OPT_NODEREF
73             /* || (OPT_RECURSE && !OPT_TRAVERSETOP): */
74             USE_DESKTOP( || (option_mask32 & (BIT_RECURSE|BIT_TRAVERSETOP)) == BIT_RECURSE)
75         ) {
76                 chown_func = lchown;
77         }
78
79         parse_chown_usergroup_or_die(&ugid, argv[0]);
80
81         /* Ok, ready to do the deed now */
82         argv++;
83         do {
84                 char *arg = *argv;
85
86                 if (OPT_TRAVERSETOP) {
87                         /* resolves symlink (even recursive) */
88                         arg = xmalloc_realpath(arg);
89                         if (!arg)
90                                 continue;
91                 }
92
93                 if (!recursive_action(arg,
94                                 OPT_RECURSE,    // recurse
95                                 OPT_TRAVERSE,   // follow links if -L
96                                 FALSE,          // depth first
97                                 fileAction,     // file action
98                                 fileAction,     // dir action
99                                 NULL,           // user data
100                                 0)              // depth
101                 ) {
102                         retval = EXIT_FAILURE;
103                 }
104
105                 if (OPT_TRAVERSETOP)
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 */