summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRichard Jones <rjones@redhat.com>2010-09-09 14:11:20 +0100
committerRichard W.M. Jones <rjones@redhat.com>2010-10-23 15:09:09 +0100
commit595c74b8f1b979ca64e6169de38d776d13a46885 (patch)
tree9b322efcc42fec396150014648168c824324ab62
parent7a5a9886a33c2f0294d4cae3d9a0c99c47acf695 (diff)
downloadlibguestfs-595c74b8f1b979ca64e6169de38d776d13a46885.tar.gz
libguestfs-595c74b8f1b979ca64e6169de38d776d13a46885.tar.xz
libguestfs-595c74b8f1b979ca64e6169de38d776d13a46885.zip
fish: Fix 'edit' command to work with any file.
(cherry picked from commit b5c287bcd456bdb02d8ec0443483df34f4fd6b5d)
-rw-r--r--fish/edit.c86
-rw-r--r--fish/guestfish.pod3
2 files changed, 19 insertions, 70 deletions
diff --git a/fish/edit.c b/fish/edit.c
index a010057d..10950f93 100644
--- a/fish/edit.c
+++ b/fish/edit.c
@@ -24,62 +24,20 @@
#include <unistd.h>
#include <fcntl.h>
#include <inttypes.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#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[])
{
char filename[] = "/tmp/guestfishXXXXXX";
char buf[256];
const char *editor;
- char *content, *content_new;
+ struct stat oldstat, newstat;
int r, fd;
if (argc != 1) {
@@ -105,23 +63,24 @@ do_edit (const char *cmd, int argc, char *argv[])
return -1;
}
- if ((content = guestfs_cat (g, argv[0])) == NULL) {
+ snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
+
+ if (guestfs_download (g, argv[0], buf) == -1) {
close (fd);
unlink (filename);
return -1;
}
- if (xwrite (fd, content, strlen (content)) == -1) {
- close (fd);
+ if (close (fd) == -1) {
+ perror (filename);
unlink (filename);
- free (content);
return -1;
}
- if (close (fd) == -1) {
+ /* Get the old stat. */
+ if (stat (filename, &oldstat) == -1) {
perror (filename);
unlink (filename);
- free (content);
return -1;
}
@@ -133,36 +92,29 @@ do_edit (const char *cmd, int argc, char *argv[])
if (r != 0) {
perror (buf);
unlink (filename);
- free (content);
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);
return -1;
}
- unlink (filename);
-
/* Changed? */
- if (strlen (content) == size && STREQLEN (content, content_new, size)) {
- free (content);
- free (content_new);
+ if (oldstat.st_ctime == newstat.st_ctime &&
+ oldstat.st_size == newstat.st_size) {
+ unlink (filename);
return 0;
}
/* Write new content. */
- if (guestfs_write (g, argv[0], content_new, size) == -1) {
- free (content);
- free (content_new);
+ if (guestfs_upload (g, filename, argv[0]) == -1) {
+ unlink (filename);
return -1;
}
- free (content);
- free (content_new);
+ unlink (filename);
return 0;
}
diff --git a/fish/guestfish.pod b/fish/guestfish.pod
index 5737c464..e5ce4928 100644
--- a/fish/guestfish.pod
+++ b/fish/guestfish.pod
@@ -709,9 +709,6 @@ The editor is C<$EDITOR>. However if you use the alternate
commands C<vi> or C<emacs> you will get those corresponding
editors.
-NOTE: This will not work reliably for large files
-(> 2 MB) or binary files containing \0 bytes.
-
=head2 glob
glob command args...