summaryrefslogtreecommitdiff
path: root/src/basic/fileio.c
diff options
context:
space:
mode:
authorDaniel Mack <github@zonque.org>2015-11-12 13:57:38 +0100
committerDaniel Mack <github@zonque.org>2015-11-12 13:57:38 +0100
commit1f920a33814552fb7ddfba52e62ac9cbdf8184fe (patch)
treeb4e3d384216e3c88ab0a2b51d6b36cc933e3a97f /src/basic/fileio.c
parentaafdda22388a8a1b6966130ef4df0b59ee5fad72 (diff)
parent4de2402b603ea2f518f451d06f09e15aeae54fab (diff)
Merge pull request #1863 from poettering/network-man
man patch fix, and port journalctl --sync to use CLOCK_MONOTONIC timestamp files
Diffstat (limited to 'src/basic/fileio.c')
-rw-r--r--src/basic/fileio.c36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/basic/fileio.c b/src/basic/fileio.c
index 619dafb517..be6e327690 100644
--- a/src/basic/fileio.c
+++ b/src/basic/fileio.c
@@ -28,8 +28,10 @@
#include "fileio.h"
#include "fs-util.h"
#include "hexdecoct.h"
+#include "parse-util.h"
#include "path-util.h"
#include "random-util.h"
+#include "stdio-util.h"
#include "string-util.h"
#include "strv.h"
#include "umask-util.h"
@@ -1149,3 +1151,37 @@ int tempfn_random_child(const char *p, const char *extra, char **ret) {
*ret = path_kill_slashes(t);
return 0;
}
+
+int write_timestamp_file_atomic(const char *fn, usec_t n) {
+ char ln[DECIMAL_STR_MAX(n)+2];
+
+ /* Creates a "timestamp" file, that contains nothing but a
+ * usec_t timestamp, formatted in ASCII. */
+
+ if (n <= 0 || n >= USEC_INFINITY)
+ return -ERANGE;
+
+ xsprintf(ln, USEC_FMT "\n", n);
+
+ return write_string_file(fn, ln, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_ATOMIC);
+}
+
+int read_timestamp_file(const char *fn, usec_t *ret) {
+ _cleanup_free_ char *ln = NULL;
+ uint64_t t;
+ int r;
+
+ r = read_one_line_file(fn, &ln);
+ if (r < 0)
+ return r;
+
+ r = safe_atou64(ln, &t);
+ if (r < 0)
+ return r;
+
+ if (t <= 0 || t >= (uint64_t) USEC_INFINITY)
+ return -ERANGE;
+
+ *ret = (usec_t) t;
+ return 0;
+}