-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuseOperations.c
1671 lines (1479 loc) · 49.1 KB
/
fuseOperations.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
//
// Created by paul on 5/27/20.
//
#include "common.h"
#include "templatefs.h"
#include "logStuff.h"
#include <stdbool.h>
#include <sys/file.h> /* flock(2) */
#include <sys/epoll.h>
#include <sys/wait.h>
#ifdef HAVE_SETXATTR
#include <sys/xattr.h>
#endif
#include <fuse3/fuse.h>
#include "fuseOperations.h"
#include "processTemplate.h"
typedef struct {
char * path;
DIR * dir;
int fd;
} tFSTree;
typedef struct {
tFSTree mountpoint; ///< absolute path to the mount point
tFSTree templates; ///< absolute path to the top of the template hierarchy
} tPrivateData;
typedef struct {
const char * path; ///< absolute path to the file
int fd; ///< file descriptor. -1 if fd not open/valid */
bool isTemplate; ///< true if there's a template file to process, else pass request through
bool isExecutable; ///< true if the templatee file is _executable_
byte * contents; ///< the cached result of processing the template file
size_t length; ///< length of the cached file contents
} tFHFile;
typedef struct {
DIR * dp;
struct dirent * entry;
off_t offset;
} tFHDir;
typedef struct {
/* * * UNION MUST BE FIRST * * */
/* tFHHandle. tFHFile, and tFHDir are cast back and
* forth, based on the value of the type field */
union {
tFHFile file;
tFHDir directory;
};
/* how the union should be accessed */
enum {
notInitialized = 0, ///< hasn't been set yet
isFile, ///< use the 'file' field of the union
isDirectory ///< use the 'directory' field of the union
} type; ///< indicates which field of the union to use
} tFileHandle;
/**
* @brief a buffer that expands as it fills
* @see newElasticBuffer()
* @see releaseElasticBuffer()
* @see makeRoom()
* @see getSpacePtr()
* @see increaseAvailable()
*/
typedef struct {
char * data; ///< pointer to the data, caution: may move when realloc'd
ssize_t available; ///< amount of data currently in the buffer
ssize_t remaining; ///< amount of unused space lwft
ssize_t headroom; ///< the amount of 'space' that's added
} tElasticBuffer;
// ------------------------------------------------------------------------------
/**
* @brief allocate a new, empty buffer
* @param size initial size of buffer
* @param headroom amount to ensure we have ready to fill
* @return the buffer structure, or NULL if failed.
*/
tElasticBuffer * newElasticBuffer( size_t size, size_t headroom )
{
tElasticBuffer * result = calloc(1, sizeof(tElasticBuffer));
if ( result != NULL ) {
result->data = calloc( 1, size);
result->available = 0;
if ( result->data != NULL ) {
result->remaining = size;
}
result->headroom = headroom;
}
return result;
}
/**
* @brief release the resources in use by the elastic buffer
* @param buf the elastic buffer
*/
void releaseElasticBuffer( tElasticBuffer * buf )
{
free( buf->data );
free( buf );
}
/**
* @brief ensure that there's at least 'headroom' bytes available in the buffer
*
* Caution: calling realloc() may move the data buffer.
*
* @param buf the elastic buffer to check/expand
* @return the number of bytes now available
*/
static size_t makeRoom( tElasticBuffer * buf )
{
if ( buf->remaining < buf->headroom ) {
buf->remaining += buf->headroom * 2;
buf->data = realloc( buf->data, buf->available + buf->remaining );
}
return buf->remaining;
}
/**
* @brief
* @param buf
* @return
*/
char * getSpacePtr( tElasticBuffer * buf )
{
/* Caution: if makeRoom causes a realloc, make sure it
* happens *before* doing any pointer arithmetic */
makeRoom( buf );
return buf->data + buf->available;
}
/**
* @brief increase
* @param buf
* @param additional
*/
void increaseAvailable( tElasticBuffer * buf, size_t additional )
{
buf->available += additional;
buf->remaining -= additional;
makeRoom(buf);
}
/**
* @brief substitute -errno if result == -1
*
* In a nutshell, most linux filesystem functions invariably return -1 on error,
* and return the actual error that occurred as a positive integer in 'errno'.
* libfuse adopts the (much saner) convention of returning -errno rather than -1
* when an error is being reported.
*
* This convention crops up throughout operation functions. So define it
* as a common inline function.
*
* @param result the result of a 'standard' filesystem function.
* @return if result == -1, then return -errno, otherwise just pass back result unmodified.
*/
static inline int fixupResult( int result )
{
if ( result == -1 )
{
result = -errno;
}
return result;
}
// ------------------------------------------------------------------------------
void setHandle( struct fuse_file_info * fi, tFileHandle * fh )
{
if ( fi != NULL ) {
fi->fh = (uint64_t) fh;
}
}
tFHFile * getFileHandle( struct fuse_file_info * fi )
{
tFileHandle * fh = NULL;
if ( fi != NULL ) {
fh = (tFileHandle *) fi->fh;
}
return ( fh != NULL && fh->type == isFile ) ? &fh->file : NULL;
}
void setFileHandle( struct fuse_file_info * fi, tFHFile * fileHandle )
{
if ( fileHandle != NULL ) {
tFileHandle * fh = (tFileHandle *)fileHandle;
fh->type = isFile;
setHandle( fi, fh );
}
}
tFHDir * getDirHandle( struct fuse_file_info * fi )
{
tFileHandle * fh = NULL;
if ( fi != NULL ) {
fh = (tFileHandle *) fi->fh;
}
return ( fh != NULL && fh->type == isDirectory ) ? &fh->directory : NULL;
}
void setDirHandle( struct fuse_file_info * fi, tFHDir * dirHandle )
{
if ( dirHandle != NULL ) {
tFileHandle * fh = (tFileHandle *)dirHandle;
fh->type = isDirectory;
setHandle( fi, fh );
}
}
void releaseHandle( struct fuse_file_info * fi )
{
if ( (void *)fi->fh != NULL ) {
free( (void *)fi->fh );
fi->fh = (uint64_t) NULL;
}
}
// ------------------------------------------------------------------------------
/**
* @brief retrieve the tPrivateData structure we stashed in the fuse context earlier.
* See also initPrivateData()
* @return pointer to the private data structure, or NULL on error.
*/
tPrivateData * getPrivateData( void )
{
tPrivateData * result = NULL;
struct fuse_context * fc = fuse_get_context();
if ( fc != NULL ) {
result = fc->private_data;
}
return result;
}
int getMountpointFD( void )
{
int result = -1;
tPrivateData * privateData = getPrivateData();
if ( privateData != NULL )
{
result = privateData->mountpoint.fd;
}
return result;
}
int getTemplateFD( void )
{
int result = -1;
tPrivateData * privateData = getPrivateData();
if ( privateData != NULL )
{
result = privateData->templates.fd;
}
return result;
}
static inline bool hasTemplate( const char * path )
{
bool result = ( faccessat( getTemplateFD(),
&path[1],
R_OK,
AT_SYMLINK_NOFOLLOW ) == 0 );
errno = 0;
return ( result );
}
static inline bool isExecutable( const char * path )
{
bool result = ( faccessat( getTemplateFD(),
&path[1],
X_OK,
AT_SYMLINK_NOFOLLOW ) == 0 );
errno = 0;
return ( result );
}
int setupFSTree( tFSTree * tree, const char * path )
{
int result = 0;
tree->path = realpath( path, NULL );
if ( tree->path == NULL || access( tree->path, F_OK ) != 0 ) {
logCritical( "fatal: path \'%s\' is invalid", tree->path );
result = -errno;
} else {
tree->fd = -1;
tree->dir = opendir( tree->path );
if ( tree->dir != NULL ) {
tree->fd = dirfd( tree->dir );
}
if ( tree->fd == -1 ) {
result = -errno;
}
}
return result;
}
void * initPrivateData( const char * mountPath, const char * templatePath )
{
logEntry( "\'%s\',\'%s\'", mountPath, templatePath );
tPrivateData * result = calloc( 1, sizeof( tPrivateData ) );
if ( result != NULL ) {
setupFSTree( &result->mountpoint, mountPath );
setupFSTree( &result->templates, templatePath );
}
return (void *) result;
}
// ------------------------------------------------------------------------------
/**
* Initialize filesystem
*
* The return value will passed in the `private_data` field of
* `struct fuse_context` to all file operations, and as a
* parameter to the destroy() method. It overrides the initial
* value provided to fuse_main() / fuse_new().
*/
void * initFsOp( struct fuse_conn_info * UNUSED( conn ), struct fuse_config * cfg )
{
logEntry( "%p,%p", conn, cfg );
cfg->use_ino = 1;
cfg->nullpath_ok = 1;
/* Pick up changes from lower filesystem right away. This is also necessary
* for better hardlink support. When the kernel calls the unlink() handler,
* it does not know the inode of the to-be-removed entry and therefore can
* not invalidate the cache of the associated inode - resulting in an
* incorrect st_nlink value being reported for any remaining hardlinks to
* this inode. */
cfg->entry_timeout = 0;
cfg->attr_timeout = 0;
cfg->negative_timeout = 0;
/* Note: we've already set up private data, so pass that back, or it'll be lost */
return (void *)getPrivateData();
}
/** Get file attributes.
*
* Similar to stat(). The 'st_dev' and 'st_blksize' fields are
* ignored. The 'st_ino' field is ignored except if the 'use_ino'
* mount option is given. In that case it is passed to userspace,
* but libfuse and the kernel will still assign a different
* inode for internal use (called the "nodeid").
*
* `fi` will always be NULL if the file is not currently open, but
* may also be NULL if the file is open.
*/
int getFileAttrOp( const char * path, struct stat * stbuf, struct fuse_file_info * fi )
{
int result;
logEntry( "\'%s\', %p, %p", path, stbuf, fi );
tFHFile * fh = getFileHandle( fi );
bool isTemplate;
/* ToDo: handle templates better - particularly timestamps */
if ( fh == NULL ) {
isTemplate = hasTemplate( path );
} else {
isTemplate = fh->isTemplate;
}
if ( fh == NULL ) {
int rootFD;
if ( isTemplate ) {
rootFD = getTemplateFD();
} else {
rootFD = getMountpointFD();
}
result = fixupResult( fstatat( rootFD,
&path[ 1 ],
stbuf,
AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW ) );
} else {
result = fixupResult( fstat( fh->fd, stbuf ) );
}
if ( isTemplate ) {
/* if it's a template, report it as read-only/not executable */
mode_t mask = S_IWUSR | S_IWGRP | S_IWOTH;
if ( !S_ISDIR( stbuf->st_mode ) ) {
/* if it's not a directory, clear the exec bits too */
mask = mask | S_IXUSR | S_IXGRP | S_IXOTH;
}
stbuf->st_mode = stbuf->st_mode & ~mask;
/* return the length of the cached contents */
if ( fh != NULL && fh->contents != NULL ) {
stbuf->st_size = fh->length;
}
}
return result;
}
/**
* Check file access permissions
*
* This will be called for the access() system call. If the
* 'default_permissions' mount option is given, this method is not
* called.
*
* This method is not called under Linux kernel versions 2.4.x
*/
int fileAccessOp( const char * path, int mask )
{
logEntry( "\'%s\', %d", path, mask );
return fixupResult( faccessat( getMountpointFD(),
&path[ 1 ],
mask,
AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW ) );
}
/** Read the target of a symbolic link
*
* The data should be filled with a null terminated string. The
* data size argument includes the space for the terminating
* null character. If the linkname is too long to fit in the
* data, it should be truncated. The return value should be 0
* for success.
*/
int readSymlinkOp( const char * path, char * buf, size_t size )
{
int result;
logEntry( "\'%s\', %p, %d", path, buf, size );
result = fixupResult( readlinkat( getMountpointFD(),
&path[ 1 ],
buf,
size - 1 ) );
if ( result >= 0 ) {
buf[ result ] = '\0';
result = 0;
}
return result;
}
/** Open directory
*
* Unless the 'default_permissions' mount option is given,
* this method should check if opendir is permitted for this
* directory. Optionally opendir may also return an arbitrary
* filehandle in the fuse_file_info structure, which will be
* passed to readdir, releasedir and fsyncdir.
*/
int openDirOp( const char * path, struct fuse_file_info * fi )
{
int fd;
logEntry( "\'%s\',%p", path, fi );
tFHDir * dh = (tFHDir *) malloc( sizeof( tFileHandle ) );
if ( dh == NULL ) {
return -ENOMEM;
}
if ( strcmp( path, "/" ) == 0 ) {
fd = dup( getMountpointFD() );
if ( lseek( fd, 0, SEEK_SET ) == -1 ) {
fuse_log( FUSE_LOG_ERR, "error: lseek failed (%d: %s)", errno, strerror( errno ) );
return -errno;
}
} else {
fd = openat( getMountpointFD(), &path[ 1 ], O_RDONLY );
}
if ( fd == -1 ) {
fuse_log( FUSE_LOG_ERR, "file descriptor is invalid (%d: %s)", errno,
strerror( errno ) );
return -errno;
} else {
dh->dp = fdopendir( fd );
if ( dh->dp == NULL ) {
fuse_log( FUSE_LOG_ERR, "failed to open directory \'%s\'", path );
free( dh );
return -errno;;
}
dh->offset = 0;
dh->entry = NULL;
setDirHandle( fi, dh );
}
return 0;
}
/** Read directory
*
* @brief return the contents of the directory pointed to by path
*
* A FUSEs filesystem may choose between two implementations for readdir.
* This implementation keeps track of the offsets of the directory
* entries. It uses the offset parameter and always passes non-zero
* offset to the filler function. When the data is full (or an error
* happens) the filler function will return '1'.
*
* @param path
* @param buf
* @param filler
* @param offset
* @param fi
* @param flags
* @return
*/
int readDirOp( const char * UNUSED( path ),
void * buf,
fuse_fill_dir_t filler,
off_t offset,
struct fuse_file_info * fi,
enum fuse_readdir_flags flags )
{
(void)flags;
/* ToDo: merge entries from the mount point and corresponding template directory
* Otherwise a template file that does not have a corresponding file under the
* mountpoint will never be visible */
logEntry( "\'%s\',%p", path, fi );
tFHDir * dh = getDirHandle( fi );
if ( dh == NULL ) {
return -ENOTDIR;
}
if ( offset != dh->offset ) {
#ifndef __FreeBSD__
seekdir( dh->dp, offset );
#else
/* Subtract the one that we add when calling telldir() below */
seekdir( dh->dp, offset - 1 );
#endif
dh->entry = NULL;
dh->offset = offset;
}
do {
struct stat st;
off_t nextoff;
enum fuse_fill_dir_flags fill_flags = 0;
if ( !dh->entry ) {
dh->entry = readdir( dh->dp );
if ( !dh->entry ) {
break;
}
}
#ifdef HAVE_FSTATAT
if (flags & FUSE_READDIR_PLUS) {
int result;
result = fstatat( dirfd(d->dp),
d->entry->d_name,
&st,
AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW);
if (result != -1)
{
fill_flags |= FUSE_FILL_DIR_PLUS;
}
}
#endif
if ( !( fill_flags & FUSE_FILL_DIR_PLUS ) ) {
memset( &st, 0, sizeof( st ) );
st.st_ino = dh->entry->d_ino;
st.st_mode = dh->entry->d_type << 12;
}
nextoff = telldir( dh->dp );
#ifdef __FreeBSD__
/* Under FreeBSD, telldir() may return 0 the first time it is called.
* But for libfuse, an offset of zero means that offsets are not
* supported, so we shift everything by one. */
nextoff++;
#endif
if ( filler( buf,
dh->entry->d_name,
&st,
nextoff,
fill_flags ) ) {
break;
}
dh->entry = NULL;
dh->offset = nextoff;
} while ( 1 );
return 0;
}
/**
* @brief Release directory
* @param path
* @param fi
* @return
*/
int releaseDirOp( const char * UNUSED( path ), struct fuse_file_info * fi )
{
logEntry( "\'%s\',%p", path, fi );
tFHDir * dh = getDirHandle( fi );
if ( dh != NULL && dh->dp != NULL ) {
closedir( dh->dp );
dh->dp = NULL;
}
releaseHandle( fi );
return 0;
}
/**
* @brief Create a file node
*
* This is called for creation of all non-directory, non-symlink
* nodes. If the filesystem defines a create() method, then for
* regular files that will be called instead.
*
* @param path
* @param mode
* @param rdev
* @return
*/
int mkNodOp( const char * path, mode_t mode, dev_t rdev )
{
logEntry( "\'%s\',%d,%d", path, mode, rdev );
if ( S_ISFIFO( mode ) ) {
return fixupResult( mkfifoat( getMountpointFD(),
&path[ 1 ],
mode ) );
} else {
return fixupResult( mknodat( getMountpointFD(),
&path[ 1 ],
mode,
rdev ) );
}
}
/**
* @brief Create a directory
*
* Note that the mode argument may not have the type specification
* bits set, i.e. S_ISDIR(mode) can be false. To obtain the
* correct directory type bits use mode|S_IFDIR
*
* @param path
* @param mode
* @return
*/
int createDirOp( const char * path, mode_t mode )
{
logEntry( "\'%s\',%d", path, mode );
return fixupResult( mkdirat( getMountpointFD(), &path[ 1 ], mode ) );
}
/**
* @brief Remove a file
*
* @param path
* @return
*/
int fileUnlinkOp( const char * path )
{
logEntry( "\'%s\'", path );
return fixupResult( unlinkat( getMountpointFD(), &path[ 1 ], 0 ) );
}
/**
* @brief Remove a directory
* @param path
* @return
*/
int removeDirOp( const char * path )
{
logEntry( "\'%s\'", path );
return fixupResult( unlinkat(getMountpointFD(),
&path[ 1 ],
AT_REMOVEDIR ) );
}
/**
* @brief Create a symbolic link
* @param from
* @param to
* @return
*/
int createSymlinkOp( const char * from, const char * to )
{
logEntry( "\'%s\',\'%s\'", from, to );
return fixupResult( symlinkat( from, getMountpointFD(), &to[ 1 ] ) );
}
/** Rename a file
*
* *flags* may be `RENAME_EXCHANGE` or `RENAME_NOREPLACE`. If
* RENAME_NOREPLACE is specified, the filesystem must not
* overwrite *newname* if it exists and return an error
* instead. If `RENAME_EXCHANGE` is specified, the filesystem
* must atomically exchange the two files, i.e. both must
* exist and neither may be deleted.
*/
int renameFsObjOp( const char * from, const char * to, unsigned int flags )
{
logEntry( "\'%s\',\'%s\',%u", from, to, flags );
return fixupResult( renameat2( getMountpointFD(),
&from[ 1 ],
getMountpointFD(),
&to[ 1 ],
flags) );
}
/** Create a hard link to a file */
int linkFileOp( const char * from, const char * to )
{
/* ToDo: fixup from and to */
logEntry( "\'%s\',\'%s\'", from, to );
return fixupResult( linkat( getMountpointFD(),
&from[ 1 ],
getMountpointFD(),
&to[ 1 ],
0 ) );
}
/**
* @brief Change the permission bits of a file
*
* `fi` will always be NULL if the file is not currenlty open, but
* may also be NULL if the file is open.
*/
int chmodFileOp( const char * path, mode_t mode, struct fuse_file_info * fi )
{
logEntry( "\'%s\',%p,%p", path, mode, fi );
tFHFile * fh = getFileHandle( fi );
if ( fh != NULL ) {
return fixupResult( fchmod( fh->fd, mode ) );
} else {
return fixupResult( fchmodat( getMountpointFD(),
&path[ 1 ],
mode,
AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW ) );
}
}
/** Change the owner and group of a file
*
* `fi` will always be NULL if the file is not currenlty open, but
* may also be NULL if the file is open.
*
* Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
* expected to reset the setuid and setgid bits.
*/
int chownFileOp( const char * path, uid_t uid, gid_t gid, struct fuse_file_info * fi )
{
logEntry( "\'%s\',%u,%u,%p", path, uid, gid, fi );
tFHFile * fh = getFileHandle( fi );
if ( fh ) {
return fixupResult( fchown( fh->fd, uid, gid ) );
} else {
return fixupResult( fchownat( getMountpointFD(),
&path[ 1 ],
uid,
gid,
AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW ) );
}
}
/** Change the size of a file
*
* `fi` will always be NULL if the file is not currently open, but
* may also be NULL if the file is open.
*
* Unless FUSE_CAP_HANDLE_KILLPRIV is disabled, this method is
* expected to reset the setuid and setgid bits.
*/
int truncateFileOp( const char * path, off_t size, struct fuse_file_info * fi )
{
int result = 0;
logEntry( "\'%s\',%u,%p", path, size, fi );
if ( fi == NULL ) {
fuse_log( FUSE_LOG_WARNING, "truncating \'%s\' with null fuse_file_info", path );
result = fixupResult( truncate( path, size ) );
} else {
tFHFile * fh = getFileHandle( fi );
if ( fh == NULL ) {
fuse_log( FUSE_LOG_ERR, "attempt to truncate \'%s\' with invalid fileHandle",
path );
result = -EINVAL;
} else {
if ( fh->isTemplate ) {
/* a template file is treated as 'read-only', and truncating isn't allowed */
result = -EPERM;
} else {
result = fixupResult( ftruncate( fh->fd, size ) );
}
}
}
return result;
}
#ifdef HAVE_UTIMENSAT
/**
* Change the access and modification times of a file with
* nanosecond resolution
*
* This supersedes the old utime() interface. New applications
* should use this.
*
* `fi` will always be NULL if the file is not currenlty open, but
* may also be NULL if the file is open.
*
* See the utimensat(2) man page for details.
*/
int utimensOp( const char *path,
const struct timespec ts[2],
struct fuse_file_info *fi)
{
/* don't use utime/utimes since they follow symlinks */
if (fi)
{
return fixupResult( futimens(fi->fh, ts) );
}
else
{
return fixupResult( utimensat(0, path, ts, AT_EMPTY_PATH | AT_SYMLINK_NOFOLLOW) );
}
}
#endif
/**
* Create and open a file
*
* If the file does not exist, first create it with the specified
* mode, and then open it.
*
* If this method is not implemented or under Linux kernel
* versions earlier than 2.6.15, the mknod() and open() methods
* will be called instead.
*
* Note: file will always be created under the mount point, we won't be
* asked to create files that reside in the template hierarchy
*/
int createFileOp( const char * path, mode_t mode, struct fuse_file_info * fi )
{
logEntry( "\'%s\',%u,%p", path, mode, fi );
tFHFile * fh = (tFHFile *) calloc( 1, sizeof( tFileHandle ) );
if ( fh == NULL ) {
return -ENOMEM;
}
int fd = fixupResult( openat( getMountpointFD(),
&path[ 1 ],
fi->flags,
mode ) );
if ( fd >= 0 ) {
fh->path = strdup( path );
fh->fd = fd;
}
setFileHandle( fi, fh );
return 0;
}
/**
* @brief
* @param fd
* @param buffer
* @param size
* @return
*/
int executeTemplate( tFHFile *fh, byte ** buffer, size_t * size )
{
int result = 0;
tPrivateData * privateData;
char * argv[3];
int stdoutPipe[2];
int stderrPipe[2];
const int kPipeReadIdx = 0;
const int kPipeWriteIdx = 1;
if ( pipe( stdoutPipe ) == -1 ) {
logError("unable to create pipe for stdout");
result = -errno;
} else if ( pipe( stderrPipe ) == -1 ) {
logError("unable to create pipe for stderr");
result = -errno;
} else {
privateData = getPrivateData();
pid_t pid = fork();
switch (pid)
{
case -1: /* the fork() failed */
logError( "failed to execute template");
result = -errno;
break;
case 0: /* we are the child */
if ( privateData != NULL ) {
/* set up the argv array */
asprintf( &argv[ 0 ], "%s%s", privateData->templates.path, fh->path );
asprintf( &argv[ 1 ], "%s%s", privateData->mountpoint.path, fh->path );
argv[ 2 ] = NULL;
dup2( stdoutPipe[ kPipeWriteIdx ], STDOUT_FILENO);
close( stdoutPipe[ kPipeReadIdx ] );
fprintf( stdout, "child wrote to stdout\n" );
dup2( stderrPipe[ kPipeWriteIdx ], STDERR_FILENO);
close( stderrPipe[ kPipeReadIdx ] );
fprintf( stderr, "child wrote to stderr\n" );
logDebug( "child: execve( %s, %s )", argv[ 0 ], argv[ 1 ] );
execve( argv[ 0 ], argv, globals.envp );
/* !!! not expected to return !!! */
result = -EXIT_FAILURE;
}
break;
default:
close( stdoutPipe[ kPipeWriteIdx ] );
close( stderrPipe[ kPipeWriteIdx ] );
static struct epoll_event events[2];
int epfd = epoll_create(4);
events[0].events = EPOLLIN;
events[0].data.fd = stdoutPipe[ kPipeReadIdx ];
epoll_ctl( epfd, EPOLL_CTL_ADD, events[0].data.fd, &events[0] );
events[1].events = EPOLLIN;
events[1].data.fd = stderrPipe[ kPipeReadIdx ];
epoll_ctl( epfd, EPOLL_CTL_ADD, events[1].data.fd, &events[1] );
tElasticBuffer * stdoutBuf = newElasticBuffer( 16384, 2048 );
tElasticBuffer * stderrBuf = newElasticBuffer( 16384, 2048 );
/* event loop to drain the pipes. Also watches for
* EPOLLHUP, which implies the child exited */
bool streamEOF = false;
do {
int eventCount = epoll_wait( epfd, events, 2, 10000 );
for ( int i = 0; i < eventCount; i++ ) {
int eventFD = events[ i ].data.fd;
if ( events[ i ].events & EPOLLIN ) {
tElasticBuffer * buff = NULL;
if ( eventFD == stdoutPipe[ kPipeReadIdx ] ) {
buff = stdoutBuf;
} else if ( eventFD == stderrPipe[ kPipeReadIdx ] ) {
buff = stderrBuf;
} else {
logError( "epoll shows data available on"
" unknown file descriptor %d", eventFD );
/* throw away the data, or we'll continue to get the same event */
lseek( eventFD, 0, SEEK_END );
}
if ( buff != NULL ) {