checking in the recent efforts at optimizing clam
[feisty_meow.git] / nucleus / library / filesystem / directory.cpp
index e4e3adcdb92ed7bef3e23995145ddcc7d4cffd63..4f6dfc2227d1376ffdf9d565a984467a4d40df13 100644 (file)
@@ -15,7 +15,6 @@
 #include "directory.h"
 #include "filename.h"
 
-#include <algorithms/shell_sort.h>
 #include <application/windoze_helper.h>
 #include <basis/astring.h>
 #include <basis/contracts.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <sys/stat.h>
-#ifdef __UNIX__
+
+#include "../algorithms/sorts.h"
+#if defined(__UNIX__) || defined(__GNU_WINDOWS__)
   #include <dirent.h>
   #include <fnmatch.h>
   #include <string.h>
   #include <unistd.h>
 #endif
-#ifdef __WIN32__
-  #include <direct.h>
-#endif
+//#ifdef _MSC_VER
+//  #include <direct.h>
+//#endif
 
 /*
 #ifdef __WIN32__
@@ -47,6 +48,9 @@
 #endif
 */
 
+//#define DEBUG_DIRECTORY
+  // uncomment for noisier runs.
+
 #undef LOG
 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
 
@@ -98,26 +102,26 @@ astring directory::absolute_path(const astring &rel_path)
 {
   char abs_path[MAX_ABS_PATH + 1];
   abs_path[0] = '\0';
-#ifdef __WIN32__
-  if (!_fullpath(abs_path, rel_path.s(), MAX_ABS_PATH)) return "";
-  return abs_path;
-#else
+//#ifdef _MSC_VER
+//  if (!_fullpath(abs_path, rel_path.s(), MAX_ABS_PATH)) return "";
+//  return abs_path;
+//#else
   if (!realpath(rel_path.s(), abs_path)) return "";
   return abs_path;
-#endif
+//#endif
 }
 
 astring directory::current()
 {
   astring to_return(".");  // failure result.
-#ifdef __WIN32__
-  flexichar buffer[MAX_ABS_PATH + 1] = { '\0' };
-  GetCurrentDirectory(MAX_ABS_PATH, buffer);
-  to_return = from_unicode_temp(buffer);
-#else
+//#ifdef _MSC_VER
+//  flexichar buffer[MAX_ABS_PATH + 1] = { '\0' };
+//  GetCurrentDirectory(MAX_ABS_PATH, buffer);
+//  to_return = from_unicode_temp(buffer);
+//#else
   char buffer[MAX_ABS_PATH + 1] = { '\0' };
   if (realpath(".", buffer)) to_return = buffer;
-#endif
+//#endif
   return to_return;
 }
 
@@ -148,7 +152,8 @@ bool directory::rescan()
   _folders->reset();
   astring cur_dir = ".";
   astring par_dir = "..";
-#ifdef __WIN32__
+/*
+#ifdef _MSC_VER
   // start reading the directory.
   WIN32_FIND_DATA wfd;
   astring real_path_spec = *_path + "/" + *_pattern;
@@ -161,10 +166,11 @@ bool directory::rescan()
     if (!strcmp(filename_transcoded.s(), par_dir.s())) continue;
 
 #ifdef UNICODE
-//temp
-    to_unicode_persist(fudgemart, filename_transcoded);
-    if (memcmp((wchar_t*)fudgemart, wfd.cFileName, wcslen(wfd.cFileName)*2))
+  #ifdef DEBUG_DIRECTORY
+    to_unicode_persist(kludgemart, filename_transcoded);
+    if (memcmp((wchar_t*)kludgemart, wfd.cFileName, wcslen(wfd.cFileName)*2))
       printf("failed to compare the string before and after transcoding\n");
+  #endif
 #endif
 
 //wprintf(to_unicode_temp("file is %ls\n"), (wchar_t*)to_unicode_temp(filename_transcoded));
@@ -178,18 +184,20 @@ bool directory::rescan()
       _files->concatenate(filename_transcoded);
 
 #ifdef UNICODE
-      to_unicode_persist(fudgemart2, temp_name.raw());
-      FILE *fpjunk = _wfopen(fudgemart2, to_unicode_temp("rb"));
+  #ifdef DEBUG_DIRECTORY
+      to_unicode_persist(kludgemart2, temp_name.raw());
+      FILE *fpjunk = _wfopen(kludgemart2, to_unicode_temp("rb"));
       if (!fpjunk)
         LOG(astring("failed to open the file for testing: ") + temp_name.raw() + "\n");
       if (fpjunk) fclose(fpjunk);
+  #endif
 #endif
 
        }
   } while (FindNextFile(search_handle, &wfd));
   FindClose(search_handle);
-#endif
-#ifdef __UNIX__
+#else
+*/
   DIR *dir = opendir(_path->s());
 //hmmm: could check errno to determine what caused the problem.
   if (!dir) return false;
@@ -202,6 +210,13 @@ bool directory::rescan()
     // make sure that the filename matches the pattern also.
     if (add_it && !fnmatch(_pattern->s(), file, 0)) {
       filename temp_name(*_path, file);
+      if (!temp_name.is_normal()) {
+//#ifdef DEBUG_DIRECTORY
+        LOG(astring("skipping abnormal file:  ") + temp_name);
+//#endif
+        entry = readdir(dir);
+        continue;  // cannot be adding goofy named pipes etc; cannot manage those.
+      }
       // add this to the appropriate list.
       if (temp_name.is_directory())
         _folders->concatenate(file);
@@ -211,7 +226,7 @@ bool directory::rescan()
     entry = readdir(dir);
   }
   closedir(dir);
-#endif
+//#endif
   shell_sort(_files->access(), _files->length());
   shell_sort(_folders->access(), _folders->length());
 
@@ -221,10 +236,9 @@ bool directory::rescan()
 
 bool directory::make_directory(const astring &path)
 {
-#ifdef __UNIX__
+#if defined(__UNIX__) || defined(__GNU_WINDOWS__)
   int mk_ret = mkdir(path.s(), 0777);
-#endif
-#ifdef __WIN32__
+#else
   int mk_ret = mkdir(path.s());
 #endif
   return !mk_ret;
@@ -232,10 +246,9 @@ bool directory::make_directory(const astring &path)
 
 bool directory::remove_directory(const astring &path)
 {
-#ifdef __UNIX__
+#if defined(__UNIX__) || defined(__GNU_WINDOWS__)
   int rm_ret = rmdir(path.s());
-#endif
-#ifdef __WIN32__
+#else
   int rm_ret = rmdir(path.s());
 #endif
   return !rm_ret;
@@ -243,15 +256,16 @@ bool directory::remove_directory(const astring &path)
 
 bool directory::recursive_create(const astring &directory_name)
 {
-//  FUNCDEF("recursive_create");
+  FUNCDEF("recursive_create");
   filename dir(directory_name);
   string_array pieces;
-  dir.separate(pieces);
+  bool rooted;
+  dir.separate(rooted, pieces);
   for (int i = 0; i < pieces.length(); i++) {
     // check each location along the way.
     string_array partial = pieces.subarray(0, i);
     filename curr;
-    curr.join(partial);  // this is our current location.
+    curr.join(rooted, partial);  // this is our current location.
     // make sure, if we see a drive letter component, that we call it
     // a proper directory name.
     if (curr.raw()[curr.raw().end()] == ':')
@@ -269,3 +283,4 @@ bool directory::recursive_create(const astring &directory_name)
 }
 
 } // namespace.
+