]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
Make check_private_dir trimodal (check/create/ignore), not bimodal (create/ignore).
authorNick Mathewson <nickm@torproject.org>
Tue, 9 Nov 2004 07:12:31 +0000 (07:12 +0000)
committerNick Mathewson <nickm@torproject.org>
Tue, 9 Nov 2004 07:12:31 +0000 (07:12 +0000)
svn:r2733

src/common/util.c
src/common/util.h
src/or/config.c
src/or/rendservice.c
src/or/router.c

index 292b7cfd5b460fc2d853ac393094bc58ca701423..8311f95be500959bba0872e3e2cbec57b06faddf 100644 (file)
@@ -701,9 +701,11 @@ file_status_t file_status(const char *fname)
 }
 
 /** Check whether dirname exists and is private.  If yes return 0.  If
- * it does not exist, and create is set, try to create it and return 0
- * on success.  Else return -1. */
-int check_private_dir(const char *dirname, int create)
+ * it does not exist, and check==CPD_CREATE is set, try to create it
+ * and return 0 on success. If it does not exist, and
+ * check==CPD_CHECK, and we think we can create it, return 0.  Else
+ * return -1. */
+int check_private_dir(const char *dirname, cpd_check_t check)
 {
   int r;
   struct stat st;
@@ -714,23 +716,26 @@ int check_private_dir(const char *dirname, int create)
           strerror(errno));
       return -1;
     }
-    if (!create) {
+    if (check == CPD_NONE) {
       log(LOG_WARN, "Directory %s does not exist.", dirname);
       return -1;
-    }
-    log(LOG_INFO, "Creating directory %s", dirname);
+    } else if (check == CPD_CREATE) {
+      log(LOG_INFO, "Creating directory %s", dirname);
 #ifdef MS_WINDOWS
-    r = mkdir(dirname);
+      r = mkdir(dirname);
 #else
-    r = mkdir(dirname, 0700);
+      r = mkdir(dirname, 0700);
 #endif
-    if (r) {
-      log(LOG_WARN, "Error creating directory %s: %s", dirname,
-          strerror(errno));
-      return -1;
-    } else {
-      return 0;
+      if (r) {
+        log(LOG_WARN, "Error creating directory %s: %s", dirname,
+            strerror(errno));
+        return -1;
+      }
     }
+
+    /* XXXX In the case where check==CPD_CHECK, we should look at the
+     * parent directory a little harder. */
+    return 0;
   }
   if (!(st.st_mode & S_IFDIR)) {
     log(LOG_WARN, "%s is not a directory", dirname);
index c925834daf5f9b52e5eb7ea9dc9d353a86203bb7..9f116b57963a3b633a58ae2f08c9fcf4d9ba6794 100644 (file)
@@ -88,7 +88,8 @@ int read_all(int fd, char *buf, size_t count, int isSocket);
 typedef enum { FN_ERROR, FN_NOENT, FN_FILE, FN_DIR} file_status_t;
 file_status_t file_status(const char *filename);
 
-int check_private_dir(const char *dirname, int create);
+typedef enum { CPD_NONE, CPD_CREATE, CPD_CHECK } cpd_check_t;
+int check_private_dir(const char *dirname, cpd_check_t check);
 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);
index cfa697093991773585cdbd93a697f82f7ae657be..cebff82d39d19d63f1cef13714ac2019c3307424 100644 (file)
@@ -241,9 +241,8 @@ options_act(void) {
     }
   }
 
-/*XXX in options_validate, we should check if this is going to fail */
   /* Ensure data directory is private; create if possible. */
-  if (check_private_dir(options->DataDirectory, 1) != 0) {
+  if (check_private_dir(options->DataDirectory, CPD_CREATE) != 0) {
     log_fn(LOG_ERR, "Couldn't access/create private data directory %s",
            options->DataDirectory);
     return -1;
@@ -1014,6 +1013,13 @@ options_validate(or_options_t *options)
   if (normalize_log_options(options))
     return -1;
 
+
+  if (options->DataDirectory &&
+      check_private_dir(options->DataDirectory, CPD_CHECK != 0)) {
+    log_fn(LOG_WARN, "Can't create directory %s", options->DataDirectory);
+    result = -1;
+  }
+
   /* Special case if no options are given. */
   if (!options->Logs) {
     options->Logs = config_line_prepend(NULL, "Log", "notice-err stdout");
index ebbc618d1b691e3d7c167cb517eadbdc1158a755..3bc1238d34f109598f598e3fc9d4a0287b9a081b 100644 (file)
@@ -295,7 +295,7 @@ int rend_service_load_keys(void)
     log_fn(LOG_INFO, "Loading hidden-service keys from '%s'", s->directory);
 
     /* Check/create directory */
-    if (check_private_dir(s->directory, 1) < 0)
+    if (check_private_dir(s->directory, CPD_CREATE) < 0)
       return -1;
 
     /* Load key */
index c47080e562334ebef50049180ebad824ab038873..75f2fe6a4111d79c2905fa054a6602049918ab2c 100644 (file)
@@ -259,12 +259,12 @@ int init_keys(void) {
   }
   /* Make sure DataDirectory exists, and is private. */
   datadir = options->DataDirectory;
-  if (check_private_dir(datadir, 1)) {
+  if (check_private_dir(datadir, CPD_CREATE)) {
     return -1;
   }
   /* Check the key directory. */
   tor_snprintf(keydir,sizeof(keydir),"%s/keys", datadir);
-  if (check_private_dir(keydir, 1)) {
+  if (check_private_dir(keydir, CPD_CREATE)) {
     return -1;
   }
   cp = keydir + strlen(keydir); /* End of string. */