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
  
/* 
|| This file is part of Pike. For copyright information see COPYRIGHT. 
|| Pike is distributed under GPL, LGPL and MPL. See the file COPYING 
|| for more information. 
*/ 
 
#include "module.h" 
#include "config.h" 
#include "operators.h" 
#include "interpret.h" 
#include "pike_error.h" 
#include "threads.h" 
#include "builtin_functions.h" 
#include "module_support.h" 
#include "program.h" 
#include "bignum.h" 
#include "backend.h" 
#include "pike_types.h" 
 
#ifdef HAVE_LIBFUSE 
/* Attempt to use FUSE API version 2.9 (if possible). */ 
#define FUSE_USE_VERSION 29 
#include <fuse.h> 
#include <fcntl.h> 
#include <dirent.h> 
#include <errno.h> 
#include <sys/statfs.h> 
#ifdef HAVE_SETXATTR 
#include <sys/xattr.h> 
#endif 
 
// All docs live in module.pmod.in This file, and Fuse, requires C99. 
 
#define THISC ((struct fuse_cmd_storage *)Pike_fp->current_storage) 
static struct program *fuse_cmd_program; 
struct fuse_cmd_storage 
{ 
    struct fuse *f; 
    struct fuse_cmd *cmd; 
}; 
 
static void f_fuse_cmd_process( INT32 UNUSED(args) ) 
{ 
    struct svalue *sp = Pike_sp; 
    fuse_process_cmd(THISC->f, THISC->cmd); 
} 
 
static struct object *global_fuse_obj; // There Can Be Only One 
 
#define DEFAULT_ERRNO() do{if((TYPEOF(Pike_sp[-1]) == T_INT) && Pike_sp[-1].u.integer) return -Pike_sp[-1].u.integer;return -ENOENT;}while(0) 
 
static int pf_getattr(const char *path, struct stat *stbuf) 
{ 
    extern struct program *stat_program; 
    struct stat *st; 
    push_text( path ); 
    apply( global_fuse_obj, "getattr", 1 ); 
    if( TYPEOF(Pike_sp[-1]) != PIKE_T_OBJECT || 
        !(st = get_storage( Pike_sp[-1].u.object, stat_program)) ) 
        DEFAULT_ERRNO(); 
    *stbuf = *st; 
    return 0; 
} 
 
static int pf_readlink(const char *path, char *buf, size_t size) 
{ 
    int res; 
    push_text( path ); 
    apply( global_fuse_obj, "readlink", 1 ); 
    if( (TYPEOF(Pike_sp[-1]) != PIKE_T_STRING) || 
        (Pike_sp[-1].u.string->size_shift) ) 
        DEFAULT_ERRNO(); 
    if( Pike_sp[-1].u.string->len >= (int)size ) 
        return -ENAMETOOLONG; 
    memcpy( buf, Pike_sp[-1].u.string->str, Pike_sp[-1].u.string->len+1); 
    return 0; 
} 
 
static struct program *getdir_program; 
struct getdir_storage 
{ 
    fuse_dirh_t h; 
    fuse_dirfil_t filler; 
}; 
#define THISGD ((struct getdir_storage *)Pike_fp->current_storage) 
static void f_getdir_callback( INT32 args ) 
{ 
    char *name; 
    get_all_args( NULL, args, "%s", &name ); 
    THISGD->filler( THISGD->h, name, 0, 0 ); 
} 
 
static void push_getdir_callback( fuse_dirh_t h, fuse_dirfil_t filler ) 
{ 
    struct object *o=clone_object( getdir_program, 0 ); 
    ((struct getdir_storage*)o->storage)->h = h; 
    ((struct getdir_storage*)o->storage)->filler = filler; 
    push_object( o ); 
} 
 
static int pf_getdir(const char *path, fuse_dirh_t h, fuse_dirfil_t filler) 
{ 
    push_text( path ); 
    push_getdir_callback( h, filler ); 
    apply( global_fuse_obj, "readdir", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_mknod(const char *path, mode_t mode, dev_t rdev) 
{ 
    push_text( path ); 
    push_int( mode ); 
    push_int( rdev ); 
    apply( global_fuse_obj, "mknod", 3 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_mkdir(const char *path, mode_t mode) 
{ 
    push_text( path ); 
    push_int( mode ); 
    apply( global_fuse_obj, "mkdir", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_unlink(const char *path) 
{ 
    push_text( path ); 
    apply( global_fuse_obj, "unlink", 1 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_rmdir(const char *path) 
{ 
    push_text( path ); 
    apply( global_fuse_obj, "rmdir", 1 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_symlink(const char *from, const char *to) 
{ 
    push_text( from ); 
    push_text( to ); 
    apply( global_fuse_obj, "symlink", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_rename(const char *from, const char *to) 
{ 
    push_text( from ); 
    push_text( to ); 
    apply( global_fuse_obj, "rename", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_link(const char *from, const char *to) 
{ 
    push_text( from ); 
    push_text( to ); 
    apply( global_fuse_obj, "link", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_chmod(const char *path, mode_t mode) 
{ 
    push_text( path ); 
    push_int( mode ); 
    apply( global_fuse_obj, "chmod", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_chown(const char *path, uid_t uid, gid_t gid) 
{ 
    push_text( path ); 
    push_int( uid ); 
    push_int( gid ); 
    apply( global_fuse_obj, "chown", 3 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_truncate(const char *path, off_t size) 
{ 
    push_text( path ); 
    push_int64( size ); 
    apply( global_fuse_obj, "truncate", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
#if FUSE_VERSION < 26 
static int pf_utime(const char *path, struct utimbuf *buf) 
{ 
    push_text( path ); 
    push_int( buf->actime ); 
    push_int( buf->modtime ); 
    apply( global_fuse_obj, "utime", 3 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
#else 
static int pf_utimens(const char *path, const struct timespec tv[2]) 
{ 
    push_text( path ); 
    push_int64( (INT64)tv[0].tv_sec*(INT64)1000000000 + (INT64)tv[0].tv_nsec ); 
    push_int64( (INT64)tv[1].tv_sec*(INT64)1000000000 + (INT64)tv[1].tv_nsec ); 
    apply( global_fuse_obj, "utimens", 3 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
#endif 
 
static int pf_open(const char *path, struct fuse_file_info *fi) 
{ 
    push_text( path ); 
    push_int( fi->flags ); 
    apply( global_fuse_obj, "open", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_read(const char *path, char *buf, size_t size, off_t offset, 
                    struct fuse_file_info *UNUSED(fi)) 
{ 
    push_text( path ); 
    push_int( size ); 
    push_int64( offset ); 
    apply( global_fuse_obj, "read", 3 ); 
 
    if( (TYPEOF(Pike_sp[-1]) != PIKE_T_STRING) || 
        (Pike_sp[-1].u.string->size_shift) ) 
        DEFAULT_ERRNO(); 
    if (((size_t)Pike_sp[-1].u.string->len) > size) { 
        return -ENAMETOOLONG; 
    } 
    memcpy( buf, Pike_sp[-1].u.string->str, Pike_sp[-1].u.string->len ); 
    return Pike_sp[-1].u.string->len; 
} 
 
static int pf_write(const char *path, const char *buf, size_t size, 
                    off_t offset, struct fuse_file_info *UNUSED(fi)) 
{ 
    push_text( path ); 
    push_string( make_shared_binary_string(buf, size ) ); 
    push_int64( offset ); 
    apply( global_fuse_obj, "write", 3 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_statfs(const char *path, struct statvfs *stbuf) 
{ 
    struct mapping *m; 
    struct svalue *val; 
 
    push_text( path ); 
    apply( global_fuse_obj, "statfs", 1 ); 
    if( TYPEOF(Pike_sp[-1]) != PIKE_T_MAPPING ) 
        DEFAULT_ERRNO(); 
    m = Pike_sp[-1].u.mapping; 
    memset( stbuf, 0, sizeof(*stbuf) ); 
    stbuf->f_namemax = 4096; 
    stbuf->f_bsize = 1024; 
#define STSET(X)    do {                              \ 
      if ((val=simple_mapping_string_lookup( m,#X )) && \ 
          (TYPEOF(*val) == T_INT))                      \ 
        stbuf->f_##X=val->u.integer;                    \ 
    } while(0) 
    STSET(bsize); 
    STSET(blocks); 
    STSET(bfree); 
    STSET(bavail); 
    STSET(files); 
    STSET(ffree); 
    if (((val=simple_mapping_string_lookup( m,"namemax" )) && 
         (TYPEOF(*val) == T_INT)) || 
        /* namelen is compat. */ 
        ((val=simple_mapping_string_lookup( m,"namelen" )) && 
         (TYPEOF(*val) == T_INT))) { 
      stbuf->f_namemax = val->u.integer; 
    } 
#undef STSET 
    return 0; 
} 
 
static int pf_release(const char *path, struct fuse_file_info *UNUSED(fi)) 
{ 
    push_text( path ); 
    apply( global_fuse_obj, "release", 1 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_fsync(const char *path, int isdatasync, 
                     struct fuse_file_info *UNUSED(fi)) 
{ 
    push_text( path ); 
    push_int( isdatasync ); 
    apply( global_fuse_obj, "fsync", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_setxattr(const char *path, const char *name, const char *value, 
                        size_t size, int flags) 
{ 
    push_text( path ); 
    push_text( name ); 
    push_string( make_shared_binary_string( value, size ) ); 
    push_int( flags ); 
    apply( global_fuse_obj, "setxattr", 4 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_getxattr(const char *path, const char *name, char *value, 
                       size_t size) 
{ 
    unsigned int ds; 
    push_text( path ); 
    push_text( name ); 
    apply( global_fuse_obj, "getxattr", 2 ); 
    if( TYPEOF(Pike_sp[-1]) != PIKE_T_STRING || 
        (Pike_sp[-1].u.string->size_shift) ) 
        DEFAULT_ERRNO(); 
    ds = Pike_sp[-1].u.string->len <<Pike_sp[-1].u.string->size_shift; 
    if( !size ) 
        return ds; 
    if( size < ds ) 
        return -ERANGE; 
    memcpy( value, Pike_sp[-1].u.string->str, ds ); 
    return ds; 
} 
 
static int pf_listxattr(const char *path, char *list, size_t size) 
{ 
    unsigned int ds; 
 
    push_text( path ); 
    apply( global_fuse_obj, "listxattr", 1 ); 
    if( TYPEOF(Pike_sp[-1]) != PIKE_T_ARRAY ) 
        DEFAULT_ERRNO(); 
    push_string( make_shared_binary_string( "\0", 1 ) ); 
    o_multiply(); 
    if( TYPEOF(Pike_sp[-1]) != PIKE_T_STRING || 
        (Pike_sp[-1].u.string->size_shift) ) 
        DEFAULT_ERRNO(); 
    /* We need to account for the terminating NUL. */ 
    ds = (Pike_sp[-1].u.string->len + 1) <<Pike_sp[-1].u.string->size_shift; 
    if( !size ) 
        return ds; 
    if( size < ds ) 
        return -ERANGE; 
    memcpy( list, Pike_sp[-1].u.string->str, ds ); 
    return ds; 
} 
 
static int pf_removexattr(const char *path, const char *name) 
{ 
    push_text( path ); 
    push_text( name ); 
    apply( global_fuse_obj, "removexattr", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_creat( const char *path, mode_t mode, struct fuse_file_info *fi) 
{ 
    push_text( path ); 
    push_int( mode ); 
    push_int( fi->flags ); 
    apply( global_fuse_obj, "creat", 3 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_access( const char *path, int mode) 
{ 
    push_text( path ); 
    push_int( mode ); 
    apply( global_fuse_obj, "access", 2 ); 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
        DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
} 
 
static int pf_lock( const char *path, struct fuse_file_info *fi, int cmd, struct flock *lck ) 
{ 
  push_text( path ); 
  push_int( cmd ); 
 
  push_static_text("owner"); push_int( fi->lock_owner ); 
  ref_push_string(literal_type_string);  push_int( lck->l_type ); 
  push_static_text("whence");push_int( lck->l_whence ); 
  push_static_text("start");   push_int( lck->l_start ); 
  push_static_text("len");   push_int( lck->l_len ); 
  push_static_text("pid");   push_int( lck->l_pid ); 
  f_aggregate_mapping( 6 * 2 ); 
  apply( global_fuse_obj, "lock", 3 ); 
 
  if( TYPEOF(Pike_sp[-1]) == PIKE_T_MAPPING ) 
  { 
    struct svalue *tmp; 
#define COUT(X)                                                         \ 
    if((tmp=simple_mapping_string_lookup(Pike_sp[-1].u.mapping,#X))     \ 
       && TYPEOF(*tmp)==PIKE_T_INT) lck->l_##X = tmp->u.integer; 
    COUT(type);  COUT(whence); 
    COUT(start);  COUT(len); 
    COUT(pid); 
#undef COUT 
    return 0; 
  } 
  else 
  { 
    if (TYPEOF(Pike_sp[-1]) != T_INT) 
      DEFAULT_ERRNO(); 
    return -Pike_sp[-1].u.integer; 
  } 
} 
 
static struct fuse_operations pike_fuse_oper = { 
    .getattr    = pf_getattr, 
    .readlink   = pf_readlink, 
    .getdir     = pf_getdir, 
    .mknod      = pf_mknod, 
    .mkdir      = pf_mkdir, 
    .unlink     = pf_unlink, 
    .rmdir      = pf_rmdir, 
    .symlink    = pf_symlink, 
    .rename     = pf_rename, 
    .link       = pf_link, 
    .chmod      = pf_chmod, 
    .chown      = pf_chown, 
    .truncate   = pf_truncate, 
#if FUSE_VERSION >= 26 
    .utimens    = pf_utimens, 
#else 
    .utime      = pf_utime, 
#endif 
#if FUSE_VERSION >= 26 
    .lock       = pf_lock, 
#endif 
    .open       = pf_open, 
    .read       = pf_read, 
    .write      = pf_write, 
    .statfs     = pf_statfs, 
    .release    = pf_release, 
    .fsync      = pf_fsync, 
    .setxattr   = pf_setxattr, 
    .getxattr   = pf_getxattr, 
    .listxattr  = pf_listxattr, 
    .removexattr= pf_removexattr, 
    .access     = pf_access, 
    .create     = pf_creat, 
}; 
 
struct passon { 
    struct fuse *f; 
    struct fuse_cmd *cmd; 
}; 
 
static void low_dispatch_fuse_command( void *ptr ) 
{ 
    struct passon *x = (struct passon *)ptr; 
    struct svalue *old = Pike_sp; 
    fuse_process_cmd( x->f, x->cmd ); 
    pop_n_elems( Pike_sp - old ); 
} 
 
static void dispatch_fuse_command( struct fuse *f, struct fuse_cmd *cmd, void *UNUSED(a) ) 
{ 
    struct passon x = { 
        .f = f, 
        .cmd = cmd 
    }; 
 
    call_with_interpreter(low_dispatch_fuse_command, &x ); 
} 
 
static int global_fuse_fd; 
static char *global_fuse_mp; 
static struct fuse *global_fuse; 
static void pf_fuse_teardown( ) 
{ 
    if( global_fuse ) 
    { 
#if FUSE_VERSION <= 25 
        fuse_teardown( global_fuse, global_fuse_fd, global_fuse_mp ); 
#else 
        fuse_teardown( global_fuse, global_fuse_mp ); 
#endif 
        global_fuse = NULL; 
    } 
} 
 
static void f_fuse_run( INT32 nargs ) 
{ 
    struct fuse *fuse; 
    int i, multi, fd; 
    char **argv, *mountpoint; 
    struct array *args; 
    if( global_fuse_obj ) 
        Pike_error("There can be only one.\n" 
                   "You have to run multiple processes to have multiple FUSE filesystems\n"); 
 
    get_all_args( NULL, nargs, "%o%a", &global_fuse_obj, &args ); 
 
    argv = malloc( sizeof(char *) * args->size ); 
    for( i = 0; i<args->size; i++ ) 
    { 
        if( TYPEOF(args->item[i]) != PIKE_T_STRING || 
            string_has_null(args->item[i].u.string) ) 
        { 
            free( argv ); 
            Pike_error("Argument %d is not a nonbinary string\n", i ); 
        } 
        argv[i] = args->item[i].u.string->str; 
    } 
    fuse = fuse_setup(args->size, argv, &pike_fuse_oper, sizeof(pike_fuse_oper), 
                      &mountpoint, &multi, &fd ); 
    free( argv ); 
 
    global_fuse = fuse; 
    global_fuse_mp = mountpoint; 
    global_fuse_fd = fd; 
    atexit( pf_fuse_teardown ); 
 
    if (fuse == NULL) 
        Pike_error("Fuse init failed\n"); 
 
    enable_external_threads(); 
    THREADS_ALLOW(); 
    if( !fuse_exited( fuse ) ) 
        fuse_loop_mt_proc( fuse, dispatch_fuse_command, 0 ); 
    THREADS_DISALLOW(); 
    // NOTE: Returning to pike tends to hang the kernel, unfortunately, unless pike exits correctly. 
    // Hence exit here. 
#if FUSE_VERSION <= 25 
    fuse_teardown( fuse, fd, mountpoint ); 
#else 
    fuse_teardown( fuse, mountpoint); 
#endif 
    global_fuse = NULL; 
    exit(0); 
    global_fuse_obj=NULL; 
} 
 
PIKE_MODULE_EXIT 
{ 
  free_program( getdir_program ); 
  free_program( fuse_cmd_program ); 
} 
 
/*! @decl constant FUSE_MAJOR_VERSION 
 *! @decl constant FUSE_MINOR_VERSION 
 *! 
 *!  The version of FUSE 
 */ 
 
PIKE_MODULE_INIT 
{ 
    ADD_FUNCTION( "run", f_fuse_run, tFunc(tObj tArr(tStr),tVoid ), 0 ); 
 
    start_new_program( ); 
    { 
        ADD_STORAGE( struct getdir_storage ); 
        ADD_FUNCTION( "`()", f_getdir_callback, tFunc(tStr tOr(tInt,tVoid) tOr(tInt,tVoid),tVoid ), 0 ); 
    } 
    getdir_program = end_program(); 
 
    start_new_program( ); 
    { 
        ADD_STORAGE( struct fuse_cmd_storage ); 
        ADD_FUNCTION( "`()", f_fuse_cmd_process, tFunc(tVoid,tVoid), 0 ); 
    } 
    fuse_cmd_program = end_program(); 
 
    add_integer_constant("F_RDLCK", F_GETLK, 0 ); 
    add_integer_constant("F_WRLCK", F_WRLCK, 0 ); 
    add_integer_constant("F_UNLCK", F_UNLCK, 0 ); 
    add_integer_constant("F_GETLK", F_GETLK, 0 ); 
    add_integer_constant("F_SETLK", F_SETLK, 0 ); 
    add_integer_constant("F_SETLKW", F_SETLKW, 0 ); 
    add_integer_constant("FUSE_MAJOR_VERSION", FUSE_MAJOR_VERSION, 0); 
    add_integer_constant("FUSE_MINOR_VERSION", FUSE_MINOR_VERSION, 0); 
} 
#else 
PIKE_MODULE_EXIT 
{ 
} 
 
PIKE_MODULE_INIT 
{ 
  HIDE_MODULE(); 
} 
#endif /* HAVE_LIBFUSE */