s3: free popt context in utils
[bbaumbach/samba-autobuild/.git] / source3 / utils / mvxattr.c
1 /*
2    Unix SMB/CIFS implementation.
3    xattr renaming
4    Copyright (C) Ralph Boehme 2017
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "popt_common.h"
23 #include <ftw.h>
24
25 static struct rename_xattr_state {
26         int follow_symlink;
27         int print;
28         int force;
29         int verbose;
30         char *xattr_from;
31         char *xattr_to;
32 } state;
33
34 static int rename_xattr(const char *path,
35                         const struct stat *sb,
36                         int typeflag,
37                         struct FTW *ftwbuf)
38 {
39         ssize_t len;
40         int ret;
41
42         if (typeflag == FTW_SL) {
43                 d_printf("Ignoring symlink %s\n", path);
44                 return 0;
45         }
46
47         if (state.verbose) {
48                 d_printf("%s\n", path);
49         }
50
51         len = getxattr(path, state.xattr_from, NULL, 0);
52         if (len < 0) {
53                 if (errno == ENOATTR) {
54                         return 0;
55                 }
56                 d_printf("getxattr [%s] failed [%s]\n",
57                          path, strerror(errno));
58                 return -1;
59         }
60
61         {
62                 uint8_t buf[len];
63
64                 len = getxattr(path, state.xattr_from, &buf[0], len);
65                 if (len == -1) {
66                         d_printf("getxattr [%s] failed [%s]\n",
67                                  path, strerror(errno));
68                         return -1;
69                 }
70
71                 ret = setxattr(path, state.xattr_to, &buf[0], len, XATTR_CREATE);
72                 if (ret != 0) {
73                         if (errno != EEXIST) {
74                                 d_printf("setxattr [%s] failed [%s]\n",
75                                          path, strerror(errno));
76                                 return -1;
77                         }
78                         if (!state.force) {
79                                 d_printf("destination [%s:%s] exists, use -f to force\n",
80                                          path, state.xattr_to);
81                                 return -1;
82                         }
83                         ret = setxattr(path, state.xattr_to, &buf[0], len, XATTR_REPLACE);
84                         if (ret != 0) {
85                                 d_printf("setxattr [%s:%s] failed [%s]\n",
86                                          path, state.xattr_to, strerror(errno));
87                                 return -1;
88                         }
89                 }
90
91                 ret = removexattr(path, state.xattr_from);
92                 if (ret != 0) {
93                         d_printf("removexattr [%s:%s] failed [%s]\n",
94                                  path, state.xattr_from, strerror(errno));
95                         return -1;
96                 }
97
98                 if (state.print) {
99                         d_printf("Renamed %s to %s on %s\n",
100                                  state.xattr_from, state.xattr_to, path);
101                 }
102         }
103
104         return 0;
105 }
106
107 int main(int argc, const char *argv[])
108 {
109         int c;
110         const char *path = NULL;
111         poptContext pc = NULL;
112         struct poptOption long_options[] = {
113                 POPT_AUTOHELP
114                 {
115                         .longName   = "from",
116                         .shortName  = 's',
117                         .argInfo    = POPT_ARG_STRING,
118                         .arg        = &state.xattr_from,
119                         .val        = 's',
120                         .descrip    = "xattr source name",
121                 },
122                 {
123                         .longName   = "to",
124                         .shortName  = 'd',
125                         .argInfo    = POPT_ARG_STRING,
126                         .arg        = &state.xattr_to,
127                         .val        = 'd',
128                         .descrip    = "xattr destination name",
129                 },
130                 {
131                         .longName   = "follow-symlinks",
132                         .shortName  = 'l',
133                         .argInfo    = POPT_ARG_NONE,
134                         .arg        = &state.follow_symlink,
135                         .val        = 'l',
136                         .descrip    = "follow symlinks, the default is to "
137                                       "ignore them",
138                 },
139                 {
140                         .longName   = "print",
141                         .shortName  = 'p',
142                         .argInfo    = POPT_ARG_NONE,
143                         .arg        = &state.print,
144                         .val        = 'p',
145                         .descrip    = "print files where the xattr got "
146                                       "renamed",
147                 },
148                 {
149                         .longName   = "verbose",
150                         .shortName  = 'v',
151                         .argInfo    = POPT_ARG_NONE,
152                         .arg        = &state.verbose,
153                         .val        = 'v',
154                         .descrip    = "print files as they are checked",
155                 },
156                 {
157                         .longName   = "force",
158                         .shortName  = 'f',
159                         .argInfo    = POPT_ARG_NONE,
160                         .arg        = &state.force,
161                         .val        = 'f',
162                         .descrip    = "force overwriting of destination xattr",
163                 },
164                 POPT_TABLEEND
165         };
166         TALLOC_CTX *frame = talloc_stackframe();
167         const char *s = NULL;
168         int ret = 0;
169
170         if (getuid() != 0) {
171                 d_printf("%s only works as root!\n", argv[0]);
172                 ret = 1;
173                 goto done;
174         }
175
176         pc = poptGetContext(NULL, argc, argv, long_options, 0);
177         poptSetOtherOptionHelp(pc, "-s STRING -d STRING PATH [PATH ...]");
178
179         while ((c = poptGetNextOpt(pc)) != -1) {
180                 switch (c) {
181                 case 's':
182                         s = poptGetOptArg(pc);
183                         state.xattr_from = talloc_strdup(frame, s);
184                         if (state.xattr_from == NULL) {
185                                 ret = 1;
186                                 goto done;
187                         }
188                         break;
189                 case 'd':
190                         s = poptGetOptArg(pc);
191                         state.xattr_to = talloc_strdup(frame, s);
192                         if (state.xattr_to == NULL) {
193                                 ret = 1;
194                                 goto done;
195                         }
196                         break;
197                 }
198         }
199
200         if (state.xattr_from == NULL || state.xattr_to == NULL) {
201                 poptPrintUsage(pc, stderr, 0);
202                 ret = 1;
203                 goto done;
204         }
205
206         if (poptPeekArg(pc) == NULL) {
207                 poptPrintUsage(pc, stderr, 0);
208                 ret = 1;
209                 goto done;
210         }
211
212         while ((path = poptGetArg(pc)) != NULL) {
213                 ret = nftw(path, rename_xattr, 256,
214                            state.follow_symlink ? 0 : FTW_PHYS);
215         }
216
217 done:
218         poptFreeContext(pc);
219
220         TALLOC_FREE(frame);
221         return ret;
222 }