-
Notifications
You must be signed in to change notification settings - Fork 1
/
shadow_ops.cc
755 lines (577 loc) · 18.8 KB
/
shadow_ops.cc
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
* Copyright (c) 2009-2013 Riverbed Technology, Inc.
*
* This file is part of Shadowfs
*
* Shadowfs 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, either version 2 of the License, or
* (at your option) any later version.
* Shadowfs is distributed in the hope that it will 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 Shadowfs. If not, see <http://www.gnu.org/licenses/>.
*/
#include "shadowfs.h"
#include <map>
struct ShadowFileState {
int local_fd;
int shadow_fd;
bool offline;
};
typedef std::map<std::string, ShadowFileState*> OpenFileTable;
OpenFileTable open_files_;
static int shadow_getattr(const char *path, struct stat *stbuf)
{
std::string local_path = std::string(DATA_DIR) + path;
int res;
res = lstat(local_path.c_str(), stbuf);
if (res == -1)
return -errno;
return 0;
}
static int shadow_access(const char *path, int mask)
{
int res;
std::string local_path = DATA_DIR + path;
res = access(local_path.c_str(), mask);
if (res == -1)
return -errno;
return 0;
}
static int shadow_readlink(const char *path, char *buf, size_t size)
{
std::string local_path = DATA_DIR + path;
int res;
res = readlink(local_path.c_str(), buf, size - 1);
if (res == -1)
return -errno;
buf[res] = '\0';
return 0;
}
static int shadow_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
std::string local_path = DATA_DIR + path;
DIR *dp;
struct dirent *de;
(void) offset;
(void) fi;
dp = opendir(local_path.c_str());
if (dp == NULL)
return -errno;
while ((de = readdir(dp)) != NULL) {
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);
return 0;
}
static int shadow_mknod(const char *path, mode_t mode, dev_t rdev)
{
std::string local_path = DATA_DIR + path;
int res;
/* On Linux this could just be 'mknod(path, mode, rdev)' but this
is more portable */
if (S_ISREG(mode)) {
dsyslog("mknod(%s): regular file, mode %o\n", local_path.c_str(), mode);
res = open(local_path.c_str(), O_CREAT | O_EXCL | O_WRONLY, mode);
if (res >= 0)
res = close(res);
} else if (S_ISFIFO(mode))
res = mkfifo(local_path.c_str(), mode);
else
res = mknod(local_path.c_str(), mode, rdev);
if (res == -1)
return -errno;
// Need to set permissions to the calling user
fuse_context* ctx = fuse_get_context();
chown(local_path.c_str(), ctx->uid, ctx->gid);
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
if (S_ISREG(mode)) {
res = open(shadow_path.c_str(), O_CREAT | O_EXCL | O_WRONLY, mode);
if (res >= 0)
res = close(res);
} else if (S_ISFIFO(mode))
res = mkfifo(shadow_path.c_str(), mode);
else
res = mknod(shadow_path.c_str(), mode, rdev);
if (res == -1)
syslog(LOG_ERR, "error in shadow mknod(%s): %s\n",
shadow_path.c_str(), strerror(errno));
chown(shadow_path.c_str(), ctx->uid, ctx->gid);
return 0;
}
static int shadow_create(const char *path, mode_t mode, struct fuse_file_info *fi)
{
std::string local_path = DATA_DIR + path;
ShadowFileState* info;
int fd;
OpenFileTable::iterator iter = open_files_.find(std::string(path));
if (iter != open_files_.end()) {
// Something got screwed up in the fuse layer and it didn't
// properly call release() on the old file handle. The safe
// thing to do is to keep the ShadowFileState object around,
// but close the open file descriptors.
//syslog(LOG_ERR, "file %s opened multiple times (info %p)\n", path, info);
/*
if (close(info->local_fd) != 0) {
syslog(LOG_ERR, "error in close(%d): %s\n",
info->local_fd, strerror(errno));
}
if (info->shadow_fd != -1 && close(info->shadow_fd) != 0) {
syslog(LOG_ERR, "error in close(%d): %s\n",
info->shadow_fd, strerror(errno));
}
*/
}
fd = creat(local_path.c_str(), mode);
// TODO remove flags?
dsyslog("creat(%s) 0x%x returned %d\n", local_path.c_str(), fi->flags, fd);
if (fd == -1)
return -errno;
info = new ShadowFileState();
fi->fh = (uint64_t)info;
info->local_fd = fd;
info->shadow_fd = -1;
info->offline = false;
open_files_[std::string(path)] = info;
if ((fi->flags & O_ACCMODE) == O_RDONLY) {
dsyslog("creat(%s) write-mode not set\n", local_path.c_str());
return 0;
}
if (is_offline(path)) {
info->offline = true;
return 0;
}
std::string shadow_path = get_shadow_path(path);
fd = creat(shadow_path.c_str(), fi->flags);
dsyslog("shadow creat(%s) returned %d\n", shadow_path.c_str(), fd);
if (fd == -1) {
syslog(LOG_ERR, "error in shadow creat(%s): %s\n",
shadow_path.c_str(), strerror(errno));
} else {
info->shadow_fd = fd;
}
return 0;
}
static int shadow_mkdir(const char *path, mode_t mode)
{
std::string local_path = DATA_DIR + path;
int res;
res = mkdir(local_path.c_str(), mode);
if (res == -1)
return -errno;
// Need to set permissions to the calling user
fuse_context* ctx = fuse_get_context();
chown(local_path.c_str(), ctx->uid, ctx->gid);
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = mkdir(shadow_path.c_str(), mode);
if (res == -1) {
syslog(LOG_ERR, "error in shadow mkdir(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
chown(shadow_path.c_str(), ctx->uid, ctx->gid);
return 0;
}
static int shadow_unlink(const char *path)
{
std::string local_path = DATA_DIR + path;
int res;
res = unlink(local_path.c_str());
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = unlink(shadow_path.c_str());
if (res == -1) {
syslog(LOG_ERR, "error in shadow unlink(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
static int shadow_rmdir(const char *path)
{
std::string local_path = DATA_DIR + path;
int res;
res = rmdir(local_path.c_str());
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = rmdir(shadow_path.c_str());
if (res == -1) {
syslog(LOG_ERR, "error in shadow rmdir(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
static int shadow_symlink(const char *from, const char *to)
{
std::string local_to = DATA_DIR + to;
int res;
// In this case don't modify from since it should remain unchanged
// in both copies.
res = symlink(from, local_to.c_str());
if (res == -1)
return -errno;
// Need to set permissions to the calling user
fuse_context* ctx = fuse_get_context();
chown(local_to.c_str(), ctx->uid, ctx->gid);
if (is_offline(to)) {
return 0;
}
std::string shadow_to = get_shadow_path(to);
res = symlink(from, shadow_to.c_str());
if (res == -1) {
syslog(LOG_ERR, "error in shadow symlink(%s -> %s): %s\n",
from, shadow_to.c_str(), strerror(errno));
}
chown(shadow_to.c_str(), ctx->uid, ctx->gid);
return 0;
}
static int shadow_rename(const char *from, const char *to)
{
std::string local_from = DATA_DIR + from;
std::string local_to = DATA_DIR + to;
int res;
res = rename(local_from.c_str(), local_to.c_str());
if (res == -1)
return -errno;
if (is_offline(to)) {
return 0;
}
std::string shadow_from = get_shadow_path(from);
std::string shadow_to = get_shadow_path(to);
res = rename(shadow_from.c_str(), shadow_to.c_str());
if (res == -1) {
syslog(LOG_ERR, "error in shadow rename(%s -> %s): %s\n",
from, shadow_to.c_str(), strerror(errno));
}
return 0;
}
static int shadow_link(const char *from, const char *to)
{
std::string local_from = DATA_DIR + from;
std::string local_to = DATA_DIR + to;
int res;
res = link(local_from.c_str(), local_to.c_str());
if (res == -1)
return -errno;
// Need to set permissions to the calling user
fuse_context* ctx = fuse_get_context();
chown(local_to.c_str(), ctx->uid, ctx->gid);
if (is_offline(to)) {
return 0;
}
std::string shadow_from = get_shadow_path(from);
std::string shadow_to = get_shadow_path(to);
res = link(shadow_from.c_str(), shadow_to.c_str());
if (res == -1) {
syslog(LOG_ERR, "error in shadow link(%s -> %s): %s\n",
from, shadow_to.c_str(), strerror(errno));
}
chown(shadow_to.c_str(), ctx->uid, ctx->gid);
return 0;
}
static int shadow_chmod(const char *path, mode_t mode)
{
std::string local_path = DATA_DIR + path;
int res;
res = chmod(local_path.c_str(), mode);
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = chmod(shadow_path.c_str(), mode);
if (res == -1) {
syslog(LOG_ERR, "error in shadow chmod(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
static int shadow_chown(const char *path, uid_t uid, gid_t gid)
{
std::string local_path = DATA_DIR + path;
int res;
res = lchown(local_path.c_str(), uid, gid);
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = chown(shadow_path.c_str(), uid, gid);
if (res == -1) {
syslog(LOG_ERR, "error in shadow chown(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
static int shadow_truncate(const char *path, off_t size)
{
std::string local_path = DATA_DIR + path;
int res;
res = truncate(local_path.c_str(), size);
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = truncate(shadow_path.c_str(), size);
if (res == -1) {
syslog(LOG_ERR, "error in shadow truncate(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
static int shadow_utimens(const char *path, const struct timespec ts[2])
{
std::string local_path = DATA_DIR + path;
int res;
struct timeval tv[2];
tv[0].tv_sec = ts[0].tv_sec;
tv[0].tv_usec = ts[0].tv_nsec / 1000;
tv[1].tv_sec = ts[1].tv_sec;
tv[1].tv_usec = ts[1].tv_nsec / 1000;
res = utimes(local_path.c_str(), tv);
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = utimes(shadow_path.c_str(), tv);
if (res == -1) {
syslog(LOG_ERR, "error in shadow utimes(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
static int shadow_open(const char *path, struct fuse_file_info *fi)
{
std::string local_path = DATA_DIR + path;
ShadowFileState* info;
int fd;
OpenFileTable::iterator iter = open_files_.find(std::string(path));
if (iter != open_files_.end()) {
// Something got screwed up in the fuse layer and it didn't
// properly call release() on the old file handle. The safe
// thing to do is to keep the ShadowFileState object around,
// but close the open file descriptors.
//syslog(LOG_ERR, "file %s opened multiple times (info %p)\n", path, info);
/*
if (close(info->local_fd) != 0) {
syslog(LOG_ERR, "error in close(%d): %s\n",
info->local_fd, strerror(errno));
}
if (info->shadow_fd != -1 && close(info->shadow_fd) != 0) {
syslog(LOG_ERR, "error in close(%d): %s\n",
info->shadow_fd, strerror(errno));
}
*/
}
fd = open(local_path.c_str(), fi->flags);
dsyslog("open(%s) 0x%x returned %d\n", local_path.c_str(), fi->flags, fd);
if (fd == -1)
return -errno;
info = new ShadowFileState();
fi->fh = (uint64_t)info;
info->local_fd = fd;
info->shadow_fd = -1;
info->offline = false;
open_files_[std::string(path)] = info;
if ((fi->flags & O_ACCMODE) == O_RDONLY) {
dsyslog("open(%s) write-mode not set\n", local_path.c_str());
return 0;
}
if (is_offline(path)) {
info->offline = true;
return 0;
}
std::string shadow_path = get_shadow_path(path);
fd = open(shadow_path.c_str(), fi->flags);
dsyslog("shadow open(%s) returned %d\n", shadow_path.c_str(), fd);
if (fd == -1) {
syslog(LOG_ERR, "error in shadow open(%s): %s\n",
shadow_path.c_str(), strerror(errno));
} else {
info->shadow_fd = fd;
}
return 0;
}
static int shadow_read(const char *path, char *buf, size_t size, off_t offset,
struct fuse_file_info *fi)
{
std::string local_path = DATA_DIR + path;
ShadowFileState* info = (ShadowFileState*)fi->fh;
int res = pread(info->local_fd, buf, size, offset);
if (res == -1)
res = -errno;
return res;
}
static int shadow_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
std::string local_path = DATA_DIR + path;
ShadowFileState* info = (ShadowFileState*)fi->fh;
int res = pwrite(info->local_fd, buf, size, offset);
if (res == -1)
return -errno;
if (info->offline) {
return res;
}
if (info->shadow_fd == -1) {
syslog(LOG_ERR, "shadow write(%s): no fd open for writing\n", path);
return res;
}
int res2 = pwrite(info->shadow_fd, buf, size, offset);
if (res2 == -1) {
syslog(LOG_ERR, "error in shadow write(%s): %s\n",
path, strerror(errno));
}
return res;
}
static int shadow_release(const char *path, struct fuse_file_info *fi)
{
ShadowFileState* info = (ShadowFileState*)fi->fh;
if (close(info->local_fd) != 0) {
syslog(LOG_ERR, "error in close(%d): %s\n",
info->local_fd, strerror(errno));
}
if (info->shadow_fd != -1 && close(info->shadow_fd) != 0) {
syslog(LOG_ERR, "error in close(%d): %s\n",
info->shadow_fd, strerror(errno));
}
delete info;
fi->fh = 0;
int n = open_files_.erase(std::string(path));
if (n != 1) {
syslog(LOG_ERR, "error in release(%s): %d entries in open file table\n",
path, n);
}
return 0;
}
static int shadow_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
std::string local_path = DATA_DIR + path;
ShadowFileState* info = (ShadowFileState*)fi->fh;
int res = fsync(info->local_fd);
if (res == -1)
return -errno;
if (info->shadow_fd == -1)
return res;
int res2 = fsync(info->shadow_fd);
if (res2 == -1) {
syslog(LOG_ERR, "error in shadow fsync(%s): %s\n",
path, strerror(errno));
}
return res;
}
#ifdef HAVE_SETXATTR
/* xattr operations are optional and can safely be left unimplemented */
static int shadow_setxattr(const char *path, const char *name, const char *value,
size_t size, int flags)
{
std::string local_path = DATA_DIR + path;
int res = lsetxattr(local_path.c_str(), name, value, size, flags);
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = lsetxattr(shadow_path.c_str(), name, value, size, flags);
if (res == -1) {
syslog(LOG_ERR, "error in shadow setxattr(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
static int shadow_getxattr(const char *path, const char *name, char *value,
size_t size)
{
std::string local_path = DATA_DIR + path;
int res = lgetxattr(local_path.c_str(), name, value, size);
if (res == -1)
return -errno;
return res;
}
static int shadow_listxattr(const char *path, char *list, size_t size)
{
std::string local_path = DATA_DIR + path;
int res = llistxattr(local_path.c_str(), list, size);
if (res == -1)
return -errno;
return res;
}
static int shadow_removexattr(const char *path, const char *name)
{
std::string local_path = DATA_DIR + path;
int res = lremovexattr(local_path.c_str(), name);
if (res == -1)
return -errno;
if (is_offline(path)) {
return 0;
}
std::string shadow_path = get_shadow_path(path);
res = lremovexattr(shadow_path.c_str(), name);
if (res == -1) {
syslog(LOG_ERR, "error in shadow setxattr(%s): %s\n",
shadow_path.c_str(), strerror(errno));
}
return 0;
}
#endif /* HAVE_SETXATTR */
struct fuse_operations shadow_ops;
void init_shadow_ops()
{
memset(&shadow_ops, 0, sizeof(shadow_ops));
shadow_ops.getattr = shadow_getattr;
shadow_ops.access = shadow_access;
shadow_ops.readlink = shadow_readlink;
shadow_ops.readdir = shadow_readdir;
shadow_ops.mknod = shadow_mknod;
shadow_ops.create = shadow_create;
shadow_ops.mkdir = shadow_mkdir;
shadow_ops.symlink = shadow_symlink;
shadow_ops.unlink = shadow_unlink;
shadow_ops.rmdir = shadow_rmdir;
shadow_ops.rename = shadow_rename;
shadow_ops.link = shadow_link;
shadow_ops.chmod = shadow_chmod;
shadow_ops.chown = shadow_chown;
shadow_ops.truncate = shadow_truncate;
shadow_ops.utimens = shadow_utimens;
shadow_ops.open = shadow_open;
shadow_ops.read = shadow_read;
shadow_ops.write = shadow_write;
shadow_ops.release = shadow_release;
shadow_ops.fsync = shadow_fsync;
#ifdef HAVE_SETXATTR
shadow_ops.setxattr = shadow_setxattr;
shadow_ops.getxattr = shadow_getxattr;
shadow_ops.listxattr = shadow_listxattr;
shadow_ops.removexattr = shadow_removexattr;
#endif
};