[Libguestfs] [PATCH 1/3] fish: Refactor error handling in the 'edit' command.
Richard W.M. Jones
rjones at redhat.com
Fri Feb 10 10:38:58 UTC 2012
--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
virt-p2v converts physical machines to virtual machines. Boot with a
live CD or over the network (PXE) and turn machines into Xen guests.
http://et.redhat.com/~rjones/virt-p2v
-------------- next part --------------
>From ab0a50971f494a2ace8d70c5cb7a925e7b9e9c1b Mon Sep 17 00:00:00 2001
From: "Richard W.M. Jones" <rjones at redhat.com>
Date: Fri, 10 Feb 2012 09:31:34 +0000
Subject: [PATCH 1/3] fish: Refactor error handling in the 'edit' command.
This is just code motion.
---
fish/edit.c | 41 +++++++++++++++++------------------------
1 files changed, 17 insertions(+), 24 deletions(-)
diff --git a/fish/edit.c b/fish/edit.c
index b615ef4..e0204b0 100644
--- a/fish/edit.c
+++ b/fish/edit.c
@@ -43,7 +43,7 @@ run_edit (const char *cmd, size_t argc, char *argv[])
if (argc != 1) {
fprintf (stderr, _("use '%s filename' to edit a file\n"), cmd);
- return -1;
+ goto error0;
}
/* Choose an editor. */
@@ -60,38 +60,31 @@ run_edit (const char *cmd, size_t argc, char *argv[])
/* Handle 'win:...' prefix. */
remotefilename = win_prefix (argv[0]);
if (remotefilename == NULL)
- return -1;
+ goto error0;
/* Download the file and write it to a temporary. */
fd = mkstemp (filename);
if (fd == -1) {
perror ("mkstemp");
- free (remotefilename);
- return -1;
+ goto error1;
}
snprintf (buf, sizeof buf, "/dev/fd/%d", fd);
if (guestfs_download (g, remotefilename, buf) == -1) {
close (fd);
- unlink (filename);
- free (remotefilename);
- return -1;
+ goto error2;
}
if (close (fd) == -1) {
perror (filename);
- unlink (filename);
- free (remotefilename);
- return -1;
+ goto error2;
}
/* Get the old stat. */
if (stat (filename, &oldstat) == -1) {
perror (filename);
- unlink (filename);
- free (remotefilename);
- return -1;
+ goto error2;
}
/* Edit it. */
@@ -101,17 +94,13 @@ run_edit (const char *cmd, size_t argc, char *argv[])
r = system (buf);
if (r != 0) {
perror (buf);
- unlink (filename);
- free (remotefilename);
- return -1;
+ goto error2;
}
/* Get the new stat. */
if (stat (filename, &newstat) == -1) {
perror (filename);
- unlink (filename);
- free (remotefilename);
- return -1;
+ goto error2;
}
/* Changed? */
@@ -123,13 +112,17 @@ run_edit (const char *cmd, size_t argc, char *argv[])
}
/* Write new content. */
- if (guestfs_upload (g, filename, remotefilename) == -1) {
- unlink (filename);
- free (remotefilename);
- return -1;
- }
+ if (guestfs_upload (g, filename, remotefilename) == -1)
+ goto error2;
unlink (filename);
free (remotefilename);
return 0;
+
+ error2:
+ unlink (filename);
+ error1:
+ free (remotefilename);
+ error0:
+ return -1;
}
--
1.7.9
More information about the Libguestfs
mailing list