X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=fuse%2Fguestmount.c;h=d17982a7b2ed75c1e2ff5b86df2c72ce56929706;hp=ed8b08c10f7f5f7788ba39feabfa1464c30b90aa;hb=f48cd1f262312ad278a293a20ab442dcfc076a69;hpb=a15f118e3b2221e68732ad0545f770b3324d593b diff --git a/fuse/guestmount.c b/fuse/guestmount.c index ed8b08c..d17982a 100644 --- a/fuse/guestmount.c +++ b/fuse/guestmount.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -40,6 +41,7 @@ #include #include #include +#include #include #include @@ -441,6 +443,7 @@ fg_rename (const char *from, const char *to) if (read_only) return -EROFS; + dir_cache_invalidate (from); dir_cache_invalidate (to); /* XXX It's not clear how close the 'mv' command is to the @@ -533,14 +536,22 @@ fg_utimens (const char *path, const struct timespec ts[2]) time_t mtsecs = ts[1].tv_sec; long mtnsecs = ts[1].tv_nsec; +#ifdef UTIME_NOW if (atnsecs == UTIME_NOW) atnsecs = -1; +#endif +#ifdef UTIME_OMIT if (atnsecs == UTIME_OMIT) atnsecs = -2; +#endif +#ifdef UTIME_NOW if (mtnsecs == UTIME_NOW) mtnsecs = -1; +#endif +#ifdef UTIME_OMIT if (mtnsecs == UTIME_OMIT) mtnsecs = -2; +#endif r = guestfs_utimens (g, path, atsecs, atnsecs, mtsecs, mtnsecs); if (r == -1) @@ -643,7 +654,17 @@ fg_write (const char *path, const char *buf, size_t size, dir_cache_invalidate (path); - return -ENOSYS; /* XXX */ + /* See fg_read. */ + const size_t limit = 2 * 1024 * 1024; + if (size > limit) + size = limit; + + int r; + r = guestfs_pwrite (g, path, buf, size, offset); + if (r == -1) + return error (); + + return r; } static int @@ -833,6 +854,7 @@ static struct fuse_operations fg_operations = { struct drv { struct drv *next; char *filename; + const char *format; }; struct mp { @@ -849,7 +871,7 @@ fuse_help (void) { const char *tmp_argv[] = { program_name, "--help", NULL }; fuse_main (2, (char **) tmp_argv, &fg_operations, NULL); - exit (0); + exit (EXIT_SUCCESS); } static void __attribute__((noreturn)) @@ -868,6 +890,7 @@ usage (int status) "Options:\n" " -a|--add image Add image\n" " --dir-cache-timeout Set readdir cache timeout (default 5 sec)\n" + " --format[=raw|..] Force disk format for -a option\n" " --fuse-help Display extra FUSE options\n" " --help Display help message and exit\n" " -m|--mount dev[:mnt] Mount dev on mnt (if omitted, /)\n" @@ -887,6 +910,10 @@ usage (int status) int main (int argc, char *argv[]) { + setlocale (LC_ALL, ""); + bindtextdomain (PACKAGE, LOCALEBASEDIR); + textdomain (PACKAGE); + enum { HELP_OPTION = CHAR_MAX + 1 }; /* The command line arguments are broadly compatible with (a subset @@ -896,6 +923,7 @@ main (int argc, char *argv[]) static const struct option long_options[] = { { "add", 1, 0, 'a' }, { "dir-cache-timeout", 1, 0, 0 }, + { "format", 2, 0, 0 }, { "fuse-help", 0, 0, 0 }, { "help", 0, 0, HELP_OPTION }, { "mount", 1, 0, 'm' }, @@ -914,7 +942,8 @@ main (int argc, char *argv[]) struct mp *mps = NULL; struct mp *mp; char *p; - int c, i, r; + const char *format = NULL; + int c, r; int option_index; struct sigaction sa; @@ -927,7 +956,7 @@ main (int argc, char *argv[]) fuse_argv = realloc (fuse_argv, (1+fuse_argc) * sizeof (char *)); \ if (!fuse_argv) { \ perror ("realloc"); \ - exit (1); \ + exit (EXIT_FAILURE); \ } \ fuse_argv[fuse_argc-1] = (str); \ fuse_argv[fuse_argc] = NULL; \ @@ -950,10 +979,9 @@ main (int argc, char *argv[]) g = guestfs_create (); if (g == NULL) { fprintf (stderr, _("guestfs_create: failed to create handle\n")); - exit (1); + exit (EXIT_FAILURE); } - guestfs_set_autosync (g, 1); guestfs_set_recovery_proc (g, 0); ADD_FUSE_ARG (program_name); @@ -993,24 +1021,31 @@ main (int argc, char *argv[]) guestfs_set_trace (g, 1); guestfs_set_recovery_proc (g, 1); } + else if (STREQ (long_options[option_index].name, "format")) { + if (!optarg || STREQ (optarg, "")) + format = NULL; + else + format = optarg; + } else { fprintf (stderr, _("%s: unknown long option: %s (%d)\n"), program_name, long_options[option_index].name, option_index); - exit (1); + exit (EXIT_FAILURE); } break; case 'a': if (access (optarg, R_OK) != 0) { perror (optarg); - exit (1); + exit (EXIT_FAILURE); } drv = malloc (sizeof (struct drv)); if (!drv) { perror ("malloc"); - exit (1); + exit (EXIT_FAILURE); } drv->filename = optarg; + drv->format = format; drv->next = drvs; drvs = drv; break; @@ -1019,7 +1054,7 @@ main (int argc, char *argv[]) mp = malloc (sizeof (struct mp)); if (!mp) { perror ("malloc"); - exit (1); + exit (EXIT_FAILURE); } p = strchr (optarg, ':'); if (p) { @@ -1050,15 +1085,18 @@ main (int argc, char *argv[]) guestfs_set_verbose (g, verbose); break; - case 'V': - printf ("%s %s\n", program_name, PACKAGE_VERSION); - exit (0); + case 'V': { + struct guestfs_version *v = guestfs_version (g); + printf ("%s %"PRIi64".%"PRIi64".%"PRIi64"%s\n", program_name, + v->major, v->minor, v->release, v->extra); + exit (EXIT_SUCCESS); + } case HELP_OPTION: - usage (0); + usage (EXIT_SUCCESS); default: - usage (1); + usage (EXIT_FAILURE); } } @@ -1067,7 +1105,7 @@ main (int argc, char *argv[]) fprintf (stderr, _("%s: must have at least one -a and at least one -m option\n"), program_name); - exit (1); + exit (EXIT_FAILURE); } /* We'd better have a mountpoint. */ @@ -1075,18 +1113,18 @@ main (int argc, char *argv[]) fprintf (stderr, _("%s: you must specify a mountpoint in the host filesystem\n"), program_name); - exit (1); + exit (EXIT_FAILURE); } /* Do the guest drives and mountpoints. */ add_drives (drvs); if (guestfs_launch (g) == -1) - exit (1); + exit (EXIT_FAILURE); mount_mps (mps); /* FUSE example does this, not clear if it's necessary, but ... */ if (guestfs_umask (g, 0) == -1) - exit (1); + exit (EXIT_FAILURE); /* At the last minute, remove the libguestfs error handler. In code * above this point, the default error handler has been used which @@ -1127,15 +1165,23 @@ static void add_drives (struct drv *drv) { int r; + struct guestfs_add_drive_opts_argv ad_optargs; if (drv) { add_drives (drv->next); - if (!read_only) - r = guestfs_add_drive (g, drv->filename); - else - r = guestfs_add_drive_ro (g, drv->filename); + + ad_optargs.bitmask = 0; + if (read_only) { + ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_READONLY_BITMASK; + ad_optargs.readonly = 1; + } + if (drv->format) { + ad_optargs.bitmask |= GUESTFS_ADD_DRIVE_OPTS_FORMAT_BITMASK; + ad_optargs.format = drv->format; + } + r = guestfs_add_drive_opts_argv (g, drv->filename, &ad_optargs); if (r == -1) - exit (1); + exit (EXIT_FAILURE); } } @@ -1147,11 +1193,14 @@ mount_mps (struct mp *mp) if (mp) { mount_mps (mp->next); - if (!read_only) - r = guestfs_mount (g, mp->device, mp->mountpoint); - else - r = guestfs_mount_ro (g, mp->device, mp->mountpoint); + + /* Don't use guestfs_mount here because that will default to mount + * options -o sync,noatime. For more information, see guestfs(3) + * section "LIBGUESTFS GOTCHAS". + */ + const char *options = read_only ? "ro" : ""; + r = guestfs_mount_options (g, options, mp->device, mp->mountpoint); if (r == -1) - exit (1); + exit (EXIT_FAILURE); } }