[or-cvs] Some platforms have weird translations when you open files ...

Nick Mathewson nickm at seul.org
Wed Sep 8 07:16:36 UTC 2004


Update of /home/or/cvsroot/src/common
In directory moria.mit.edu:/tmp/cvs-serv29891/src/common

Modified Files:
	crypto.c util.c util.h 
Log Message:
Some platforms have weird translations when you open files in "test" mode; make read/write_str_to_file aware.

Index: crypto.c
===================================================================
RCS file: /home/or/cvsroot/src/common/crypto.c,v
retrieving revision 1.102
retrieving revision 1.103
diff -u -d -r1.102 -r1.103
--- crypto.c	24 Aug 2004 21:57:10 -0000	1.102
+++ crypto.c	8 Sep 2004 07:16:33 -0000	1.103
@@ -345,7 +345,7 @@
   tor_assert(env && keyfile);
 
   /* open the keyfile */
-  f_pr=fopen(keyfile,"rb");
+  f_pr=fopen(keyfile,"r");
   if (!f_pr)
     return -1;
 
@@ -449,7 +449,7 @@
   s = tor_malloc(len+1);
   strncpy(s, cp, len);
   s[len] = '\0';
-  r = write_str_to_file(fname, s);
+  r = write_str_to_file(fname, s, 0);
   BIO_free(bio);
   free(s);
   return r;

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/src/common/util.c,v
retrieving revision 1.131
retrieving revision 1.132
diff -u -d -r1.131 -r1.132
--- util.c	2 Sep 2004 18:25:50 -0000	1.131
+++ util.c	8 Sep 2004 07:16:33 -0000	1.132
@@ -119,6 +119,10 @@
 #include "strlcat.c"
 #endif
 
+#ifndef O_BINARY
+#define O_BINARY 0
+#endif
+
 /** Allocate a chunk of <b>size</b> bytes of memory, and return a pointer to
  * result.  On error, log and terminate the process.  (Same as malloc(size),
  * but never returns NULL.)
@@ -1459,33 +1463,29 @@
  * This function replaces the old file atomically, if possible.
  */
 int
-write_str_to_file(const char *fname, const char *str)
+write_str_to_file(const char *fname, const char *str, int bin)
 {
   char tempname[1024];
   int fd;
-  FILE *file;
+  size_t len;
   if ((strlcpy(tempname,fname,1024) >= 1024) ||
       (strlcat(tempname,".tmp",1024) >= 1024)) {
     log(LOG_WARN, "Filename %s.tmp too long (>1024 chars)", fname);
     return -1;
   }
-  if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC, 0600)) < 0) {
+  if ((fd = open(tempname, O_WRONLY|O_CREAT|O_TRUNC|(bin?O_BINARY:0), 0600))
+      < 0) {
     log(LOG_WARN, "Couldn't open %s for writing: %s", tempname,
         strerror(errno));
     return -1;
   }
-  if (!(file = fdopen(fd, "w"))) {
-    log(LOG_WARN, "Couldn't fdopen %s for writing: %s", tempname,
-        strerror(errno));
-    close(fd);
-    return -1;
-  }
-  if (fputs(str,file) == EOF) {
+  len = strlen(str);
+  if (write_all(fd, str, len, 0) != len) {
     log(LOG_WARN, "Error writing to %s: %s", tempname, strerror(errno));
-    fclose(file);
+    close(fd);
     return -1;
   }
-  if (fclose(file) == EOF) {
+  if (close(fd)) {
     log(LOG_WARN,"Error flushing to %s: %s", tempname, strerror(errno));
     return -1;
   }
@@ -1521,7 +1521,7 @@
 /** Read the contents of <b>filename</b> into a newly allocated string; return the
  * string on success or NULL on failure.
  */
-char *read_file_to_str(const char *filename) {
+char *read_file_to_str(const char *filename, int bin) {
   int fd; /* router file */
   struct stat statbuf;
   char *string;
@@ -1533,7 +1533,7 @@
     return NULL;
   }
 
-  fd = open(filename,O_RDONLY,0);
+  fd = open(filename,O_RDONLY|(bin?O_BINARY:0),0);
   if (fd<0) {
     log_fn(LOG_WARN,"Could not open %s.",filename);
     return NULL;

Index: util.h
===================================================================
RCS file: /home/or/cvsroot/src/common/util.h,v
retrieving revision 1.88
retrieving revision 1.89
diff -u -d -r1.88 -r1.89
--- util.h	2 Sep 2004 18:25:50 -0000	1.88
+++ util.h	8 Sep 2004 07:16:33 -0000	1.89
@@ -219,8 +219,8 @@
 
 file_status_t file_status(const char *filename);
 int check_private_dir(const char *dirname, int create);
-int write_str_to_file(const char *fname, const char *str);
-char *read_file_to_str(const char *filename);
+int write_str_to_file(const char *fname, const char *str, int bin);
+char *read_file_to_str(const char *filename, int bin);
 int parse_line_from_file(char *line, int maxlen, FILE *f, char **key_out, char **value_out);
 char *expand_filename(const char *filename);
 int replace_file(const char *from, const char *to);



More information about the tor-commits mailing list