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

[or-cvs] Two new functions like write_bytes_to_file: one takes a lis...



Update of /home/or/cvsroot/tor/src/common
In directory moria:/tmp/cvs-serv21569/src/common

Modified Files:
	util.c util.h 
Log Message:
Two new functions like write_bytes_to_file: one takes a list of byte-and-len structs; one appends.

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/util.c,v
retrieving revision 1.222
retrieving revision 1.223
diff -u -d -r1.222 -r1.223
--- util.c	10 Sep 2005 02:34:32 -0000	1.222
+++ util.c	13 Sep 2005 06:19:31 -0000	1.223
@@ -884,10 +884,10 @@
   return write_bytes_to_file(fname, str, strlen(str), bin);
 }
 
-/** As write_str_to_file, but does not assume a NUL-terminated *
- * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
-int write_bytes_to_file(const char *fname, const char *str, size_t len,
-                        int bin)
+/* DOCDOC */
+static int
+write_chunks_to_file_impl(const char *fname, const smartlist_t *chunks,
+                          int open_flags)
 {
   size_t tempname_len;
   char *tempname;
@@ -896,29 +896,38 @@
   tempname_len = strlen(fname)+16;
   tor_assert(tempname_len > strlen(fname)); /*check for overflow*/
   tempname = tor_malloc(tempname_len);
-  if (tor_snprintf(tempname, tempname_len, "%s.tmp", fname)<0) {
-    log(LOG_WARN, "Failed to generate filename");
-    goto err;
+  if (open_flags & O_APPEND) {
+    strlcpy(tempname, fname, tempname_len);
+  } else {
+    if (tor_snprintf(tempname, tempname_len, "%s.tmp", fname)<0) {
+      log(LOG_WARN, "Failed to generate filename");
+      goto err;
+    }
   }
-  if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC|(bin?O_BINARY:O_TEXT), 0600))
+  if ((fd = open(tempname, open_flags, 0600))
       < 0) {
     log(LOG_WARN, "Couldn't open \"%s\" for writing: %s", tempname,
         strerror(errno));
     goto err;
   }
-  result = write_all(fd, str, len, 0);
-  if (result < 0 || (size_t)result != len) {
-    log(LOG_WARN, "Error writing to \"%s\": %s", tempname, strerror(errno));
-    close(fd);
-    goto err;
-  }
+  SMARTLIST_FOREACH(chunks, sized_chunk_t *, chunk,
+  {
+    result = write_all(fd, chunk->bytes, chunk->len, 0);
+    if (result < 0 || (size_t)result != chunk->len) {
+      log(LOG_WARN, "Error writing to \"%s\": %s", tempname, strerror(errno));
+      close(fd);
+      goto err;
+    }
+  });
   if (close(fd)) {
     log(LOG_WARN,"Error flushing to \"%s\": %s", tempname, strerror(errno));
     goto err;
   }
-  if (replace_file(tempname, fname)) {
-    log(LOG_WARN, "Error replacing \"%s\": %s", fname, strerror(errno));
-    goto err;
+  if (!(open_flags & O_APPEND)) {
+    if (replace_file(tempname, fname)) {
+      log(LOG_WARN, "Error replacing \"%s\": %s", fname, strerror(errno));
+      goto err;
+    }
   }
   tor_free(tempname);
   return 0;
@@ -927,6 +936,45 @@
   return -1;
 }
 
+/* DOCDOC */
+int
+write_chunks_to_file(const char *fname, const smartlist_t *chunks, int bin)
+{
+  int flags = O_WRONLY|O_CREAT|O_TRUNC|(bin?O_BINARY:O_TEXT);
+  return write_chunks_to_file_impl(fname, chunks, flags);
+}
+
+/** As write_str_to_file, but does not assume a NUL-terminated *
+ * string. Instead, we write <b>len</b> bytes, starting at <b>str</b>. */
+int
+write_bytes_to_file(const char *fname, const char *str, size_t len,
+                    int bin)
+{
+  int flags = O_WRONLY|O_CREAT|O_TRUNC|(bin?O_BINARY:O_TEXT);
+  int r;
+  sized_chunk_t c = { str, len };
+  smartlist_t *chunks = smartlist_create();
+  smartlist_add(chunks, &c);
+  r = write_chunks_to_file_impl(fname, chunks, flags);
+  smartlist_free(chunks);
+  return r;
+}
+
+/* DOCDOC */
+int
+append_bytes_to_file(const char *fname, const char *str, size_t len,
+                     int bin)
+{
+  int flags = O_WRONLY|O_CREAT|O_APPEND|(bin?O_BINARY:O_TEXT);
+  int r;
+  sized_chunk_t c = { str, len };
+  smartlist_t *chunks = smartlist_create();
+  smartlist_add(chunks, &c);
+  r = write_chunks_to_file_impl(fname, chunks, flags);
+  smartlist_free(chunks);
+  return r;
+}
+
 /** Read the contents of <b>filename</b> into a newly allocated
  * string; return the string on success or NULL on failure.
  */

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/util.h,v
retrieving revision 1.138
retrieving revision 1.139
diff -u -d -r1.138 -r1.139
--- util.h	8 Sep 2005 18:33:51 -0000	1.138
+++ util.h	13 Sep 2005 06:19:31 -0000	1.139
@@ -121,6 +121,16 @@
 int write_str_to_file(const char *fname, const char *str, int bin);
 int write_bytes_to_file(const char *fname, const char *str, size_t len,
                         int bin);
+typedef struct sized_chunk_t {
+  const char *bytes;
+  size_t len;
+} sized_chunk_t;
+struct smartlist_t;
+int write_chunks_to_file(const char *fname, const struct smartlist_t *chunks,
+                         int bin);
+int append_bytes_to_file(const char *fname, const char *str, size_t len,
+                         int bin);
+
 char *read_file_to_str(const char *filename, int bin);
 char *parse_line_from_str(char *line, char **key_out, char **value_out);
 char *expand_filename(const char *filename);