[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] Introduce write_str_if_not_equal()



commit c212578bf05138801597dcce23f52780c9b7dbb3
Author: Neel Chauhan <neel@xxxxxxxxx>
Date:   Fri Jul 24 18:14:15 2020 -0700

    Introduce write_str_if_not_equal()
---
 src/feature/relay/router.c | 14 +++++---------
 src/lib/fs/files.c         | 20 ++++++++++++++++++++
 src/lib/fs/files.h         |  2 ++
 3 files changed, 27 insertions(+), 9 deletions(-)

diff --git a/src/feature/relay/router.c b/src/feature/relay/router.c
index 48f53a263d..25263468c8 100644
--- a/src/feature/relay/router.c
+++ b/src/feature/relay/router.c
@@ -870,15 +870,11 @@ router_write_fingerprint(int hashed, int ed25519_identity)
   tor_asprintf(&fingerprint_line, "%s %s\n", options->Nickname, fingerprint);
 
   /* Check whether we need to write the (hashed-)fingerprint file. */
-
-  cp = read_file_to_str(keydir, RFTS_IGNORE_MISSING, NULL);
-  if (!cp || strcmp(cp, fingerprint_line)) {
-    if (write_str_to_file(keydir, fingerprint_line, 0)) {
-      log_err(LD_FS, "Error writing %s%s line to file",
-              hashed ? "hashed " : "",
-              ed25519_identity ? "ed25519 identity" : "fingerprint");
-      goto done;
-    }
+  if (write_str_if_not_equal(keydir, fingerprint_line)) {
+    log_err(LD_FS, "Error writing %s%s line to file",
+            hashed ? "hashed " : "",
+            ed25519_identity ? "ed25519 identity" : "fingerprint");
+    goto done;
   }
 
   log_notice(LD_GENERAL, "Your Tor %s identity key %s fingerprint is '%s %s'",
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. */



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits