diff options
Diffstat (limited to 'coreutils/chown.c')
-rw-r--r-- | coreutils/chown.c | 108 |
1 files changed, 94 insertions, 14 deletions
diff --git a/coreutils/chown.c b/coreutils/chown.c index 3380677bc..439b62ac8 100644 --- a/coreutils/chown.c +++ b/coreutils/chown.c | |||
@@ -17,25 +17,31 @@ static struct bb_uidgid_t ugid = { -1, -1 }; | |||
17 | 17 | ||
18 | static int (*chown_func)(const char *, uid_t, gid_t) = chown; | 18 | static int (*chown_func)(const char *, uid_t, gid_t) = chown; |
19 | 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) | ||
20 | #define OPT_RECURSE (option_mask32 & 1) | 25 | #define OPT_RECURSE (option_mask32 & 1) |
21 | #define OPT_NODEREF (option_mask32 & 2) | 26 | #define OPT_NODEREF (option_mask32 & 2) |
22 | #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 4) SKIP_DESKTOP(0)) | 27 | #define OPT_VERBOSE (USE_DESKTOP(option_mask32 & 0x04) SKIP_DESKTOP(0)) |
23 | #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 8) SKIP_DESKTOP(0)) | 28 | #define OPT_CHANGED (USE_DESKTOP(option_mask32 & 0x08) SKIP_DESKTOP(0)) |
24 | #define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0)) | 29 | #define OPT_QUIET (USE_DESKTOP(option_mask32 & 0x10) SKIP_DESKTOP(0)) |
25 | #define OPT_STR ("Rh" USE_DESKTOP("vcf")) | 30 | /* POSIX options |
26 | |||
27 | /* TODO: | ||
28 | * -H if a command line argument is a symbolic link to a directory, traverse it | ||
29 | * -L traverse every symbolic link to a directory encountered | 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 | ||
30 | * -P do not traverse any symbolic links (default) | 33 | * -P do not traverse any symbolic links (default) |
31 | */ | 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)) | ||
32 | 41 | ||
33 | static int fileAction(const char *fileName, struct stat *statbuf, | 42 | static int fileAction(const char *fileName, struct stat *statbuf, |
34 | void ATTRIBUTE_UNUSED *junk, int depth) | 43 | void ATTRIBUTE_UNUSED *junk, int depth) |
35 | { | 44 | { |
36 | // TODO: -H/-L/-P | ||
37 | // if (depth ... && S_ISLNK(statbuf->st_mode)) .... | ||
38 | |||
39 | if (!chown_func(fileName, | 45 | if (!chown_func(fileName, |
40 | (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid, | 46 | (ugid.uid == (uid_t)-1) ? statbuf->st_uid : ugid.uid, |
41 | (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid) | 47 | (ugid.gid == (gid_t)-1) ? statbuf->st_gid : ugid.gid) |
@@ -62,16 +68,31 @@ int chown_main(int argc, char **argv) | |||
62 | getopt32(argc, argv, OPT_STR); | 68 | getopt32(argc, argv, OPT_STR); |
63 | argv += optind; | 69 | argv += optind; |
64 | 70 | ||
65 | if (OPT_NODEREF) chown_func = lchown; | 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 | } | ||
66 | 78 | ||
67 | parse_chown_usergroup_or_die(&ugid, argv[0]); | 79 | parse_chown_usergroup_or_die(&ugid, argv[0]); |
68 | 80 | ||
69 | /* Ok, ready to do the deed now */ | 81 | /* Ok, ready to do the deed now */ |
70 | argv++; | 82 | argv++; |
71 | do { | 83 | do { |
72 | if (!recursive_action(*argv, | 84 | char *arg = *argv; |
73 | OPT_RECURSE, // recurse | 85 | |
74 | FALSE, // follow links: TODO: -H/-L/-P | 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 | ||
75 | FALSE, // depth first | 96 | FALSE, // depth first |
76 | fileAction, // file action | 97 | fileAction, // file action |
77 | fileAction, // dir action | 98 | fileAction, // dir action |
@@ -80,7 +101,66 @@ int chown_main(int argc, char **argv) | |||
80 | ) { | 101 | ) { |
81 | retval = EXIT_FAILURE; | 102 | retval = EXIT_FAILURE; |
82 | } | 103 | } |
104 | |||
105 | if (OPT_TRAVERSETOP) | ||
106 | free(arg); | ||
83 | } while (*++argv); | 107 | } while (*++argv); |
84 | 108 | ||
85 | return retval; | 109 | return retval; |
86 | } | 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 | */ | ||