X-Git-Url: http://git.annexia.org/?p=libguestfs.git;a=blobdiff_plain;f=fish%2Fedit.c;h=80ae44bd15baeda3abc5dcd97fa42a1991e97283;hp=1b6ce23cac165acf49e25f9c7e58c9d9da6c5d61;hb=4accf3197235300029d04d0ab637b66eaaf9f8df;hpb=c17ff8145718ed86419a7f6c5000b73dd54e324a diff --git a/fish/edit.c b/fish/edit.c index 1b6ce23..80ae44b 100644 --- a/fish/edit.c +++ b/fish/edit.c @@ -1,5 +1,5 @@ /* guestfish - the filesystem interactive shell - * Copyright (C) 2009 Red Hat Inc. + * Copyright (C) 2009-2011 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -24,62 +24,21 @@ #include #include #include +#include +#include #include "fish.h" /* guestfish edit command, suggested by Ján Ondrej, implemented by RWMJ */ -static char * -load_file (const char *filename, size_t *len_r) -{ - int fd, r, start; - char *content = NULL, *p; - char buf[65536]; - - *len_r = 0; - - fd = open (filename, O_RDONLY); - if (fd == -1) { - perror (filename); - return NULL; - } - - while ((r = read (fd, buf, sizeof buf)) > 0) { - start = *len_r; - *len_r += r; - p = realloc (content, *len_r + 1); - if (p == NULL) { - perror ("realloc"); - free (content); - return NULL; - } - content = p; - memcpy (content + start, buf, r); - content[start+r] = '\0'; - } - - if (r == -1) { - perror (filename); - free (content); - return NULL; - } - - if (close (fd) == -1) { - perror (filename); - free (content); - return NULL; - } - - return content; -} - int -do_edit (const char *cmd, int argc, char *argv[]) +run_edit (const char *cmd, size_t argc, char *argv[]) { - char filename[] = "/tmp/guestfishXXXXXX"; + TMP_TEMPLATE_ON_STACK (filename); char buf[256]; const char *editor; - char *content, *content_new; + char *remotefilename; + struct stat oldstat, newstat; int r, fd; if (argc != 1) { @@ -88,9 +47,9 @@ do_edit (const char *cmd, int argc, char *argv[]) } /* Choose an editor. */ - if (strcasecmp (cmd, "vi") == 0) + if (STRCASEEQ (cmd, "vi")) editor = "vi"; - else if (strcasecmp (cmd, "emacs") == 0) + else if (STRCASEEQ (cmd, "emacs")) editor = "emacs -nw"; else { editor = getenv ("EDITOR"); @@ -98,30 +57,40 @@ do_edit (const char *cmd, int argc, char *argv[]) editor = "vi"; /* could be cruel here and choose ed(1) */ } + /* Handle 'win:...' prefix. */ + remotefilename = win_prefix (argv[0]); + if (remotefilename == NULL) + return -1; + /* Download the file and write it to a temporary. */ fd = mkstemp (filename); if (fd == -1) { perror ("mkstemp"); + free (remotefilename); return -1; } - if ((content = guestfs_cat (g, argv[0])) == NULL) { + snprintf (buf, sizeof buf, "/dev/fd/%d", fd); + + if (guestfs_download (g, remotefilename, buf) == -1) { close (fd); unlink (filename); + free (remotefilename); return -1; } - if (xwrite (fd, content, strlen (content)) == -1) { - close (fd); + if (close (fd) == -1) { + perror (filename); unlink (filename); - free (content); + free (remotefilename); return -1; } - if (close (fd) == -1) { + /* Get the old stat. */ + if (stat (filename, &oldstat) == -1) { perror (filename); unlink (filename); - free (content); + free (remotefilename); return -1; } @@ -133,36 +102,34 @@ do_edit (const char *cmd, int argc, char *argv[]) if (r != 0) { perror (buf); unlink (filename); - free (content); + free (remotefilename); return -1; } - /* Reload it. */ - size_t size; - content_new = load_file (filename, &size); - if (content_new == NULL) { + /* Get the new stat. */ + if (stat (filename, &newstat) == -1) { + perror (filename); unlink (filename); - free (content); + free (remotefilename); return -1; } - unlink (filename); - /* Changed? */ - if (strlen (content) == size && strncmp (content, content_new, size) == 0) { - free (content); - free (content_new); + if (oldstat.st_ctime == newstat.st_ctime && + oldstat.st_size == newstat.st_size) { + unlink (filename); + free (remotefilename); return 0; } /* Write new content. */ - if (guestfs_write_file (g, argv[0], content_new, size) == -1) { - free (content); - free (content_new); + if (guestfs_upload (g, filename, remotefilename) == -1) { + unlink (filename); + free (remotefilename); return -1; } - free (content); - free (content_new); + unlink (filename); + free (remotefilename); return 0; }