Zsh Mailing List Archive
Messages sorted by:
Reverse Date,
Date,
Thread,
Author
[PATCH 1/3] Move readhistfile() after flockhistfile().
---
Src/hist.c | 96 +++++++++++++++++++++++++++---------------------------
1 file changed, 48 insertions(+), 48 deletions(-)
diff --git a/Src/hist.c b/Src/hist.c
index f7e53de74..4e1f24afe 100644
--- a/Src/hist.c
+++ b/Src/hist.c
@@ -2525,6 +2525,54 @@ readhistline(int start, char **bufp, int *bufsiz, FILE *in)
return 0;
}
+#ifdef HAVE_FCNTL_H
+static int flock_fd = -1;
+
+/*
+ * Lock file using fcntl(). Return 0 on success, 1 on failure of
+ * locking mechanism, 2 on permanent failure (e.g. permission).
+ */
+
+static int
+flockhistfile(char *fn, int keep_trying)
+{
+ struct flock lck;
+ long sleep_us = 0x10000; /* about 67 ms */
+ time_t end_time;
+
+ if (flock_fd >= 0)
+ return 0; /* already locked */
+
+ if ((flock_fd = open(unmeta(fn), O_RDWR | O_NOCTTY)) < 0)
+ return errno == ENOENT ? 0 : 2; /* "successfully" locked missing file */
+
+ lck.l_type = F_WRLCK;
+ lck.l_whence = SEEK_SET;
+ lck.l_start = 0;
+ lck.l_len = 0; /* lock the whole file */
+
+ /*
+ * Timeout is ten seconds.
+ */
+ end_time = time(NULL) + (time_t)10;
+ while (fcntl(flock_fd, F_SETLKW, &lck) == -1) {
+ if (!keep_trying || time(NULL) >= end_time ||
+ /*
+ * Randomise wait to minimise clashes with shells exiting at
+ * the same time.
+ */
+ !zsleep_random(sleep_us, end_time)) {
+ close(flock_fd);
+ flock_fd = -1;
+ return 1;
+ }
+ sleep_us <<= 1;
+ }
+
+ return 0;
+}
+#endif
+
/**/
void
readhistfile(char *fn, int err, int readflags)
@@ -2721,54 +2769,6 @@ readhistfile(char *fn, int err, int readflags)
zleentry(ZLE_CMD_SET_HIST_LINE, curhist);
}
-#ifdef HAVE_FCNTL_H
-static int flock_fd = -1;
-
-/*
- * Lock file using fcntl(). Return 0 on success, 1 on failure of
- * locking mechanism, 2 on permanent failure (e.g. permission).
- */
-
-static int
-flockhistfile(char *fn, int keep_trying)
-{
- struct flock lck;
- long sleep_us = 0x10000; /* about 67 ms */
- time_t end_time;
-
- if (flock_fd >= 0)
- return 0; /* already locked */
-
- if ((flock_fd = open(unmeta(fn), O_RDWR | O_NOCTTY)) < 0)
- return errno == ENOENT ? 0 : 2; /* "successfully" locked missing file */
-
- lck.l_type = F_WRLCK;
- lck.l_whence = SEEK_SET;
- lck.l_start = 0;
- lck.l_len = 0; /* lock the whole file */
-
- /*
- * Timeout is ten seconds.
- */
- end_time = time(NULL) + (time_t)10;
- while (fcntl(flock_fd, F_SETLKW, &lck) == -1) {
- if (!keep_trying || time(NULL) >= end_time ||
- /*
- * Randomise wait to minimise clashes with shells exiting at
- * the same time.
- */
- !zsleep_random(sleep_us, end_time)) {
- close(flock_fd);
- flock_fd = -1;
- return 1;
- }
- sleep_us <<= 1;
- }
-
- return 0;
-}
-#endif
-
/**/
void
savehistfile(char *fn, int err, int writeflags)
--
2.20.1
Messages sorted by:
Reverse Date,
Date,
Thread,
Author