-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathutil.c
1772 lines (1591 loc) · 40 KB
/
util.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
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
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* util.c */
/*
* Copyright (c) 2022 Stephen D. Adams <stephen@sdadams.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/stat.h>
#include <sys/syslimits.h>
#include <sys/types.h>
#include <errno.h>
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "ds.h"
#include "muxfs.h"
#include "gen.h"
struct muxfs_args muxfs_cmdline;
static int muxfs_restore_reg(dind, dind, const char *, int, struct stat *,
struct muxfs_meta *);
static int muxfs_restore_symlink(dind, dind, const char *, struct stat *,
struct muxfs_meta *);
/*
* This enum classifies the difference of a directory's content with respect to
* the influence of a single specified file.
*/
enum muxfs_dir_patch_type {
MUXFS_SUBSTITUTE, /* The content of the file is different. */
MUXFS_PLUS, /* The filename is not listed in the base directory. */
MUXFS_MINUS, /* The filename is not listed in the patched directory. */
};
struct muxfs_dir_patch {
enum muxfs_dir_patch_type type;
const char *fname;
const uint8_t *sum;
};
static int
muxfs_dir_patch_sums(uint8_t *as_is_out, uint8_t *with_patch_out,
enum muxfs_chk_alg_type alg, dind dev_index, int dirfd,
struct muxfs_dir *dir, struct muxfs_dir_patch *patch)
{
int rc;
size_t chksz;
struct stat subst;
struct dirent *dirent;
size_t i, dnamelen, fnamelen, entind1, entind2;
const char *dname;
int match;
ino_t subino;
struct muxfs_meta submeta;
struct muxfs_chk as_is_content_chk, with_patch_content_chk;
int patched;
rc = 1;
chksz = muxfs_chk_size(alg);
fnamelen = strlen(patch->fname);
muxfs_chk_init(&as_is_content_chk, alg);
muxfs_chk_init(&with_patch_content_chk, alg);
entind1 = 0;
for (i = 0; i < dir->ent_count; ++i) {
dirent = dir->ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 2) == 0))
continue;
if ((dnamelen == 6) && (strncmp(".muxfs", dname, 6)
== 0))
continue;
if (strcmp(dname, patch->fname) >= 0)
break;
++entind1;
}
patched = 0;
entind2 = 0;
for (i = 0; i < dir->ent_count; ++i) {
dirent = dir->ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 2) == 0))
continue;
if ((dnamelen == 6) && (strncmp(".muxfs", dname, 6) == 0))
continue;
if (fstatat(dirfd, dname, &subst, AT_SYMLINK_NOFOLLOW))
goto out;
subino = subst.st_ino;
if (muxfs_meta_read(&submeta, dev_index, subino))
goto out;
muxfs_chk_update(&as_is_content_chk, (uint8_t *)dname,
dnamelen);
muxfs_chk_update(&as_is_content_chk, &submeta.checksums[0],
chksz);
if (entind2 == entind1) {
patched = 1;
match = ((dnamelen == fnamelen) &&
(strncmp(patch->fname, dname, fnamelen) == 0));
if (patch->type == MUXFS_MINUS) {
if (!match)
goto out;
++entind2;
continue;
}
muxfs_chk_update(&with_patch_content_chk,
(uint8_t *)patch->fname, fnamelen);
muxfs_chk_update(&with_patch_content_chk, patch->sum,
chksz);
switch (patch->type) {
case MUXFS_SUBSTITUTE:
if (!match)
goto out;
++entind2;
continue;
break;
case MUXFS_PLUS:
if (match)
goto out;
break;
default:
exit(-1); /* Programming error. */
}
}
muxfs_chk_update(&with_patch_content_chk, (uint8_t *)dname,
dnamelen);
muxfs_chk_update(&with_patch_content_chk, &submeta.checksums[0],
chksz);
++entind2;
}
if (!patched) {
if (patch->type != MUXFS_PLUS)
goto out;
muxfs_chk_update(&with_patch_content_chk,
(uint8_t *)patch->fname, fnamelen);
muxfs_chk_update(&with_patch_content_chk, patch->sum, chksz);
}
muxfs_chk_final(as_is_out, &as_is_content_chk);
muxfs_chk_final(with_patch_out, &with_patch_content_chk);
rc = 0;
out:
return rc;
}
MUXFS int
muxfs_dir_meta_recompute(struct muxfs_cud *pcud_out, dind dev_index,
const struct muxfs_cud *ccud_in)
{
int rc;
struct muxfs_dev *dev;
int root_fd, dirfd;
struct stat st;
enum muxfs_chk_alg_type alg;
size_t chksz;
ino_t ino;
struct muxfs_dir dir;
struct muxfs_desc pre_desc, post_desc;
uint64_t eno;
struct muxfs_meta db_pre_meta, pre_meta, post_meta;
struct muxfs_dir_patch patch;
rc = 1;
if (muxfs_dev_get(&dev, dev_index, 0))
goto out;
root_fd = dev->root_fd;
alg = dev->conf.chk_alg_type;
chksz = muxfs_chk_size(alg);
if (muxfs_pushdir(&dir, root_fd, ccud_in->path))
goto out;
if ((dirfd = openat(root_fd, ccud_in->path, O_RDONLY|O_NOFOLLOW)) == -1)
goto out2;
if (fstat(dirfd, &st))
goto out3;
if (!S_ISDIR(st.st_mode))
goto out3;
ino = st.st_ino;
if (muxfs_meta_read(&db_pre_meta, dev_index, ino))
goto out3;
eno = db_pre_meta.header.eno;
if (muxfs_desc_init_from_stat(&pre_desc, &st, eno))
goto out3;
if (muxfs_desc_init_from_stat(&post_desc, &st, eno))
goto out3;
/*
* If this seems backwards it is because we are differencing the
* directory after having applied the operation.
*/
switch (ccud_in->type) {
case MUXFS_CUD_CREATE:
patch.type = MUXFS_MINUS;
break;
case MUXFS_CUD_UPDATE:
patch.type = MUXFS_SUBSTITUTE;
break;
case MUXFS_CUD_DELETE:
patch.type = MUXFS_PLUS;
break;
default:
exit(-1); /* Programming error. */
}
patch.fname = ccud_in->fname;
patch.sum = &ccud_in->pre_meta.checksums[0];
if (muxfs_dir_patch_sums(post_desc.content_checksum,
pre_desc.content_checksum, alg, dev_index, dirfd, &dir,
&patch))
goto out3;
pre_meta.header = post_meta.header = (struct muxfs_meta_header) {
.flags = MF_ASSIGNED,
.eno = eno,
};
muxfs_desc_chk_meta(&pre_meta.checksums[0], &pre_desc, alg);
muxfs_desc_chk_meta(&post_meta.checksums[0], &post_desc, alg);
memcpy(&pre_meta.checksums[chksz], pre_desc.content_checksum, chksz);
memcpy(&post_meta.checksums[chksz], post_desc.content_checksum, chksz);
if (bcmp(&pre_meta.checksums[0], &db_pre_meta.checksums[0], chksz) != 0)
goto out3;
if (bcmp(&pre_meta.checksums[chksz], &db_pre_meta.checksums[chksz],
chksz) != 0)
goto out3;
if (muxfs_meta_write(&post_meta, dev_index, ino))
goto out3;
if (fsync(dev->meta_fd))
exit(-1);
if (muxfs_readback(dev_index, ccud_in->path, 0, &post_meta))
goto out3;
pcud_out->type = MUXFS_CUD_UPDATE;
pcud_out->pre_meta = pre_meta;
rc = 0;
out3:
if (close(dirfd))
exit(-1);
out2:
if (muxfs_popdir(&dir))
exit(-1);
out:
return rc;
}
/*
* Returns 1 if 'path' points to the root directory, otherwise returns 0.
* Assumes that 'path' has been through muxfs_path_sanitize().
*/
static int
muxfs_path_is_root(const char *path)
{
return strcmp(".", path) == 0;
}
/*
* Computes the meta checksum of 'path' in dev 'i'. If the computed checksum
* does not match that in the meta.db file then 1 is returned. Otherwise 0 is
* returned. If 'shallow' is non-zero then the content checksum used to
* compute the meta checksum will be taken from that in the meta.db file,
* otherwise the content checksum will be computed from the file content. If
* 'expected' is not NULL and the computed checksum does not match that in
* 'expected' then 1 is returned.
*/
MUXFS int
muxfs_readback(dind i, const char *path, int shallow,
const struct muxfs_meta *expected)
{
struct muxfs_dev *dev;
int root_fd;
enum muxfs_chk_alg_type alg;
size_t chksz;
struct stat st;
ino_t ino;
struct muxfs_meta meta;
uint64_t eno;
struct muxfs_desc desc;
uint8_t meta_chk_buf[MUXFS_CHKSZ_MAX];
if (muxfs_dev_get(&dev, i, 0))
goto fail;
root_fd = dev->root_fd;
alg = dev->conf.chk_alg_type;
chksz = muxfs_chk_size(alg);
if (fstatat(root_fd, path, &st, AT_SYMLINK_NOFOLLOW))
goto fail;
ino = st.st_ino;
if (muxfs_meta_read(&meta, i, ino))
goto fail;
eno = meta.header.eno;
if (muxfs_desc_init_from_stat(&desc, &st, eno))
goto fail;
if (shallow)
memcpy(desc.content_checksum, &meta.checksums[chksz], chksz);
else {
if (muxfs_desc_chk_node_content(&desc, i, path))
goto fail;
}
muxfs_desc_chk_meta(meta_chk_buf, &desc, alg);
if (bcmp(meta_chk_buf, &meta.checksums[0], chksz) != 0)
goto fail;
if ((expected != NULL) &&
(bcmp(meta_chk_buf, &expected->checksums[0], chksz) != 0))
goto fail;
return 0;
fail:
return 1;
}
MUXFS int
muxfs_parent_readback(dind i, const char *path)
{
char ppath[PATH_MAX];
size_t path_len;
if (muxfs_path_is_root(path))
return 1;
path_len = strlen(path);
memcpy(ppath, path, path_len);
ppath[path_len] = '\0';
if (muxfs_path_pop(NULL, ppath, NULL)) {
memset(ppath, 0, PATH_MAX);
strcpy(ppath, ".");
}
return muxfs_readback(i, ppath, 0, NULL);
}
static void
muxfs_path_trailing_seps_strip(char *path, size_t path_len)
{
char *sep;
sep = strrchr(path, '/');
if (sep == NULL)
return;
while ((sep - path) == path_len) {
*sep = '\0';
--path_len;
if (path_len == 0)
return;
sep = strrchr(path, '/');
if (sep == NULL)
return;
}
}
/*
* Replaces '/' in path with '\0' in order to split the path into directory and
* filename components. If fname_out is not NULL then *fname_out is set to
* point to the start of the filename component after the replaced '/'.
* Returns 1 if there was not a preceeding path component, otherwise returns 0.
*/
MUXFS int
muxfs_path_pop(const char **fname_out, char *path, size_t *path_len_inout)
{
char *sep, *fname;
size_t path_len;
if (path_len_inout != NULL)
path_len = *path_len_inout;
else
path_len = strlen(path);
muxfs_path_trailing_seps_strip(path, path_len);
path_len = strlen(path);
if (path_len == 0)
return 1;
sep = strrchr(path, '/');
if (sep == NULL)
return 1;
fname = sep + 1;
*sep = '\0';
path_len = strlen(path);
if (path_len == 0)
return 1;
muxfs_path_trailing_seps_strip(path, path_len);
path_len = strlen(path);
if (path_len == 0)
return 1;
if (path_len_inout != NULL)
*path_len_inout = path_len;
if (fname_out != NULL)
*fname_out = fname;
return 0;
}
/* Returns 1 if the final path component of 'path' is ".muxfs", 0 otherwise. */
static int
muxfs_path_is_dot_muxfs(const char *_path)
{
char path[PATH_MAX];
const char *fname;
size_t path_len;
path_len = strlen(_path);
memcpy(path, _path, path_len);
path[path_len] = '\0';
if (muxfs_path_pop(&fname, path, &path_len))
fname = path;
if ((strlen(fname) == 6) && (strncmp(fname, ".muxfs", 6) == 0))
return 1;
return 0;
}
MUXFS int
muxfs_path_sanitize(const char **path_inout)
{
const char *path;
for (path = *path_inout; path[0] == '/'; ++path);
if (path[0] == '\0')
path = ".";
if (muxfs_path_is_dot_muxfs(path))
return 1;
*path_inout = path;
return 0;
}
MUXFS int
muxfs_ancestors_meta_recompute(dind dev_index, struct muxfs_cud *cud)
{
char path[PATH_MAX];
const char *fname;
size_t path_len;
struct muxfs_cud pcud, ccud;
/* There are no ancestors of the root path. */
if (muxfs_path_is_root(cud->path))
return 0;
ccud = *cud;
if (strlen(cud->path) >= PATH_MAX)
exit(-1);
path_len = strlen(cud->path);
memcpy(path, cud->path, path_len);
path[path_len] = '\0';
while (!muxfs_path_pop(&fname, path, &path_len)) {
ccud.path = path;
ccud.fname = fname;
if (muxfs_dir_meta_recompute(&pcud, dev_index, &ccud))
return 1;
ccud = pcud;
}
if (path_len == 0)
exit(-1); /* Programming error. */
/* Account for the special case of the root directory. */
ccud.path = ".";
ccud.fname = path;
if (muxfs_dir_meta_recompute(&pcud, dev_index, &ccud))
return 1;
return 0;
}
MUXFS int
muxfs_existsat(int *exists_out, int fd, const char *path)
{
struct stat st;
if (fstatat(fd, path, &st, AT_SYMLINK_NOFOLLOW)) {
if (errno != ENOENT)
return 1;
*exists_out = 0;
return 0;
}
*exists_out = 1;
return 0;
}
MUXFS int
muxfs_dir_is_empty(int *empty_out, char const *path)
{
struct muxfs_dir dir;
size_t i, dnamelen;
const char *dname;
struct dirent *dirent;
int empty;
if (muxfs_pushdir(&dir, AT_FDCWD, path))
return 1;
empty = 1;
for (i = 0; i < dir.ent_count; ++i) {
dirent = dir.ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 1) == 0))
continue;
empty = 0;
break;
}
if (muxfs_popdir(&dir))
exit(-1);
*empty_out = empty;
return 0;
}
static int
muxfs_alphasort(const void *v1, const void *v2)
{
const struct dirent **d1, **d2;
d1 = (const struct dirent **)v1;
d2 = (const struct dirent **)v2;
return alphasort(d1, d2);
}
/*
* After use muxfs_popdir() must be called on 'dir_out' and must be done
* in-order with respect to all other muxfs_dspop() and muxfs_popdir() calls.
* Read ds.h for more information.
*/
MUXFS int
muxfs_pushdir(struct muxfs_dir *dir_out, int fd, const char *path)
{
struct stat st;
size_t blksz;
uint8_t *dirbuf;
int dirfd;
struct dirent *dirent;
ssize_t rdsz, i, rdend;
struct dirent **ent_array;
size_t ent_count, j;
if (fstatat(fd, path, &st, AT_SYMLINK_NOFOLLOW))
return 1;
if (!S_ISDIR(st.st_mode))
return 1;
blksz = st.st_blksize;
if (muxfs_dspush((void **)&dirbuf, blksz))
exit(-1);
if ((dirfd = openat(fd, path, O_RDONLY|O_DIRECTORY|O_NOFOLLOW)) == -1)
goto fail;
rdend = 0;
ent_count = 0;
while ((rdsz = getdents(dirfd, &dirbuf[rdend], blksz)) > 0) {
for (i = 0; i < rdsz; i += dirent->d_reclen) {
dirent = (struct dirent *)&dirbuf[i];
++ent_count;
}
rdend += i;
if (muxfs_dsgrow((void **)&dirbuf, blksz))
exit(-1);
}
if (rdsz == -1)
goto fail2;
if (close(dirfd))
exit(-1);
if (muxfs_dspush((void **)&ent_array,
ent_count * sizeof(struct dirent *)))
exit(-1);
for (i = 0, j = 0; i < rdend; i += dirent->d_reclen, ++j)
dirent = ent_array[j] = (struct dirent *)&dirbuf[i];
qsort(ent_array, ent_count, sizeof(struct dirent *), muxfs_alphasort);
*dir_out = (struct muxfs_dir) {
.base = dirbuf,
.ent_array = ent_array,
.ent_count = ent_count,
};
return 0;
fail2:
if (close(dirfd))
exit(-1);
fail:
if (muxfs_dspop(dirbuf))
exit(-1);
return 1;
}
MUXFS int
muxfs_popdir(struct muxfs_dir *dir)
{
if (muxfs_dspop(dir->ent_array))
exit(-1);
if (muxfs_dspop(dir->base))
exit(-1);
return 0;
}
/*
* 'path' is required to be null-terminated and pointing to a buffer of
* capacity PATH_MAX, 'len' is provided so that 'path' may be mutated, then
* returned to its original state.
*/
static int
muxfs_removeat_impl(int fd, char *path, size_t len)
{
int rc;
struct stat st;
struct muxfs_dir dir;
struct dirent *dirent;
size_t i, sublen, dnamelen;
const char *dname;
if (fstatat(fd, path, &st, AT_SYMLINK_NOFOLLOW))
return 1;
if (S_ISDIR(st.st_mode)) {
rc = 1;
if (muxfs_pushdir(&dir, fd, path))
goto dirout;
for (i = 0; i < dir.ent_count; ++i) {
dirent = dir.ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 2) == 0))
continue;
if ((dnamelen == 6) && (strncmp(".muxfs", dname, 6)
== 0))
goto dirout2;
sublen = len + 1 + dnamelen;
if (sublen >= PATH_MAX)
goto dirout2;
strcat(path, "/");
strcat(path, dname);
if (muxfs_removeat_impl(fd, path, sublen))
goto dirout2;
path[len] = '\0';
}
if (unlinkat(fd, path, AT_REMOVEDIR))
goto dirout2;
rc = 0;
dirout2:
if (muxfs_popdir(&dir))
exit(-1);
dirout:
return rc;
}
if (!(S_ISREG(st.st_mode) || S_ISLNK(st.st_mode)))
return 1;
if (unlinkat(fd, path, 0))
return 1;
return 0;
}
MUXFS int
muxfs_removeat(int fd, const char *_path)
{
char path[PATH_MAX];
size_t len;
len = strlen(_path);
memset(path, 0, PATH_MAX);
memcpy(path, _path, len);
return muxfs_removeat_impl(fd, path, len);
}
static int
muxfs_restore_dir(dind ddev_index, dind sdev_index, const char *path,
int sfd, struct stat *sst, struct muxfs_meta *expected)
{
int rc;
struct muxfs_dev *sdev,
*ddev;
struct stat dst;
char pathbuf[PATH_MAX];
size_t pathlen,
dnamelen;
const char *dname;
struct muxfs_dir dir;
struct dirent *dirent;
size_t i;
enum muxfs_chk_alg_type alg;
size_t chksz;
struct muxfs_chk chk;
uint8_t sum[MUXFS_CHKSZ_MAX];
struct stat subst;
ino_t subino;
struct muxfs_meta submeta;
int subfd;
int dfd;
ino_t dino;
struct muxfs_assign assign;
int exists;
rc = 1;
subfd = -1;
if (muxfs_dev_get(&ddev, ddev_index, 0))
goto out;
if (muxfs_dev_get(&sdev, sdev_index, 0))
goto out;
alg = sdev->conf.chk_alg_type;
chksz = muxfs_chk_size(alg);
pathlen = strlen(path);
if (fstatat(ddev->root_fd, path, &dst, AT_SYMLINK_NOFOLLOW)) {
if (errno != ENOENT)
goto out;
exists = 0;
} else {
exists = 1;
if (!S_ISDIR(dst.st_mode)) {
if (!(S_ISREG(dst.st_mode) || S_ISLNK(dst.st_mode)))
goto out;
if (unlinkat(ddev->root_fd, path, 0))
goto out;
exists = 0;
}
}
if (!exists) {
if (mkdirat(ddev->root_fd, path, sst->st_mode))
goto out;
}
/* Chmod first to prevent privilege escalation. */
if (fchmodat(ddev->root_fd, path, sst->st_mode, AT_SYMLINK_NOFOLLOW))
goto out;
if (fchownat(ddev->root_fd, path, sst->st_uid, sst->st_gid,
AT_SYMLINK_NOFOLLOW))
goto out;
/* Chmod again since chown can unset suid/sgid bits. */
if (fchmodat(ddev->root_fd, path, sst->st_mode, AT_SYMLINK_NOFOLLOW))
goto out;
if (muxfs_pushdir(&dir, sdev->root_fd, path))
goto out;
muxfs_chk_init(&chk, alg);
for (i = 0; i < dir.ent_count; ++i) {
dirent = dir.ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 2) == 0))
continue;
if ((dnamelen == 6) && (strncmp(".muxfs", dname, 6) == 0))
continue;
if (pathlen + 1 + dnamelen >= PATH_MAX)
goto out2;
if (fstatat(sfd, dname, &subst, AT_SYMLINK_NOFOLLOW))
goto out2;
subino = subst.st_ino;
if (muxfs_meta_read(&submeta, sdev_index, subino))
goto out2;
muxfs_chk_update(&chk, (uint8_t *)dname, dnamelen);
muxfs_chk_update(&chk, &submeta.checksums[0], chksz);
}
muxfs_chk_final(sum, &chk);
if (bcmp(sum, &expected->checksums[chksz], chksz) != 0) {
if (muxfs_state_restore_push_back(sdev_index, path))
exit(-1);
goto out2;
}
for (i = 0; i < dir.ent_count; ++i) {
dirent = dir.ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 1) == 0))
continue;
if ((dnamelen == 6) && (strncmp(".muxfs", dname, 6) == 0))
continue;
if (pathlen + 1 + dnamelen >= PATH_MAX)
goto out2;
memset(pathbuf, 0 , PATH_MAX);
strcpy(pathbuf, path);
strcat(pathbuf, "/");
strcat(pathbuf, dname);
if (fstatat(sfd, dname, &subst, AT_SYMLINK_NOFOLLOW))
goto out2;
subino = subst.st_ino;
if (muxfs_meta_read(&submeta, sdev_index, subino))
goto out2;
if (S_ISLNK(subst.st_mode)) {
if (muxfs_restore_symlink(ddev_index, sdev_index,
pathbuf, &subst, &submeta))
goto subout;
} else {
if ((subfd = openat(sdev->root_fd, pathbuf,
O_RDONLY|O_NOFOLLOW)) == -1)
goto out2;
if (S_ISDIR(subst.st_mode)) {
if (muxfs_restore_dir(ddev_index, sdev_index,
pathbuf, subfd, &subst, &submeta))
goto subout;
} else if (S_ISREG(subst.st_mode)) {
if (muxfs_restore_reg(ddev_index, sdev_index,
pathbuf, subfd, &subst, &submeta))
goto subout;
} else {
if (muxfs_state_restore_push_back(sdev_index,
pathbuf))
exit(-1);
goto subout;
}
}
if (muxfs_readback(ddev_index, pathbuf, 0, &submeta))
goto subout;
if (subfd != -1) {
if (close(subfd))
exit(-1);
subfd = -1;
}
continue;
subout:
if (subfd != -1) {
if (close(subfd))
exit(-1);
subfd = -1;
}
goto out2;
}
if (muxfs_popdir(&dir))
exit(-1);
if (muxfs_pushdir(&dir, ddev->root_fd, path))
goto out;
for (i = 0; i < dir.ent_count; ++i) {
dirent = dir.ent_array[i];
dname = dirent->d_name;
dnamelen = dirent->d_namlen;
if ((dnamelen == 1) && (strncmp(".", dname, 1) == 0))
continue;
if ((dnamelen == 2) && (strncmp("..", dname, 1) == 0))
continue;
if ((dnamelen == 6) && (strncmp(".muxfs", dname, 6) == 0))
continue;
if (muxfs_existsat(&exists, sfd, dname))
goto out2;
if (!exists) {
memset(pathbuf, 0 , PATH_MAX);
if (pathlen + 1 + dnamelen >= PATH_MAX)
goto out2;
strcpy(pathbuf, path);
strcat(pathbuf, "/");
strcat(pathbuf, dname);
muxfs_removeat(ddev->root_fd, pathbuf);
}
}
if ((dfd = openat(ddev->root_fd, path, O_RDONLY)) == -1)
goto out2;
if (fstat(dfd, &dst))
goto out3;
dino = dst.st_ino;
assign = (struct muxfs_assign) {
.flags = AF_ASSIGNED,
.ino = dino
};
if (muxfs_meta_write(expected, ddev_index, dino))
goto out3;
if (muxfs_assign_write(&assign, ddev_index, expected->header.eno))
goto out3;
if (fsync(dfd))
exit(-1);
if (fsync(ddev->meta_fd))
exit(-1);
if (fsync(ddev->assign_fd))
exit(-1);
rc = 0;
out3:
if (close(dfd))
exit(-1);
out2:
if (muxfs_popdir(&dir))
exit(-1);
out:
return rc;
}
static int
muxfs_copy_reg(int dfd, int sfd, size_t content_sz)
{
int rc;
struct muxfs_range r;
size_t i_offset, txsz;
uint64_t i;
uint8_t content_buf[MUXFS_BLOCK_SIZE];
rc = 1;
r.byte_begin = 0;
r.byte_end = content_sz;
muxfs_range_compute(&r, 0);
for (i = r.blk_index_begin; i < r.blk_index_end; ++i) {
i_offset = i * MUXFS_BLOCK_SIZE;
txsz = MUXFS_BLOCK_SIZE;
if (i_offset + txsz > content_sz)
txsz = content_sz - i_offset;
if (pread(sfd, content_buf, txsz, i_offset) != txsz)
goto out;
if (pwrite(dfd, content_buf, txsz, i_offset) != txsz)
goto out;
}
rc = 0;
out:
return rc;
}
static int
muxfs_restore_reg(dind ddev_index, dind sdev_index, const char *path,
int sfd, struct stat *sst, struct muxfs_meta *expected)
{
int rc;
struct muxfs_dev *sdev,
*ddev;
enum muxfs_chk_alg_type alg;
size_t chksz;
uint64_t eno;
size_t content_sz;
int dfd;
struct stat dst;
ino_t dino;
struct muxfs_assign assign;
struct stat slfile_st;
int dlfd,
slfd;
int exists;
rc = 1;
dfd = -1;
dlfd = -1;
slfd = -1;
if (muxfs_dev_get(&ddev, ddev_index, 0))
goto out;
if (muxfs_dev_get(&sdev, sdev_index, 0))
goto out;
alg = sdev->conf.chk_alg_type;
chksz = muxfs_chk_size(alg);
eno = expected->header.eno;
content_sz = sst->st_size;
if (muxfs_readback(sdev_index, path, 0, expected))
goto out;
if ((dfd = openat(ddev->root_fd, path, O_WRONLY|O_CREAT|O_TRUNC,
sst->st_mode)) == -1)
goto out;
if (muxfs_copy_reg(dfd, sfd, content_sz))
goto out;
if (fchown(dfd, sst->st_uid, sst->st_gid))
goto out;
if (fstat(dfd, &dst))