summaryrefslogtreecommitdiff
path: root/src/bin/psql/command.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/command.c')
-rw-r--r--src/bin/psql/command.c27
1 files changed, 24 insertions, 3 deletions
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index 2ad76b41fc2..710faaca0af 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -17,6 +17,7 @@
#ifdef HAVE_PWD_H
#include <pwd.h>
#endif
+#include <utime.h>
#ifndef WIN32
#include <sys/types.h> /* for umask() */
#include <sys/stat.h> /* for stat() */
@@ -2413,7 +2414,6 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
const char *fname;
bool error = false;
int fd;
-
struct stat before,
after;
@@ -2438,13 +2438,13 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
!ret ? strerror(errno) : "");
return false;
}
+#endif
/*
* No canonicalize_path() here. EDIT.EXE run from CMD.EXE prepends the
* current directory to the supplied path unless we use only
* backslashes, so we do that.
*/
-#endif
#ifndef WIN32
snprintf(fnametmp, sizeof(fnametmp), "%s%spsql.edit.%d.sql", tmpdir,
"/", (int) getpid());
@@ -2493,6 +2493,24 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
psql_error("%s: %s\n", fname, strerror(errno));
error = true;
}
+ else
+ {
+ struct utimbuf ut;
+
+ /*
+ * Try to set the file modification time of the temporary file
+ * a few seconds in the past. Otherwise, the low granularity
+ * (one second, or even worse on some filesystems) that we can
+ * portably measure with stat(2) could lead us to not
+ * recognize a modification, if the user typed very quickly.
+ *
+ * This is a rather unlikely race condition, so don't error
+ * out if the utime(2) call fails --- that would make the cure
+ * worse than the disease.
+ */
+ ut.modtime = ut.actime = time(NULL) - 2;
+ (void) utime(fname, &ut);
+ }
}
}
@@ -2512,7 +2530,10 @@ do_edit(const char *filename_arg, PQExpBuffer query_buf,
error = true;
}
- if (!error && before.st_mtime != after.st_mtime)
+ /* file was edited if the size or modification time has changed */
+ if (!error &&
+ (before.st_size != after.st_size ||
+ before.st_mtime != after.st_mtime))
{
stream = fopen(fname, PG_BINARY_R);
if (!stream)