vfs_aio_fork: Use a shorter random delay
[Samba.git] / source3 / utils / mvxattr.c
blob9cc2ec0d7819c8fa5dde694146b1befa17d1abce
1 /*
2 Unix SMB/CIFS implementation.
3 xattr renaming
4 Copyright (C) Ralph Boehme 2017
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.
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.
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/>.
20 #include "includes.h"
21 #include "system/filesys.h"
22 #include "popt_common.h"
23 #include <ftw.h>
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;
34 static int rename_xattr(const char *path,
35 const struct stat *sb,
36 int typeflag,
37 struct FTW *ftwbuf)
39 ssize_t len;
40 int ret;
42 if (typeflag == FTW_SL) {
43 d_printf("Ignoring symlink %s\n", path);
44 return 0;
47 if (state.verbose) {
48 d_printf("%s\n", path);
51 len = getxattr(path, state.xattr_from, NULL, 0);
52 if (len < 0) {
53 if (errno == ENOATTR) {
54 return 0;
56 d_printf("getxattr [%s] failed [%s]\n",
57 path, strerror(errno));
58 return -1;
62 uint8_t buf[len];
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;
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;
78 if (!state.force) {
79 d_printf("destination [%s:%s] exists, use -f to force\n",
80 path, state.xattr_to);
81 return -1;
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;
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;
98 if (state.print) {
99 d_printf("Renamed %s to %s on %s\n",
100 state.xattr_from, state.xattr_to, path);
104 return 0;
107 int main(int argc, const char *argv[])
109 int c;
110 const char *path = NULL;
111 poptContext pc;
112 struct poptOption long_options[] = {
113 POPT_AUTOHELP
114 {"from", 's', POPT_ARG_STRING, &state.xattr_from, 's', "xattr source name" },
115 {"to", 'd', POPT_ARG_STRING, &state.xattr_to, 'd', "xattr destination name" },
116 {"follow-symlinks", 'l', POPT_ARG_NONE, &state.follow_symlink, 'l', "follow symlinks, the default is to ignore them" },
117 {"print", 'p', POPT_ARG_NONE, &state.print, 'p', "print files where the xattr got renamed" },
118 {"verbose", 'v', POPT_ARG_NONE, &state.verbose, 'v', "print files as they are checked" },
119 {"force", 'f', POPT_ARG_NONE, &state.force, 'f', "force overwriting of destination xattr" },
120 POPT_TABLEEND
122 TALLOC_CTX *frame = talloc_stackframe();
123 const char *s = NULL;
124 int ret = 0;
126 if (getuid() != 0) {
127 d_printf("%s only works as root!\n", argv[0]);
128 ret = 1;
129 goto done;
132 pc = poptGetContext(NULL, argc, argv, long_options, 0);
133 poptSetOtherOptionHelp(pc, "-s STRING -d STRING PATH [PATH ...]");
135 while ((c = poptGetNextOpt(pc)) != -1) {
136 switch (c) {
137 case 's':
138 s = poptGetOptArg(pc);
139 state.xattr_from = talloc_strdup(frame, s);
140 if (state.xattr_from == NULL) {
141 ret = 1;
142 goto done;
144 break;
145 case 'd':
146 s = poptGetOptArg(pc);
147 state.xattr_to = talloc_strdup(frame, s);
148 if (state.xattr_to == NULL) {
149 ret = 1;
150 goto done;
152 break;
156 if (state.xattr_from == NULL || state.xattr_to == NULL) {
157 poptPrintUsage(pc, stderr, 0);
158 ret = 1;
159 goto done;
162 if (poptPeekArg(pc) == NULL) {
163 poptPrintUsage(pc, stderr, 0);
164 ret = 1;
165 goto done;
168 while ((path = poptGetArg(pc)) != NULL) {
169 ret = nftw(path, rename_xattr, 256,
170 state.follow_symlink ? 0 : FTW_PHYS);
173 poptFreeContext(pc);
175 done:
176 TALLOC_FREE(frame);
177 return ret;