Bring the backup extension feature from the perl-like file mode
implementation, although currently not making use of it.
---
fish/edit.c | 2 +-
fish/file-edit.c | 31 ++++++++++++++++++++++++++++++-
fish/file-edit.h | 4 +++-
3 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/fish/edit.c b/fish/edit.c
index 5b851e7..0170daf 100644
--- a/fish/edit.c
+++ b/fish/edit.c
@@ -58,7 +58,7 @@ run_edit (const char *cmd, size_t argc, char *argv[])
if (remotefilename == NULL)
return -1;
- r = edit_file_editor (g, remotefilename, editor);
+ r = edit_file_editor (g, remotefilename, editor, NULL);
return r;
}
diff --git a/fish/file-edit.c b/fish/file-edit.c
index 408d20e..8efa452 100644
--- a/fish/file-edit.c
+++ b/fish/file-edit.c
@@ -35,9 +35,12 @@
#include "guestfs-internal-frontend.h"
static char *generate_random_name (const char *filename);
+static char *generate_backup_name (const char *filename,
+ const char *backup_extension);
int
-edit_file_editor (guestfs_h *g, const char *filename, const char *editor)
+edit_file_editor (guestfs_h *g, const char *filename, const char *editor,
+ const char *backup_extension)
{
CLEANUP_FREE char *tmpdir = guestfs_get_tmpdir (g);
CLEANUP_UNLINK_FREE char *tmpfilename = NULL;
@@ -120,6 +123,17 @@ edit_file_editor (guestfs_h *g, const char *filename, const char
*editor)
GUESTFS_COPY_ATTRIBUTES_ALL, 1, -1) == -1)
return -1;
+ /* Backup or overwrite the file. */
+ if (backup_extension) {
+ CLEANUP_FREE char *backupname = NULL;
+
+ backupname = generate_backup_name (filename, backup_extension);
+ if (backupname == NULL)
+ return -1;
+
+ if (guestfs_mv (g, filename, backupname) == -1)
+ return -1;
+ }
if (guestfs_mv (g, newname, filename) == -1)
return -1;
@@ -159,3 +173,18 @@ generate_random_name (const char *filename)
return ret; /* caller will free */
}
+
+static char *
+generate_backup_name (const char *filename, const char *backup_extension)
+{
+ char *ret;
+
+ assert (backup_extension != NULL);
+
+ if (asprintf (&ret, "%s%s", filename, backup_extension) == -1) {
+ perror ("asprintf");
+ return NULL;
+ }
+
+ return ret; /* caller will free */
+}
diff --git a/fish/file-edit.h b/fish/file-edit.h
index eff1c7d..6622b3a 100644
--- a/fish/file-edit.h
+++ b/fish/file-edit.h
@@ -23,10 +23,12 @@
/**
* Edit 'filename' using the specified 'editor' application.
+ * If 'backup_extension' is not null, then a copy of 'filename' is saved
+ * with 'backup_extension' appended to its file name.
*
* Returns -1 for failure, 0 otherwise.
*/
extern int edit_file_editor (guestfs_h *g, const char *filename,
- const char *editor);
+ const char *editor, const char *backup_extension);
#endif
--
1.9.3