1 /*****************************************************************************\
4 * Author : Chris Koeritz *
6 *******************************************************************************
7 * Copyright (c) 2001-$now By Author. This program is free software; you can *
8 * redistribute it and/or modify it under the terms of the GNU General Public *
9 * License as published by the Free Software Foundation; either version 2 of *
10 * the License or (at your option) any later version. This is online at: *
11 * http://www.fsf.org/copyleft/gpl.html *
12 * Please send any updates to: fred@gruntose.com *
13 \*****************************************************************************/
15 #include "directory.h"
18 #include <application/windoze_helper.h>
19 #include <basis/astring.h>
20 #include <basis/contracts.h>
21 #include <basis/functions.h>
22 #include <basis/utf_conversion.h>
23 #include <loggers/program_wide_logger.h>
24 #include <structures/string_array.h>
30 #include "../algorithms/sorts.h"
31 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
38 // #include <direct.h>
43 const int MAX_ABS_PATH = 2048;
44 #elif defined(__APPLE__)
45 const int MAX_ABS_PATH = 2048;
47 const int MAX_ABS_PATH = MAX_ABS_PATH;
51 //#define DEBUG_DIRECTORY
52 // uncomment for noisier runs.
55 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
57 using namespace algorithms;
58 using namespace basis;
59 using namespace loggers;
60 using namespace structures;
62 namespace filesystem {
64 directory::directory(const astring &path, const char *pattern)
65 : _scanned_okay(false),
67 _files(new string_array),
68 _folders(new string_array),
69 _pattern(new astring(pattern))
70 { reset(path, pattern); }
72 directory::directory(const directory &to_copy)
73 : _scanned_okay(false),
75 _files(new string_array),
76 _folders(new string_array),
78 { reset(*to_copy._path, to_copy._pattern->observe()); }
80 directory::~directory()
82 _scanned_okay = false;
89 const astring &directory::path() const { return *_path; }
91 const astring &directory::pattern() const { return *_pattern; }
93 directory &directory::operator =(const directory &to_copy)
95 if (this == &to_copy) return *this; // oops.
96 _scanned_okay = false;
97 reset(*to_copy._path, to_copy._pattern->observe());
101 astring directory::absolute_path(const astring &rel_path)
103 char abs_path[MAX_ABS_PATH + 1];
106 // if (!_fullpath(abs_path, rel_path.s(), MAX_ABS_PATH)) return "";
109 if (!realpath(rel_path.s(), abs_path)) return "";
114 astring directory::current()
116 astring to_return("."); // failure result.
118 // flexichar buffer[MAX_ABS_PATH + 1] = { '\0' };
119 // GetCurrentDirectory(MAX_ABS_PATH, buffer);
120 // to_return = from_unicode_temp(buffer);
122 char buffer[MAX_ABS_PATH + 1] = { '\0' };
123 if (realpath(".", buffer)) to_return = buffer;
128 bool directory::reset(const astring &path, const char *pattern)
129 { *_path = path; *_pattern = pattern; return rescan(); }
131 bool directory::move_up(const char *pattern)
133 astring currdir = current();
134 return reset(currdir + "/..", pattern);
137 bool directory::move_down(const astring &subdir, const char *pattern)
139 astring currdir = current();
140 return reset(currdir + "/" + subdir, pattern);
143 const string_array &directory::files() const { return *_files; }
145 const string_array &directory::directories() const { return *_folders; }
147 bool directory::rescan()
150 _scanned_okay = false;
153 astring cur_dir = ".";
154 astring par_dir = "..";
157 // start reading the directory.
159 astring real_path_spec = *_path + "/" + *_pattern;
160 HANDLE search_handle = FindFirstFile(to_unicode_temp(real_path_spec), &wfd);
161 if (search_handle == INVALID_HANDLE_VALUE) return false; // bad path.
163 // ignore the two standard directory entries.
164 astring filename_transcoded(from_unicode_temp(wfd.cFileName));
165 if (!strcmp(filename_transcoded.s(), cur_dir.s())) continue;
166 if (!strcmp(filename_transcoded.s(), par_dir.s())) continue;
169 #ifdef DEBUG_DIRECTORY
170 to_unicode_persist(kludgemart, filename_transcoded);
171 if (memcmp((wchar_t*)kludgemart, wfd.cFileName, wcslen(wfd.cFileName)*2))
172 printf("failed to compare the string before and after transcoding\n");
176 //wprintf(to_unicode_temp("file is %ls\n"), (wchar_t*)to_unicode_temp(filename_transcoded));
178 filename temp_name(*_path, filename_transcoded.s());
180 // add this to the appropriate list.
181 if (temp_name.is_directory()) {
182 _folders->concatenate(filename_transcoded);
184 _files->concatenate(filename_transcoded);
187 #ifdef DEBUG_DIRECTORY
188 to_unicode_persist(kludgemart2, temp_name.raw());
189 FILE *fpjunk = _wfopen(kludgemart2, to_unicode_temp("rb"));
191 LOG(astring("failed to open the file for testing: ") + temp_name.raw() + "\n");
192 if (fpjunk) fclose(fpjunk);
197 } while (FindNextFile(search_handle, &wfd));
198 FindClose(search_handle);
201 DIR *dir = opendir(_path->s());
202 //hmmm: could check errno to determine what caused the problem.
203 if (!dir) return false;
204 dirent *entry = readdir(dir);
206 char *file = entry->d_name;
208 if (!strcmp(file, cur_dir.s())) add_it = false;
209 if (!strcmp(file, par_dir.s())) add_it = false;
210 // make sure that the filename matches the pattern also.
211 if (add_it && !fnmatch(_pattern->s(), file, 0)) {
212 filename temp_name(*_path, file);
213 if (!temp_name.is_normal()) {
214 //#ifdef DEBUG_DIRECTORY
215 LOG(astring("skipping abnormal file: ") + temp_name);
217 entry = readdir(dir);
218 continue; // cannot be adding goofy named pipes etc; cannot manage those.
220 // add this to the appropriate list.
221 if (temp_name.is_directory())
222 _folders->concatenate(file);
224 _files->concatenate(file);
226 entry = readdir(dir);
230 shell_sort(_files->access(), _files->length());
231 shell_sort(_folders->access(), _folders->length());
233 _scanned_okay = true;
237 bool directory::make_directory(const astring &path)
239 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
240 int mk_ret = mkdir(path.s(), 0777);
242 int mk_ret = mkdir(path.s());
247 bool directory::remove_directory(const astring &path)
249 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
250 int rm_ret = rmdir(path.s());
252 int rm_ret = rmdir(path.s());
257 bool directory::recursive_create(const astring &directory_name)
259 FUNCDEF("recursive_create");
260 filename dir(directory_name);
263 dir.separate(rooted, pieces);
264 for (int i = 0; i < pieces.length(); i++) {
265 // check each location along the way.
266 string_array partial = pieces.subarray(0, i);
268 curr.join(rooted, partial); // this is our current location.
269 // make sure, if we see a drive letter component, that we call it
270 // a proper directory name.
271 if (curr.raw()[curr.raw().end()] == ':')
272 curr = curr.raw() + "/";
274 if (curr.is_directory()) {
275 continue; // that's good.
277 return false; // if it's an existing file, we're hosed.
279 // the directory at this place doesn't exist yet. let's create it.
280 if (!directory::make_directory(curr.raw())) return false;