-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtagdb_fs.lc
829 lines (744 loc) · 19.3 KB
/
tagdb_fs.lc
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
#include <stdlib.h>
#include <ctype.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include "params.h"
#include "util.h"
#include "tagdb.h"
#include "tagdb_fs.h"
#include "tagdb_util.h"
#include "path_util.h"
#include "fs_util.h"
#include "set_ops.h"
#include "file_log.h"
#include "fs_util.h"
#include "subfs.h"
static file_id_t get_id_number_from_file_name(char *name, char**new_start)
{
/* Returns 0 if there isn't an ID that can be extracted from the file */
char *p = strstr(name, FIS);
file_id_t file_id = 0;
*new_start = name;
if (p)
{
/* Make sure it's really a number here */
for (char *q = name; q < p; q++)
{
if (!isdigit(*q))
{
return 0;
}
}
file_id = strtoull(name,NULL,10);
*new_start = p + FIS_LENGTH; /* Put new start on the character after the colon */
}
return file_id;
}
Tag *path_to_tag (const char *path)
{
Tag *res = NULL;
if (g_strcmp0(path, "/") == 0)
{
return NULL;
}
char *base = g_path_get_basename(path);
char *dir = g_path_get_dirname(path);
tagdb_key_t path_key = path_extract_key(dir);
if (path_key)
{
res = lookup_tag(DB, base);
if (res != NULL)
{
GList *tags = get_tags_list(DB, path_key);
if (!(g_list_find(tags, res) ||
stage_lookup(STAGE, path_key, tag_id(res))))
{
res = NULL;
}
debug("path_to_tag, res = %d", res);
g_list_free(tags);
}
key_destroy(path_key);
}
g_free(base);
g_free(dir);
return res;
}
Tag *is_directory (const char *path)
{
if (g_strcmp0(path, "/") == 0)
{
/* XXX: We are, of course assuming that there isn't any tag
* at memory address 1. Generally a safe assumption, haha.
*/
return (Tag*)TRUE;
}
return path_to_tag(path);
}
char *file_realpath (File *f)
{
return tagfs_realpath_i(get_file_id((AbstractFile*)f));
}
// returns the file in our copies directory corresponding to
// the one in path
// should only be called on regular files since
// directories are only virtual
char *tagfs_realpath_i (file_id_t id)
{
char *res = g_strdup_printf("%s/%ld", FSDATA->copiesdir, id);
debug("realpath = \"%s\"", res);
return res;
}
File *path_to_file (const char *path)
{
char *base = g_path_get_basename(path);
char *dir = g_path_get_dirname(path);
char *new_start;
file_id_t idx = get_id_number_from_file_name(base, &new_start);
tagdb_key_t key = path_extract_key(dir);
File *f;
if (idx != 0)
{
f = retrieve_file(DB, idx);
}
else
{
f = tagdb_lookup_file(DB, key, new_start);
}
key_destroy(key);
g_free(base);
g_free(dir);
return f;
}
// turn the path into a file in the copies directory
// path_to_file_search_string + tagdb_query + file_realpath
// NULL for a file that DNE
char *get_file_copies_path (const char *path)
{
File *f = path_to_file(path);
if (f)
return file_realpath(f);
else
return NULL;
}
/* Translates the path into a NULL-terminated
* vector of Tag IDs, the key format for our
* FileTrie
*/
tagdb_key_t path_extract_key (const char *path)
{
if (strcmp(path, "/") == 0)
{
return key_new();
}
/* Get the path components */
char **comps = split_path(path);
tagdb_key_t key = key_new();
for (int i = 0; comps[i] != NULL; i++)
{
debug("path_extract_key:comps[i]=%s\n", comps[i]);
Tag *t = lookup_tag(DB, comps[i]);
if (t == NULL)
{
debug("path_extract_key t == NULL\n");
g_strfreev(comps);
key_destroy(key);
return NULL;
}
key_push_end(key, tag_id(t));
}
g_strfreev(comps);
return key;
}
%(path_check path)
{
return TRUE;
}
// Also returns the file object if it is a file
File *is_file (const char *path)
{
return path_to_file(path);
}
%(op utimens path timespecs)
{
int retstat = -1;
File *f = path_to_file(path);
if (f)
{
char *fpath = file_realpath(f);
retstat = utimensat(0, fpath, timespecs, 0);
g_free(fpath);
}
return retstat;
}
%(op getattr path statbuf)
{
int retstat = -ENOENT;
Tag *t = is_directory(path);
if (t)
{
statbuf->st_mode = DIR_PERMS;
if (t != (Tag*)TRUE)
{
statbuf->st_ino = 0 - tag_id(t);
}
else
{
statbuf->st_ino = 0;
}
retstat = 0;
}
else
{
File *f = is_file(path);
if (f)
{
char *fpath = file_realpath(f);
debug("getattr:fpath = %s", fpath);
int stat = lstat(fpath, statbuf);
statbuf->st_ino = file_id(f);
if (stat == -1)
{
retstat = -errno;
}
else
{
retstat = 0;
}
debug("getattr:retstat = %d", retstat);
g_free(fpath);
}
}
return retstat;
}
int _move_directory(const char *path, const char *new_path);
int _move_file(const char *path, const char *newpath);
%(op rename path newpath)
{
int retstat = 0;
tagdb_begin_transaction(DB);
if (is_directory(path))
{
retstat = _move_directory(path, newpath);
}
else
{
retstat = _move_file(path, newpath);
}
tagdb_end_transaction(DB);
return retstat;
}
int _move_directory(const char *path, const char *newpath)
{
char *oldbase = g_path_get_basename(path);
char *newbase = g_path_get_basename(newpath);
char *newdir = g_path_get_dirname(newpath);
Tag *t = lookup_tag(DB, oldbase);
int retstat = 0;
if (t)
{
if (is_directory(newpath))
{
retstat = -EEXIST;
}
else
{
set_tag_name(DB, t, newbase);
tagdb_key_t key = path_extract_key(newdir);
stage_add(STAGE, key, (AbstractFile*)t);
key_destroy(key);
}
}
else
{
retstat = -ENOENT;
}
g_free(oldbase);
g_free(newbase);
g_free(newdir);
return retstat;
}
int _move_file(const char *path, const char *newpath)
{
int retstat = 0;
char *oldbase = g_path_get_basename(path);
char *newbase = g_path_get_basename(newpath);
char *olddir = g_path_get_dirname(path);
char *newdir = g_path_get_dirname(newpath);
File *f = path_to_file(path);
if (f)
{
char *old_start;
char *new_start;
file_id_t old_id = get_id_number_from_file_name(oldbase, &old_start); /* get the part of the file name after an ID */
file_id_t new_id = get_id_number_from_file_name(newbase, &new_start);
if ((old_id == new_id) || (old_id && !new_id))
{
set_file_name(DB, f, new_start);
if (strcmp(olddir, newdir) != 0)
{
/* If there are any tags in the new path not in
* the old path, then we add tags. If the tags
* to add have no value attached to them, then
* we don't change the file for them.
*
* If the only tags in the new path are also
* present in the old path, then we remove tags
* that are present in the first, but not in
* the second.
*
* Note that this works because a file shouldn't
* appear in a location preceded by tags that it
* does not have. Since a user could access a file
* from anywhere if he has the ID, this assumption
* may be invalid. This case isn't really worth
* fixing though.
*/
tagdb_key_t new_tags = path_extract_key(newdir);
tagdb_key_t old_tags = path_extract_key(olddir);
GList *to_add = NULL;
KL(new_tags, i)
{
key_elem_t this_tag = key_ref(new_tags, i);
if (!key_contains(old_tags, this_tag))
{
to_add = g_list_prepend(to_add, TO_SP(this_tag));
}
} KL_END;
if (!to_add)
{
KL(old_tags, i)
{
key_elem_t this_tag = key_ref(old_tags, i);
if (!key_contains(new_tags, this_tag))
{
remove_tag_from_file(DB, f, this_tag);
}
} KL_END;
}
else
{
LL(to_add, it)
{
/* XXX: Be sure to add tag values here when
* we get to that
*/
add_tag_to_file(DB, f, TO_S(it->data), 0);
} LL_END;
}
g_list_free(to_add);
key_destroy(new_tags);
key_destroy(old_tags);
}
}
else
{
retstat = -ENOENT;
}
}
else
{
retstat = -ENOENT;
}
g_free(oldbase);
g_free(newbase);
g_free(olddir);
g_free(newdir);
return retstat;
}
%(op mknod path mode dev)
{
int retstat = 0;
char *base = g_path_get_basename(path);
tagdb_begin_transaction(DB);
File *f = tagdb_make_file(DB, base);
tagdb_end_transaction(DB);
char *fpath = file_realpath(f);
/*
if (S_ISREG(mode)) {
retstat = open(fpath, O_CREAT | O_EXCL | O_WRONLY, mode);
if (retstat >= 0)
retstat = close(retstat);
}
else */
if (S_ISFIFO(mode)) {
retstat = mkfifo(fpath, mode);
}
else
{
retstat = mknod(fpath, mode, dev);
}
g_free(base);
g_free(fpath);
return retstat;
}
int make_a_file_and_return_its_real_path(const char *path, char **result);
%(op create path mode fi)
{
int retstat = 0;
char *realpath = NULL;
tagdb_begin_transaction(DB);
int res = make_a_file_and_return_its_real_path(path, &realpath);
tagdb_end_transaction(DB);
if (res == -ENOENT)
{
log_msg("Invalid path in create\n");
retstat = res;
goto END;
}
int fd = open(realpath, fi->flags, mode);
if (fd)
fi->fh = fd;
else
retstat = -1;
END:
g_free(realpath);
return retstat;
}
%(op symlink path linkpath)
{
int retstat = 0;
char *realpath = NULL;
tagdb_begin_transaction(DB);
int res = make_a_file_and_return_its_real_path(linkpath, &realpath);
tagdb_end_transaction(DB);
if (res == -ENOENT)
{
log_msg("Invalid path in symlink");
retstat = res;
}
else if (symlink(path, realpath) < 0)
{
retstat = -errno;
}
g_free(realpath);
return retstat;
}
%(op readlink linkpath buf bufsize)
{
int retstat = 0;
ssize_t bytes_written = 0;
char *realpath = NULL;
realpath = get_file_copies_path(linkpath);
if ((bytes_written = readlink(realpath, buf, bufsize)) < 0)
{
retstat = -errno;
}
else
{
buf[bytes_written] = 0;
}
g_free(realpath);
return retstat;
}
int make_a_file_and_return_its_real_path(const char *path, char **result)
{
int retstat = 0;
char *base = g_path_get_basename(path);
char *dir = g_path_get_dirname(path);
/* The "copy index" can't legally start a name.
* We just strip it so a user can't create it
*/
char *new_start;
get_id_number_from_file_name(base, &new_start);
tagdb_key_t tags = path_extract_key(dir);
if (!tags)
{
*result = NULL;
retstat = -ENOENT;
goto BAD_PATH_END;
}
File *f = tagdb_make_file(DB, new_start);
KL(tags, i)
{
add_tag_to_file(DB, f, key_ref(tags,i), NULL);
} KL_END;
*result = file_realpath(f);
// Has to happen before resource cleanup on a bad path since key_destroy
// doesn't tolerate NULLs.
key_destroy(tags);
BAD_PATH_END:
g_free(base);
g_free(dir);
return retstat;
}
%(op mkdir path mode)
{
%(log);
int retstat = 0;
char *base = g_path_get_basename(path);
char *dir = g_path_get_dirname(path);
char *p = NULL;
get_id_number_from_file_name(base, &p);
/* if there's any kind of id preceding the file, then we musn't accept it */
if (base == p)
{
tagdb_begin_transaction(DB);
Tag *t = lookup_tag(DB, base);
if (t == NULL)
{
// Make a new tag object
// give it int type with default value 0
t = tagdb_make_tag(DB, base);
if (t == NULL)
{
retstat = -ENOTDIR;
tagdb_end_transaction(DB);
goto TAGDB_FS_MKDIR_END;
}
}
tagdb_key_t key = path_extract_key(dir);
stage_add(STAGE, key, (AbstractFile*)t);
key_destroy(key);
tagdb_end_transaction(DB);
}
else
{
retstat = -ENOTDIR;
}
TAGDB_FS_MKDIR_END:
g_free(base);
g_free(dir);
return retstat;
}
%(op rmdir path)
{
/* Unconditionally removes the tag. References to the tag by id will still exist
* in the FileCabinet and also in the Files themselves, but they should be
* systematically ignored by TagDB since the Tag record will no longer show up
*/
int retstat = -1;
char *dir = g_path_get_dirname(path);
char *base = g_path_get_basename(path);
tagdb_key_t key = path_extract_key(dir);
tagdb_key_t path_key = path_extract_key(path);
Tag *t = lookup_tag(DB, base);
if (!can_remove_tag(DB, t))
{
goto TAGDB_FS_RMDIR_END;
}
file_id_t tag_id = tag_id(t);
stage_remove(STAGE, key, (AbstractFile *)t);
stage_remove_tag(STAGE, (AbstractFile *)t);
assert(stage_lookup(STAGE, key, tag_id) == NULL);
if (t)
{
tagdb_begin_transaction(DB);
GList *files = tagdb_tag_files(DB, t);
LL (files, it)
{
File *f = it->data;
remove_tag_from_file(DB, f, tag_id);
} LL_END;
if (delete_tag(DB, t));
{
assert(lookup_tag(DB, base) == NULL);
assert(retrieve_tag(DB, tag_id) == NULL);
g_list_free(files);
t = NULL;
retstat = 0;
}
tagdb_end_transaction(DB);
}
TAGDB_FS_RMDIR_END:
g_free(dir);
g_free(base);
key_destroy(key);
key_destroy(path_key);
return retstat;
}
%(op unlink path)
{
int retstat = 0;
File *f = path_to_file(path);
if (f)
{
char *fpath = file_realpath(f);
if (fpath)
retstat = unlink(fpath);
tagdb_begin_transaction(DB);
delete_file(DB, f);
tagdb_end_transaction(DB);
g_free(fpath);
}
return retstat;
}
%(op open path f_info)
{
int retstat = 0;
int fd;
// get the file id from the search path if necessary and get
// the realpath from the id
char *fpath = get_file_copies_path(path);
fd = open(fpath, f_info->flags);
g_free(fpath);
f_info->fh = fd;
log_fi(f_info);
return retstat;
}
%(op write path buf size offset fi)
{
%(log)
return file_info_write(fi, buf, size, offset);
}
%(op truncate path newsize)
{
%(log)
int retstat = 0;
char *fpath = get_file_copies_path(path);
if (fpath != NULL)
{
retstat = truncate(fpath, newsize);
if (retstat < 0)
log_error("tagfs_truncate truncate");
}
g_free(fpath);
return retstat;
}
%(op ftruncate path size fi)
{
%(log)
return file_info_truncate(fi, size);
}
%(op read path buffer size offset f_info)
{
%(log)
int r= file_info_read(f_info, buffer, size, offset);
if (r == -1)
{
perror("file_info_read");
}
return r;
}
%(op fsync path datasync f_info)
{
%(log)
return file_info_fsync(f_info, datasync);
}
%(op chmod path mode)
{
char *file_realpath = get_file_copies_path(path);
if (file_realpath)
{
int res = chmod(file_realpath, mode);
g_free(file_realpath);
return res;
}
else
{
return -1;
}
}
%(op chown path uid gid)
{
char *file_realpath = get_file_copies_path(path);
int res = chown(file_realpath, uid, gid);
g_free(file_realpath);
return res;
}
int _readdir_list_file(fuse_fill_dir_t filler, void *buf, const char *name)
{
int res = 0;
if (filler(buf, name, NULL, 0))
{
error("tagdb_fs_readdir: filler: buffer full");
res = 1;
}
return res;
}
%(op readdir path buffer filler offset f_info)
{
#define list_file(__name) \
if (_readdir_list_file(filler, buffer, (const char*)(__name)))\
{\
res = -1;\
goto READDIR_END;\
}
int res = 0;
GList *f = NULL;
GList *t = NULL;
GHashTable* seen = NULL;
GList *s = NULL;
GList *combined_tags = NULL;
char fname[MAX_FILE_NAME_LENGTH];
const char *le_name = NULL;
GList *prefixed_files = NULL;
tagdb_key_t tags = path_extract_key(path);
if (g_strcmp0(path, "/") == 0)
{
f = tagdb_untagged_items(DB);
t = g_hash_table_get_values(DB->tags);
}
else
{
f = get_files_list(DB, tags);
t = get_tags_list(DB, tags);
}
if (tags)
{
s = stage_list_position(STAGE, tags);
}
seen = g_hash_table_new(g_str_hash, g_str_equal);
LL(f, it)
{
if (it->data)
{
File *seen_file = g_hash_table_lookup(seen, file_name(it->data));
if (seen_file && (seen_file != it->data))
{
prefixed_files = g_list_prepend(prefixed_files, it->data);
/* maybe better to do this one once, but hey */
prefixed_files = g_list_prepend(prefixed_files, seen_file);
}
else
{
g_hash_table_insert(seen, (gpointer)file_name(it->data), it->data);
}
}
} LL_END;
{
GList *seen_file_names = g_hash_table_get_keys(seen);
LL(seen_file_names, it)
{
list_file(it->data);
} LL_END;
g_list_free(seen_file_names);
}
g_hash_table_destroy(seen);
seen = g_hash_table_new(g_direct_hash, g_direct_equal);
LL(prefixed_files, it)
{
le_name = file_to_string(it->data, fname);
File *seen_file = g_hash_table_lookup(seen, (gpointer)it->data);
if (!seen_file)
{
list_file(le_name);
g_hash_table_insert(seen, (gpointer)it->data, it->data);
}
} LL_END;
g_hash_table_destroy(seen);
combined_tags = g_list_concat(t, s);
seen = g_hash_table_new(g_direct_hash, g_direct_equal);
LL(combined_tags, it)
{
if (!g_hash_table_lookup(seen, it->data))
{
le_name = tag_to_string1(it->data, fname, MAX_FILE_NAME_LENGTH);
list_file(le_name);
g_hash_table_insert(seen, it->data, it->data);
}
} LL_END;
READDIR_END:
g_hash_table_destroy(seen);
key_destroy(tags);
g_list_free(f);
g_list_free(prefixed_files);
g_list_free(combined_tags);
return res;
}
%(subfs_component)