list-filesystems: Use core list-filesystems API (RHBZ#642933).
[libguestfs.git] / fuse / guestmount.c
1 /* guestmount - mount guests using libguestfs and FUSE
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  * Derived from the example program 'fusexmp.c':
19  * Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>
20  *
21  * This program can be distributed under the terms of the GNU GPL.
22  * See the file COPYING.
23  */
24
25 #define FUSE_USE_VERSION 26
26
27 #include <config.h>
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <stdint.h>
32 #include <inttypes.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <getopt.h>
36 #include <fcntl.h>
37 #include <dirent.h>
38 #include <errno.h>
39 #include <signal.h>
40 #include <time.h>
41 #include <assert.h>
42 #include <sys/time.h>
43 #include <sys/types.h>
44 #include <locale.h>
45
46 #include <fuse.h>
47 #include <guestfs.h>
48
49 #include "progname.h"
50
51 #include "guestmount.h"
52 #include "options.h"
53 #include "dircache.h"
54
55 /* See <attr/xattr.h> */
56 #ifndef ENOATTR
57 #define ENOATTR ENODATA
58 #endif
59
60 guestfs_h *g = NULL;
61 int read_only = 0;
62 int verbose = 0;
63 int inspector = 0;
64 const char *libvirt_uri;
65 int dir_cache_timeout = 60;
66
67 /* This is ugly: guestfs errors are strings, FUSE wants -errno.  We
68  * have to do the conversion as best we can.
69  */
70 #define MAX_ERRNO 256
71
72 static int
73 error (void)
74 {
75   int i;
76   const char *err = guestfs_last_error (g);
77
78   if (!err)
79     return -EINVAL;
80
81   if (verbose)
82     fprintf (stderr, "%s\n", err);
83
84   /* Add a few of our own ... */
85
86   /* This indicates guestfsd died.  Translate into a hard EIO error.
87    * Arguably we could relaunch the guest if we hit this error.
88    */
89   if (strstr (err, "call launch before using this function"))
90     return -EIO;
91
92   /* See if it matches an errno string in the host. */
93   for (i = 0; i < MAX_ERRNO; ++i) {
94     const char *e = strerror (i);
95     if (e && strstr (err, e) != NULL)
96       return -i;
97   }
98
99   /* Too bad, return a generic error. */
100   return -EINVAL;
101 }
102
103 static struct guestfs_xattr_list *
104 copy_xattr_list (const struct guestfs_xattr *first, size_t num)
105 {
106   struct guestfs_xattr_list *xattrs;
107
108   xattrs = malloc (sizeof *xattrs);
109   if (xattrs == NULL) {
110     perror ("malloc");
111     return NULL;
112   }
113
114   xattrs->len = num;
115   xattrs->val = malloc (num * sizeof (struct guestfs_xattr));
116   if (xattrs->val == NULL) {
117     perror ("malloc");
118     free (xattrs);
119     return NULL;
120   }
121
122   size_t i;
123   for (i = 0; i < num; ++i) {
124     xattrs->val[i].attrname = strdup (first[i].attrname);
125     xattrs->val[i].attrval_len = first[i].attrval_len;
126     xattrs->val[i].attrval = malloc (first[i].attrval_len);
127     memcpy (xattrs->val[i].attrval, first[i].attrval, first[i].attrval_len);
128   }
129
130   return xattrs;
131 }
132
133 static int
134 fg_readdir (const char *path, void *buf, fuse_fill_dir_t filler,
135             off_t offset, struct fuse_file_info *fi)
136 {
137   time_t now;
138   time (&now);
139
140   dir_cache_remove_all_expired (now);
141
142   struct guestfs_dirent_list *ents;
143
144   ents = guestfs_readdir (g, path);
145   if (ents == NULL)
146     return error ();
147
148   size_t i;
149   for (i = 0; i < ents->len; ++i) {
150     struct stat stat;
151     memset (&stat, 0, sizeof stat);
152
153     stat.st_ino = ents->val[i].ino;
154     switch (ents->val[i].ftyp) {
155     case 'b': stat.st_mode = S_IFBLK; break;
156     case 'c': stat.st_mode = S_IFCHR; break;
157     case 'd': stat.st_mode = S_IFDIR; break;
158     case 'f': stat.st_mode = S_IFIFO; break;
159     case 'l': stat.st_mode = S_IFLNK; break;
160     case 'r': stat.st_mode = S_IFREG; break;
161     case 's': stat.st_mode = S_IFSOCK; break;
162     case 'u':
163     case '?':
164     default:  stat.st_mode = 0;
165     }
166
167     /* Copied from the example, which also ignores 'offset'.  I'm
168      * not quite sure how this is ever supposed to work on large
169      * directories. XXX
170      */
171     if (filler (buf, ents->val[i].name, &stat, 0))
172       break;
173   }
174
175   /* Now prepopulate the directory caches.  This step is just an
176    * optimization, don't worry if it fails.
177    */
178   char **names = malloc ((ents->len + 1) * sizeof (char *));
179   if (names) {
180     for (i = 0; i < ents->len; ++i)
181       names[i] = ents->val[i].name;
182     names[i] = NULL;
183
184     struct guestfs_stat_list *ss = guestfs_lstatlist (g, path, names);
185     if (ss) {
186       for (i = 0; i < ss->len; ++i) {
187         if (ss->val[i].ino >= 0) {
188           struct stat statbuf;
189
190           statbuf.st_dev = ss->val[i].dev;
191           statbuf.st_ino = ss->val[i].ino;
192           statbuf.st_mode = ss->val[i].mode;
193           statbuf.st_nlink = ss->val[i].nlink;
194           statbuf.st_uid = ss->val[i].uid;
195           statbuf.st_gid = ss->val[i].gid;
196           statbuf.st_rdev = ss->val[i].rdev;
197           statbuf.st_size = ss->val[i].size;
198           statbuf.st_blksize = ss->val[i].blksize;
199           statbuf.st_blocks = ss->val[i].blocks;
200           statbuf.st_atime = ss->val[i].atime;
201           statbuf.st_mtime = ss->val[i].mtime;
202           statbuf.st_ctime = ss->val[i].ctime;
203
204           lsc_insert (path, names[i], now, &statbuf);
205         }
206       }
207       guestfs_free_stat_list (ss);
208     }
209
210     struct guestfs_xattr_list *xattrs = guestfs_lxattrlist (g, path, names);
211     if (xattrs) {
212       size_t ni, num;
213       struct guestfs_xattr *first;
214       struct guestfs_xattr_list *copy;
215       for (i = 0, ni = 0; i < xattrs->len; ++i, ++ni) {
216         assert (strlen (xattrs->val[i].attrname) == 0);
217         if (xattrs->val[i].attrval_len > 0) {
218           ++i;
219           first = &xattrs->val[i];
220           num = 0;
221           for (; i < xattrs->len && strlen (xattrs->val[i].attrname) > 0; ++i)
222             num++;
223
224           copy = copy_xattr_list (first, num);
225           if (copy)
226             xac_insert (path, names[ni], now, copy);
227
228           i--;
229         }
230       }
231       guestfs_free_xattr_list (xattrs);
232     }
233
234     char **links = guestfs_readlinklist (g, path, names);
235     if (links) {
236       for (i = 0; names[i] != NULL; ++i) {
237         if (links[i][0])
238           /* Note that rlc_insert owns the string links[i] after this, */
239           rlc_insert (path, names[i], now, links[i]);
240         else
241           /* which is why we have to free links[i] here. */
242           free (links[i]);
243       }
244       free (links);             /* free the array, not the strings */
245     }
246
247     free (names);
248   }
249
250   guestfs_free_dirent_list (ents);
251
252   return 0;
253 }
254
255 static int
256 fg_getattr (const char *path, struct stat *statbuf)
257 {
258   const struct stat *buf;
259
260   buf = lsc_lookup (path);
261   if (buf) {
262     memcpy (statbuf, buf, sizeof *statbuf);
263     return 0;
264   }
265
266   struct guestfs_stat *r;
267
268   r = guestfs_lstat (g, path);
269   if (r == NULL)
270     return error ();
271
272   statbuf->st_dev = r->dev;
273   statbuf->st_ino = r->ino;
274   statbuf->st_mode = r->mode;
275   statbuf->st_nlink = r->nlink;
276   statbuf->st_uid = r->uid;
277   statbuf->st_gid = r->gid;
278   statbuf->st_rdev = r->rdev;
279   statbuf->st_size = r->size;
280   statbuf->st_blksize = r->blksize;
281   statbuf->st_blocks = r->blocks;
282   statbuf->st_atime = r->atime;
283   statbuf->st_mtime = r->mtime;
284   statbuf->st_ctime = r->ctime;
285
286   guestfs_free_stat (r);
287
288   return 0;
289 }
290
291 /* Nautilus loves to use access(2) to test everything about a file,
292  * such as whether it's executable.  Therefore treat this a lot like
293  * fg_getattr.
294  */
295 static int
296 fg_access (const char *path, int mask)
297 {
298   struct stat statbuf;
299   int r;
300
301   if (read_only && (mask & W_OK))
302     return -EROFS;
303
304   r = fg_getattr (path, &statbuf);
305   if (r < 0 || mask == F_OK)
306     return r;
307
308   struct fuse_context *fuse = fuse_get_context ();
309   int ok = 1;
310
311   if (mask & R_OK)
312     ok = ok &&
313       (  fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IRUSR
314        : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IRGRP
315        : statbuf.st_mode & S_IROTH);
316   if (mask & W_OK)
317     ok = ok &&
318       (  fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IWUSR
319        : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IWGRP
320        : statbuf.st_mode & S_IWOTH);
321   if (mask & X_OK)
322     ok = ok &&
323       (  fuse->uid == statbuf.st_uid ? statbuf.st_mode & S_IXUSR
324        : fuse->gid == statbuf.st_gid ? statbuf.st_mode & S_IXGRP
325        : statbuf.st_mode & S_IXOTH);
326
327   return ok ? 0 : -EACCES;
328 }
329
330 static int
331 fg_readlink (const char *path, char *buf, size_t size)
332 {
333   const char *r;
334   int free_it = 0;
335
336   r = rlc_lookup (path);
337   if (!r) {
338     r = guestfs_readlink (g, path);
339     if (r == NULL)
340       return error ();
341     free_it = 1;
342   }
343
344   /* Note this is different from the real readlink(2) syscall.  FUSE wants
345    * the string to be always nul-terminated, even if truncated.
346    */
347   size_t len = strlen (r);
348   if (len > size - 1)
349     len = size - 1;
350
351   memcpy (buf, r, len);
352   buf[len] = '\0';
353
354   if (free_it) {
355     char *tmp = (char *) r;
356     free (tmp);
357   }
358
359   return 0;
360 }
361
362 static int
363 fg_mknod (const char *path, mode_t mode, dev_t rdev)
364 {
365   int r;
366
367   if (read_only) return -EROFS;
368
369   dir_cache_invalidate (path);
370
371   r = guestfs_mknod (g, mode, major (rdev), minor (rdev), path);
372   if (r == -1)
373     return error ();
374
375   return 0;
376 }
377
378 static int
379 fg_mkdir (const char *path, mode_t mode)
380 {
381   int r;
382
383   if (read_only) return -EROFS;
384
385   dir_cache_invalidate (path);
386
387   r = guestfs_mkdir_mode (g, path, mode);
388   if (r == -1)
389     return error ();
390
391   return 0;
392 }
393
394 static int
395 fg_unlink (const char *path)
396 {
397   int r;
398
399   if (read_only) return -EROFS;
400
401   dir_cache_invalidate (path);
402
403   r = guestfs_rm (g, path);
404   if (r == -1)
405     return error ();
406
407   return 0;
408 }
409
410 static int
411 fg_rmdir (const char *path)
412 {
413   int r;
414
415   if (read_only) return -EROFS;
416
417   dir_cache_invalidate (path);
418
419   r = guestfs_rmdir (g, path);
420   if (r == -1)
421     return error ();
422
423   return 0;
424 }
425
426 static int
427 fg_symlink (const char *from, const char *to)
428 {
429   int r;
430
431   if (read_only) return -EROFS;
432
433   dir_cache_invalidate (to);
434
435   r = guestfs_ln_s (g, from, to);
436   if (r == -1)
437     return error ();
438
439   return 0;
440 }
441
442 static int
443 fg_rename (const char *from, const char *to)
444 {
445   int r;
446
447   if (read_only) return -EROFS;
448
449   dir_cache_invalidate (from);
450   dir_cache_invalidate (to);
451
452   /* XXX It's not clear how close the 'mv' command is to the
453    * rename syscall.  We might need to add the rename syscall
454    * to the guestfs(3) API.
455    */
456   r = guestfs_mv (g, from, to);
457   if (r == -1)
458     return error ();
459
460   return 0;
461 }
462
463 static int
464 fg_link (const char *from, const char *to)
465 {
466   int r;
467
468   if (read_only) return -EROFS;
469
470   dir_cache_invalidate (from);
471   dir_cache_invalidate (to);
472
473   r = guestfs_ln (g, from, to);
474   if (r == -1)
475     return error ();
476
477   return 0;
478 }
479
480 static int
481 fg_chmod (const char *path, mode_t mode)
482 {
483   int r;
484
485   if (read_only) return -EROFS;
486
487   dir_cache_invalidate (path);
488
489   r = guestfs_chmod (g, mode, path);
490   if (r == -1)
491     return error ();
492
493   return 0;
494 }
495
496 static int
497 fg_chown (const char *path, uid_t uid, gid_t gid)
498 {
499   int r;
500
501   if (read_only) return -EROFS;
502
503   dir_cache_invalidate (path);
504
505   r = guestfs_lchown (g, uid, gid, path);
506   if (r == -1)
507     return error ();
508
509   return 0;
510 }
511
512 static int
513 fg_truncate (const char *path, off_t size)
514 {
515   int r;
516
517   if (read_only) return -EROFS;
518
519   dir_cache_invalidate (path);
520
521   r = guestfs_truncate_size (g, path, size);
522   if (r == -1)
523     return error ();
524
525   return 0;
526 }
527
528 static int
529 fg_utimens (const char *path, const struct timespec ts[2])
530 {
531   int r;
532
533   if (read_only) return -EROFS;
534
535   dir_cache_invalidate (path);
536
537   time_t atsecs = ts[0].tv_sec;
538   long atnsecs = ts[0].tv_nsec;
539   time_t mtsecs = ts[1].tv_sec;
540   long mtnsecs = ts[1].tv_nsec;
541
542 #ifdef UTIME_NOW
543   if (atnsecs == UTIME_NOW)
544     atnsecs = -1;
545 #endif
546 #ifdef UTIME_OMIT
547   if (atnsecs == UTIME_OMIT)
548     atnsecs = -2;
549 #endif
550 #ifdef UTIME_NOW
551   if (mtnsecs == UTIME_NOW)
552     mtnsecs = -1;
553 #endif
554 #ifdef UTIME_OMIT
555   if (mtnsecs == UTIME_OMIT)
556     mtnsecs = -2;
557 #endif
558
559   r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs);
560   if (r == -1)
561     return error ();
562
563   return 0;
564 }
565
566 /* This call is quite hard to emulate through the guestfs(3) API.  In
567  * one sense it's a little like access (see above) because it tests
568  * whether opening a file would succeed given the flags.  But it also
569  * has side effects such as truncating the file if O_TRUNC is given.
570  * Therefore we need to emulate it ... painfully.
571  */
572 static int
573 fg_open (const char *path, struct fuse_file_info *fi)
574 {
575   int r, exists;
576
577   if (fi->flags & O_WRONLY) {
578     if (read_only)
579       return -EROFS;
580   }
581
582   exists = guestfs_exists (g, path);
583   if (exists == -1)
584     return error ();
585
586   if (fi->flags & O_CREAT) {
587     if (read_only)
588       return -EROFS;
589
590     dir_cache_invalidate (path);
591
592     /* Exclusive?  File must not exist already. */
593     if (fi->flags & O_EXCL) {
594       if (exists)
595         return -EEXIST;
596     }
597
598     /* Create?  Touch it and optionally truncate it. */
599     r = guestfs_touch (g, path);
600     if (r == -1)
601       return error ();
602
603     if (fi->flags & O_TRUNC) {
604       r = guestfs_truncate (g, path);
605       if (r == -1)
606         return error ();
607     }
608   } else {
609     /* Not create, just check it exists. */
610     if (!exists)
611       return -ENOENT;
612   }
613
614   return 0;
615 }
616
617 static int
618 fg_read (const char *path, char *buf, size_t size, off_t offset,
619          struct fuse_file_info *fi)
620 {
621   char *r;
622   size_t rsize;
623
624   if (verbose)
625     fprintf (stderr, "fg_read: %s: size %zu offset %ju\n",
626              path, size, offset);
627
628   /* The guestfs protocol limits size to somewhere over 2MB.  We just
629    * reduce the requested size here accordingly and push the problem
630    * up to every user.  http://www.jwz.org/doc/worse-is-better.html
631    */
632   const size_t limit = 2 * 1024 * 1024;
633   if (size > limit)
634     size = limit;
635
636   r = guestfs_pread (g, path, size, offset, &rsize);
637   if (r == NULL)
638     return error ();
639
640   /* This should never happen, but at least it stops us overflowing
641    * the output buffer if it does happen.
642    */
643   if (rsize > size)
644     rsize = size;
645
646   memcpy (buf, r, rsize);
647   free (r);
648
649   return rsize;
650 }
651
652 static int
653 fg_write (const char *path, const char *buf, size_t size,
654           off_t offset, struct fuse_file_info *fi)
655 {
656   if (read_only) return -EROFS;
657
658   dir_cache_invalidate (path);
659
660   /* See fg_read. */
661   const size_t limit = 2 * 1024 * 1024;
662   if (size > limit)
663     size = limit;
664
665   int r;
666   r = guestfs_pwrite (g, path, buf, size, offset);
667   if (r == -1)
668     return error ();
669
670   return r;
671 }
672
673 static int
674 fg_statfs (const char *path, struct statvfs *stbuf)
675 {
676   struct guestfs_statvfs *r;
677
678   r = guestfs_statvfs (g, path);
679   if (r == NULL)
680     return error ();
681
682   stbuf->f_bsize = r->bsize;
683   stbuf->f_frsize = r->frsize;
684   stbuf->f_blocks = r->blocks;
685   stbuf->f_bfree = r->bfree;
686   stbuf->f_bavail = r->bavail;
687   stbuf->f_files = r->files;
688   stbuf->f_ffree = r->ffree;
689   stbuf->f_favail = r->favail;
690   stbuf->f_fsid = r->fsid;
691   stbuf->f_flag = r->flag;
692   stbuf->f_namemax = r->namemax;
693
694   guestfs_free_statvfs (r);
695
696   return 0;
697 }
698
699 static int
700 fg_release (const char *path, struct fuse_file_info *fi)
701 {
702   /* Just a stub. This method is optional and can safely be left
703    * unimplemented.
704    */
705   return 0;
706 }
707
708 /* Emulate this by calling sync. */
709 static int fg_fsync(const char *path, int isdatasync,
710                      struct fuse_file_info *fi)
711 {
712   int r;
713
714   r = guestfs_sync (g);
715   if (r == -1)
716     return error ();
717
718   return 0;
719 }
720
721 static int
722 fg_setxattr (const char *path, const char *name, const char *value,
723              size_t size, int flags)
724 {
725   int r;
726
727   if (read_only) return -EROFS;
728
729   dir_cache_invalidate (path);
730
731   /* XXX Underlying guestfs(3) API doesn't understand the flags. */
732   r = guestfs_lsetxattr (g, name, value, size, path);
733   if (r == -1)
734     return error ();
735
736   return 0;
737 }
738
739 /* The guestfs(3) API for getting xattrs is much easier to use
740  * than the real syscall.  Unfortunately we now have to emulate
741  * the real syscall using that API :-(
742  */
743 static int
744 fg_getxattr (const char *path, const char *name, char *value,
745              size_t size)
746 {
747   const struct guestfs_xattr_list *xattrs;
748   int free_attrs = 0;
749
750   xattrs = xac_lookup (path);
751   if (xattrs == NULL) {
752     xattrs = guestfs_lgetxattrs (g, path);
753     if (xattrs == NULL)
754       return error ();
755     free_attrs = 1;
756   }
757
758   size_t i;
759   int r = -ENOATTR;
760   for (i = 0; i < xattrs->len; ++i) {
761     if (STREQ (xattrs->val[i].attrname, name)) {
762       size_t sz = xattrs->val[i].attrval_len;
763       if (sz > size)
764         sz = size;
765       memcpy (value, xattrs->val[i].attrval, sz);
766       r = 0;
767       break;
768     }
769   }
770
771   if (free_attrs)
772     guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
773
774   return r;
775 }
776
777 /* Ditto as above. */
778 static int
779 fg_listxattr (const char *path, char *list, size_t size)
780 {
781   const struct guestfs_xattr_list *xattrs;
782   int free_attrs = 0;
783
784   xattrs = xac_lookup (path);
785   if (xattrs == NULL) {
786     xattrs = guestfs_lgetxattrs (g, path);
787     if (xattrs == NULL)
788       return error ();
789     free_attrs = 1;
790   }
791
792   size_t i;
793   ssize_t copied = 0;
794   for (i = 0; i < xattrs->len; ++i) {
795     size_t len = strlen (xattrs->val[i].attrname) + 1;
796     if (size >= len) {
797       memcpy (list, xattrs->val[i].attrname, len);
798       size -= len;
799       list += len;
800       copied += len;
801     } else {
802       copied = -ERANGE;
803       break;
804     }
805   }
806
807   if (free_attrs)
808     guestfs_free_xattr_list ((struct guestfs_xattr_list *) xattrs);
809
810   return copied;
811 }
812
813 static int
814 fg_removexattr(const char *path, const char *name)
815 {
816   int r;
817
818   if (read_only) return -EROFS;
819
820   dir_cache_invalidate (path);
821
822   r = guestfs_lremovexattr (g, name, path);
823   if (r == -1)
824     return error ();
825
826   return 0;
827 }
828
829 static struct fuse_operations fg_operations = {
830   .getattr      = fg_getattr,
831   .access       = fg_access,
832   .readlink     = fg_readlink,
833   .readdir      = fg_readdir,
834   .mknod        = fg_mknod,
835   .mkdir        = fg_mkdir,
836   .symlink      = fg_symlink,
837   .unlink       = fg_unlink,
838   .rmdir        = fg_rmdir,
839   .rename       = fg_rename,
840   .link         = fg_link,
841   .chmod        = fg_chmod,
842   .chown        = fg_chown,
843   .truncate     = fg_truncate,
844   .utimens      = fg_utimens,
845   .open         = fg_open,
846   .read         = fg_read,
847   .write        = fg_write,
848   .statfs       = fg_statfs,
849   .release      = fg_release,
850   .fsync        = fg_fsync,
851   .setxattr     = fg_setxattr,
852   .getxattr     = fg_getxattr,
853   .listxattr    = fg_listxattr,
854   .removexattr  = fg_removexattr,
855 };
856
857 static void __attribute__((noreturn))
858 fuse_help (void)
859 {
860   const char *tmp_argv[] = { program_name, "--help", NULL };
861   fuse_main (2, (char **) tmp_argv, &fg_operations, NULL);
862   exit (EXIT_SUCCESS);
863 }
864
865 static void __attribute__((noreturn))
866 usage (int status)
867 {
868   if (status != EXIT_SUCCESS)
869     fprintf (stderr, _("Try `%s --help' for more information.\n"),
870              program_name);
871   else {
872     fprintf (stdout,
873            _("%s: FUSE module for libguestfs\n"
874              "%s lets you mount a virtual machine filesystem\n"
875              "Copyright (C) 2009-2010 Red Hat Inc.\n"
876              "Usage:\n"
877              "  %s [--options] [-- [--FUSE-options]] mountpoint\n"
878              "Options:\n"
879              "  -a|--add image       Add image\n"
880              "  -c|--connect uri     Specify libvirt URI for -d option\n"
881              "  --dir-cache-timeout  Set readdir cache timeout (default 5 sec)\n"
882              "  -d|--domain guest    Add disks from libvirt guest\n"
883              "  --format[=raw|..]    Force disk format for -a option\n"
884              "  --fuse-help          Display extra FUSE options\n"
885              "  -i|--inspector       Automatically mount filesystems\n"
886              "  --help               Display help message and exit\n"
887              "  -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n"
888              "  -n|--no-sync         Don't autosync\n"
889              "  -o|--option opt      Pass extra option to FUSE\n"
890              "  -r|--ro              Mount read-only\n"
891              "  --selinux            Enable SELinux support\n"
892              "  -v|--verbose         Verbose messages\n"
893              "  -V|--version         Display version and exit\n"
894              "  -x|--trace           Trace guestfs API calls\n"
895              ),
896              program_name, program_name, program_name);
897   }
898   exit (status);
899 }
900
901 int
902 main (int argc, char *argv[])
903 {
904   setlocale (LC_ALL, "");
905   bindtextdomain (PACKAGE, LOCALEBASEDIR);
906   textdomain (PACKAGE);
907
908   enum { HELP_OPTION = CHAR_MAX + 1 };
909
910   /* The command line arguments are broadly compatible with (a subset
911    * of) guestfish.  Thus we have to deal mainly with -a, -m and --ro.
912    */
913   static const char *options = "a:c:d:im:no:rv?Vx";
914   static const struct option long_options[] = {
915     { "add", 1, 0, 'a' },
916     { "connect", 1, 0, 'c' },
917     { "dir-cache-timeout", 1, 0, 0 },
918     { "domain", 1, 0, 'd' },
919     { "format", 2, 0, 0 },
920     { "fuse-help", 0, 0, 0 },
921     { "help", 0, 0, HELP_OPTION },
922     { "inspector", 0, 0, 'i' },
923     { "mount", 1, 0, 'm' },
924     { "no-sync", 0, 0, 'n' },
925     { "option", 1, 0, 'o' },
926     { "ro", 0, 0, 'r' },
927     { "selinux", 0, 0, 0 },
928     { "trace", 0, 0, 'x' },
929     { "verbose", 0, 0, 'v' },
930     { "version", 0, 0, 'V' },
931     { 0, 0, 0, 0 }
932   };
933
934   struct drv *drvs = NULL;
935   struct drv *drv;
936   struct mp *mps = NULL;
937   struct mp *mp;
938   char *p;
939   const char *format = NULL;
940   int c, r;
941   int option_index;
942   struct sigaction sa;
943
944   int fuse_argc = 0;
945   const char **fuse_argv = NULL;
946
947 #define ADD_FUSE_ARG(str)                                               \
948   do {                                                                  \
949     fuse_argc ++;                                                       \
950     fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *));   \
951     if (!fuse_argv) {                                                   \
952       perror ("realloc");                                               \
953       exit (EXIT_FAILURE);                                                         \
954     }                                                                   \
955     fuse_argv[fuse_argc-1] = (str);                                     \
956     fuse_argv[fuse_argc] = NULL;                                        \
957   } while (0)
958
959   /* LC_ALL=C is required so we can parse error messages. */
960   setenv ("LC_ALL", "C", 1);
961
962   /* Set global program name that is not polluted with libtool artifacts.  */
963   set_program_name (argv[0]);
964
965   memset (&sa, 0, sizeof sa);
966   sa.sa_handler = SIG_IGN;
967   sa.sa_flags = SA_RESTART;
968   sigaction (SIGPIPE, &sa, NULL);
969
970   /* Various initialization. */
971   init_dir_caches ();
972
973   g = guestfs_create ();
974   if (g == NULL) {
975     fprintf (stderr, _("guestfs_create: failed to create handle\n"));
976     exit (EXIT_FAILURE);
977   }
978
979   guestfs_set_recovery_proc (g, 0);
980
981   ADD_FUSE_ARG (program_name);
982   /* MUST be single-threaded.  You cannot have two threads accessing the
983    * same libguestfs handle, and opening more than one handle is likely
984    * to be very expensive.
985    */
986   ADD_FUSE_ARG ("-s");
987
988   /* If developing, add ./appliance to the path.  Note that libtools
989    * interferes with this because uninstalled guestfish is a shell
990    * script that runs the real program with an absolute path.  Detect
991    * that too.
992    *
993    * BUT if LIBGUESTFS_PATH environment variable is already set by
994    * the user, then don't override it.
995    */
996   if (getenv ("LIBGUESTFS_PATH") == NULL &&
997       argv[0] &&
998       (argv[0][0] != '/' || strstr (argv[0], "/.libs/lt-") != NULL))
999     guestfs_set_path (g, "appliance:" GUESTFS_DEFAULT_PATH);
1000
1001   for (;;) {
1002     c = getopt_long (argc, argv, options, long_options, &option_index);
1003     if (c == -1) break;
1004
1005     switch (c) {
1006     case 0:                     /* options which are long only */
1007       if (STREQ (long_options[option_index].name, "dir-cache-timeout"))
1008         dir_cache_timeout = atoi (optarg);
1009       else if (STREQ (long_options[option_index].name, "fuse-help"))
1010         fuse_help ();
1011       else if (STREQ (long_options[option_index].name, "selinux"))
1012         guestfs_set_selinux (g, 1);
1013       else if (STREQ (long_options[option_index].name, "format")) {
1014         if (!optarg || STREQ (optarg, ""))
1015           format = NULL;
1016         else
1017           format = optarg;
1018       }
1019       else {
1020         fprintf (stderr, _("%s: unknown long option: %s (%d)\n"),
1021                  program_name, long_options[option_index].name, option_index);
1022         exit (EXIT_FAILURE);
1023       }
1024       break;
1025
1026     case 'a':
1027       OPTION_a;
1028       break;
1029
1030     case 'c':
1031       OPTION_c;
1032       break;
1033
1034     case 'd':
1035       OPTION_d;
1036       break;
1037
1038     case 'i':
1039       OPTION_i;
1040       break;
1041
1042     case 'm':
1043       OPTION_m;
1044       break;
1045
1046     case 'n':
1047       OPTION_n;
1048       break;
1049
1050     case 'o':
1051       ADD_FUSE_ARG ("-o");
1052       ADD_FUSE_ARG (optarg);
1053       break;
1054
1055     case 'r':
1056       OPTION_r;
1057       break;
1058
1059     case 'v':
1060       OPTION_v;
1061       break;
1062
1063     case 'V':
1064       OPTION_V;
1065       break;
1066
1067     case 'x':
1068       OPTION_x;
1069       ADD_FUSE_ARG ("-f");
1070       guestfs_set_recovery_proc (g, 1);
1071       break;
1072
1073     case HELP_OPTION:
1074       usage (EXIT_SUCCESS);
1075
1076     default:
1077       usage (EXIT_FAILURE);
1078     }
1079   }
1080
1081   /* Check we have the right options. */
1082   if (!drvs || !(mps || inspector)) {
1083     fprintf (stderr,
1084              _("%s: must have at least one -a/-d and at least one -m/-i option\n"),
1085              program_name);
1086     exit (EXIT_FAILURE);
1087   }
1088
1089   /* We'd better have a mountpoint. */
1090   if (optind+1 != argc) {
1091     fprintf (stderr,
1092              _("%s: you must specify a mountpoint in the host filesystem\n"),
1093              program_name);
1094     exit (EXIT_FAILURE);
1095   }
1096
1097   /* Do the guest drives and mountpoints. */
1098   add_drives (drvs, 'a');
1099   if (guestfs_launch (g) == -1)
1100     exit (EXIT_FAILURE);
1101   if (inspector)
1102     inspect_mount ();
1103   mount_mps (mps);
1104
1105   free_drives (drvs);
1106   free_mps (mps);
1107
1108   /* FUSE example does this, not clear if it's necessary, but ... */
1109   if (guestfs_umask (g, 0) == -1)
1110     exit (EXIT_FAILURE);
1111
1112   /* At the last minute, remove the libguestfs error handler.  In code
1113    * above this point, the default error handler has been used which
1114    * sends all errors to stderr.  Now before entering FUSE itself we
1115    * want to silence errors so we can convert them (see error()
1116    * function above).
1117    */
1118   guestfs_set_error_handler (g, NULL, NULL);
1119
1120   /* Finish off FUSE args. */
1121   ADD_FUSE_ARG (argv[optind]);
1122
1123   /*
1124     It says about the line containing the for-statement:
1125     error: assuming signed overflow does not occur when simplifying conditional to constant [-Wstrict-overflow]
1126
1127   if (verbose) {
1128     fprintf (stderr, "guestmount: invoking FUSE with args [");
1129     for (i = 0; i < fuse_argc; ++i) {
1130       if (i > 0) fprintf (stderr, ", ");
1131       fprintf (stderr, "%s", fuse_argv[i]);
1132     }
1133     fprintf (stderr, "]\n");
1134   }
1135   */
1136
1137   r = fuse_main (fuse_argc, (char **) fuse_argv, &fg_operations, NULL);
1138
1139   /* Cleanup. */
1140   guestfs_close (g);
1141   free_dir_caches ();
1142
1143   exit (r == -1 ? 1 : 0);
1144 }