Browse Source

Use tor_listdir in test.c instead of duplicating ode.

svn:r4981
Nick Mathewson 20 years ago
parent
commit
d26523e089
2 changed files with 19 additions and 49 deletions
  1. 6 0
      src/common/compat.h
  2. 13 49
      src/or/test.c

+ 6 - 0
src/common/compat.h

@@ -128,6 +128,12 @@ struct tm *tor_gmtime_r(const time_t *timep, struct tm *result);
 /* ===== File compatibility */
 int replace_file(const char *from, const char *to);
 
+#ifdef MS_WINDOWS
+#define PATH_SEPARATOR "\\"
+#else
+#define PATH_SEPARATOR "/"
+#endif
+
 /* ===== Net compatibility */
 #ifdef MS_WINDOWS
 /** On windows, you have to call close() on fds returned by open(),

+ 13 - 49
src/or/test.c

@@ -74,56 +74,21 @@ get_fname(const char *name)
 static void
 remove_directory(void)
 {
-#ifdef MS_WINDOWS
-  char *pattern;
-  HANDLE handle;
-  WIN32_FIND_DATA findData;
-
-  setup_directory();
-  pattern = tor_malloc(strlen(temp_dir)+16);
-  tor_snprintf(pattern, strlen(temp_dir)+16, "%s\\*", temp_dir);
-  handle = FindFirstFile(pattern, &findData);
-  if (handle == INVALID_HANDLE_VALUE) {
-    perror("Can't remove");
-    return;
+  smartlist_t *elements = tor_listdir(temp_dir);
+  if (elements) {
+    SMARTLIST_FOREACH(elements, const char *, cp,
+       {
+         size_t len = strlen(cp)+strlen(temp_dir)+16;
+         char *tmp = tor_malloc(len);
+         tor_snprintf(tmp, len, "%s"PATH_SEPARATOR"%s", temp_dir, cp);
+         unlink(tmp);
+         tor_free(tmp);
+       });
+    SMARTLIST_FOREACH(elements, char *, cp, tor_free(cp));
+    smartlist_free(elements);
   }
-  while (1) {
-    size_t dlen = strlen(findData.cFileName)+strlen(temp_dir)+16;
-    char *deleteable = tor_malloc(dlen);
-    tor_snprintf(deleteable, dlen, "%s\\%s", temp_dir, findData.cFileName);
-    unlink(deleteable);
-    tor_free(deleteable);
-    if (!FindNextFile(handle, &findData)) {
-      if (GetLastError() != ERROR_NO_MORE_FILES) {
-        perror("error reading dir");
-      }
-      break;
-    }
-  }
-  FindClose(handle);
-  tor_free(pattern);
-#else
-  DIR *dirp;
-  struct dirent *de;
-  setup_directory();
-  if (!(dirp = opendir(temp_dir))) {
-    perror("Can't open temporary directory to remove files");
-    return;
-  }
-  while ((de = readdir(dirp)) != NULL) {
-    /* Only "." and ".." start with ., since we don't create any dotfiles. */
-    if (de->d_name[0] == '.') continue;
-    if (unlink(get_fname(de->d_name))) {
-      printf("Couldn't remove temprorary file \"%s/%s\"",temp_dir,de->d_name);
-      perror("");
-    }
-#if 0
-    printf("==%s\n", de->d_name);
-#endif
-  }
-  closedir(dirp);
-#endif
   rmdir(temp_dir);
+  
 }
 
 static void
@@ -615,7 +580,6 @@ static int
 _compare_without_first_ch(const void *a, const void **b)
 {
   const char *s1 = a, *s2 = *b;
-  printf("%s v %s\n",s1, s2);
   return strcasecmp(s1+1, s2);
 }