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
|
/*
* Description: find a file in a branch
*
* License: BSD-style license
*
* original implementation by Radek Podgorny
*
* License: BSD-style license
* Copyright: Radek Podgorny <radek@podgorny.cz>,
* Bernd Schubert <bernd-schubert@gmx.de>
*
*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <sys/statvfs.h>
#include <stdbool.h>
#include "unionfs.h"
#include "opts.h"
#include "stats.h"
#include "debug.h"
#include "hashtable.h"
#include "general.h"
#include "string.h"
/**
* Hide metadata. As is causes a slight slowndown this is optional
*
*/
static bool hide_meta_files(int branch, const char *path, struct dirent *de)
{
if (uopt.hide_meta_files == false) RETURN(false);
fprintf(stderr, "uopt.branches[branch].path = %s path = %s\n", uopt.branches[branch].path, path);
fprintf(stderr, "METANAME = %s, de->d_name = %s\n", METANAME, de->d_name);
// TODO Would it be faster to add hash comparison?
// HIDE out .unionfs directory
if (strcmp(uopt.branches[branch].path, path) == 0
&& strcmp(METANAME, de->d_name) == 0) {
RETURN(true);
}
// HIDE fuse META files
if (strncmp(FUSE_META_FILE, de->d_name, FUSE_META_LENGTH) == 0)
RETURN(true);
RETURN(false);
}
/**
* Check if fname has a hiding tag and return its status.
* Also, add this file and to the hiding hash table.
* Warning: If fname has the tag, fname gets modified.
*/
static bool is_hiding(struct hashtable *hides, char *fname) {
DBG("%s\n", fname);
char *tag;
tag = whiteout_tag(fname);
if (tag) {
// even more important, ignore the file without the tag!
// hint: tag is a pointer to the flag-suffix within de->d_name
*tag = '\0'; // this modifies fname!
// add to hides (only if not there already)
if (!hashtable_search(hides, fname)) {
hashtable_insert(hides, strdup(fname), malloc(1));
}
RETURN(true);
}
RETURN(false);
}
/**
* Read whiteout files
*/
static void read_whiteouts(const char *path, struct hashtable *whiteouts, int branch) {
DBG("%s\n", path);
char p[PATHLEN_MAX];
if (BUILD_PATH(p, uopt.branches[branch].path, METADIR, path)) return;
DIR *dp = opendir(p);
if (dp == NULL) return;
struct dirent *de;
while ((de = readdir(dp)) != NULL) {
is_hiding(whiteouts, de->d_name);
}
closedir(dp);
}
/**
* unionfs-fuse readdir function
*/
int unionfs_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset, struct fuse_file_info *fi) {
DBG("%s\n", path);
(void)offset;
(void)fi;
int i = 0;
int rc = 0;
// we will store already added files here to handle same file names across different branches
struct hashtable *files = create_hashtable(16, string_hash, string_equal);
struct hashtable *whiteouts = NULL;
if (uopt.cow_enabled) whiteouts = create_hashtable(16, string_hash, string_equal);
bool subdir_hidden = false;
for (i = 0; i < uopt.nbranches; i++) {
if (subdir_hidden) break;
char p[PATHLEN_MAX];
if (BUILD_PATH(p, uopt.branches[i].path, path)) {
rc = -ENAMETOOLONG;
goto out;
}
// check if branches below this branch are hidden
int res = path_hidden(path, i);
if (res < 0) {
rc = res; // error
goto out;
}
if (res > 0) subdir_hidden = true;
DIR *dp = opendir(p);
if (dp == NULL) {
if (uopt.cow_enabled) read_whiteouts(path, whiteouts, i);
continue;
}
struct dirent *de;
while ((de = readdir(dp)) != NULL) {
// already added in some other branch
if (hashtable_search(files, de->d_name) != NULL) continue;
// check if we need file hiding
if (uopt.cow_enabled) {
// file should be hidden from the user
if (hashtable_search(whiteouts, de->d_name) != NULL) continue;
}
if (hide_meta_files(i, p, de) == true) continue;
// fill with something dummy, we're interested in key existence only
hashtable_insert(files, strdup(de->d_name), malloc(1));
struct stat st;
memset(&st, 0, sizeof(st));
st.st_ino = de->d_ino;
st.st_mode = de->d_type << 12;
if (filler(buf, de->d_name, &st, 0)) break;
}
closedir(dp);
if (uopt.cow_enabled) read_whiteouts(path, whiteouts, i);
}
out:
hashtable_destroy(files, 1);
if (uopt.cow_enabled) hashtable_destroy(whiteouts, 1);
if (uopt.stats_enabled && strcmp(path, "/") == 0) {
filler(buf, "stats", NULL, 0);
}
RETURN(rc);
}
/**
* check if a directory on all paths is empty
* return 0 if empty, 1 if not and negative value on error
*
* TODO: This shares lots of code with unionfs_readdir(), can we merge
* both functions?
*/
int dir_not_empty(const char *path) {
DBG("%s\n", path);
int i = 0;
int rc = 0;
int not_empty = 0;
struct hashtable *whiteouts = NULL;
if (uopt.cow_enabled) whiteouts = create_hashtable(16, string_hash, string_equal);
bool subdir_hidden = false;
for (i = 0; i < uopt.nbranches; i++) {
if (subdir_hidden) break;
char p[PATHLEN_MAX];
if (BUILD_PATH(p, uopt.branches[i].path, path)) {
rc = -ENAMETOOLONG;
goto out;
}
// check if branches below this branch are hidden
int res = path_hidden(path, i);
if (res < 0) {
rc = res; // error
goto out;
}
if (res > 0) subdir_hidden = true;
DIR *dp = opendir(p);
if (dp == NULL) {
if (uopt.cow_enabled) read_whiteouts(path, whiteouts, i);
continue;
}
struct dirent *de;
while ((de = readdir(dp)) != NULL) {
// Ignore . and ..
if ((strcmp(de->d_name, ".") == 0) || (strcmp(de->d_name, "..") == 0))
continue;
// check if we need file hiding
if (uopt.cow_enabled) {
// file should be hidden from the user
if (hashtable_search(whiteouts, de->d_name) != NULL) continue;
}
if (hide_meta_files(i, p, de) == true) continue;
// When we arrive here, a valid entry was found
not_empty = 1;
closedir(dp);
goto out;
}
closedir(dp);
if (uopt.cow_enabled) read_whiteouts(path, whiteouts, i);
}
out:
if (uopt.cow_enabled) hashtable_destroy(whiteouts, 1);
if (rc) RETURN(rc);
RETURN(not_empty);
}
|