-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmfd.c
860 lines (723 loc) · 28.6 KB
/
mfd.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
/*
This file is part of ESFS, a FUSE-based filesystem that supports snapshots.
ESFS is Copyright (C) 2013, 2014 Elod Csirmaz
<http://www.epcsirmaz.com/> <https://github.com/csirmaz>.
ESFS is based on Big Brother File System (fuse-tutorial)
Copyright (C) 2012 Joseph J. Pfeiffer, Jr., Ph.D. <pfeiffer@cs.nmsu.edu>,
and was forked from it on 21 August 2013.
Big Brother File System can be distributed under the terms of
the GNU GPLv3. See the file COPYING.
See also <http://www.cs.nmsu.edu/~pfeiffer/fuse-tutorial/>.
Big Brother File System was derived from function prototypes found in
/usr/include/fuse/fuse.h
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
fuse.h is licensed under the LGPLv2.
ESFS 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 3 of the License, or (at your option) any later
version.
ESFS 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* NOTE: A Perl script is used to replace $ with esfs_ and $$ with ESFS_
* in this file. To write $, use \$.
*/
/* This file contains functions related to snapshot files and
* changes to the dentries of main files.
*
* Files in snapshots
* ==================
*
* Each snapshot acts as an overlay over the files in the main space,
* or that combined with later snapshots.
* Each file has its own overlay, at a path that mirrors its original
* location. It is stored in two files, in a .map and a .dat file.
*
* The .map file contains a list of pointers for each block in the file
* pointing to blocks in the .dat file, which stores data in those blocks
* that have been modified since the creation of the snapshot. If this
* pointer is non-0, the block need not be saved again.
*
* All original dentry information about a file is saved in the header
* of the .map file, including its size.
*
* If a file did not exist when the snapshot was taken, this fact
* is saved in the header of the .map file.
*
* The presence of the .map file means that the file in the main space is
* already dirty, and dentry changes need not be saved again in the snapshot.
*
* Reading a snapshot
* ==================
*
* Although a snapshot can be thought of as an overlay, reading
* starts at the snapshot.
*
* Get the map file.
* If there is no map file, or it is too short, or it contains
* 0 for the block, go to the next snapshot (forward in time).
*
* Ultimately, the main file can be used to read data.
*/
/** Returns if a filename in a snapshot should be presented, and if yes, how.
*
* Returns:
* 0 - if the file is internal, and should not be shown
* 1 - if the file should be shown (directories)
* 2 - if the file should be shown, but without the last $$EXT_LEN characters
*/
static inline int $mfd_filter_name(char *name)
{
$$PATH_LEN_T plen;
plen = strlen(name);
if(plen <= $$EXT_LEN) { return 1; }
name = name + plen - $$EXT_LEN;
if(strcmp(name, $$EXT_DAT) == 0) { return 0; }
if(strcmp(name, $$EXT_MAP) == 0) { return 2; }
return 1;
}
/** Write the map header to the map file
*
* Returns:
* * 0 on success
* * -errno on error
*/
// TODO 2 If this fails, the filesystem may be broken
static inline int $mfd_save_mapheader(const struct $mfd_t *mfd, const struct $fsdata_t *fsdata)
{
int ret;
// Return if there are no snapshots or the file was opened for read-only
if(mfd->mapfd < 0) { return 0; }
ret = pwrite(mfd->mapfd, &(mfd->mapheader), sizeof(struct $mapheader_t), 0);
if(unlikely(ret == -1)) {
ret = errno;
$dlogi("ERROR mfd_save_mapheader: Failed to write .map header, error %d = %s\n", ret, strerror(ret));
return -ret;
}
if(unlikely(ret != sizeof(struct $mapheader_t))) {
$dlogi("ERROR mfd_save_mapheader: Failed: only written %d bytes into .map header\n", ret);
return -EIO;
}
return 0;
}
/** Load the map header from the map file
*
* Returns:
* * 0 on success
* * -errno on error
*/
static inline int $mfd_load_mapheader(struct $mapheader_t *maphead, int fd, const struct $fsdata_t *fsdata)
{
int ret;
ret = pread(fd, maphead, sizeof(struct $mapheader_t), 0);
if(unlikely(ret == -1)) {
ret = errno;
$dlogi("ERROR Failed to read .map, error %d = %s\n", ret, strerror(ret));
return -ret;
}
if(unlikely(ret != sizeof(struct $mapheader_t))) {
$dlogi("ERROR Only read %d bytes instead of %ld from .map. Broken FS?\n", ret, sizeof(struct $mapheader_t));
return -EIO;
}
// Check the version and the signature
if(maphead->$version != 12000 || strncmp(maphead->signature, "ESFS", 4) != 0) {
$dlogi("ERROR version or signature bad in map file. Broken FS?\n");
return -EFAULT;
}
return 0;
}
// breaks below are not errors, but we want to skip opening/creating the dat file
// if the file was empty or nonexistent when the snapshot was taken
#define $$MFD_OPEN_DAT_FILE \
if(maphead->exists == 0) { \
mfd->datfd = $$MFD_FD_ENOENT; \
break; \
} \
if(unlikely(maphead->fstat.st_size == 0)) { \
mfd->datfd = $$MFD_FD_ZLEN; \
break; \
} \
if($get_dat_prefix_path(fdat, vpath, fsdata->sn_lat_dir, fsdata->sn_lat_dir_len) != 0) { \
waserror = ENAMETOOLONG; \
break; \
} \
fd_dat = open(fdat, O_WRONLY | O_CREAT | O_APPEND | O_NOATIME, S_IRWXU); \
if(fd_dat == -1){ \
fd_dat = errno; \
$dlogi("ERROR mfd_open_sn: Failed to open .dat at '%s', error %d = %s (1)\n", fdat, fd_dat, strerror(fd_dat)); \
waserror = fd_dat; \
break; \
} \
$dlogdbg("mfd_open_sn: Opened dat file at '%s' FD '%d' (vpath='%s')\n", fdat, fd_dat, vpath); \
mfd->datfd = fd_dat;
/** Opens (and initialises) the snapshot-related parts of a main MFD
*
* This is done by
* opening (and creating and initialising, if necessary) the .map and .dat files.
* Call this before modifying any file, including renaming it.
*
* Sets:
* * mfd->mapfd, the map file opened for RDWR or a negative value if unused -- see types.h
* * mfd->datfd, the dat file opened for WR|APPEND or a negative value if unused -- see types.h
* * mfd->mapheader
* * mfd->locklabel
* * mfd->sn_number
*
* Saves:
* * stats of the file into the map file, unless the map file already exists.
*
* Returns:
* * 0 - on success
* * -errno - on failure
*/
static int $mfd_open_sn(
struct $mfd_t *mfd,
const char *vpath, /**< the virtual path in the main space */
const char *fpath_in, /**< the real path of the file in the main space, or NULL */
struct $fsdata_t *fsdata // cannot be const because of the locking
)
{
char fmap[$$PATH_MAX];
char fdat[$$PATH_MAX];
char fpath_redo[$$PATH_MAX];
const char *fpath_use;
int fd; // map file FD
int fd_dat; // dat file FD
int ret;
int waserror = 0; // positive on error
int mylock = -1;
struct $mapheader_t *maphead;
// Calculate fpath if needed
if(fpath_in == NULL) { // We need to re-calculate fpath if we're re-initialising as it is not cached
if($map_path(fpath_redo, vpath, fsdata) != 0) { return -ENAMETOOLONG; }
fpath_use = fpath_redo;
} else {
fpath_use = fpath_in;
}
// Pointers
maphead = &(mfd->mapheader);
// Default values
mfd->sn_number = fsdata->sn_number; /* to see if a new snapshot was created */
mfd->is_main = $$mfd_main; /* for safety's sake */
mfd->locklabel = $string2locklabel(fpath_use); // We use fpath and not vpath here as the same label needs to be generated from sn_steps
mfd->latest_written_block_cache = 0;
strcpy(mfd->vpath, vpath); /* to be able to reinitialise the mfd in case there is a new snapshot */
// No snapshots?
if(fsdata->sn_is_any == 0) {
$dlogdbg("mfd_open_sn: no snapshots found, so returning\n");
mfd->mapfd = $$MFD_FD_NOSN;
mfd->datfd = $$MFD_FD_NOSN;
return 0;
}
// Get the paths of the map file
if($get_map_prefix_path(fmap, vpath, fsdata->sn_lat_dir, fsdata->sn_lat_dir_len) != 0) {
$dlogi("ERROR mfd_open_sn: file name too long\n");
return -ENAMETOOLONG;
}
// Create path
// $mkpath is resilient to parallel runs, so we don't lock before calling it.
ret = $mkpath(fmap, NULL, S_IRWXU);
if(ret < 0) { // error
$dlogi("ERROR mfd_open_sn: mkpath failed with '%d' = '%s'\n", -ret, strerror(-ret));
return ret;
}
// We need to lock here. Even if O_CREAT | O_EXCL is thread-safe, if we lock afterwards,
// and two threads race to create a new map file, it might not be sure that the one creating it
// will be the one getting the lock, and so we'd need additional checks to decide who
// needs to initialise the mapheader.
$dlogdbg("mfd_open_sn: getting lock for label '%lu'... (vpath='%s', fpath='%s')\n", mfd->locklabel, vpath, fpath_use);
if(unlikely((mylock = $mflock_lock(fsdata, mfd->locklabel)) < 0)) {
$dlogi("ERROR mfd_open_sn: mflock_lock(%lu) failed with '%d'='%s'\n", mfd->locklabel, -mylock, strerror(-mylock));
return mylock;
}
do { // [A]
// Open or create the map file
fd = open(fmap, O_RDWR | O_CREAT | O_EXCL | O_NOATIME, S_IRWXU);
if(fd == -1) {
fd = errno;
if(unlikely(fd != EEXIST)) { // Other error
$dlogi("ERROR mfd_open_sn: Failed to open .map at %s, error %d = %s\n", fmap, fd, strerror(fd));
waserror = fd;
break; // [A]
}
// The .map file already exists, which means that the main file is already dirty.
// TODO 2 If we aren't actually to set up an mfd, just ensure that the mapheader is written, simply return here
// Otherwise, we open the .map file again.
fd = open(fmap, O_RDWR);
if(unlikely(fd == -1)) {
waserror = errno;
$dlogi("ERROR mfd_open_sn: Failed to open .map again at %s, error %d = %s\n", fmap, waserror, strerror(waserror));
break; // [A]
}
$dlogdbg("mfd_open_sn: Managed to open .map file again at '%s', FD '%d'\n", fmap, fd);
mfd->mapfd = fd;
do { // [B] From here we either return with a positive errno, or -1 if we need to try again
if(unlikely((ret = $mfd_load_mapheader(maphead, fd, fsdata)) != 0)) {
waserror = -ret;
$dlogi("ERROR mfd_open_sn: Failed to read .map at '%s', error '%d' = '%s'\n", fmap, waserror, strerror(waserror));
break; // [B]
}
// Release lock; mark as released.
if(unlikely((ret = $mflock_unlock(fsdata, mylock)) != 0)) {
waserror = -ret;
$dlogi("ERROR mfd_open_sn: during unlock; err %d = %s\n", waserror, strerror(waserror));
break; // [B]
}
mylock = -1;
// Open or create the dat file if necessary
$dlogdbg("mfd_open_sn: about to open the dat file, vpath='%s'\n", vpath);
$$MFD_OPEN_DAT_FILE
} while(0); // [B]
if(waserror != 0) { // Cleanup in case of error
close(fd);
break; // [A]
}
// Continue below
} else { // ----- We've created a new .map file -----
do { // [C]
// We've created the .map file; let's save data about the main file.
$dlogdbg("mfd_open_sn: created a new map file at %s FD %d\n", fmap, fd);
mfd->mapfd = fd;
// Default values for a new mapheader
maphead->$version = 12000;
strncpy(maphead->signature, "ESFS", 4);
maphead->exists = 1;
// stat the main file
$dlogdbg("mfd_open_sn: stating '%s'\n", fpath_use);
if(lstat(fpath_use, &(maphead->fstat)) != 0) {
ret = errno;
if(ret == ENOENT) { // main file does not exist (yet?)
// WARNING In this case, mfd.mapheader.fstat remains uninitialised!
maphead->exists = 0;
} else { // some other error
$dlogi("ERROR mfd_open_sn: Failed to stat main file at %s, error %d = %s\n", fpath_use, ret, strerror(ret));
waserror = ret;
break; // [C]
}
}
// mfd's are not currently allowed for directories
if(unlikely(S_ISDIR(maphead->fstat.st_mode))) {
waserror = EISDIR;
$dlogi("FAILED mfd_open_sn: mfds are not allowed for directories\n");
break; // [C]
}
// write into the map file
if(unlikely((ret = $mfd_save_mapheader(mfd, fsdata)) != 0)) {
waserror = -ret;
$dlogi("ERROR mfd_open_sn: during saving the mapheader; err %d = %s\n", waserror, strerror(waserror));
break; // [C]
}
// Release lock; mark as released.
if(unlikely((ret = $mflock_unlock(fsdata, mylock)) != 0)) {
waserror = -ret;
$dlogi("ERROR mfd_open_sn: during unlock; err %d = %s\n", waserror, strerror(waserror));
break; // [C]
}
mylock = -1;
// Read information about the file as it was at the time of the snapshot
// and open or create the dat file if necessary
$$MFD_OPEN_DAT_FILE
} while(0); // [C]
if(waserror != 0) { // Cleanup
close(fd);
// TODO 2: Clean up directories created by $mkpath based on the 'firstcreated' it can return.
// However, be aware that other files being opened might already be using the directories
// created, so using $recursive_remove here is not a safe option.
// TODO 2: Clean up the dat file?
// If the lock has already been released, the map file has been initialised successfully,
// and other threads might be reading it. So don't delete it.
if(mylock >= 0) { unlink(fmap); }
break; // [A]
}
} // end else
} while(0); // [A]
// Release lock if it hasn't been released TODO Is this needed?
if(mylock >= 0) {
$dlogdbg("mfd_open_sn: Releasing leftover lock\n");
if(unlikely((ret = $mflock_unlock(fsdata, mylock)) != 0)) {
$dlogi("ERROR mfd_open_sn: mflock_unlock failed with '%d'='%s'\n", -ret, strerror(-ret));
waserror = -ret;
}
}
return -waserror;
}
/** Marks a main MFD as read-only */
static inline void $mfd_open_sn_rdonly(
struct $mfd_t *mfd
)
{
mfd->mapfd = $$MFD_FD_RDONLY;
mfd->datfd = $$MFD_FD_RDONLY;
mfd->is_main = $$mfd_main; /* for safety's sake */
}
/** Closes the snapshot-related parts of a main MFD
*
* Returns:
* * 0 on success
* * -errno on error (the last errno)
*/
static inline int $mfd_close_sn(struct $mfd_t *mfd, struct $fsdata_t *fsdata)
{
int waserror = 0;
if(mfd->datfd >= 0) {
if(unlikely(close(mfd->datfd) != 0)) {
waserror = errno;
$dlogi("ERROR mfd_close_sn: close(datfd=%d) failed with '%d'='%s'\n", mfd->datfd, waserror, strerror(waserror));
}
}
if(mfd->mapfd >= 0) {
if(unlikely(close(mfd->mapfd) != 0)) {
waserror = errno;
$dlogi("ERROR mfd_close_sn: close(mapfd=%d) failed with '%d'='%s'\n", mfd->mapfd, waserror, strerror(waserror));
}
}
return -waserror;
}
/** Initialises the map (and dat) files without leaving them open
*
* Returns:
* * 0 on success
* * -errno on error
*/
static inline int $mfd_init_sn(
const char *vpath,
const char *fpath,
struct $fsdata_t *fsdata
)
{
struct $mfd_t mymfd;
int ret;
// It's somewhat wasteful to allocate a whole mfd here,
// but we need the mapheader, which is included in it, anyway.
if((ret = $mfd_open_sn(&mymfd, vpath, fpath, fsdata)) == 0) {
ret = $mfd_close_sn(&mymfd, fsdata);
}
return ret;
}
/** Ensures that a snapshot has not been created since the mfd was initialised.
* If yes, re-initialise the mfd.
*
* Returns:
* * 0 - on success
* * -errno - on error
*/
static inline int $mfd_validate(
struct $mfd_t *mfd,
struct $fsdata_t *fsdata
)
{
int ret;
if(fsdata->sn_number == mfd->sn_number) { return 0; }
// We won't use the snapshot if the main file is opened for read only
if(mfd->mapfd == $$MFD_FD_RDONLY) { return 0; }
$dlogdbg("! Reinitialising the mfd\n");
if((ret = $mfd_close_sn(mfd, fsdata)) != 0) {
$dlogi("ERROR mfd_close_sn failed with err %d = %s\n", -ret, strerror(-ret));
return ret;
}
if((ret = $mfd_open_sn(mfd, mfd->vpath, NULL, fsdata)) != 0) {
$dlogi("ERROR mfd_open_sn failed with err %d = %s\n", -ret, strerror(-ret));
return ret;
}
mfd->sn_number = fsdata->sn_number;
return 0;
}
/** Ensures that a snapshot has not been created since the in-snapshot mfd was initialised.
* For now, only returns an error if there is a new snapshot.
*
* Returns:
* * 0 - on success
* * -errno - on error
*/
// TODO 2 Save the necessary data and reinitialise mfd
static inline int $mfd_in_sn_validate(
struct $mfd_t *mfd,
struct $fsdata_t *fsdata
)
{
if(fsdata->sn_number == mfd->sn_number) { return 0; }
$dlogi("ERROR: A snapshot has been created since this in-snapshot file was opened.\n");
return -EREMCHG;
}
/** See $mfd_get_sn_steps */
#define $$SN_STEPS_F_FILE 00000001
#define $$SN_STEPS_F_DIR 00000002
#define $$SN_STEPS_F_TYPE_UNKNOWN 00000004
#define $$SN_STEPS_F_FIRSTONLY 00000010
#define $$SN_STEPS_F_SKIPOPENDAT 00000020
#define $$SN_STEPS_F_SKIPOPENDIR 00000040
#define $$SN_STEPS_F_STATDIR 00000100
/** Closes filehandles and frees the memory associated with sn_steps
*
* Returns:
* * 0 - on success
* * -errno - on failure
*/
static int $mfd_destroy_sn_steps(struct $mfd_t *mfd, const struct $fsdata_t *fsdata)
{
int waserror = 0;
int i;
int j;
if(mfd->sn_first_file >= 0) {
for(i = mfd->sn_current; i >= 0; i--) {
j = mfd->sn_steps[i].mapfd;
if(j >= 0 && close(j) != 0) { waserror = -errno; }
j = mfd->sn_steps[i].datfd;
if(j >= 0 && close(j) != 0) { waserror = -errno; }
if(mfd->sn_steps[i].dirfd != NULL && closedir(mfd->sn_steps[i].dirfd) != 0) { waserror = -errno; }
}
}
free(mfd->sn_steps);
return waserror;
}
/** Sets up mfd->sn_steps for a path inside a snapshot.
* Can also open the map, dat, main files and directories.
*
* Flags:
* * $$SN_STEPS_F_FILE | $$SN_STEPS_F_DIR | $$SN_STEPS_F_TYPE_UNKNOWN -- what type to expect
* * $$SN_STEPS_F_FIRSTONLY -- stop after the first file found
* * $$SN_STEPS_F_SKIPOPENDAT -- skip opening the dat files and the main file
* * $$SN_STEPS_F_SKIPOPENDIR -- skip opening the directories (but stat them into mfd->mapheader.fstat)
* * $$SN_STEPS_F_STATDIR -- stat directories into mfd->mapheader.fstat
*
* Sets:
* * mfd->sn_current - index of the selected snapshot
* * mfd->sn_first_file - index of the first snapshot with information about the file
* * mfd->sn_steps
* * mfd->mapheader - from the first map file found, or
* * mfd->mapheader.fstat - from a main file or a directory
* * mfd->locklabel
* * mfd->sn_number
*
* Returns:
* * 0 - on success
* * -errno - on failure
*/
static int $mfd_get_sn_steps(
struct $mfd_t *mfd,
const struct $snpath_t *snpath,
const struct $fsdata_t *fsdata,
int flags /**< See $$SN_STEPS_F_OPENFILE, $$SN_STEPS_F_OPENDIR, $$SN_STEPS_F_FIRSTONLY */
)
{
int waserror = 0; // negative on error
int sni, ret, fd;
char knowntype;
char *s;
char mypath[$$PATH_MAX];
char mysnpath[$$PATH_MAX];
DIR *dirfd;
struct $mapheader_t maphead;
struct stat mystat;
// Default values
mfd->sn_number = fsdata->sn_number;
// Get the roots of the snapshots and the main space
if(unlikely((ret = $sn_get_paths_to(mfd, snpath, fsdata)) != 0)) {
$dlogi("ERROR mfd_get_sn_steps: sn_get_paths_to failed with %d = %s\n", -ret, strerror(-ret));
return ret;
}
// All that remains is to add the path
// Get the first path
if(snpath->is_there == $$snpath_full) {
strcpy(mypath, snpath->inpath);
} else {
mypath[0] = '\0';
}
// Initialise all FDs for the benefit of $mfd_destroy_sn_steps
mfd->sn_first_file = -1;
for(sni = mfd->sn_current; sni >= 0; sni--) {
mfd->sn_steps[sni].mapfd = $$SN_STEPS_NOTOPEN;
mfd->sn_steps[sni].datfd = $$SN_STEPS_NOTOPEN;
mfd->sn_steps[sni].dirfd = NULL;
}
for(sni = mfd->sn_current; sni >= 0; sni--) {
s = mfd->sn_steps[sni].path;
if(unlikely(strlen(mypath) + strlen(s) >= $$PATH_MAX)) {
waserror = -ENAMETOOLONG;
$dlogi("ERROR mfd_get_sn_steps: filename too long\n");
break;
}
strcat(s, mypath); // s is a pointer!
$dlogdbg("sn_step %d: '%s'\n", sni, s);
// IF WE DON'T KNOW WHETHER TO EXPECT A FILE OR A DIRECTORY
// Used when we want to stat a path
knowntype = '?';
if(flags & $$SN_STEPS_F_TYPE_UNKNOWN) {
// Test for a directory: try to stat the name as-is
ret = 0;
if(lstat(mfd->sn_steps[sni].path, &mystat) != 0) {
ret = errno;
if(ret == ENOENT) {
// Try treating this as a file
knowntype = 'f';
} else {
$dlogi("ERROR mfd_get_sn_steps: lstat on '%s' failed with %d = %s/n", mfd->sn_steps[sni].path, ret, strerror(ret));
waserror = -ret;
break;
}
} else {
knowntype = (S_ISDIR(mystat.st_mode) ? 'd' : 'f'); // 'd' also means that the directory stat is available
}
$dlogdbg("unknown type at '%s' is recognised as '%c'\n", mfd->sn_steps[sni].path, knowntype);
}
// DIRECTORY
if((knowntype == 'd') || (flags & $$SN_STEPS_F_DIR)) {
// Try to open the directory
if(!(flags & $$SN_STEPS_F_SKIPOPENDIR)) {
dirfd = opendir(mfd->sn_steps[sni].path);
if(dirfd == NULL) {
ret = errno;
if(ret == ENOENT) {
mfd->sn_steps[sni].mapfd = $$SN_STEPS_UNUSED;
mfd->sn_steps[sni].datfd = $$SN_STEPS_UNUSED;
continue;
} else {
$dlogi("ERROR mfd_get_sn_steps: opendir on '%s' failed with err %d = %s/n", mfd->sn_steps[sni].path, ret, strerror(ret));
waserror = -ret;
break;
}
}
// Successfully opened the directory
mfd->sn_steps[sni].dirfd = dirfd;
}
if(mfd->sn_first_file == -1) {
mfd->sn_first_file = sni;
if((flags & $$SN_STEPS_F_STATDIR) || (flags & $$SN_STEPS_F_SKIPOPENDIR)) {
if(knowntype == 'd') {
memcpy(&(mfd->mapheader.fstat), &mystat, sizeof(struct stat));
} else {
if(lstat(mfd->sn_steps[sni].path, &(mfd->mapheader.fstat)) != 0) {
ret = errno;
if(ret == ENOENT) {
mfd->sn_steps[sni].mapfd = $$SN_STEPS_UNUSED;
mfd->sn_steps[sni].datfd = $$SN_STEPS_UNUSED;
} else {
$dlogi("ERROR mfd_get_sn_steps: lstat on '%s' failed with err %d = %s/n", mfd->sn_steps[sni].path, ret, strerror(ret));
waserror = -ret;
break;
}
}
}
}
if(flags & $$SN_STEPS_F_FIRSTONLY) { break; }
}
// FILE
} else if((knowntype == 'f') || (flags & $$SN_STEPS_F_FILE)) {
if(sni > 0) { // in a snapshot
// Read the map file for a read directive
if(unlikely((ret = $get_map_path(mysnpath, mfd->sn_steps[sni].path)) != 0)) {
waserror = ret;
break;
}
$dlogdbg("opening the map file '%s'\n", mysnpath);
fd = open(mysnpath, O_RDONLY);
if(fd == -1) {
ret = errno;
if(ret == ENOENT) {
// This snapshot has no information about this file
mfd->sn_steps[sni].mapfd = $$SN_STEPS_UNUSED;
mfd->sn_steps[sni].datfd = $$SN_STEPS_UNUSED;
continue;
} else {
$dlogi("ERROR mfd_get_sn_steps: open on '%s' failed with err %d = %s/n", mysnpath, ret, strerror(ret));
waserror = -ret;
break;
}
}
// We save this here so that mfd_destroy_sn_steps would close it on error
mfd->sn_steps[sni].mapfd = fd;
do {
if(unlikely((ret = $mfd_load_mapheader(&maphead, fd, fsdata)) != 0)) {
$dlogi("ERROR mfd_get_sn_steps: mfd_load_mapheader failed with err %d = %s\n", -ret, strerror(-ret));
waserror = ret;
break;
}
/*
* Check here if we need to use a different path one level up
* and modify mypath.
* (See the comment about path maps in fuse_path_write.c)
*/
} while(0);
// Open the dat file
if(!(flags & $$SN_STEPS_F_SKIPOPENDAT)) {
if(unlikely((ret = $get_dat_path(mysnpath, mfd->sn_steps[sni].path)) != 0)) {
waserror = ret;
break;
}
$dlogdbg("opening the dat file '%s'\n", mysnpath);
fd = open(mysnpath, O_RDONLY);
if(fd == -1) {
ret = errno;
if(ret == ENOENT) {
mfd->sn_steps[sni].datfd = $$SN_STEPS_UNUSED;
continue;
} else {
$dlogi("ERROR mfd_get_sn_steps: open on '%s' failed with %d = %s/n", mysnpath, ret, strerror(ret));
waserror = -ret;
break;
}
}
// We save this here so that mfd_destroy_sn_steps would close it on error
mfd->sn_steps[sni].datfd = fd;
}
} else { // a main file
mfd->sn_steps[sni].mapfd = $$SN_STEPS_MAIN;
// Initialise the lock label
mfd->locklabel = $string2locklabel(mfd->sn_steps[sni].path);
if(flags & $$SN_STEPS_F_SKIPOPENDAT) {
mfd->sn_steps[sni].datfd = $$SN_STEPS_NOTOPEN;
} else {
// Try to open the file
$dlogdbg("opening the main file '%s'\n", mfd->sn_steps[sni].path);
fd = open(mfd->sn_steps[sni].path, O_RDONLY);
if(fd == -1) {
ret = errno;
if(ret == ENOENT) {
mfd->sn_steps[sni].datfd = $$SN_STEPS_UNUSED;
continue;
} else {
$dlogi("ERROR mfd_get_sn_steps: open on '%s' failed with %d = %s/n", mysnpath, ret, strerror(ret));
waserror = -ret;
break;
}
}
// We save this here so that mfd_destroy_sn_steps would close it on error
mfd->sn_steps[sni].datfd = fd;
}
}
if(mfd->sn_first_file == -1) { // first existing file found
mfd->sn_first_file = sni;
if(sni > 0) {
// Save the mapheader from the first map file found
$dlogdbg("stating: saving the map header\n");
memcpy(&(mfd->mapheader), &maphead, sizeof(struct $mapheader_t));
} else {
// stat the main file as well
$dlogdbg("stating the main file '%s'\n", mfd->sn_steps[sni].path);
if(unlikely(lstat(mfd->sn_steps[sni].path, &(mfd->mapheader.fstat)) != 0)) {
waserror = -errno;
$dlogi("ERROR mfd_get_sn_steps: stating '%s' failed with err %d = %s\n", mfd->sn_steps[sni].path, -waserror, strerror(-waserror));
break;
}
}
if(flags & $$SN_STEPS_F_FIRSTONLY) { break; }
}
} else {
$dlogi("ERROR mfd_get_sn_steps: Wrong flag!");
waserror = -EBADE;
}
if(waserror != 0) {
break;
}
} // end for
if(waserror != 0) {
$mfd_destroy_sn_steps(mfd, fsdata);
return waserror;
}
return 0;
}