This repository has been archived by the owner on Oct 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathxdp-fuse.c
2234 lines (1853 loc) · 52.8 KB
/
xdp-fuse.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
#include "config.h"
#define FUSE_USE_VERSION 26
#include <glib-unix.h>
#include <fuse_lowlevel.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <stdlib.h>
#include <assert.h>
#include <glib/gprintf.h>
#include <gio/gio.h>
#include <pthread.h>
#include "xdg-app-portal-error.h"
#include "xdp-fuse.h"
#include "xdp-util.h"
#include "xdg-app-utils.h"
/* Layout:
"/ (STD_DIRS:1)
"by-app/" (STD_DIRS:2)
"org.gnome.gedit/" (APP_DIR:app id)
"$id/" (APP_DOC_DIR:app_id<<32|doc_id)
<same as DOC_DIR>
"$id" (APP_DOC_DIR:(app_id==0)<<32|doc_idid)
$basename (APP_DOC_FILE:app_id<<32|doc_id) (app id == 0 if not in app dir)
$tmpfile (TMPFILE:tmp_id)
*/
#define BY_APP_INO 2
#define NON_DOC_DIR_PERMS 0500
#define DOC_DIR_PERMS 0700
/* The (fake) directories don't really change */
#define DIRS_ATTR_CACHE_TIME 60.0
/* We pretend that the file is hardlinked. This causes most apps to do
a truncating overwrite, which suits us better, as we do the atomic
rename ourselves anyway. This way we don't weirdly change the inode
after the rename. */
#define DOC_FILE_NLINK 2
typedef enum {
STD_DIRS_INO_CLASS,
TMPFILE_INO_CLASS,
APP_DIR_INO_CLASS,
APP_DOC_DIR_INO_CLASS,
APP_DOC_FILE_INO_CLASS,
} XdpInodeClass;
#define BY_APP_NAME "by-app"
static GHashTable *app_name_to_id;
static GHashTable *app_id_to_name;
static guint32 next_app_id = 1;
G_LOCK_DEFINE(app_id);
static GThread *fuse_thread = NULL;
static struct fuse_session *session = NULL;
static struct fuse_chan *main_ch = NULL;
static char *mount_path = NULL;
static pthread_t fuse_pthread = 0;
static int
steal_fd (int *fdp)
{
int fd = *fdp;
*fdp = -1;
return fd;
}
static int
get_user_perms (const struct stat *stbuf)
{
/* Strip out exec and setuid bits */
return stbuf->st_mode & 0666;
}
static double
get_attr_cache_time (int st_mode)
{
if (S_ISDIR (st_mode))
return DIRS_ATTR_CACHE_TIME;
return 0.0;
}
static double
get_entry_cache_time (fuse_ino_t inode)
{
/* We have to disable entry caches because otherwise we have a race
on rename. The kernel set the target inode as NOEXIST after a
rename, which breaks in the tmp over real case due to us reusing
the old non-temp inode. */
return 0.0;
}
/******************************* XdpTmp *******************************
*
* XdpTmp is a ref-counted object representing a temporary file created
* on the outer filesystem which is stored next to a real file in the fuse
* filesystem. Its useful to support write-to-tmp-then-rename-over-target
* operations.
*
* locking:
*
* The global list of outstanding Tmp are protected by the tmp_files
* lock. Use it when doing lookups by name or id, or when changing
* the list (add/remove) or name of a tmpfile.
*
* Each instance has a mutex that locks access to the backing path,
* as it can be removed at runtime. Use get/steal_backing_basename() to
* safely access it.
*
******************************* XdpTmp *******************************/
static volatile gint next_tmp_id = 1;
typedef struct
{
volatile gint ref_count;
/* These are immutable, no lock needed */
guint64 parent_inode;
guint32 tmp_id;
XdgAppDbEntry *entry;
/* Changes always done under tmp_files lock */
char *name;
GMutex mutex;
/* protected by mutex */
char *backing_basename;
} XdpTmp;
/* Owns a ref to the files */
static GList *tmp_files = NULL;
G_LOCK_DEFINE(tmp_files);
static XdpTmp *
xdp_tmp_ref (XdpTmp *tmp)
{
g_atomic_int_inc (&tmp->ref_count);
return tmp;
}
static void
xdp_tmp_unref (XdpTmp *tmp)
{
if (g_atomic_int_dec_and_test (&tmp->ref_count))
{
xdg_app_db_entry_unref (tmp->entry);
g_free (tmp->name);
g_free (tmp->backing_basename);
g_free (tmp);
}
}
char *
xdp_tmp_get_backing_basename (XdpTmp *tmp)
{
char *res;
g_mutex_lock (&tmp->mutex);
res = g_strdup (tmp->backing_basename);
g_mutex_unlock (&tmp->mutex);
return res;
}
char *
xdp_tmp_steal_backing_basename (XdpTmp *tmp)
{
char *res;
g_mutex_lock (&tmp->mutex);
res = tmp->backing_basename;
tmp->backing_basename = NULL;
g_mutex_unlock (&tmp->mutex);
return res;
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC(XdpTmp, xdp_tmp_unref)
/* Must first take tmp_files lock */
static XdpTmp *
find_tmp_by_name_nolock (guint64 parent_inode,
const char *name)
{
GList *l;
for (l = tmp_files; l != NULL; l = l->next)
{
XdpTmp *tmp = l->data;
if (tmp->parent_inode == parent_inode &&
strcmp (tmp->name, name) == 0)
return xdp_tmp_ref (tmp);
}
return NULL;
}
/* Takes tmp_files lock */
static XdpTmp *
find_tmp_by_name (guint64 parent_inode,
const char *name)
{
AUTOLOCK(tmp_files);
return find_tmp_by_name_nolock (parent_inode, name);
}
/* Takes tmp_files lock */
static XdpTmp *
find_tmp_by_id (guint32 tmp_id)
{
GList *l;
AUTOLOCK(tmp_files);
for (l = tmp_files; l != NULL; l = l->next)
{
XdpTmp *tmp = l->data;
if (tmp->tmp_id == tmp_id)
return xdp_tmp_ref (tmp);
}
return NULL;
}
/* Caller must hold tmp_files lock */
static XdpTmp *
xdp_tmp_new_nolock (fuse_ino_t parent,
XdgAppDbEntry *entry,
const char *name,
const char *tmp_basename)
{
XdpTmp *tmp;
g_autofree char *tmp_dirname = NULL;
/* We store the pathname instead of dir_fd + basename, because
its very easy to get a lot of tempfiles leaking and that would
mean quite a lot of open fds */
tmp_dirname = xdp_entry_dup_dirname (entry);
tmp = g_new0 (XdpTmp, 1);
tmp->ref_count = 2; /* One owned by tmp_files */
tmp->tmp_id = g_atomic_int_add (&next_tmp_id, 1);
tmp->parent_inode = parent;
tmp->name = g_strdup (name);
tmp->entry = xdg_app_db_entry_ref (entry);
tmp->backing_basename = g_strdup (tmp_basename);
tmp_files = g_list_prepend (tmp_files, tmp);
return tmp;
}
/* Caller must own tmp_files lock */
static void
xdp_tmp_unlink_nolock (XdpTmp *tmp)
{
g_autofree char *backing_basename = NULL;
backing_basename = xdp_tmp_steal_backing_basename (tmp);
if (backing_basename)
{
glnx_fd_close int dir_fd = xdp_entry_open_dir (tmp->entry);
if (dir_fd)
unlinkat (dir_fd, backing_basename, 0);
}
tmp_files = g_list_remove (tmp_files, tmp);
xdp_tmp_unref (tmp);
}
/******************************* XdpFh *******************************
*
* XdpFh is a ref-counted object representing an open file on the
* filesystem. Normally it has a regular fd you can do only the allowed
* i/o on, although in the case of a direct write to a document file
* it has two fds, one is the read-only fd to the file, and the other
* is a read-write to a temporary file which is only used once the
* file is truncated (and is renamed over the real file on close).
*
* locking:
*
* The global list of outstanding Fh is protected by the open_files
* lock. Use it when doing lookups by inode, or when changing
* the list (add/remove), or when otherwise traversing the list.
*
* Each instance has a mutex that must be locked when doing some
* kind of operation on the file handle, to serialize both lower
* layer i/o as well as access to the members.
*
* To avoid deadlocks or just slow locking, never aquire the
* open_files lock and a lock on a Fh at the same time.
*
******************************* XdpFh *******************************/
typedef struct
{
volatile gint ref_count;
/* These are immutable, no lock needed */
guint32 tmp_id;
fuse_ino_t inode;
int dir_fd;
char *trunc_basename;
char *real_basename;
gboolean can_write;
/* These need a lock whenever they are used */
int fd;
int trunc_fd;
gboolean truncated;
gboolean readonly;
GMutex mutex;
} XdpFh;
static GList *open_files = NULL;
G_LOCK_DEFINE(open_files);
static XdpFh *
xdp_fh_ref (XdpFh *fh)
{
g_atomic_int_inc (&fh->ref_count);
return fh;
}
static void
xdp_fh_finalize (XdpFh *fh)
{
if (fh->truncated)
{
fsync (fh->trunc_fd);
if (renameat (fh->dir_fd, fh->trunc_basename,
fh->dir_fd, fh->real_basename) != 0)
g_warning ("Unable to replace truncated document");
}
else if (fh->trunc_basename)
unlinkat (fh->dir_fd, fh->trunc_basename, 0);
if (fh->fd >= 0)
close (fh->fd);
if (fh->trunc_fd >= 0)
close (fh->trunc_fd);
if (fh->dir_fd >= 0)
close (fh->dir_fd);
g_clear_pointer (&fh->trunc_basename, g_free);
g_clear_pointer (&fh->real_basename, g_free);
g_free (fh);
}
static void
xdp_fh_unref (XdpFh *fh)
{
if (g_atomic_int_dec_and_test (&fh->ref_count))
{
/* There is a tiny race here where fhs can be on the open_files list
with refcount 0, so make sure to skip such while under the open_files
lock */
{
AUTOLOCK (open_files);
open_files = g_list_remove (open_files, fh);
}
xdp_fh_finalize (fh);
}
}
G_DEFINE_AUTOPTR_CLEANUP_FUNC(XdpFh, xdp_fh_unref)
static void
xdp_fh_lock (XdpFh *fh)
{
g_mutex_lock (&fh->mutex);
}
static void
xdp_fh_unlock (XdpFh *fh)
{
g_mutex_unlock (&fh->mutex);
}
static inline void xdp_fh_auto_unlock_helper (XdpFh **fhp)
{
if (*fhp)
xdp_fh_unlock (*fhp);
}
static inline XdpFh *xdp_fh_auto_lock_helper (XdpFh *fh)
{
if (fh)
xdp_fh_lock (fh);
return fh;
}
#define XDP_FH_AUTOLOCK(_fh) G_GNUC_UNUSED __attribute__((cleanup(xdp_fh_auto_unlock_helper))) XdpFh * G_PASTE(xdp_fh_auto_unlock, __LINE__) = xdp_fh_auto_lock_helper (fh)
static XdpFh *
xdp_fh_new (fuse_ino_t inode,
struct fuse_file_info *fi,
int fd,
XdpTmp *tmp)
{
XdpFh *fh = g_new0 (XdpFh, 1);
fh->inode = inode;
fh->fd = fd;
if (tmp)
fh->tmp_id = tmp->tmp_id;
fh->dir_fd = -1;
fh->trunc_fd = -1;
fh->ref_count = 1; /* Owned by fuse_file_info fi */
fi->fh = (gsize)fh;
AUTOLOCK (open_files);
open_files = g_list_prepend (open_files, fh);
return fh;
}
static int
xdp_fh_get_fd_nolock (XdpFh *fh)
{
if (fh->truncated)
return fh->trunc_fd;
else
return fh->fd;
}
static int
xdp_fh_fstat (XdpFh *fh,
struct stat *stbuf)
{
struct stat tmp_stbuf;
int fd;
fd = xdp_fh_get_fd_nolock (fh);
if (fd < 0)
return -ENOSYS;
if (fstat (fd, &tmp_stbuf) != 0)
return -errno;
stbuf->st_nlink = DOC_FILE_NLINK;
stbuf->st_mode = S_IFREG | get_user_perms (&tmp_stbuf);
if (!fh->can_write)
stbuf->st_mode &= ~(0222);
stbuf->st_size = tmp_stbuf.st_size;
stbuf->st_uid = tmp_stbuf.st_uid;
stbuf->st_gid = tmp_stbuf.st_gid;
stbuf->st_blksize = tmp_stbuf.st_blksize;
stbuf->st_blocks = tmp_stbuf.st_blocks;
stbuf->st_atim = tmp_stbuf.st_atim;
stbuf->st_mtim = tmp_stbuf.st_mtim;
stbuf->st_ctim = tmp_stbuf.st_ctim;
return 0;
}
static int
xdp_fh_fstat_locked (XdpFh *fh,
struct stat *stbuf)
{
XDP_FH_AUTOLOCK (fh);
return xdp_fh_fstat (fh, stbuf);
}
static int
xdp_fh_truncate_locked (XdpFh *fh, off_t size, struct stat *newattr)
{
int fd;
XDP_FH_AUTOLOCK (fh);
if (fh->trunc_fd >= 0 && !fh->truncated)
{
if (size != 0)
return -EACCES;
fh->truncated = TRUE;
fd = fh->trunc_fd;
}
else
{
fd = xdp_fh_get_fd_nolock (fh);
if (fd == -1)
return -EIO;
if (ftruncate (fd, size) != 0)
return - errno;
}
if (newattr)
{
int res = xdp_fh_fstat (fh, newattr);
if (res < 0)
return res;
}
return 0;
}
static void
mark_open_tmp_file_readonly (guint32 tmp_id)
{
GList *found = NULL;
GList *l;
{
AUTOLOCK (open_files);
for (l = open_files; l != NULL; l = l->next)
{
XdpFh *fh = l->data;
/* See xdp_fh_unref for details of this ref_count check */
if (g_atomic_int_get (&fh->ref_count) > 0 &&
fh->tmp_id == tmp_id && fh->fd >= 0)
found = g_list_prepend (found, xdp_fh_ref (fh));
}
}
/* We do the actual updates outside of the open_files lock to avoid
potentially blocking for a long time with it held */
for (l = found; l != NULL; l = l->next)
{
XdpFh *fh = l->data;
XDP_FH_AUTOLOCK (fh);
fh->readonly = TRUE;
xdp_fh_unref (fh);
}
g_list_free (found);
}
static XdpFh *
find_open_fh (fuse_ino_t ino)
{
GList *l;
AUTOLOCK (open_files);
for (l = open_files; l != NULL; l = l->next)
{
XdpFh *fh = l->data;
/* See xdp_fh_unref for details of this ref_count check */
if (fh->inode == ino &&
g_atomic_int_get (&fh->ref_count) > 0)
return xdp_fh_ref (fh);
}
return NULL;
}
/******************************* Main *******************************/
static XdpInodeClass
get_class (guint64 inode)
{
return (inode >> (64-8)) & 0xff;
}
static guint64
get_class_ino (guint64 inode)
{
return inode & ((1L << (64-8)) - 1);
}
static guint32
get_app_id_from_app_doc_ino (guint64 inode)
{
return inode >> 32;
}
static guint32
get_doc_id_from_app_doc_ino (guint64 inode)
{
return inode & 0xffffffff;
}
static guint64
make_inode (XdpInodeClass class, guint64 inode)
{
return ((guint64)class) << (64-8) | (inode & 0xffffffffffffff);
}
static guint64
make_app_doc_dir_inode (guint32 app_id, guint32 doc_id)
{
return make_inode (APP_DOC_DIR_INO_CLASS,
((guint64)app_id << 32) | (guint64)doc_id);
}
static guint64
make_app_doc_file_inode (guint32 app_id, guint32 doc_id)
{
return make_inode (APP_DOC_FILE_INO_CLASS,
((guint64)app_id << 32) | (guint64)doc_id);
}
static gboolean
name_looks_like_id (const char *name)
{
int i;
/* No zeros in front, we need canonical form */
if (name[0] == '0')
return FALSE;
for (i = 0; i < 8; i++)
{
char c = name[i];
if (c == 0)
break;
if (!g_ascii_isdigit(c) &&
!(c >= 'a' && c <= 'f'))
return FALSE;
}
if (name[i] != 0)
return FALSE;
return TRUE;
}
static guint32
get_app_id_from_name (const char *name)
{
guint32 id;
char *myname;
AUTOLOCK(app_id);
id = GPOINTER_TO_UINT (g_hash_table_lookup (app_name_to_id, name));
if (id != 0)
return id;
id = next_app_id++;
/* We rely this to not overwrap into the high byte in the inode */
g_assert (id < 0x00ffffff);
myname = g_strdup (name);
g_hash_table_insert (app_name_to_id, myname, GUINT_TO_POINTER (id));
g_hash_table_insert (app_id_to_name, GUINT_TO_POINTER (id), myname);
return id;
}
static const char *
get_app_name_from_id (guint32 id)
{
AUTOLOCK(app_id);
return g_hash_table_lookup (app_id_to_name, GUINT_TO_POINTER (id));
}
static void
fill_app_name_hash (void)
{
g_auto(GStrv) keys = NULL;
int i;
keys = xdp_list_apps ();
for (i = 0; keys[i] != NULL; i++)
get_app_id_from_name (keys[i]);
}
static gboolean
app_can_see_doc (XdgAppDbEntry *entry, guint32 app_id)
{
const char *app_name = get_app_name_from_id (app_id);
if (app_id == 0)
return TRUE;
if (app_name != NULL &&
xdp_entry_has_permissions (entry, app_name, XDP_PERMISSION_FLAGS_READ))
return TRUE;
return FALSE;
}
static gboolean
app_can_write_doc (XdgAppDbEntry *entry, guint32 app_id)
{
const char *app_name = get_app_name_from_id (app_id);
if (app_id == 0)
return TRUE;
if (app_name != NULL &&
xdp_entry_has_permissions (entry, app_name, XDP_PERMISSION_FLAGS_WRITE))
return TRUE;
return FALSE;
}
static int
xdp_stat (fuse_ino_t ino,
struct stat *stbuf,
XdgAppDbEntry **entry_out)
{
XdpInodeClass class = get_class (ino);
guint64 class_ino = get_class_ino (ino);
g_autoptr (XdgAppDbEntry) entry = NULL;
struct stat tmp_stbuf;
g_autoptr(XdpTmp) tmp = NULL;
g_autofree char *backing_basename = NULL;
stbuf->st_ino = ino;
switch (class)
{
case STD_DIRS_INO_CLASS:
switch (class_ino)
{
case FUSE_ROOT_ID:
stbuf->st_mode = S_IFDIR | NON_DOC_DIR_PERMS;
stbuf->st_nlink = 2;
break;
case BY_APP_INO:
stbuf->st_mode = S_IFDIR | NON_DOC_DIR_PERMS;
stbuf->st_nlink = 2;
break;
default:
return ENOENT;
}
break;
case APP_DIR_INO_CLASS:
if (get_app_name_from_id (class_ino) == 0)
return ENOENT;
stbuf->st_mode = S_IFDIR | NON_DOC_DIR_PERMS;
stbuf->st_nlink = 2;
break;
case APP_DOC_DIR_INO_CLASS:
{
guint32 app_id = get_app_id_from_app_doc_ino (class_ino);
guint32 doc_id = get_doc_id_from_app_doc_ino (class_ino);
entry = xdp_lookup_doc (doc_id);
if (entry == NULL || !app_can_see_doc (entry, app_id))
return ENOENT;
stbuf->st_mode = S_IFDIR | DOC_DIR_PERMS;
stbuf->st_nlink = 2;
break;
}
case APP_DOC_FILE_INO_CLASS:
{
guint32 app_id = get_app_id_from_app_doc_ino (class_ino);
guint32 doc_id = get_doc_id_from_app_doc_ino (class_ino);
gboolean can_write;
entry = xdp_lookup_doc (doc_id);
if (entry == NULL)
return ENOENT;
can_write = app_can_write_doc (entry, app_id);
stbuf->st_nlink = DOC_FILE_NLINK;
if (xdp_entry_stat (entry, &tmp_stbuf, AT_SYMLINK_NOFOLLOW) != 0)
return ENOENT;
stbuf->st_mode = S_IFREG | get_user_perms (&tmp_stbuf);
if (!can_write)
stbuf->st_mode &= ~(0222);
stbuf->st_size = tmp_stbuf.st_size;
stbuf->st_uid = tmp_stbuf.st_uid;
stbuf->st_gid = tmp_stbuf.st_gid;
stbuf->st_blksize = tmp_stbuf.st_blksize;
stbuf->st_blocks = tmp_stbuf.st_blocks;
stbuf->st_atim = tmp_stbuf.st_atim;
stbuf->st_mtim = tmp_stbuf.st_mtim;
stbuf->st_ctim = tmp_stbuf.st_ctim;
break;
}
case TMPFILE_INO_CLASS:
tmp = find_tmp_by_id (class_ino);
if (tmp == NULL)
return ENOENT;
stbuf->st_mode = S_IFREG;
stbuf->st_nlink = DOC_FILE_NLINK;
backing_basename = xdp_tmp_get_backing_basename (tmp);
{
glnx_fd_close int dir_fd = xdp_entry_open_dir (tmp->entry);
if (backing_basename == NULL ||
dir_fd == -1 ||
fstatat (dir_fd, backing_basename, &tmp_stbuf, 0) != 0)
return ENOENT;
}
stbuf->st_mode = S_IFREG | get_user_perms (&tmp_stbuf);
stbuf->st_size = tmp_stbuf.st_size;
stbuf->st_uid = tmp_stbuf.st_uid;
stbuf->st_gid = tmp_stbuf.st_gid;
stbuf->st_blksize = tmp_stbuf.st_blksize;
stbuf->st_blocks = tmp_stbuf.st_blocks;
stbuf->st_atim = tmp_stbuf.st_atim;
stbuf->st_mtim = tmp_stbuf.st_mtim;
stbuf->st_ctim = tmp_stbuf.st_ctim;
break;
default:
return ENOENT;
}
if (entry && entry_out)
*entry_out = g_steal_pointer (&entry);
return 0;
}
static void
xdp_fuse_getattr (fuse_req_t req,
fuse_ino_t ino,
struct fuse_file_info *fi)
{
struct stat stbuf = { 0 };
g_autoptr(XdpFh) fh = NULL;
int res;
g_debug ("xdp_fuse_getattr %lx (fi=%p)", ino, fi);
/* Fuse passes fi in to verify EOF during read/write/seek, but not during fstat */
if (fi != NULL)
{
XdpFh *fh = (gpointer)fi->fh;
res = xdp_fh_fstat_locked (fh, &stbuf);
if (res == 0)
{
fuse_reply_attr (req, &stbuf, get_attr_cache_time (stbuf.st_mode));
return;
}
}
fh = find_open_fh (ino);
if (fh)
{
res = xdp_fh_fstat_locked (fh, &stbuf);
if (res == 0)
{
fuse_reply_attr (req, &stbuf, get_attr_cache_time (stbuf.st_mode));
return;
}
}
if ((res = xdp_stat (ino, &stbuf, NULL)) != 0)
fuse_reply_err (req, res);
else
fuse_reply_attr (req, &stbuf, get_attr_cache_time (stbuf.st_mode));
}
static int
xdp_lookup (fuse_ino_t parent,
const char *name,
fuse_ino_t *inode,
struct stat *stbuf,
XdgAppDbEntry **entry_out,
XdpTmp **tmp_out)
{
XdpInodeClass parent_class = get_class (parent);
guint64 parent_class_ino = get_class_ino (parent);
g_autoptr (XdgAppDbEntry) entry = NULL;
g_autoptr (XdpTmp) tmp = NULL;
if (entry_out)
*entry_out = NULL;
if (tmp_out)
*tmp_out = NULL;
switch (parent_class)
{
case STD_DIRS_INO_CLASS:
switch (parent_class_ino)
{
case FUSE_ROOT_ID:
if (strcmp (name, BY_APP_NAME) == 0)
{
*inode = make_inode (STD_DIRS_INO_CLASS, BY_APP_INO);
if (xdp_stat (*inode, stbuf, NULL) == 0)
return 0;
}
else if (name_looks_like_id (name))
{
*inode = make_app_doc_dir_inode (0, xdp_id_from_name (name));
if (xdp_stat (*inode, stbuf, NULL) == 0)
return 0;
}
break;
case BY_APP_INO:
if (xdg_app_is_valid_name (name))
{
guint32 app_id = get_app_id_from_name (name);
*inode = make_inode (APP_DIR_INO_CLASS, app_id);
if (xdp_stat (*inode, stbuf, NULL) == 0)
return 0;
}
break;
default:
break;
}
break;
case APP_DIR_INO_CLASS:
{
if (name_looks_like_id (name))
{
*inode = make_app_doc_dir_inode (parent_class_ino,
xdp_id_from_name (name));
if (xdp_stat (*inode, stbuf, NULL) == 0)
return 0;
}
}
break;
case APP_DOC_DIR_INO_CLASS:
{
guint32 app_id = get_app_id_from_app_doc_ino (parent_class_ino);
guint32 doc_id = get_doc_id_from_app_doc_ino (parent_class_ino);
entry = xdp_lookup_doc (doc_id);
if (entry != NULL)
{
g_autofree char *basename = xdp_entry_dup_basename (entry);
if (strcmp (name, basename) == 0)
{
*inode = make_app_doc_file_inode (app_id, doc_id);
if (xdp_stat (*inode, stbuf, NULL) == 0)
{
if (entry_out)
*entry_out = g_steal_pointer (&entry);
return 0;
}
break;
}
}
tmp = find_tmp_by_name (parent, name);
if (tmp != NULL)
{
*inode = make_inode (TMPFILE_INO_CLASS, tmp->tmp_id);
if (xdp_stat (*inode, stbuf, NULL) == 0)
{
if (entry_out)
*entry_out = g_steal_pointer (&entry);
if (tmp_out)
*tmp_out = g_steal_pointer (&tmp);
return 0;
}
break;
}
break;
}
case TMPFILE_INO_CLASS:
case APP_DOC_FILE_INO_CLASS:
return ENOTDIR;