summaryrefslogtreecommitdiff
path: root/src/lib
diff options
context:
space:
mode:
authorGeorge Kadianakis <desnacked@riseup.net>2020-08-05 14:57:20 +0300
committerGeorge Kadianakis <desnacked@riseup.net>2020-08-05 14:57:20 +0300
commitafd88ee87fa27fd7f9d9f63222ac472cdd975f68 (patch)
tree884b7f2a65f1ac40a68d565361b64e4249c67061 /src/lib
parent04926126ee7fd9bbaefe5890d238fd8156124a5b (diff)
parentfcf4954cc83570818d6be15f2117e31cc3eda34e (diff)
downloadtor-afd88ee87fa27fd7f9d9f63222ac472cdd975f68.tar.gz
tor-afd88ee87fa27fd7f9d9f63222ac472cdd975f68.zip
Merge remote-tracking branch 'tor-gitlab/mr/88'
Diffstat (limited to 'src/lib')
-rw-r--r--src/lib/fs/files.c20
-rw-r--r--src/lib/fs/files.h2
2 files changed, 22 insertions, 0 deletions
diff --git a/src/lib/fs/files.c b/src/lib/fs/files.c
index a0b5a40aac..189d2cb646 100644
--- a/src/lib/fs/files.c
+++ b/src/lib/fs/files.c
@@ -718,6 +718,26 @@ read_file_to_str, (const char *filename, int flags, struct stat *stat_out))
return string;
}
+/** Attempt to read a file <b>fname</b>. If the file's contents is
+ * equal to the string <b>str</b>, return 0. Otherwise, attempt to
+ * overwrite the file with the contents of <b>str</b> and return
+ * the value of write_str_to_file().
+ */
+int
+write_str_to_file_if_not_equal(const char *fname, const char *str)
+{
+ char *fstr = read_file_to_str(fname, RFTS_IGNORE_MISSING, NULL);
+ int rv;
+
+ if (!fstr || strcmp(str, fstr)) {
+ rv = write_str_to_file(fname, str, 0);
+ } else {
+ rv = 0;
+ }
+ tor_free(fstr);
+ return rv;
+}
+
#if !defined(HAVE_GETDELIM) || defined(TOR_UNIT_TESTS)
#include "ext/getdelim.c"
#endif
diff --git a/src/lib/fs/files.h b/src/lib/fs/files.h
index a109cd6248..62b79c4cd8 100644
--- a/src/lib/fs/files.h
+++ b/src/lib/fs/files.h
@@ -91,6 +91,8 @@ int append_bytes_to_file(const char *fname, const char *str, size_t len,
int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
int bin);
+int write_str_to_file_if_not_equal(const char *fname, const char *str);
+
/** Flag for read_file_to_str: open the file in binary mode. */
#define RFTS_BIN 1
/** Flag for read_file_to_str: it's okay if the file doesn't exist. */