aboutsummaryrefslogtreecommitdiff
path: root/src/fuse/gnunet-fuse.c
blob: 69c226cb62775a66e87687344325a7e8aba71e32 (plain) (blame)
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
/*
  This file is part of gnunet-fuse.
  (C) 2012 Christian Grothoff (and other contributing authors)
  
  gnunet-fuse is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published
  by the Free Software Foundation; either version 3, or (at your
  option) any later version.

  gnunet-fuse 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 General Public License for more details.
 
  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/

/**
 * @file fuse/gnunet-fuse.c
 * @brief fuse tool
 * @author Christian Grothoff
 * @author Mauricio Günther
 */
#include "gnunet-fuse.h"
#include "gfs_download.h"

/**
 * Anonymity level to use.
 */
unsigned int anonymity_level;

/**
 * Configuration to use.
 */
const struct GNUNET_CONFIGURATION_Handle *cfg;

/**
 * Return code from 'main' (0 on success).
 */
static int ret;

/**
 * Flag to determine if we should run in single-threaded mode.
 */
static int single_threaded;

/**
 * Mounted URI (as string).
 */
static char *source;

/**
 * Mount point.
 */
static char *directory;

/**
 * Root of the file tree.
 */
static struct GNUNET_FUSE_PathInfo *root;


/**
 * Function used to process entries in a directory; adds the 
 * respective entry to the parent directory.
 *
 * @param cls closure with the 'struct GNUNET_FUSE_PathInfo' of the parent
 * @param filename name of the file in the directory
 * @param uri URI of the file
 * @param metadata metadata for the file; metadata for
 *        the directory if everything else is NULL/zero
 * @param length length of the available data for the file
 *           (of type size_t since data must certainly fit
 *            into memory; if files are larger than size_t
 *            permits, then they will certainly not be
 *            embedded with the directory itself).
 * @param data data available for the file (length bytes)
 */
static void 
process_directory_entry (void *cls,
			 const char *filename,
			 const struct GNUNET_FS_Uri *
			 uri,
			 const struct
			 GNUNET_CONTAINER_MetaData *
			 meta, size_t length,
			 const void *data)
{
  struct GNUNET_FUSE_PathInfo *parent = cls;
  struct GNUNET_FUSE_PathInfo *pi;
  int is_directory;

  if (NULL == filename)
    return; /* info about the directory itself */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Adding file `%s' to directory `%s'\n",
	      filename,
	      parent->filename);
  is_directory = GNUNET_FS_meta_data_test_for_directory (meta);
  if (GNUNET_SYSERR == is_directory)
    is_directory = GNUNET_NO; /* if in doubt, say no */
  pi = GNUNET_FUSE_path_info_create (parent, filename, uri, is_directory);
  GNUNET_FUSE_path_info_done (pi);
}


/**
 * Load and parse a directory.
 *
 * @param pi path to the directory
 * @param eno where to store 'errno' on errors
 * @return GNUNET_OK on success
 */
int
GNUNET_FUSE_load_directory (struct GNUNET_FUSE_PathInfo *pi,
			    int * eno)
{
  size_t size;
  void *data;
  struct GNUNET_DISK_MapHandle *mh;
  struct GNUNET_DISK_FileHandle *fh;

  /* Need to download directory; store to temporary file */
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Downloading directory `%s'\n",
	      pi->filename);
  pi->tmpfile = GNUNET_DISK_mktemp ("gnunet-fuse-tempfile");
  if (GNUNET_OK != GNUNET_FUSE_download_file (pi,
					      0,
					      GNUNET_FS_uri_chk_get_file_size (pi->uri)))
  {
    UNLINK (pi->tmpfile);
    GNUNET_free (pi->tmpfile);
    pi->tmpfile = NULL;
    *eno = EIO; /* low level IO error */
    return GNUNET_SYSERR;
  }

  size = (size_t) GNUNET_FS_uri_chk_get_file_size (pi->uri);
  fh = GNUNET_DISK_file_open (pi->tmpfile,
			      GNUNET_DISK_OPEN_READ,
			      GNUNET_DISK_PERM_NONE);			      
  if (NULL == fh)
  {
    *eno = EIO;
    return GNUNET_SYSERR;
  }
  data = GNUNET_DISK_file_map (fh, 
			       &mh,
			       GNUNET_DISK_MAP_TYPE_READ,
			       size);
  if (NULL == data)
  {
    GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fh));
    return - ENOMEM;
  }
  *eno = 0;
  if (GNUNET_OK !=
      GNUNET_FS_directory_list_contents (size,
					 data, 0LL,
					 &process_directory_entry,
					 pi))
    *eno = ENOTDIR;
  GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_unmap (mh));
  GNUNET_DISK_file_close (fh);
  if (0 != *eno)
    return GNUNET_SYSERR;
  return GNUNET_OK;
} 


/**
 * Obtain an existing path info entry from the global map.
 *
 * @param path path the entry represents
 * @param eno where to store 'errno' on errors
 * @return NULL if no such path entry exists
 */
struct GNUNET_FUSE_PathInfo *
GNUNET_FUSE_path_info_get (const char *path,
			   int *eno)
{
  size_t slen = strlen (path) + 1;
  char buf[slen];
  struct GNUNET_FUSE_PathInfo *pi;
  struct GNUNET_FUSE_PathInfo *pos;
  char *tok;
  
  memcpy (buf, path, slen);
  pi = root;
  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
	      "Looking up path `%s'\n",
	      path);
  GNUNET_mutex_lock (pi->lock);
  for (tok = strtok (buf, "/"); NULL != tok; tok = strtok (NULL, "/"))
  {
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Searching for token `%s'\n",
		tok);
    if (NULL == pi->tmpfile)
    {
      if (GNUNET_OK != GNUNET_FUSE_load_directory (pi, eno))
      {
	GNUNET_mutex_unlock (pi->lock);
	return NULL; 
      }
    }

    pos = pi->child_head;
    while ( (NULL != pos) &&
	    (0 != strcmp (tok,
			  pos->filename)) )
      pos = pos->next;
    if (NULL == pos)
    {      
      GNUNET_mutex_unlock (pi->lock);
      *eno = ENOENT;
      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		  "No file with name `%s' in directory `%s'\n",
		  tok,
		  pi->filename);
      return NULL;
    }
    GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
		"Descending into directory `%s'\n",
		tok);
    GNUNET_mutex_lock (pos->lock);
    GNUNET_mutex_unlock (pi->lock);
    pi = pos;
  }
  ++pi->rc;
  GNUNET_mutex_unlock (pi->lock);
  return pi;  
}


/**
 * Create a new path info entry in the global map.
 *
 * @param parent parent directory (can be NULL)
 * @param filename name of the file to create
 * @param uri URI to use for the path
 * @param is_directory GNUNET_YES if this entry is for a directory
 * @return existing path entry if one already exists, otherwise
 *         new path entry with the desired URI; in both cases
 *         the reference counter has been incremented by 1
 */
struct GNUNET_FUSE_PathInfo *
GNUNET_FUSE_path_info_create (struct GNUNET_FUSE_PathInfo *parent,
			      const char *filename,
			      const struct GNUNET_FS_Uri *uri,
			      int is_directory)
{
  struct GNUNET_FUSE_PathInfo *pi;
  size_t len;

  if (NULL != parent)
  {
    GNUNET_mutex_lock (parent->lock);
  }

  pi = GNUNET_malloc (sizeof (struct GNUNET_FUSE_PathInfo));
  pi->parent = parent;
  pi->filename = GNUNET_strdup (filename);
  len = strlen (pi->filename);
  if ('/' == pi->filename[len - 1])
    pi->filename[len - 1] = '\0';
  pi->uri = GNUNET_FS_uri_dup (uri);
  pi->lock = GNUNET_mutex_create (GNUNET_YES);
  pi->rc = 1;
  pi->stbuf.st_mode = (S_IRUSR | S_IRGRP | S_IROTH); /* read-only */
  if (GNUNET_YES == is_directory)
  {
    pi->stbuf.st_mode |= S_IFDIR | (S_IXUSR | S_IXGRP | S_IXOTH); /* allow traversal */
  }
  else
  {
    pi->stbuf.st_mode |= S_IFREG; /* regular file */
    pi->stbuf.st_size = (off_t) GNUNET_FS_uri_chk_get_file_size (uri);
  }

  if (NULL != parent)
  {
    GNUNET_CONTAINER_DLL_insert_tail (parent->child_head,
				      parent->child_tail,
				      pi);
    GNUNET_mutex_unlock (parent->lock);
  }
  return pi;
}


/**
 * Reduce the reference counter of a path info entry.
 *
 * @param pi entry to decrement the RC of
 */
void
GNUNET_FUSE_path_info_done (struct GNUNET_FUSE_PathInfo *pi)
{
  if (GNUNET_YES == pi->delete_later)
  {
    (void) GNUNET_FUSE_path_info_delete (pi);
    return;
  }
  GNUNET_mutex_lock (pi->lock);
  --pi->rc;
  GNUNET_mutex_unlock (pi->lock);  
}


/**
 * Delete a path info entry from the tree (does not actually
 * remove anything from the file system).  Also decrements the RC.
 *
 * @param pi entry to remove
 * @return - ENOENT if the file was already deleted, 0 on success
 */
int
GNUNET_FUSE_path_info_delete (struct GNUNET_FUSE_PathInfo *pi)
{  
  struct GNUNET_FUSE_PathInfo *parent = pi->parent;
  int rc;
  int ret;

  if (NULL != parent)
  {
    ret = 0;
    GNUNET_mutex_lock (parent->lock);
    GNUNET_mutex_lock (pi->lock);
    GNUNET_CONTAINER_DLL_remove (parent->child_head,
				 parent->child_tail,
				 pi);
    pi->parent = NULL;
    GNUNET_mutex_unlock (parent->lock);
  }
  else
  {
    ret = - ENOENT;
    GNUNET_mutex_lock (pi->lock);
  }
  rc = --pi->rc;
  if (0 != rc)
  {
    pi->delete_later = GNUNET_YES;
    GNUNET_mutex_unlock (pi->lock);
  }
  else
  {
    if (NULL != pi->tmpfile)
    {
      GNUNET_break (0 == UNLINK (pi->tmpfile));
      GNUNET_free (pi->tmpfile);
    }
    GNUNET_free (pi->filename);
    GNUNET_FS_uri_destroy (pi->uri);
    GNUNET_mutex_unlock (pi->lock);
    GNUNET_mutex_destroy (pi->lock);
    GNUNET_free (pi);
  }
  return ret;
}


/**
 * Called on each node in the path info tree to clean it up.
 *
 * @param pi path info to clean up
 */
static void
cleanup_path_info (struct GNUNET_FUSE_PathInfo *pi)
{
  struct GNUNET_FUSE_PathInfo *pos;

  while (NULL != (pos = pi->child_head))
    cleanup_path_info (pos);
  ++pi->rc;
  (void) GNUNET_FUSE_path_info_delete (pi);
}


/**
 * Main function that will be run (without the scheduler!)
 *
 * @param cls closure
 * @param args remaining command-line arguments
 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
 * @param c configuration
 */
static void
run (void *cls,
     char *const *args,
     const char *cfgfile, const struct GNUNET_CONFIGURATION_Handle *c)
{
  static struct fuse_operations fops = {
    //  .mkdir = gn_mkdir,
    //  .mknod = gn_mknod,
    //  .release = gn_release,
    //  .rename = gn_rename,
    //  .rmdir = gn_rmdir,
    //  .truncate = gn_truncate,
    //  .unlink = gn_unlink,
    //  .utimens = gn_utimens,
    //  .write = gn_write,
    .getattr = gn_getattr,
    .readdir = gn_readdir,
    .open = gn_open,
    .read = gn_read
  };

  int argc;
  struct GNUNET_FS_Uri *uri;
  char *emsg;
  int eno;

  cfg = c;
  ret = 0;
  if (NULL == source)
    {
      fprintf (stderr, _("`%s' option for URI missing\n"), "-s");
      ret = 1;
      return;
    }
  if (NULL == directory)
    {
      fprintf (stderr, _("`%s' option for mountpoint missing\n"), "-d");
      ret = 2;
      return;
    }

  /* parse source string to uri */
  if (NULL == (uri = GNUNET_FS_uri_parse (source, &emsg)))
    {
      fprintf (stderr, "%s\n", emsg);
      GNUNET_free (emsg);
      ret = 3;
      return;
    }
  if ( (GNUNET_YES != GNUNET_FS_uri_test_chk (uri)) &&
       (GNUNET_YES != GNUNET_FS_uri_test_loc (uri)) )
  {
    fprintf (stderr, 
	     _("The given URI is not for a directory and can thus not be mounted\n"));
    ret = 4;
    GNUNET_FS_uri_destroy (uri);
    return;
  }

  root = GNUNET_FUSE_path_info_create (NULL, "/", uri, GNUNET_YES);
  if (GNUNET_OK != 
      GNUNET_FUSE_load_directory (root, &eno))
  {
    fprintf (stderr,
	     _("Failed to mount `%s': %s\n"),
	     source,
	     STRERROR (eno));    
    ret = 5;
    cleanup_path_info (root);
    GNUNET_FS_uri_destroy (uri);
    return;
  }

  if (GNUNET_YES == single_threaded)
    argc = 5;
  else
    argc = 2;

  {
    char *a[argc + 1];
    a[0] = "gnunet-fuse";
    a[1] = directory;
    if (GNUNET_YES == single_threaded)
      {
	a[2] = "-s";
	a[3] = "-f";
	a[4] = "-d";
      }
    a[argc] = NULL;
    fuse_main (argc, a, &fops, NULL);
  }
  cleanup_path_info (root);
  GNUNET_FS_uri_destroy (uri);
}


/**
 * The main function for gnunet-fuse.
 *
 * @param argc number of arguments from the command line
 * @param argv command line arguments
 * @return 0 ok, 1 on error
 */
int
main (int argc, char *const *argv)
{
  static const struct GNUNET_GETOPT_CommandLineOption options[] = {
    {'s', "source", "URI",
     gettext_noop ("Source you get the URI from"), 1,
     &GNUNET_GETOPT_set_string, &source},
    {'d', "directory", "PATH",
     gettext_noop ("path to your mountpoint"), 1,
     &GNUNET_GETOPT_set_string, &directory},
    {'t', "single-threaded", NULL,
     gettext_noop ("run in single-threaded mode"), 0,
     &GNUNET_GETOPT_set_one, &single_threaded},
    GNUNET_GETOPT_OPTION_END
  };
  GNUNET_log_setup ("gnunet-fuse", "DEBUG", NULL);
  return (GNUNET_OK ==
	  GNUNET_PROGRAM_run2 (argc,
			       argv,
			       "gnunet-fuse -s URI [-- FUSE-OPTIONS] DIRECTORYNAME",
			       gettext_noop
			       ("fuse"),
			       options, &run, NULL, GNUNET_YES)) ? ret : 1;
}

/* end of gnunet-fuse.c */