[tor-commits] [tor/master] Added no_tempfile parameter to write_chunks_to_file to do non-atomic writes. Implements #1376.

nickm at torproject.org nickm at torproject.org
Fri Oct 11 16:51:58 UTC 2013


commit 1bdb391ed0df979bc29ed1b62e7c0f3c9494a8d7
Author: Kevin Butler <haqkrs at gmail.com>
Date:   Sun Sep 1 00:24:07 2013 +0100

    Added no_tempfile parameter to write_chunks_to_file to do non-atomic writes. Implements #1376.
---
 changes/bug1376     |    2 ++
 src/common/util.c   |   13 ++++++++++---
 src/common/util.h   |    2 +-
 src/or/dirvote.c    |    2 +-
 src/or/routerlist.c |    4 ++--
 5 files changed, 16 insertions(+), 7 deletions(-)

diff --git a/changes/bug1376 b/changes/bug1376
new file mode 100644
index 0000000..631f2af
--- /dev/null
+++ b/changes/bug1376
@@ -0,0 +1,2 @@
+  o Minor bugfixes:
+    - Added additional argument to write_chunks_to_file to optionally skip using a temp file to do non-atomic writes. Implements ticket #1376.
\ No newline at end of file
diff --git a/src/common/util.c b/src/common/util.c
index 6e14a58..4e84d94 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -2190,12 +2190,19 @@ write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
   return -1;
 }
 
-/** Given a smartlist of sized_chunk_t, write them atomically to a file
- * <b>fname</b>, overwriting or creating the file as necessary. */
+/** Given a smartlist of sized_chunk_t, write them to a file
+ * <b>fname</b>, overwriting or creating the file as necessary. 
+ * If <b>no_tempfile</b> is 0 then the file will be written
+ * atomically. */
 int
-write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
+write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin, int no_tempfile)
 {
   int flags = OPEN_FLAGS_REPLACE|(bin?O_BINARY:O_TEXT);
+
+  if (no_tempfile) {
+    // O_APPEND stops write_chunks_to_file from using tempfiles
+    flags |= O_APPEND;
+  }
   return write_chunks_to_file_impl(fname, chunks, flags);
 }
 
diff --git a/src/common/util.h b/src/common/util.h
index 090243e..24428ad 100644
--- a/src/common/util.h
+++ b/src/common/util.h
@@ -365,7 +365,7 @@ typedef struct sized_chunk_t {
   size_t len;
 } sized_chunk_t;
 int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
-                         int bin);
+                         int bin, int no_tempfile);
 int append_bytes_to_file(const char *fname, const char *str, size_t len,
                          int bin);
 int write_bytes_to_new_file(const char *fname, const char *str, size_t len,
diff --git a/src/or/dirvote.c b/src/or/dirvote.c
index 12ceba8..456a033 100644
--- a/src/or/dirvote.c
+++ b/src/or/dirvote.c
@@ -3143,7 +3143,7 @@ dirvote_compute_consensuses(void)
     });
 
   votefile = get_datadir_fname("v3-status-votes");
-  write_chunks_to_file(votefile, votestrings, 0);
+  write_chunks_to_file(votefile, votestrings, 0, 0);
   tor_free(votefile);
   SMARTLIST_FOREACH(votestrings, sized_chunk_t *, c, tor_free(c));
   smartlist_free(votestrings);
diff --git a/src/or/routerlist.c b/src/or/routerlist.c
index 46da17e..3e8b9fb 100644
--- a/src/or/routerlist.c
+++ b/src/or/routerlist.c
@@ -428,7 +428,7 @@ trusted_dirs_flush_certs_to_disk(void)
   } DIGESTMAP_FOREACH_END;
 
   filename = get_datadir_fname("cached-certs");
-  if (write_chunks_to_file(filename, chunks, 0)) {
+  if (write_chunks_to_file(filename, chunks, 0, 0)) {
     log_warn(LD_FS, "Error writing certificates to disk.");
   }
   tor_free(filename);
@@ -1048,7 +1048,7 @@ router_rebuild_store(int flags, desc_store_t *store)
       smartlist_add(chunk_list, c);
   } SMARTLIST_FOREACH_END(sd);
 
-  if (write_chunks_to_file(fname_tmp, chunk_list, 1)<0) {
+  if (write_chunks_to_file(fname_tmp, chunk_list, 1, 1)<0) {
     log_warn(LD_FS, "Error writing router store to disk.");
     goto done;
   }





More information about the tor-commits mailing list