[Libguestfs] [PATCH 09/13] fish: edit: bring the fast-time-edit protection

Pino Toscano ptoscano at redhat.com
Thu Aug 28 13:21:12 UTC 2014


Bring from virt-edit the small hack of putting the ctime of the
temporary file few seconds back to notice file changes also on fast
editing.
---
 fish/file-edit.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/fish/file-edit.c b/fish/file-edit.c
index 3a9dd2d..e2298eb 100644
--- a/fish/file-edit.c
+++ b/fish/file-edit.c
@@ -31,6 +31,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <assert.h>
+#include <utime.h>
 
 #include "guestfs-internal-frontend.h"
 
@@ -49,6 +50,7 @@ edit_file_editor (guestfs_h *g, const char *filename, const char *editor,
   CLEANUP_FREE char *cmd = NULL;
   struct stat oldstat, newstat;
   int r, fd;
+  struct utimbuf times;
 
   /* Download the file and write it to a temporary. */
   if (asprintf (&tmpfilename, "%s/libguestfsXXXXXX", tmpdir) == -1) {
@@ -74,6 +76,23 @@ edit_file_editor (guestfs_h *g, const char *filename, const char *editor,
     return -1;
   }
 
+  /* Set the time back a few seconds on the original file.  This is so
+   * that if the user is very fast at editing, or if EDITOR is an
+   * automatic editor, then the edit might happen within the 1 second
+   * granularity of mtime, and we would think the file hasn't changed.
+   */
+  if (stat (tmpfilename, &oldstat) == -1) {
+    perror (tmpfilename);
+    return -1;
+  }
+
+  times.actime = oldstat.st_atime - 5;
+  times.modtime = oldstat.st_mtime - 5;
+  if (utime (tmpfilename, &times) == -1) {
+    perror ("utimes");
+    return -1;
+  }
+
   /* Get the old stat. */
   if (stat (tmpfilename, &oldstat) == -1) {
     perror (tmpfilename);
-- 
1.9.3




More information about the Libguestfs mailing list