3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #ifdef _WIN32 // WINDOWS
31 #define _WIN32_WINNT 0x0501
39 #define BUFSIZE MAX_PATH
41 std::vector<DirListNode> GetDirListing(std::string pathstring)
43 std::vector<DirListNode> listing;
45 WIN32_FIND_DATA FindFileData;
46 HANDLE hFind = INVALID_HANDLE_VALUE;
51 DirSpec = (LPTSTR) malloc (BUFSIZE);
55 errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
60 // Check that the input is not larger than allowed.
61 if (pathstring.size() > (BUFSIZE - 2))
63 errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
68 //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
70 sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
72 // Find the first file in the directory.
73 hFind = FindFirstFile(DirSpec, &FindFileData);
75 if (hFind == INVALID_HANDLE_VALUE)
83 // Be very sure to not include '..' in the results, it will
84 // result in an epic failure when deleting stuff.
87 node.name = FindFileData.cFileName;
88 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
89 if(node.name != "." && node.name != "..")
90 listing.push_back(node);
92 // List all the other files in the directory.
93 while (FindNextFile(hFind, &FindFileData) != 0)
96 node.name = FindFileData.cFileName;
97 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
98 if(node.name != "." && node.name != "..")
99 listing.push_back(node);
102 dwError = GetLastError();
104 if (dwError != ERROR_NO_MORE_FILES)
106 errorstream<<"GetDirListing: FindNextFile error. Error is "
107 <<dwError<<std::endl;
117 if(retval != 0) listing.clear();
119 //for(unsigned int i=0; i<listing.size(); i++){
120 // infostream<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
126 bool CreateDir(std::string path)
128 bool r = CreateDirectory(path.c_str(), NULL);
131 if(GetLastError() == ERROR_ALREADY_EXISTS)
136 bool PathExists(std::string path)
138 return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
141 bool IsDir(std::string path)
143 DWORD attr = GetFileAttributes(path.c_str());
144 return (attr != INVALID_FILE_ATTRIBUTES &&
145 (attr & FILE_ATTRIBUTE_DIRECTORY));
148 bool RecursiveDelete(std::string path)
150 infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
152 DWORD attr = GetFileAttributes(path.c_str());
153 bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
154 (attr & FILE_ATTRIBUTE_DIRECTORY));
157 infostream<<"RecursiveDelete: Deleting file "<<path<<std::endl;
158 //bool did = DeleteFile(path.c_str());
161 errorstream<<"RecursiveDelete: Failed to delete file "
168 infostream<<"RecursiveDelete: Deleting content of directory "
170 std::vector<DirListNode> content = GetDirListing(path);
171 for(int i=0; i<content.size(); i++){
172 const DirListNode &n = content[i];
173 std::string fullpath = path + DIR_DELIM + n.name;
174 bool did = RecursiveDelete(fullpath);
176 errorstream<<"RecursiveDelete: Failed to recurse to "
177 <<fullpath<<std::endl;
181 infostream<<"RecursiveDelete: Deleting directory "<<path<<std::endl;
182 //bool did = RemoveDirectory(path.c_str();
185 errorstream<<"Failed to recursively delete directory "
193 bool DeleteSingleFileOrEmptyDirectory(std::string path)
195 DWORD attr = GetFileAttributes(path.c_str());
196 bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
197 (attr & FILE_ATTRIBUTE_DIRECTORY));
200 bool did = DeleteFile(path.c_str());
205 bool did = RemoveDirectory(path.c_str());
212 #include <sys/types.h>
215 #include <sys/stat.h>
216 #include <sys/wait.h>
218 std::vector<DirListNode> GetDirListing(std::string pathstring)
220 std::vector<DirListNode> listing;
224 if((dp = opendir(pathstring.c_str())) == NULL) {
225 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
229 while ((dirp = readdir(dp)) != NULL) {
231 // Be very sure to not include '..' in the results, it will
232 // result in an epic failure when deleting stuff.
233 if(dirp->d_name[0]!='.'){
235 node.name = dirp->d_name;
236 if(node.name == "." || node.name == "..")
239 int isdir = -1; // -1 means unknown
242 POSIX doesn't define d_type member of struct dirent and
243 certain filesystems on glibc/Linux will only return
244 DT_UNKNOWN for the d_type member.
246 Also we don't know whether symlinks are directories or not.
248 #ifdef _DIRENT_HAVE_D_TYPE
249 if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
250 isdir = (dirp->d_type == DT_DIR);
251 #endif /* _DIRENT_HAVE_D_TYPE */
254 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
260 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
262 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
265 listing.push_back(node);
273 bool CreateDir(std::string path)
275 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
282 // If already exists, return true
289 bool PathExists(std::string path)
292 return (stat(path.c_str(),&st) == 0);
295 bool IsDir(std::string path)
298 if(stat(path.c_str(), &statbuf))
299 return false; // Actually error; but certainly not a directory
300 return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
303 bool RecursiveDelete(std::string path)
306 Execute the 'rm' command directly, by fork() and execve()
309 infostream<<"Removing \""<<path<<"\""<<std::endl;
313 pid_t child_pid = fork();
318 char argv_data[3][10000];
319 strcpy(argv_data[0], "/bin/rm");
320 strcpy(argv_data[1], "-rf");
321 strncpy(argv_data[2], path.c_str(), 10000);
323 argv[0] = argv_data[0];
324 argv[1] = argv_data[1];
325 argv[2] = argv_data[2];
328 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
329 <<argv[2]<<"'"<<std::endl;
331 execv(argv[0], argv);
333 // Execv shouldn't return. Failed.
342 tpid = wait(&child_status);
343 //if(tpid != child_pid) process_terminated(tpid);
344 }while(tpid != child_pid);
345 return (child_status == 0);
349 bool DeleteSingleFileOrEmptyDirectory(std::string path)
352 bool did = (rmdir(path.c_str()) == 0);
354 errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
358 bool did = (unlink(path.c_str()) == 0);
360 errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
368 void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst)
370 std::vector<DirListNode> content = GetDirListing(path);
371 for(unsigned int i=0; i<content.size(); i++){
372 const DirListNode &n = content[i];
373 std::string fullpath = path + DIR_DELIM + n.name;
374 dst.push_back(fullpath);
375 GetRecursiveSubPaths(fullpath, dst);
379 bool DeletePaths(const std::vector<std::string> &paths)
382 // Go backwards to succesfully delete the output of GetRecursiveSubPaths
383 for(int i=paths.size()-1; i>=0; i--){
384 const std::string &path = paths[i];
385 bool did = DeleteSingleFileOrEmptyDirectory(path);
387 errorstream<<"Failed to delete "<<path<<std::endl;
394 bool RecursiveDeleteContent(std::string path)
396 infostream<<"Removing content of \""<<path<<"\""<<std::endl;
397 std::vector<DirListNode> list = GetDirListing(path);
398 for(unsigned int i=0; i<list.size(); i++)
400 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
402 std::string childpath = path + DIR_DELIM + list[i].name;
403 bool r = RecursiveDelete(childpath);
406 errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
413 bool CreateAllDirs(std::string path)
417 std::vector<std::string> tocreate;
418 std::string basepath = path;
419 while(!PathExists(basepath))
421 tocreate.push_back(basepath);
422 pos = basepath.rfind(DIR_DELIM_C);
423 if(pos == std::string::npos)
425 basepath = basepath.substr(0,pos);
427 for(int i=tocreate.size()-1;i>=0;i--)
428 if(!CreateDir(tocreate[i]))