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

[or-cvs] Backport windows stat("foo\\") fix.



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

Modified Files:
      Tag: tor-0_0_9-patches
	util.c 
Log Message:
Backport windows stat("foo\\") fix.

Index: util.c
===================================================================
RCS file: /home/or/cvsroot/tor/src/common/util.c,v
retrieving revision 1.195.2.1
retrieving revision 1.195.2.2
diff -u -d -r1.195.2.1 -r1.195.2.2
--- util.c	4 Jan 2005 06:23:18 -0000	1.195.2.1
+++ util.c	16 Mar 2005 00:14:39 -0000	1.195.2.2
@@ -723,13 +723,37 @@
  *    Filesystem operations.
  */
 
+/** Clean up <b>name</b> so that we can use it in a call to "stat".  On Unix,
+ * we do nothing.  On Windows, we remove a trailing slash, unless the path is
+ * the root of a disk. */
+static void
+clean_name_for_stat(char *name)
+{
+#ifdef MS_WINDOWS
+  size_t len = strlen(name);
+  if (!len)
+    return;
+  if (name[len-1]=='\\' || name[len-1]=='/') {
+    if (len == 1 || (len==3 && name[1]==':'))
+      return;
+    name[len-1]='\0';
+  }
+#endif
+}
+
 /** Return FN_ERROR if filename can't be read, FN_NOENT if it doesn't
  * exist, FN_FILE if it is a regular file, or FN_DIR if it's a
  * directory. */
 file_status_t file_status(const char *fname)
 {
   struct stat st;
-  if (stat(fname, &st)) {
+  char *f;
+  int r;
+  f = tor_strdup(fname);
+  clean_name_for_stat(f);
+  r = stat(f, &st);
+  tor_free(f);
+  if (r) {
     if (errno == ENOENT) {
       return FN_NOENT;
     }
@@ -752,8 +776,13 @@
 {
   int r;
   struct stat st;
+  char *f;
   tor_assert(dirname);
-  if (stat(dirname, &st)) {
+  f = tor_strdup(dirname);
+  clean_name_for_stat(f);
+  r = stat(f, &st);
+  tor_free(f);
+  if (r) {
     if (errno != ENOENT) {
       log(LOG_WARN, "Directory %s cannot be read: %s", dirname,
           strerror(errno));
@@ -861,12 +890,16 @@
 char *read_file_to_str(const char *filename, int bin) {
   int fd; /* router file */
   struct stat statbuf;
-  char *string;
+  char *string, *f;
   int r;
 
   tor_assert(filename);
 
-  if (stat(filename, &statbuf) < 0) {
+  f = tor_strdup(filename);
+  clean_name_for_stat(f);
+  r = stat(f, &statbuf);
+  tor_free(f);
+  if (r < 0) {
     log_fn(LOG_INFO,"Could not stat %s.",filename);
     return NULL;
   }