-
Notifications
You must be signed in to change notification settings - Fork 38
/
lib.c
294 lines (257 loc) · 6.85 KB
/
lib.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/*
* lib.c - Common things for all utilities
*
* Copyright (c) 2017 Huawei. All Rights Reserved.
* Author: zhangyi (F) <yi.zhang@huawei.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it would be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdbool.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/xattr.h>
#include <fts.h>
#include "common.h"
#include "lib.h"
#include "path.h"
extern int flags;
extern int status;
static int ask_yn(const char *question, int def)
{
char ans[16];
print_info(_("%s ? [%s]: \n"), question, def ? _("y") : _("n"));
fflush(stdout);
while (fgets(ans, sizeof(ans)-1, stdin)) {
if (ans[0] == '\n')
return def;
else if (!strcasecmp(ans, "y\n") || !strcasecmp(ans, "yes\n"))
return 1;
else if (!strcasecmp(ans, "n\n") || !strcasecmp(ans, "no\n"))
return 0;
else
print_info(_("Illegal answer. Please input y/n or yes/no:"));
fflush(stdout);
}
return def;
}
int ask_question(const char *question, int def)
{
if (flags & FL_OPT_MASK) {
def = (flags & FL_OPT_YES) ? 1 : (flags & FL_OPT_NO) ? 0 : def;
print_info(_("%s? %s\n"), question, def ? _("y") : _("n"));
return def;
}
return ask_yn(question, def);
}
/*
* Get the value of the specified xattr
*
* @dirfd: base dir fd for relative pathname
* @value: xattr value, can be NULL if empty value
* @exit: xattr exit or not
*
* Return: a nonnegative value on success, -1 otherwise
*/
ssize_t get_xattr(int dirfd, const char *pathname, const char *xattrname,
char **value, bool *exist)
{
char *buf = NULL;
int fd;
ssize_t ret;
fd = openat(dirfd, pathname, O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_RDONLY);
if (fd < 0) {
print_err(_("Failed to openat %s: %s\n"),
pathname, strerror(errno));
return -1;
}
ret = fgetxattr(fd, xattrname, NULL, 0);
if (ret < 0) {
if (errno != ENODATA && errno != ENOTSUP)
goto fail;
if (exist)
*exist = false;
ret = 0;
goto out;
}
/* Zero size value means xattr exist but value unknown */
if (exist)
*exist = true;
if (ret == 0 || !value)
goto out;
buf = smalloc(ret+1);
ret = fgetxattr(fd, xattrname, buf, ret);
if (ret <= 0)
goto fail2;
buf[ret] = '\0';
*value = buf;
out:
close(fd);
return ret;
fail2:
free(buf);
fail:
print_err(_("Cannot fgetxattr %s %s: %s\n"), pathname,
xattrname, strerror(errno));
goto out;
}
/*
* Set the value of the specified xattr
*
* @dirfd: base dir fd for relative pathname
* @value: xattr value, can be NULL if empty value
* @size: size of xattr value
*/
int set_xattr(int dirfd, const char *pathname, const char *xattrname,
void *value, size_t size)
{
int fd;
int ret;
fd = openat(dirfd, pathname, O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_RDONLY);
if (fd < 0) {
print_err(_("Failed to openat %s: %s\n"),
pathname, strerror(errno));
return -1;
}
ret = fsetxattr(fd, xattrname, value, size, XATTR_CREATE);
if (ret && errno != EEXIST)
goto fail;
if (errno == EEXIST) {
ret = fsetxattr(fd, xattrname, value, size, XATTR_REPLACE);
if (ret)
goto fail;
}
out:
close(fd);
return ret;
fail:
print_err(_("Cannot fsetxattr %s %s: %s\n"), pathname,
xattrname, strerror(errno));
goto out;
}
/* Remove the specified xattr */
int remove_xattr(int dirfd, const char *pathname, const char *xattrname)
{
int fd;
int ret;
fd = openat(dirfd, pathname, O_CLOEXEC|O_NONBLOCK|O_NOFOLLOW|O_RDONLY);
if (fd < 0) {
print_err(_("Failed to openat %s: %s\n"),
pathname, strerror(errno));
return -1;
}
ret = fremovexattr(fd, xattrname);
if (ret)
print_err(_("Cannot fremovexattr %s %s: %s\n"), pathname,
xattrname, strerror(errno));
close(fd);
return ret;
}
static void scan_entry_init(struct scan_ctx *sctx, FTSENT *ftsent)
{
sctx->pathname = basename2(ftsent->fts_path, sctx->layer->path);
sctx->filename = ftsent->fts_name;
sctx->st = ftsent->fts_statp;
}
static inline int scan_check_entry(int (*do_check)(struct scan_ctx *),
struct scan_ctx *sctx)
{
return do_check ? do_check(sctx) : 0;
}
/*
* Scan specified directories and invoke callback to check/fix underlying
* dirs of overlay filesystem
*/
int scan_dir(struct scan_ctx *sctx, struct scan_operations *sop)
{
char *paths[2] = {sctx->layer->path, NULL};
FTS *ftsp;
FTSENT *ftsent;
int ret = 0;
ftsp = fts_open(paths, FTS_NOCHDIR | FTS_PHYSICAL, NULL);
if (ftsp == NULL) {
print_err(_("Failed to fts open %s:%s\n"),
sctx->layer->path, strerror(errno));
return -1;
}
while ((ftsent = fts_read(ftsp)) != NULL) {
/* Fillup base context */
scan_entry_init(sctx, ftsent);
print_debug(_("Scan:%-3s %2d %7lld %-40s %-20s\n"),
(ftsent->fts_info == FTS_D) ? "d" :
(ftsent->fts_info == FTS_DNR) ? "dnr" :
(ftsent->fts_info == FTS_DP) ? "dp" :
(ftsent->fts_info == FTS_F) ? "f" :
(ftsent->fts_info == FTS_NS) ? "ns" :
(ftsent->fts_info == FTS_SL) ? "sl" :
(ftsent->fts_info == FTS_SLNONE) ? "sln" :
(ftsent->fts_info == FTS_DEFAULT) ? "df" : "???",
ftsent->fts_level,
(long long)ftsent->fts_statp->st_size,
ftsent->fts_path, sctx->layer->path);
switch (ftsent->fts_info) {
case FTS_F:
sctx->result.files++;
/* Check impurities */
ret = scan_check_entry(sop->impurity, sctx);
if (ret)
goto out;
break;
case FTS_DEFAULT:
/* Check whiteouts */
ret = scan_check_entry(sop->whiteout, sctx);
if (ret)
goto out;
break;
case FTS_D:
sctx->result.directories++;
/* Check redirect xattr */
ret = scan_check_entry(sop->redirect, sctx);
if (ret)
goto out;
/* Check impurities */
ret = scan_check_entry(sop->impurity, sctx);
if (ret)
goto out;
/* Save current dir data and create new one for subdir */
ftsent->fts_pointer = sctx->dirdata;
sctx->dirdata = smalloc(sizeof(struct scan_dir_data));
break;
case FTS_DP:
/* Check impure xattr */
ret = scan_check_entry(sop->impure, sctx);
if (ret)
goto out;
/* Restore parent's dir data */
free(sctx->dirdata);
sctx->dirdata = ftsent->fts_pointer;
break;
case FTS_NS:
case FTS_DNR:
case FTS_ERR:
print_err(_("Failed to fts read %s:%s\n"),
ftsent->fts_path, strerror(ftsent->fts_errno));
ret = -1;
goto out;
}
}
out:
fts_close(ftsp);
return ret;
}