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__)
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 = "..";
156 // start reading the directory.
158 astring real_path_spec = *_path + "/" + *_pattern;
159 HANDLE search_handle = FindFirstFile(to_unicode_temp(real_path_spec), &wfd);
160 if (search_handle == INVALID_HANDLE_VALUE) return false; // bad path.
162 // ignore the two standard directory entries.
163 astring filename_transcoded(from_unicode_temp(wfd.cFileName));
164 if (!strcmp(filename_transcoded.s(), cur_dir.s())) continue;
165 if (!strcmp(filename_transcoded.s(), par_dir.s())) continue;
168 #ifdef DEBUG_DIRECTORY
169 to_unicode_persist(kludgemart, filename_transcoded);
170 if (memcmp((wchar_t*)kludgemart, wfd.cFileName, wcslen(wfd.cFileName)*2))
171 printf("failed to compare the string before and after transcoding\n");
175 //wprintf(to_unicode_temp("file is %ls\n"), (wchar_t*)to_unicode_temp(filename_transcoded));
177 filename temp_name(*_path, filename_transcoded.s());
179 // add this to the appropriate list.
180 if (temp_name.is_directory()) {
181 _folders->concatenate(filename_transcoded);
183 _files->concatenate(filename_transcoded);
186 #ifdef DEBUG_DIRECTORY
187 to_unicode_persist(kludgemart2, temp_name.raw());
188 FILE *fpjunk = _wfopen(kludgemart2, to_unicode_temp("rb"));
190 LOG(astring("failed to open the file for testing: ") + temp_name.raw() + "\n");
191 if (fpjunk) fclose(fpjunk);
196 } while (FindNextFile(search_handle, &wfd));
197 FindClose(search_handle);
199 DIR *dir = opendir(_path->s());
200 //hmmm: could check errno to determine what caused the problem.
201 if (!dir) return false;
202 dirent *entry = readdir(dir);
204 char *file = entry->d_name;
206 if (!strcmp(file, cur_dir.s())) add_it = false;
207 if (!strcmp(file, par_dir.s())) add_it = false;
208 // make sure that the filename matches the pattern also.
209 if (add_it && !fnmatch(_pattern->s(), file, 0)) {
210 filename temp_name(*_path, file);
211 if (!temp_name.is_normal()) {
212 //#ifdef DEBUG_DIRECTORY
213 LOG(astring("skipping abnormal file: ") + temp_name);
215 entry = readdir(dir);
216 continue; // cannot be adding goofy named pipes etc; cannot manage those.
218 // add this to the appropriate list.
219 if (temp_name.is_directory())
220 _folders->concatenate(file);
222 _files->concatenate(file);
224 entry = readdir(dir);
228 shell_sort(_files->access(), _files->length());
229 shell_sort(_folders->access(), _folders->length());
231 _scanned_okay = true;
235 bool directory::make_directory(const astring &path)
237 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
238 int mk_ret = mkdir(path.s(), 0777);
240 int mk_ret = mkdir(path.s());
245 bool directory::remove_directory(const astring &path)
247 #if defined(__UNIX__) || defined(__GNU_WINDOWS__)
248 int rm_ret = rmdir(path.s());
250 int rm_ret = rmdir(path.s());
255 bool directory::recursive_create(const astring &directory_name)
257 FUNCDEF("recursive_create");
258 filename dir(directory_name);
261 dir.separate(rooted, pieces);
262 for (int i = 0; i < pieces.length(); i++) {
263 // check each location along the way.
264 string_array partial = pieces.subarray(0, i);
266 curr.join(rooted, partial); // this is our current location.
267 // make sure, if we see a drive letter component, that we call it
268 // a proper directory name.
269 if (curr.raw()[curr.raw().end()] == ':')
270 curr = curr.raw() + "/";
272 if (curr.is_directory()) {
273 continue; // that's good.
275 return false; // if it's an existing file, we're hosed.
277 // the directory at this place doesn't exist yet. let's create it.
278 if (!directory::make_directory(curr.raw())) return false;