gvfsfusedaemon.c 62.7 KB
Newer Older
1
/* gvfsfusedaemon.c - FUSE file system mapping daemon for GVFS
2
 * 
3
 * Copyright (C) 2007-2008 Hans Petter Jansson
4
5
6
7
8
9
10
11
12
13
14
15
16
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
Felix Möller's avatar
Felix Möller committed
17
18
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
19
 *
20
 * Author: Hans Petter Jansson <hpj@novell.com>
21
22
 */

23
24
25
26
27
28
#include <config.h>

#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
29
#include <sys/time.h>
Alexander Larsson's avatar
Alexander Larsson committed
30
31
32
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
33
#include <errno.h>
Alexander Larsson's avatar
Alexander Larsson committed
34
35
#include <signal.h>

36
37
38
#include <glib.h>
#include <glib/gi18n.h>
#include <glib/gprintf.h>
39
#include <gio/gio.h>
40

Alexander Larsson's avatar
Alexander Larsson committed
41
42
/* stuff from common/ */
#include <gvfsdaemonprotocol.h>
Tomáš Bžatek's avatar
Tomáš Bžatek committed
43
#include <gvfsdbus.h>
44
#include <gvfsutils.h>
Alexander Larsson's avatar
Alexander Larsson committed
45

46
47
#define FUSE_USE_VERSION 26
#include <fuse.h>
48
#include <fuse_lowlevel.h>
49

50
51
#define GET_FILE_HANDLE(fi)     ((gpointer) (fi)->fh)
#define SET_FILE_HANDLE(fi, fh) ((fi)->fh = (guint64) (fh))
52

53
54
55
56
57
typedef struct {
  time_t creation_time;
  char *name;
  GFile *root;
} MountRecord;
58

59
typedef enum {
60
61
62
  FILE_OP_NONE,
  FILE_OP_READ,
  FILE_OP_WRITE
63
} FileOp;
64

65
typedef struct {
David Zeuthen's avatar
David Zeuthen committed
66
67
  gint      refcount;

68
  GMutex    mutex;
69
  gchar    *path;
70
71
  FileOp    op;
  gpointer  stream;
72
  goffset   pos;
73
  goffset   size;
74
} FileHandle;
75

76
77
78
static GThread        *subthread             = NULL;
static GMainLoop      *subthread_main_loop   = NULL;
static GVfs           *gvfs                  = NULL;
79

80
static GVolumeMonitor *volume_monitor        = NULL;
81
82

/* Contains pointers to MountRecord */
83
static GList          *mount_list            = NULL;
84
static GMutex          mount_list_mutex      = {NULL};
85

86
87
88
static time_t          daemon_creation_time;
static uid_t           daemon_uid;
static gid_t           daemon_gid;
89

90
static GMutex          global_mutex          = {NULL};
91
92
static GHashTable     *global_path_to_fh_map = NULL;
static GHashTable     *global_active_fh_map  = NULL;
93

Tomáš Bžatek's avatar
Tomáš Bžatek committed
94
95
96
static GDBusConnection *dbus_conn            = NULL;
static guint            daemon_name_watcher;

97
98
99
100
101
/* ------- *
 * Helpers *
 * ------- */

static void
102
103
104
105
log_debug (const gchar   *log_domain,
           GLogLevelFlags log_level,
           const gchar   *message,
           gpointer       unused_data)
106
{
107
108
109
110
  struct fuse_context *context;

  context = fuse_get_context ();

111
  if (gvfs_get_debug ())
112
    g_print ("fuse(%u): %s", context ? context->pid : 0, message);
113
114
}

115
typedef struct {
116
117
  gint gioerror;
  gint errno_value;
118
} ErrorMap;
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133

static gint
errno_from_error (GError *error)
{
  gint i;
  static const ErrorMap error_map [] =
    {
      { G_IO_ERROR_FAILED,             EIO          },
      { G_IO_ERROR_NOT_FOUND,          ENOENT       },
      { G_IO_ERROR_EXISTS,             EEXIST       },
      { G_IO_ERROR_IS_DIRECTORY,       EISDIR       },
      { G_IO_ERROR_NOT_DIRECTORY,      ENOTDIR      },
      { G_IO_ERROR_NOT_EMPTY,          ENOTEMPTY    },
      { G_IO_ERROR_NOT_REGULAR_FILE,   EIO          },
      { G_IO_ERROR_NOT_SYMBOLIC_LINK,  EIO          },
134
      { G_IO_ERROR_NOT_MOUNTABLE_FILE, EIO          },
135
136
137
138
139
140
      { G_IO_ERROR_FILENAME_TOO_LONG,  ENAMETOOLONG },
      { G_IO_ERROR_INVALID_FILENAME,   EIO          },
      { G_IO_ERROR_TOO_MANY_LINKS,     ELOOP        },
      { G_IO_ERROR_NO_SPACE,           ENOSPC       },
      { G_IO_ERROR_INVALID_ARGUMENT,   EINVAL       },
      { G_IO_ERROR_PERMISSION_DENIED,  EACCES       },
141
      { G_IO_ERROR_NOT_SUPPORTED,      ENOTSUP      },
142
143
144
145
146
147
148
      { G_IO_ERROR_NOT_MOUNTED,        EIO          },
      { G_IO_ERROR_ALREADY_MOUNTED,    EIO          },
      { G_IO_ERROR_CLOSED,             EIO          },
      { G_IO_ERROR_CANCELLED,          EIO          },
      { G_IO_ERROR_PENDING,            EIO          },
      { G_IO_ERROR_READ_ONLY,          EACCES       },
      { G_IO_ERROR_CANT_CREATE_BACKUP, EIO          },
149
      { G_IO_ERROR_WRONG_ETAG,         EIO          },
150
      { G_IO_ERROR_TIMED_OUT,          EIO          },
151
      { G_IO_ERROR_BUSY,               EBUSY        },
152
      { G_IO_ERROR_WOULD_BLOCK,        EAGAIN       },
153
154
      { G_IO_ERROR_WOULD_RECURSE,      EXDEV        },
      { G_IO_ERROR_WOULD_MERGE,        ENOTEMPTY    },
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
      { -1,                            -1           }
    };

  if (error->domain != G_IO_ERROR)
    return EIO;

  for (i = 0; error_map [i].gioerror >= 0; i++)
    {
      if (error_map [i].gioerror == error->code)
        break;
    }

  if (error_map [i].gioerror >= 0)
    return error_map [i].errno_value;

  return EIO;
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/* Conveys the pid of the client to filesystem backends; see
 * get_pid_for_file() in gdaemonfile.c
 *
 * can only be called during a filesystem op
 */
static void
set_pid_for_file (GFile *file)
{
  struct fuse_context *context;

  if (file == NULL)
    goto out;

  context = fuse_get_context ();
  if (context == NULL)
    goto out;

  g_object_set_data (G_OBJECT (file), "gvfs-fuse-client-pid", GUINT_TO_POINTER (context->pid));

 out:
  ;
}

196
static FileHandle *
197
file_handle_new (const gchar *path)
198
199
200
201
{
  FileHandle *file_handle;

  file_handle = g_new0 (FileHandle, 1);
David Zeuthen's avatar
David Zeuthen committed
202
  file_handle->refcount = 1;
203
  g_mutex_init (&file_handle->mutex);
204
  file_handle->op = FILE_OP_NONE;
205
  file_handle->path = g_strdup (path);
206
  file_handle->size = -1;
207

208
209
  g_hash_table_insert (global_active_fh_map, file_handle, file_handle);

210
211
212
  return file_handle;
}

David Zeuthen's avatar
David Zeuthen committed
213
214
215
216
217
218
219
static FileHandle *
file_handle_ref (FileHandle *file_handle)
{
  g_atomic_int_inc (&file_handle->refcount);
  return file_handle;
}

220
static void
David Zeuthen's avatar
David Zeuthen committed
221
222
file_handle_unref (FileHandle *file_handle)
{
223
224
  if (g_atomic_int_dec_and_test (&file_handle->refcount))
    {
225
226
      gint refs;

227
      g_mutex_lock (&global_mutex);
228
229
230

      /* Test again, since e.g. get_file_handle_for_path() might have
       * snatched the global mutex and revived the file handle between
231
       * g_atomic_int_dec_and_test() and us obtaining the global lock. */
232

233
234
235
236
      refs = g_atomic_int_get (&file_handle->refcount);

      if (refs == 0)
        g_hash_table_remove (global_path_to_fh_map, file_handle->path);
237

238
      g_mutex_unlock (&global_mutex);
239
    }
David Zeuthen's avatar
David Zeuthen committed
240
241
}

242
static void
243
file_handle_close_stream (FileHandle *file_handle)
244
{
245
  g_debug ("file_handle_close_stream\n");
246
247
248
249
  if (file_handle->stream)
    {
      switch (file_handle->op)
        {
250
251
252
253
254
255
256
257
258
259
        case FILE_OP_READ:
          g_input_stream_close (file_handle->stream, NULL, NULL);
          break;
          
        case FILE_OP_WRITE:
          g_output_stream_close (file_handle->stream, NULL, NULL);
          break;
          
        default:
          g_assert_not_reached ();
260
        }
261
      
262
263
      g_object_unref (file_handle->stream);
      file_handle->stream = NULL;
264
      file_handle->op = FILE_OP_NONE;
265
      file_handle->size = -1;
266
    }
267
}
268

269
/* Called on hash table removal */
270
271
272
static void
file_handle_free (FileHandle *file_handle)
{
273
274
  g_hash_table_remove (global_active_fh_map, file_handle);

275
  file_handle_close_stream (file_handle);
276
  g_mutex_clear (&file_handle->mutex);
277
  g_free (file_handle->path);
278
279
280
  g_free (file_handle);
}

281
282
283
284
285
static FileHandle *
get_file_handle_for_path (const gchar *path)
{
  FileHandle *fh;

286
  g_mutex_lock (&global_mutex);
287

288
289
  fh = g_hash_table_lookup (global_path_to_fh_map, path);

290
291
292
  if (fh)
    file_handle_ref (fh);

293
  g_mutex_unlock (&global_mutex);
294
295
296
297
298
299
300
301
  return fh;
}

static FileHandle *
get_or_create_file_handle_for_path (const gchar *path)
{
  FileHandle *fh;

302
  g_mutex_lock (&global_mutex);
303

304
  fh = g_hash_table_lookup (global_path_to_fh_map, path);
305
306
307
308
309
310
311
312

  if (fh)
    {
      file_handle_ref (fh);
    }
  else
    {
      fh = file_handle_new (path);
313
      g_hash_table_insert (global_path_to_fh_map, fh->path, fh);
314
315
    }

316
  g_mutex_unlock (&global_mutex);
317
318
319
320
321
322
323
  return fh;
}

static FileHandle *
get_file_handle_from_info (struct fuse_file_info *fi)
{
  FileHandle *fh;
324

325
  g_mutex_lock (&global_mutex);
326
327
328
329
330
331
332
333
334
335

  fh = GET_FILE_HANDLE (fi);

  /* If the file handle is still valid, its value won't change. If
   * invalid, it's set to NULL. */
  fh = g_hash_table_lookup (global_active_fh_map, fh);

  if (fh)
    file_handle_ref (fh);

336
  g_mutex_unlock (&global_mutex);
337
338
339
340
341
342
343
344
345
  return fh;
}

static void
reindex_file_handle_for_path (const gchar *old_path, const gchar *new_path)
{
  gchar      *old_path_internal;
  FileHandle *fh;

346
  g_mutex_lock (&global_mutex);
347

348
  if (!g_hash_table_lookup_extended (global_path_to_fh_map, old_path,
349
350
351
352
                                     (gpointer *) &old_path_internal,
                                     (gpointer *) &fh))
      goto out;

353
  g_hash_table_steal (global_path_to_fh_map, old_path);
354

355
356
  g_free (fh->path);
  fh->path = g_strdup (new_path);
357

358
  g_hash_table_insert (global_path_to_fh_map, fh->path, fh);
359
360

 out:
361
  g_mutex_unlock (&global_mutex);
362
363
}

364
static MountRecord *
365
mount_record_new (GMount *mount)
366
367
368
369
{
  MountRecord *mount_record;

  mount_record = g_new (MountRecord, 1);
370
  
371
  mount_record->root = g_mount_get_root (mount);
Michael Terry's avatar
Michael Terry committed
372
  mount_record->name = g_strdup (g_object_get_data (G_OBJECT (mount), "g-stable-name"));
373
  mount_record->creation_time = time (NULL);
374
  
375
376
377
378
379
380
  return mount_record;
}

static void
mount_record_free (MountRecord *mount_record)
{
381
382
  g_object_unref (mount_record->root);
  g_free (mount_record->name);
383
384
385
386
387
388
  g_free (mount_record);
}

static void
mount_list_lock (void)
{
389
  g_mutex_lock (&mount_list_mutex);
390
391
392
393
394
}

static void
mount_list_unlock (void)
{
395
  g_mutex_unlock (&mount_list_mutex);
396
397
398
399
400
}

static void
mount_list_free (void)
{
401
  g_list_free_full (mount_list, (GDestroyNotify) mount_record_free);
402
403
404
  mount_list = NULL;
}

405
static gboolean
406
mount_record_for_mount_exists (GMount *mount)
407
408
409
410
411
{
  GList *l;
  GFile *root;
  gboolean res;

412
  g_assert (mount != NULL);
413

414
  root = g_mount_get_root (mount);
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

  res = FALSE;
  
  mount_list_lock ();

  for (l = mount_list; l != NULL; l = l->next)
    {
      MountRecord *this_mount_record = l->data;
      
      if (g_file_equal (root, this_mount_record->root))
        {
          res = TRUE;
          break;
        }
    }

  mount_list_unlock ();

  g_object_unref (root);
  
  return res;
}

static GFile *
mount_record_find_root_by_mount_name (const gchar *mount_name)
{
  GList       *l;
  GFile *root;

  g_assert (mount_name != NULL);

  root = NULL;
  
  mount_list_lock ();

  for (l = mount_list; l != NULL; l = l->next)
    {
      MountRecord *mount_record = l->data;

      if (strcmp (mount_name, mount_record->name) == 0)
        {
          root = g_object_ref (mount_record->root);
          break;
        }
    }

  mount_list_unlock ();

  return root;
}
465
466
467
468

static void
mount_list_update (void)
{
469
  GList *mounts;
470
471
  GList *l;

472
  mounts = g_volume_monitor_get_mounts (volume_monitor);
473

474
  for (l = mounts; l != NULL; l = l->next)
475
    {
476
      GMount *mount = l->data;
477

478
      if (!mount_record_for_mount_exists (mount))
479
480
        {
          mount_list_lock ();
481
          mount_list = g_list_prepend (mount_list, mount_record_new (mount));
482
483
          mount_list_unlock ();
        }
484
      
485
      g_object_unref (mount);
486
    }
487
  
488
  g_list_free (mounts);
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
}

#if 0

static gint
file_info_get_attribute_as_int (GFileInfo *file_info, const gchar *attribute)
{
  GFileAttributeType attribute_type;
  gint               int_result;

  attribute_type = g_file_info_get_attribute_type (file_info, attribute);

  switch (attribute_type)
    {
      case G_FILE_ATTRIBUTE_TYPE_UINT32:
        int_result = g_file_info_get_attribute_uint32 (file_info, attribute);
        break;

      case G_FILE_ATTRIBUTE_TYPE_INT32:
        int_result = g_file_info_get_attribute_int32 (file_info, attribute);
        break;

      case G_FILE_ATTRIBUTE_TYPE_UINT64:
        int_result = g_file_info_get_attribute_uint64 (file_info, attribute);
        break;

      case G_FILE_ATTRIBUTE_TYPE_INT64:
        int_result = g_file_info_get_attribute_int64 (file_info, attribute);
        break;

      default:
        int_result = 0;
        g_assert_not_reached ();
        break;
    }

  return int_result;
}

#endif

static guint
file_info_get_attribute_as_uint (GFileInfo *file_info, const gchar *attribute)
{
  GFileAttributeType attribute_type;
  guint              uint_result;

  attribute_type = g_file_info_get_attribute_type (file_info, attribute);

  switch (attribute_type)
    {
      case G_FILE_ATTRIBUTE_TYPE_UINT32:
        uint_result = g_file_info_get_attribute_uint32 (file_info, attribute);
        break;

      case G_FILE_ATTRIBUTE_TYPE_INT32:
        uint_result = g_file_info_get_attribute_int32 (file_info, attribute);
        break;

      case G_FILE_ATTRIBUTE_TYPE_UINT64:
        uint_result = g_file_info_get_attribute_uint64 (file_info, attribute);
        break;

      case G_FILE_ATTRIBUTE_TYPE_INT64:
        uint_result = g_file_info_get_attribute_int64 (file_info, attribute);
        break;

      default:
        uint_result = 0;
        g_assert_not_reached ();
        break;
    }

  return uint_result;
}

static gboolean
path_is_mount_list (const gchar *path)
{
568
569
  while (*path == '/')
    path++;
570

571
  return *path == 0;
572
573
}

574

575
576
static GFile *
file_from_full_path (const gchar *path)
577
{
578
579
580
581
  gchar *mount_name;
  GFile *file = NULL;
  const gchar *s1, *s2;
  GFile *root;
582

583
584
585
586
  file = NULL;
  
  s1 = path;
  while (*s1 == '/')
587
    s1++;
588
  
589
590
591
  if (*s1)
    {
      s2 = strchr (s1, '/');
592
      if (s2 == NULL)
593
        s2 = s1 + strlen (s1);
594
      
595
      mount_name = g_strndup (s1, s2 - s1);
596
      root = mount_record_find_root_by_mount_name (mount_name);
597
      g_free (mount_name);
598
599
      
      if (root)
600
        {
601
602
603
604
          while (*s2 == '/')
            s2++;
          file = g_file_resolve_relative_path (root, s2);
          g_object_unref (root);
605
606
607
608
609
610
611
612
613
614
        }
    }

  return file;
}

/* ------------- *
 * VFS functions *
 * ------------- */

615
616
617
618
619
620
621
static gint
vfs_statfs (const gchar *path, struct statvfs *stbuf)
{
  GFile  *file;
  GError *error  = NULL;
  gint    result = 0;

622
  g_debug ("vfs_statfs: %s\n", path);
623

624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
  memset (stbuf, 0, sizeof (*stbuf));

  /* Fallback case */
  stbuf->f_bsize = 4096;
  stbuf->f_frsize = 4096;  /* Ignored by FUSE */
  stbuf->f_blocks = 0;
  stbuf->f_bfree = 0;
  stbuf->f_bavail = 0;
  stbuf->f_files = 0;
  stbuf->f_ffree = 0;
  stbuf->f_favail = 0;  /* Ignored by FUSE */
  stbuf->f_fsid = 1;  /* Ignored by FUSE */
  stbuf->f_flag = 0;  /* Ignored by FUSE */
  stbuf->f_namemax = 1024;

639
640
641
642
  if ((file = file_from_full_path (path)))
    {
      GFileInfo *file_info;

643
      file_info = g_file_query_filesystem_info (file, "filesystem::*", NULL, &error);
644
645
646

      if (file_info)
        {
647
          if (g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE))
648
649
650
651
            {
              guint64 size = g_file_info_get_attribute_uint64 (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_SIZE);
              stbuf->f_blocks = (size > 0) ? ((size - 1) / 4096 + 1) : 0;
            }
652
653
          if (g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE))
            stbuf->f_bfree = stbuf->f_bavail = g_file_info_get_attribute_uint64 (file_info, G_FILE_ATTRIBUTE_FILESYSTEM_FREE) / 4096;
654
655

          g_object_unref (file_info);
656
657
658
659
660
661
662
663
664
665
666
667
668
669
        }
      else if (error)
        {
          result = -errno_from_error (error);
          g_error_free (error);
        }
      else
        {
          result = -EIO;
        }

      g_object_unref (file);
    }

670
  g_debug ("vfs_statfs: -> %s\n", g_strerror (-result));
671
672

  return result;
673
674
}

675
676
677
678
679
680
static mode_t
file_info_get_stat_mode (GFileInfo *file_info)
{
  GFileType         file_type;
  mode_t            unix_mode;

681
  file_type = g_file_info_get_file_type (file_info);
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704

  switch (file_type)
    {
      case G_FILE_TYPE_REGULAR:
        unix_mode = S_IFREG;
        break;

      case G_FILE_TYPE_DIRECTORY:
      case G_FILE_TYPE_MOUNTABLE:
        unix_mode = S_IFDIR;
        break;

      case G_FILE_TYPE_SYMBOLIC_LINK:
      case G_FILE_TYPE_SHORTCUT:
        unix_mode = S_IFLNK;
        break;

      case G_FILE_TYPE_SPECIAL:
      default:
        unix_mode = 0;
        break;
    }

705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
  if (g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_UNIX_MODE))
    {
      mode_t mode = g_file_info_get_attribute_uint32 (file_info,
                                                      G_FILE_ATTRIBUTE_UNIX_MODE);
      unix_mode |= mode & (S_IRWXU | S_IRWXG | S_IRWXO);
    }
  else
    {
      if (file_type == G_FILE_TYPE_DIRECTORY ||
          !g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ) ||
          g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_ACCESS_CAN_READ))
        unix_mode |= S_IRUSR;
      if (!g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE) ||
          g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE))
        unix_mode |= S_IWUSR;
      if (file_type == G_FILE_TYPE_DIRECTORY ||
          !g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE) ||
          g_file_info_get_attribute_boolean (file_info, G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE))
        unix_mode |= S_IXUSR;
    }
725
  
726
727
728
  return unix_mode;
}

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
static void
set_attributes_from_info (GFileInfo *file_info, struct stat *sbuf)
{
  GTimeVal mod_time;

  sbuf->st_mode = file_info_get_stat_mode (file_info);
  sbuf->st_size = g_file_info_get_size (file_info);
  sbuf->st_uid = daemon_uid;
  sbuf->st_gid = daemon_gid;

  g_file_info_get_modification_time (file_info, &mod_time);
  sbuf->st_mtime = mod_time.tv_sec;
  sbuf->st_ctime = mod_time.tv_sec;
  sbuf->st_atime = mod_time.tv_sec;

  if (g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_TIME_CHANGED))
    sbuf->st_ctime = file_info_get_attribute_as_uint (file_info, G_FILE_ATTRIBUTE_TIME_CHANGED);
  if (g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_TIME_ACCESS))
    sbuf->st_atime = file_info_get_attribute_as_uint (file_info, G_FILE_ATTRIBUTE_TIME_ACCESS);

  if (g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE))
    sbuf->st_blksize = file_info_get_attribute_as_uint (file_info, G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE);
  if (g_file_info_has_attribute (file_info, G_FILE_ATTRIBUTE_UNIX_BLOCKS))
    sbuf->st_blocks = file_info_get_attribute_as_uint (file_info, G_FILE_ATTRIBUTE_UNIX_BLOCKS);
  else /* fake it to make 'du' work like 'du --apparent'. */
    sbuf->st_blocks = (sbuf->st_size + 511) / 512;

  /* Setting st_nlink to 1 for directories makes 'find' work */
  sbuf->st_nlink = 1;
}

static const char *query_attributes = G_FILE_ATTRIBUTE_STANDARD_TYPE ","
                                      G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK ","
                                      G_FILE_ATTRIBUTE_STANDARD_SIZE ","
                                      G_FILE_ATTRIBUTE_UNIX_MODE ","
                                      G_FILE_ATTRIBUTE_TIME_CHANGED ","
                                      G_FILE_ATTRIBUTE_TIME_MODIFIED ","
                                      G_FILE_ATTRIBUTE_TIME_ACCESS ","
                                      G_FILE_ATTRIBUTE_UNIX_BLOCK_SIZE ","
                                      G_FILE_ATTRIBUTE_UNIX_BLOCKS ","
                                      G_FILE_ATTRIBUTE_ACCESS_CAN_READ ","
                                      G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE ","
                                      G_FILE_ATTRIBUTE_ACCESS_CAN_EXECUTE;

773
774
775
776
777
778
779
static gint
getattr_for_file (GFile *file, struct stat *sbuf)
{
  GFileInfo *file_info;
  GError    *error  = NULL;
  gint       result = 0;

780
  file_info = g_file_query_info (file, query_attributes, 0, NULL, &error);
781
782
783

  if (file_info)
    {
784
      set_attributes_from_info (file_info, sbuf);
785
786
787
788
      g_object_unref (file_info);
    }
  else
    {
789
790
      if (error)
        {
791
          g_debug ("Error from GVFS: %s\n", error->message);
792
793
794
795
796
          result = -errno_from_error (error);
          g_error_free (error);
        }
      else
        {
797
          g_debug ("No file info, but no error from GVFS.\n");
798
799
          result = -EIO;
        }
800
801
802
803
804
    }

  return result;
}

805
806
/* Call with fh locked */
static gint
807
808
getattr_for_file_handle (FileHandle *fh, struct stat *sbuf)
{
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
  GFileInfo *file_info;
  GError    *error  = NULL;
  gint       result = 0;

  switch (fh->op)
    {
    case FILE_OP_READ:
      file_info = g_file_input_stream_query_info (G_FILE_INPUT_STREAM (fh->stream),
                                                  query_attributes,
                                                  NULL, &error);
      break;
    case FILE_OP_WRITE:
      file_info = g_file_output_stream_query_info (G_FILE_OUTPUT_STREAM (fh->stream),
                                                  query_attributes,
                                                  NULL, &error);
      break;
    default:
      return -ENOTSUP;
    }

  if (file_info)
    {
      set_attributes_from_info (file_info, sbuf);
      g_object_unref (file_info);
    }
  else
    {
      if (error)
        {
838
          g_debug ("Error from GVFS: %s\n", error->message);
839
840
841
842
843
          result = -errno_from_error (error);
          g_error_free (error);
        }
      else
        {
844
          g_debug ("No file info, but no error from GVFS.\n");
845
846
847
848
849
          result = -EIO;
        }
    }

  return result;
850
851
}

852
853
854
static gint
vfs_getattr (const gchar *path, struct stat *sbuf)
{
855
856
  GFile      *file;
  gint        result = 0;
857

858
  g_debug ("vfs_getattr: %s\n", path);
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877

  memset (sbuf, 0, sizeof (*sbuf));

  sbuf->st_dev = 0;                     /* dev_t     ID of device containing file */
  sbuf->st_ino = 0;                     /* ino_t     inode number */
  sbuf->st_uid = 0;                     /* uid_t     user ID of owner */
  sbuf->st_gid = 0;                     /* gid_t     group ID of owner */
  sbuf->st_rdev = 0;                    /* dev_t     device ID (if special file) */
  sbuf->st_size = 0;                    /* off_t     total size, in bytes */
  sbuf->st_blocks = 0;                  /* blkcnt_t  number of blocks allocated */
  sbuf->st_atime = 0;                   /* time_t    time of last access */
  sbuf->st_mtime = 0;                   /* time_t    time of last modification */
  sbuf->st_ctime = 0;                   /* time_t    time of last status change */
  sbuf->st_blksize = 4096;              /* blksize_t blocksize for filesystem I/O */

  if (path_is_mount_list (path))
    {
      /* Mount list */

878
      sbuf->st_mode = S_IFDIR | 0500;                   /* mode_t    protection */
879
880
881
882
883
884
885
886
887
888
      sbuf->st_nlink = 2 + g_list_length (mount_list);  /* nlink_t   number of hard links */
      sbuf->st_atime = daemon_creation_time;
      sbuf->st_mtime = daemon_creation_time;
      sbuf->st_ctime = daemon_creation_time;
      sbuf->st_uid   = daemon_uid;
      sbuf->st_gid   = daemon_gid;
    }
  else if ((file = file_from_full_path (path)))
    {
      /* Submount */
889
      FileHandle *fh = get_file_handle_for_path (path);
890

891
      if (fh)
892
        {
893
          goffset size;
894

895
          g_mutex_lock (&fh->mutex);
896

897
          result = getattr_for_file_handle (fh, sbuf);
898
          size = fh->size;
899

900
901
902
903
904
905
906
907
908
909
          g_mutex_unlock (&fh->mutex);
          file_handle_unref (fh);

          if (result == -ENOTSUP)
            {
              result = getattr_for_file (file, sbuf);

              /* Some backends don't create new files until their stream has
               * been closed. So, if the path doesn't exist, but we have a
               * stream associated with it, pretend it's there. */
910
911
912
913
914
915
916

              /* If we're tracking an open file's size, use that in preference
               * to the stat information since it may be incorrect if
               * g_file_replace writes to a temporary file. */
              if (size != -1)
                  sbuf->st_size = size;

917
918
919
920
921
922
923
924
925
926
              if (result != 0)
                {
                  result = 0;
                  sbuf->st_mode = S_IFREG | S_IRUSR | S_IWUSR | S_IXUSR;
                  sbuf->st_uid = daemon_uid;
                  sbuf->st_gid = daemon_gid;
                  sbuf->st_nlink = 1;
                  sbuf->st_blksize = 512;
                  sbuf->st_blocks = (sbuf->st_size + 511) / 512;
                }
927
928
            }
        }
929
930
931
932
      else
        {
          result = getattr_for_file (file, sbuf);
        }
933

934
935
936
937
938
939
940
      g_object_unref (file);
    }
  else
    {
      result = -ENOENT;
    }

941
  g_debug ("vfs_getattr: -> %s\n", g_strerror (-result));
942

943
944
945
946
947
948
  return result;
}

static gint
vfs_readlink (const gchar *path, gchar *target, size_t size)
{
949
  g_debug ("vfs_readlink: %s\n", path);
950

951
952
953
954
955
956
  /* This is not implemented because it would allow remote servers to launch
   * symlink attacks on the local machine.  There's not much of a use for
   * "readlink" anyway since we don't pass G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS,
   * so non-broken symlinks will be followed transparently. */

  return -ENOSYS;
957
958
}

Alexander Larsson's avatar
Alexander Larsson committed
959
960
961
962
963
964
965
966
static gint
setup_input_stream (GFile *file, FileHandle *fh)
{
  GError *error  = NULL;
  gint    result = 0;

  if (fh->stream)
    {
967
      g_debug ("setup_input_stream: have stream\n");
Alexander Larsson's avatar
Alexander Larsson committed
968
969
970

      if (fh->op == FILE_OP_READ)
        {
971
          g_debug ("setup_input_stream: doing read\n");
Alexander Larsson's avatar
Alexander Larsson committed
972
973
974
        }
      else
        {
975
          g_debug ("setup_input_stream: doing write\n");
Alexander Larsson's avatar
Alexander Larsson committed
976
977
978
979

          g_output_stream_close (fh->stream, NULL, NULL);
          g_object_unref (fh->stream);
          fh->stream = NULL;
980
          fh->size = -1;
Alexander Larsson's avatar
Alexander Larsson committed
981
982
983
984
985
        }
    }

  if (!fh->stream)
    {
986
      g_debug ("setup_input_stream: no stream\n");
Alexander Larsson's avatar
Alexander Larsson committed
987
988
989
990
991
992
993
994
995
996
997
      fh->stream = g_file_read (file, NULL, &error);
      fh->pos = 0;
    }

  if (fh->stream)
    fh->op = FILE_OP_READ;
  else
    fh->op = FILE_OP_NONE;

  if (error)
    {
998
      g_debug ("setup_input_stream: error\n");
Alexander Larsson's avatar
Alexander Larsson committed
999
1000
1001
1002
1003
1004
1005
1006
      result = -errno_from_error (error);
      g_error_free (error);
    }

  return result;
}

static gint
1007
setup_output_stream (GFile *file, FileHandle *fh, int flags)
Alexander Larsson's avatar
Alexander Larsson committed
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
{
  GError *error  = NULL;
  gint    result = 0;

  if (fh->stream)
    {
      if (fh->op == FILE_OP_WRITE)
        {
        }
      else
        {
          g_input_stream_close (fh->stream, NULL, NULL);
          g_object_unref (fh->stream);
          fh->stream = NULL;
        }
    }

  if (!fh->stream)
    {
1027
      if (flags & O_TRUNC)
1028
1029
1030
1031
        {
          fh->stream = g_file_replace (file, NULL, FALSE, 0, NULL, &error);
          fh->size = 0;
        }
1032
      else if (flags & O_APPEND)
1033
        fh->stream = g_file_append_to (file, 0, NULL, &error);
1034
1035
      else
        result = -ENOTSUP;
Alexander Larsson's avatar
Alexander Larsson committed
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
      if (fh->stream)
        fh->pos = g_seekable_tell (G_SEEKABLE (fh->stream));
    }

  if (fh->stream)
    fh->op = FILE_OP_WRITE;
  else
    fh->op = FILE_OP_NONE;

  if (error)
    {
      result = -errno_from_error (error);
      g_error_free (error);
    }

  return result;
}

1054
1055
1056
1057
1058
1059
static gint
open_common (const gchar *path, struct fuse_file_info *fi, GFile *file, int output_flags)
{
  gint        result;
  FileHandle *fh = get_or_create_file_handle_for_path (path);

1060
  g_mutex_lock (&fh->mutex);
1061
1062
1063

  SET_FILE_HANDLE (fi, fh);

1064
1065
1066
1067
1068
1069
  g_debug ("open_common: flags=%o (%s%s%s%s)\n",
           fi->flags,
           fi->flags & O_WRONLY ? "O_WRONLY " : "O_RDONLY ",
           fi->flags & O_RDWR   ? "O_RDWR "   : "",
           fi->flags & O_APPEND ? "O_APPEND " : "",
           fi->flags & O_TRUNC  ? "O_TRUNC "  : "");
1070
1071
1072
1073
1074
1075
1076
1077
1078

  /* Set up a stream here, so we can check for errors */
  set_pid_for_file (file);

  if (fi->flags & O_WRONLY || fi->flags & O_RDWR)
    result = setup_output_stream (file, fh, fi->flags | output_flags);
  else
    result = setup_input_stream (file, fh);

1079
1080
1081
  if (fh->stream)
    fi->nonseekable = !g_seekable_can_seek (G_SEEKABLE (fh->stream));

1082
  g_mutex_unlock (&fh->mutex);
1083
1084
1085
1086
1087

  /* The added reference to the file handle is released in vfs_release() */
  return result;
}

1088
1089
1090
1091
1092
1093
static gint
vfs_open (const gchar *path, struct fuse_file_info *fi)
{
  GFile *file;
  gint   result = 0;

1094
  g_debug ("vfs_open: %s\n", path);
1095

1096
1097
1098
  if (path_is_mount_list (path))
    result = -EISDIR;
  else if ((file = file_from_full_path (path)))
1099
1100
1101
1102
    {
      GFileInfo *file_info;
      GError    *error = NULL;

1103
      file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, NULL, &error);
1104
1105
1106
1107
1108
1109
1110

      if (file_info)
        {
          GFileType file_type = g_file_info_get_file_type (file_info);

          if (file_type == G_FILE_TYPE_REGULAR)
            {
1111
              result = open_common (path, fi, file, 0);
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
            }
          else if (file_type == G_FILE_TYPE_DIRECTORY)
            {
              /* EISDIR is supposedly only for attempts to write to directory handles,
               * but outside readdir(), we don't support reading them either. */
              result = -EISDIR;
            }
          else
            {
              result = -EACCES;
            }

          g_object_unref (file_info);
        }
      else
        {
1128
1129
          if (error)
            {
1130
              g_debug ("Error from GVFS: %s\n", error->message);
1131
1132
1133
1134
1135
              result = -errno_from_error (error);
              g_error_free (error);
            }
          else
            {
1136
              g_debug ("No file info, but no error from GVFS.\n");
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
              result = -EIO;
            }
        }

      g_object_unref (file);
    }
  else
    {
      result = -ENOENT;
    }

1148
  g_debug ("vfs_open: -> %s\n", g_strerror (-result));
1149

1150
1151
1152
1153
1154
1155
1156
1157
1158
  return result;
}

static gint
vfs_create (const gchar *path, mode_t mode, struct fuse_file_info *fi)
{
  GFile *file;
  gint   result = 0;

1159
  g_debug ("vfs_create: %s\n", path);
1160

1161
1162
  if (path_is_mount_list (path))
    result = -EEXIST;
1163
1164
1165
1166
1167
  if ((file = file_from_full_path (path)))
    {
      GFileInfo *file_info;
      GError    *error = NULL;

1168
1169
      set_pid_for_file (file);

1170
      file_info = g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, NULL, &error);
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186

      if (file_info)
        {
          result = -EEXIST;
          g_object_unref (file_info);
        }
      else
        {
          GFileOutputStream *file_output_stream;

          if (error)
            {
              g_error_free (error);
              error = NULL;
            }

1187
          file_output_stream = g_file_create (file, 0, NULL, &error);
1188
1189
          if (file_output_stream)
            {
1190
1191
1192
1193
              FileHandle *fh = get_or_create_file_handle_for_path (path);

              /* Success */

1194
              g_mutex_lock (&fh->mutex);
1195
1196
1197
1198
1199

              SET_FILE_HANDLE (fi, fh);

              file_handle_close_stream (fh);
              fh->stream = file_output_stream;
1200
              fh->size = 0;
1201
1202
              fh->op = FILE_OP_WRITE;

1203
              g_mutex_unlock (&fh->mutex);
1204
1205

              /* The reference added to the file handle is released in vfs_release() */
1206
1207
1208
1209
1210
1211
            }
          else
            {
              result = -errno_from_error (error);
              g_error_free (error);
            }
1212
1213
1214
1215
1216
1217
1218
1219
1220
        }

      g_object_unref (file);
    }
  else
    {
      result = -ENOENT;
    }

1221
  g_debug ("vfs_create: -> %s\n", g_strerror (-result));
1222

1223
1224
1225
1226
1227
1228
  return result;
}

static gint
vfs_release (const gchar *path, struct fuse_file_info *fi)
{
1229
  FileHandle *fh = get_file_handle_from_info (fi);
1230

1231
  g_debug ("vfs_release: %s\n", path);
1232

1233
  if (fh)
1234
    {
1235
1236
1237
1238
      g_mutex_lock (&fh->mutex);
      file_handle_close_stream (fh);
      g_mutex_unlock (&fh->mutex);

1239
1240
1241
1242
      /* get_file_handle_from_info () adds a "working ref", so unref twice. */
      file_handle_unref (fh);
      file_handle_unref (fh);
    }
1243

1244
1245
1246
  return 0;
}

1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
static gint
read_stream (FileHandle *fh, gchar *output_buf, size_t output_buf_size, off_t offset)
{
  GInputStream *input_stream;
  gint          n_bytes_skipped = 0;
  gint          n_bytes_read    = 0;
  gint          result          = 0;
  GError       *error           = NULL;

  input_stream = fh->stream;

  if (offset != fh->pos)
    {
Alexander Larsson's avatar
Alexander Larsson committed
1260
      if (g_seekable_can_seek (G_SEEKABLE (input_stream)))
1261
        {
1262
1263
          /* Can seek */

1264
          g_debug ("read_stream: seeking to offset %jd.\n", offset);
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279

          if (g_seekable_seek (G_SEEKABLE (input_stream), offset, G_SEEK_SET, NULL, &error))
            {
              fh->pos = offset;
            }
          else
            {
              result = -errno_from_error (error);
              g_error_free (error);
            }
        }
      else if (offset > fh->pos)
        {
          /* Can skip ahead */

1280
          g_debug ("read_stream: skipping to offset %jd.\n", offset);
1281
1282
1283
1284
1285
1286

          n_bytes_skipped = g_input_stream_skip (input_stream, offset - fh->pos, NULL, &error);

          if (n_bytes_skipped > 0)
            fh->pos += n_bytes_skipped;

1287
          if (offset != fh->pos)
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
            {
              if (error)
                {
                  result = -errno_from_error (error);
                  g_error_free (error);
                }
              else
                {
                  result = -EIO;
                }
            }
        }
      else
        {
Alexander Larsson's avatar
Alexander Larsson committed
1302
          /* Can't seek, can't skip backwards */
1303

1304
          g_debug ("read_stream: can't seek nor skip to offset %jd!\n", offset);
1305

Alexander Larsson's avatar
Alexander Larsson committed
1306
          result = -ENOTSUP;
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
        }
    }

  if (result == 0)
    {
      while (n_bytes_read < output_buf_size)
        {
          gboolean part_result;
          gsize    part_bytes_read = 0;

          part_result = g_input_stream_read_all (input_stream,
                                                 output_buf + n_bytes_read,
                                                 output_buf_size - n_bytes_read,
                                                 &part_bytes_read,
                                                 NULL,
                                                 &error);

          n_bytes_read += part_bytes_read;
          fh->pos += part_bytes_read;

          if (!part_result || part_bytes_read == 0)
            break;
        }

      result = n_bytes_read;

      if (n_bytes_read < output_buf_size)
        {
1335
          g_debug ("read_stream: wanted %zd bytes, but got %d.\n", output_buf_size, n_bytes_read);
1336
1337
1338
1339
1340
1341

          if (error)
            {
              result = -errno_from_error (error);
              g_error_free (error);
            }
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
        }
    }

  return result;
}

static gint
vfs_read (const gchar *path, gchar *buf, size_t size,
          off_t offset, struct fuse_file_info *fi)
{
  GFile *file;
  gint   result = 0;

1355
  g_debug ("vfs_read: %s\n", path);