-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfusecompress.c
1532 lines (1295 loc) · 32.1 KB
/
fusecompress.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
/*
FuseCompress
Copyright (C) 2005 Milan Svoboda <[email protected]>
Copyright (C) 2008, 2011 Ulrich Hecht <[email protected]>
This program can be distributed under the terms of the GNU GPL v2.
See the file COPYING.
*/
#define FUSE_USE_VERSION 26
#include "config.h"
#include <fuse.h>
#include <string.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <dirent.h>
#include <errno.h>
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#include <sys/mman.h>
#include <stdlib.h>
#include <assert.h>
#include <sys/time.h>
#include <sys/resource.h>
#ifdef HAVE_SYS_FSUID_H
#include <sys/fsuid.h>
#endif
#include <ctype.h>
#include <limits.h>
#include <syslog.h>
#include <pthread.h>
#include "structs.h"
#include "globals.h"
#include "log.h"
#include "compress.h"
#include "file.h"
#include "direct_compress.h"
#include "background_compress.h"
#include "compress_lzo.h"
#include "dedup.h"
static char DOT = '.';
static int cmpdirFd; // Open fd to cmpdir for fchdir.
static inline const char* fusecompress_getpath(const char *path)
{
if (path[1] == 0)
return &DOT;
return ++path;
}
static int fusecompress_getattr(const char *path, struct stat *stbuf)
{
int res;
const char *full;
file_t *file;
full = fusecompress_getpath(path);
DEBUG_("('%s')", full);
/* check for magic file */
if (full[0] == '_' && full[1] == 'f' && full[2] == 'c') {
#ifdef WITH_DEDUP
if (!strcmp(&full[3], "redupon")) {
dedup_redup = TRUE;
return -EINPROGRESS;
}
else if (!strcmp(&full[3], "redupoff")) {
dedup_redup = FALSE;
return -EINPROGRESS;
}
else if (!strcmp(&full[3], "stat")) {
stbuf->st_size = 0;
stbuf->st_mode = S_IFREG;
stbuf->st_nlink = comp_database.entries;
#ifdef WITH_DEDUP
stbuf->st_blocks = dedup_database.entries;
#else
stbuf->st_blocks = 0;
#endif
return 0;
}
else
#endif
return -EINVAL;
}
#ifdef WITH_DEDUP
if (dedup_enabled)
res = dedup_sys_getattr(full, stbuf);
else
#endif
res = lstat(full, stbuf);
if (res == FAIL)
{
return -errno;
}
// For non-regular files return now.
//
if (!S_ISREG(stbuf->st_mode))
{
return 0;
}
// TODO: Move direct_open before lstat
//
file = direct_open(full, FALSE);
// Invalid file->size: correct value may be in stbuf->st_size
// if file is uncompressed or in header if it is compressed.
//
if ((file->size == (off_t) -1))
{
// No need to read header if file is smaller then header.
//
if (stbuf->st_size >= sizeof(header_t))
{
res = file_read_header_name(full, &file->compressor, &stbuf->st_size);
if (res == FAIL)
{
UNLOCK(&file->lock);
return -errno;
}
}
file->size = stbuf->st_size;
}
else
{
// Only if a file is compressed, real uncompressed file size
// is in file->size
//
if (file->compressor)
{
stbuf->st_size = file->size;
}
}
#ifdef WITH_DEDUP
if (dedup_enabled)
stbuf->st_nlink = 1;
#endif
// Set right time of the last status change this way because
// there is no call that allows to change it directly on file.
//
// (tar checks this item and it is loudly when the result
// is different than what it exepects)
//
/* seems to be incorrect, see issue #36 */
/* stbuf->st_ctime = stbuf->st_mtime; */
UNLOCK(&file->lock);
return 0;
}
static int fusecompress_readlink(const char *path, char *buf, size_t size)
{
int res;
const char *full;
full = fusecompress_getpath(path);
res = readlink(full, buf, size - 1);
if (res == -1)
return -errno;
buf[res] = '\0';
return 0;
}
static int fusecompress_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
off_t offset, struct fuse_file_info *fi)
{
const char *full;
DIR *dp;
struct dirent *de;
full = fusecompress_getpath(path);
dp = opendir(full);
if (dp == NULL)
return -errno;
while ((de = readdir(dp)) != NULL)
{
struct stat st;
/* ignore FUSE temporary files */
if (strstr(de->d_name, FUSE))
continue;
/* ignore internal use files */
if (!strncmp(de->d_name, FUSECOMPRESS_PREFIX, sizeof(FUSECOMPRESS_PREFIX) - 1))
continue;
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 fusecompress_mknod(const char *path, mode_t mode, dev_t rdev)
{
int ret = 0;
const char *full;
uid_t uid;
gid_t gid;
struct fuse_context *fc;
file_t *file;
#ifdef CONFIG_OSX
int fd;
#endif
full = fusecompress_getpath(path);
DEBUG_("('%s') mode 0%o rdev 0x%x", full, mode, (unsigned int)rdev);
file = direct_open(full, TRUE);
fc = fuse_get_context();
#ifdef HAVE_SETFSUID
uid = setfsuid(fc->uid);
#endif
#ifdef HAVE_SETFSGID
gid = setfsgid(fc->gid);
#endif
#ifdef CONFIG_OSX
/* On Linux, any user can use mknod() for regular files, but on OSX
it always requires superuser privileges, so for regular files, we
use open() instead */
if (S_ISREG(mode)) {
if ((fd = open(full, O_CREAT|O_TRUNC|O_WRONLY, mode)) == -1) {
ret = -errno;
}
else {
close(fd);
}
}
else
#endif
if (mknod(full, mode, rdev) == -1) {
ret = -errno;
}
UNLOCK(&file->lock);
#ifdef HAVE_SETFSUID
setfsuid(uid);
#endif
#ifdef HAVE_SETFSGID
setfsgid(gid);
#endif
return ret;
}
static int fusecompress_mkdir(const char *path, mode_t mode)
{
int ret = 0;
const char *full;
uid_t uid;
gid_t gid;
struct fuse_context *fc;
full = fusecompress_getpath(path);
DEBUG_("('%s')", full);
fc = fuse_get_context();
#ifdef HAVE_SETFSUID
uid = setfsuid(fc->uid);
#endif
#ifdef HAVE_SETFSGID
gid = setfsgid(fc->gid);
#endif
if (mkdir(full, mode) == -1)
ret = -errno;
#ifdef HAVE_SETFSUID
setfsuid(uid);
#endif
#ifdef HAVE_SETFSGID
setfsgid(gid);
#endif
return ret;
}
static int fusecompress_rmdir(const char *path)
{
const char *full;
full = fusecompress_getpath(path);
if (rmdir(full) == -1)
return -errno;
return 0;
}
static int fusecompress_unlink(const char *path)
{
int ret = 0;
const char *full;
file_t *file;
full = fusecompress_getpath(path);
DEBUG_("('%s')", full);
file = direct_open(full, TRUE);
int res;
#ifdef WITH_DEDUP
/* file is no longer available, make sure we don't use it
as a link target */
if (dedup_enabled) {
dedup_discard(file);
res = dedup_sys_unlink(full);
}
else
#endif
res = unlink(full);
if (res == 0)
{
// Mark file as deleted
//
direct_delete(file);
}
else
{
ret = -errno;
}
UNLOCK(&file->lock);
return ret;
}
static int fusecompress_symlink(const char *from, const char *to)
{
const char *full_to;
full_to = fusecompress_getpath(to);
DEBUG_("('%s' -> '%s')", from, full_to);
if (symlink(from, full_to) == -1)
return -errno;
struct fuse_context *fc = fuse_get_context();
lchown(full_to, fc->uid, fc->gid);
return 0;
}
static int fusecompress_rename(const char *from, const char *to)
{
int ret = 0;
const char *full_from;
const char *full_to;
file_t *file_from;
file_t *file_to;
full_from = fusecompress_getpath(from);
full_to = fusecompress_getpath(to);
DEBUG_("('%s' -> '%s')", full_from, full_to);
file_from = direct_open(full_from, TRUE);
file_from->accesses++;
UNLOCK(&file_from->lock);
file_to = direct_open(full_to, TRUE);
file_to->accesses++;
UNLOCK(&file_to->lock);
LOCK(&file_from->lock);
LOCK(&file_to->lock);
file_from->accesses--;
file_to->accesses--;
int res;
#ifdef WITH_DEDUP
if (dedup_enabled)
res = dedup_sys_rename(full_from, full_to);
else
#endif
res = rename(full_from, full_to);
if (res == 0)
{
// Rename file_from to full_to
//
#ifdef WITH_DEDUP
if (dedup_enabled)
dedup_rename(file_from, file_to);
#endif
file_to = direct_rename(file_from, file_to);
}
else
{
ret = -errno;
}
UNLOCK(&file_to->lock);
UNLOCK(&file_from->lock);
return ret;
}
static int fusecompress_link(const char *from, const char *to)
{
const char *full_from;
const char *full_to;
file_t* file;
int res;
full_from = fusecompress_getpath(from);
full_to = fusecompress_getpath(to);
file = direct_open(full_from,TRUE);
if(file->compressor && !do_decompress(file)) {
res = -errno;
UNLOCK(&file->lock);
return res;
}
file->dontcompress = TRUE;
UNLOCK(&file->lock);
if (link(full_from, full_to) == FAIL)
return -errno;
return 0;
}
static int fusecompress_chmod(const char *path, mode_t mode)
{
const char *full;
full = fusecompress_getpath(path);
int res;
#ifdef WITH_DEDUP
if (dedup_enabled)
res = dedup_sys_chmod(full, mode);
else
#endif
res = chmod(full, mode);
if (res == FAIL)
return -errno;
return 0;
}
static int fusecompress_chown(const char *path, uid_t uid, gid_t gid)
{
const char *full;
full = fusecompress_getpath(path);
int res;
#ifdef WITH_DEDUP
if (dedup_enabled)
res = dedup_sys_chown(full, uid, gid);
else
#endif
res = lchown(full, uid, gid);
if (res == FAIL)
return -errno;
return 0;
}
static int fusecompress_truncate(const char *path, off_t size)
{
int ret = 0;
const char *full;
file_t *file;
int fd;
full = fusecompress_getpath(path);
DEBUG_("('%s'), new size: %zd", full, size);
STAT_(STAT_TRUNCATE);
file = direct_open(full, TRUE);
#ifdef WITH_DEDUP
if (dedup_enabled)
dedup_discard(file);
#endif
// And finally do the actual decompress (note, that we only decompress
// if size > 0, no need to run through that time consuming process
// when we're 0'ing a file out!)
//
if ((size > 0 ) && file->compressor && (!do_decompress(file)))
{
ret = -errno;
goto out;
}
if (size == 0 && file->compressor)
{
/* we don't have to decompress, but we still have to reset the
file descriptors like do_decompress() does */
descriptor_t* descriptor = NULL;
list_for_each_entry(descriptor, &file->head, list)
{
direct_close(file, descriptor);
lseek(descriptor->fd, 0, SEEK_SET);
}
file->compressor = NULL;
}
// truncate file and reset size if all ok.
//
/* This is called on both truncate() and ftruncate(). Unlike truncate(),
ftruncate() must work even if there are no write permissions,
so we use the crowbar (file_open()). */
if ((fd = file_open(full, O_WRONLY)) == FAIL)
{
ret = -errno;
goto out;
}
if (ftruncate(fd, size) == FAIL)
{
ret = -errno;
close(fd);
goto out;
}
close(fd);
file->size = size;
out:
UNLOCK(&file->lock);
return ret;
}
static int fusecompress_utime(const char *path, struct utimbuf *buf)
{
const char *full;
file_t *file;
struct timeval timesval[2];
struct timeval *timesbuf=NULL;
if (buf != NULL)
{
timesval[0].tv_usec = 0;
timesval[1].tv_usec = 0;
timesval[0].tv_sec = buf->actime;
timesval[1].tv_sec = buf->modtime;
timesbuf=timesval;
}
full = fusecompress_getpath(path);
DEBUG_("('%s')", full);
struct stat st;
if (!lstat(full, &st) && !S_ISREG(st.st_mode)) {
if (lutimes(full, timesbuf) < 0)
return -errno;
else
return 0;
}
file = direct_open(full, FALSE);
int res;
#ifdef WITH_DEDUP
if (dedup_enabled)
res = dedup_sys_utime(full, timesbuf);
else
#endif
res = lutimes(full, timesbuf);
UNLOCK(&file->lock);
if (res == -1)
return -errno;
return 0;
}
static int fusecompress_open(const char *path, struct fuse_file_info *fi)
{
int res;
const char *full;
struct stat statbuf;
file_t *file;
descriptor_t *descriptor;
full = fusecompress_getpath(path);
DEBUG_("('%s')", full);
STAT_(STAT_OPEN);
descriptor = (descriptor_t *) malloc(sizeof(descriptor_t));
if (!descriptor)
{
CRIT_("\tno memory");
//exit(EXIT_FAILURE);
return -ENOMEM;
}
file = direct_open(full, TRUE);
// if user wants to open file in O_WRONLY, we must open file for reading too
// (we need to read header...)
//
if (fi->flags & O_WRONLY)
{
fi->flags &= ~O_WRONLY; // remove O_WRONLY flag
fi->flags |= O_RDWR; // add O_RDWR flag
}
if (fi->flags & O_APPEND)
{
// TODO: Some inteligent append handling...
//
// Note: fusecompress_write is called with offset to the end of
// file - this is fuse/kernel part of work...
//
fi->flags &= ~O_APPEND;
}
#ifdef WITH_DEDUP
if (dedup_enabled && (fi->flags & (O_RDWR | O_CREAT | O_TRUNC))) {
if (do_undedup(file) == FAIL) {
UNLOCK(&file->lock);
return -errno;
}
}
#endif
descriptor->fd = file_open(full, fi->flags);
if (descriptor->fd == FAIL)
{
UNLOCK(&file->lock);
free(descriptor); // This is safe, because this descriptor has
// not yet been added to the database entry
return -errno;
}
DEBUG_("\tfd: %d", descriptor->fd);
res = fstat(descriptor->fd, &statbuf);
if (res == -1)
{
CRIT_("\tfstat failed after open was ok");
//exit(EXIT_FAILURE);
return -errno;
}
if(S_ISREG(statbuf.st_mode) && statbuf.st_nlink > 1) {
file->dontcompress = TRUE;
}
DEBUG_("\tsize on disk: %zi", statbuf.st_size);
if (statbuf.st_size >= sizeof(header_t))
{
res = file_read_header_fd(descriptor->fd, &file->compressor, &statbuf.st_size);
if (res == FAIL)
{
CRIT_("\tfile_read_header_fd failed");
//exit(EXIT_FAILURE);
return -EIO;
}
}
else
{
// File has size smaller than size of header, set compressor to NULL as
// default. Compressor method will be selected when write happens.
//
file->compressor = NULL;
}
DEBUG_("\topened with compressor: %s, uncompressed size: %zd, fd: %d",
file->compressor ? file->compressor->name : "null",
statbuf.st_size, descriptor->fd);
descriptor->file = file;
descriptor->offset = 0;
descriptor->handle = NULL;
file->accesses++;
// The file size has to be -1 (invalid) or the same as we think it is
//
DEBUG_("\tfile->size: %zi", file->size);
// Cannot be true (it is not implemented) if writing to
// noncompressible file or into rollbacked file...
//
// assert((file->size == (off_t) -1) ||
// (file->size == statbuf.st_size));
//
file->size = statbuf.st_size;
// Add ourself to the database's list of open filedata
//
list_add(&descriptor->list, &file->head);
// Set descriptor in fi->fh
//
fi->fh = (long) descriptor;
UNLOCK(&file->lock);
return 0;
}
static int fusecompress_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi)
{
int res;
file_t *file;
descriptor_t *descriptor;
DEBUG_("('%s') size: %zd, offset: %zd", path, size, offset);
STAT_(STAT_READ);
descriptor = (descriptor_t *) fi->fh;
assert(descriptor);
file = descriptor->file;
assert(file);
LOCK(&file->lock);
if (file->compressor)
{
res = direct_decompress(file, descriptor, buf, size, offset);
UNLOCK(&file->lock);
}
else
{
int fd = descriptor->fd;
UNLOCK(&file->lock);
res = pread(fd, buf, size, offset);
}
if (res == FAIL)
{
res = -errno;
// Read failed, invalidate file size in database. Right value will
// be acquired later if needed.
//
/* XXX: locking? */
file->size = -1;
}
// sched_yield();
DEBUG_("returning %d",res);
return res;
}
static int fusecompress_write(const char *path, const char *buf, size_t size,
off_t offset, struct fuse_file_info *fi)
{
int res;
file_t *file;
descriptor_t *descriptor;
DEBUG_("('%s') size: %zd, offset: %zd", path, size, offset);
STAT_(STAT_WRITE);
descriptor = (descriptor_t *) fi->fh;
assert(descriptor);
file = descriptor->file;
assert(file);
LOCK(&file->lock);
#ifdef WITH_DEDUP
if (dedup_enabled)
dedup_discard(file);
#endif
DEBUG_("\tfile->filename: %s", file->filename);
DEBUG_("offset: %zi, file->size: %zi", offset, file->size);
// Decide about type of compression applied to this file.
//
if ((!file->dontcompress) &&
(file->size == 0) &&
(file->accesses == 1) &&
(offset == 0))
{
assert(file->compressor == NULL);
file->compressor = choose_compressor(file);
DEBUG_("\tcompressor set to %s",
file->compressor ? file->compressor->name : "null");
}
file->dontcompress = TRUE;
if (file->compressor)
{
res = direct_compress(file, descriptor, buf, size, offset);
}
else
{
int fd = descriptor->fd;
UNLOCK(&file->lock);
res = pwrite(fd, buf, size, offset);
LOCK(&file->lock);
}
if (res == FAIL)
{
res = -errno;
// Read failed, invalidate file size in database. Right value will
// be acquired later if needed.
//
file->size = -1;
}
UNLOCK(&file->lock);
// sched_yield();
return res;
}
static int fusecompress_release(const char *path, struct fuse_file_info *fi)
{
const char *full;
file_t *file;
descriptor_t *descriptor;
full = fusecompress_getpath(path);
descriptor = (descriptor_t *) fi->fh;
assert(descriptor);
file = descriptor->file;
assert(file);
DEBUG_("('%s')", full);
STAT_(STAT_RELEASE);
LOCK(&file->lock);
if (file->compressor)
{
direct_close(file, descriptor);
}
// Close descriptor and remove it from list.
//
list_del(&descriptor->list);
file->accesses--;
DEBUG_("file_closing %s (fd %d)",path,descriptor->fd);
/* file may have already been closed by a failing do_decompress() */
if (descriptor->fd != -1)
file_close(&descriptor->fd);
free(descriptor);
UNLOCK(&file->lock);
return 0;
}
static int fusecompress_fsync(const char *path, int isdatasync,
struct fuse_file_info *fi)
{
int res;
descriptor_t *descriptor;
descriptor = (descriptor_t *) fi->fh;
#ifdef HAVE_FDATASYNC
if (isdatasync)
res = fdatasync(descriptor->fd);
else
#endif
res = fsync(descriptor->fd);
if (res == -1)
return -errno;
return 0;
}
static int fusecompress_statfs(const char *path, struct statvfs *stbuf)
{
int res;
const char *full;
full = fusecompress_getpath(path);
res = statvfs(full, stbuf);
if(res == -1)
return -errno;
return 0;
}
#define REISERFS_SUPER_MAGIC 0x52654973
static void *fusecompress_init(struct fuse_conn_info* conn)
{
struct statfs fs;
pthread_mutexattr_init(&locktype);
#ifdef DEBUG
#ifndef PTHREAD_MUTEX_ERRORCHECK_NP
#define PTHREAD_MUTEX_ERRORCHECK_NP PTHREAD_MUTEX_ERRORCHECK
#endif
pthread_mutexattr_settype(&locktype, PTHREAD_MUTEX_ERRORCHECK_NP);
#endif
pthread_mutex_init(&database.lock, &locktype);
pthread_mutex_init(&comp_database.lock, &locktype);
#ifdef WITH_DEDUP
pthread_mutex_init(&dedup_database.lock, &locktype);
#endif
if (fchdir(cmpdirFd) == -1)
{
CRIT_("fchdir failed!");
exit(EXIT_FAILURE);
}
// Get parameters of the underlaying filesystem and set minimal
// filesize for background and direct compression.
//
if (fstatfs(cmpdirFd, &fs) == -1)
{
CRIT_("fstatfs failed!");
exit(EXIT_FAILURE);
}
switch (fs.f_type)
{
case REISERFS_SUPER_MAGIC:
// Compress all files when reiserfs detected. Reiserfs
// should work really very good with small files...
//
min_filesize_background = 0;
break;
default:
// Limit minimal filesize for direct to 4096 - this
// is buffer size used by fuse. If data length is smaller
// than this we know that this is size of the whole
// file.
//
min_filesize_background = fs.f_bsize;
break;
}
DEBUG_("min_filesize_background: %d",
min_filesize_background);
pthread_create(&pt_comp, NULL, thread_compress, NULL);
return NULL;
}
static void fusecompress_destroy(void *arg)
{
int r;
struct timespec delay = {
.tv_sec = 1,
.tv_nsec = 0,
};
// Free database and add stuff to the background compressor if neccesary
//
INFO_("Compressing remaining files in cache");