3 Copyright (C) 2013 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 Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser 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>
219 std::vector<DirListNode> GetDirListing(std::string pathstring)
221 std::vector<DirListNode> listing;
225 if((dp = opendir(pathstring.c_str())) == NULL) {
226 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
230 while ((dirp = readdir(dp)) != NULL) {
232 // Be very sure to not include '..' in the results, it will
233 // result in an epic failure when deleting stuff.
234 if(dirp->d_name[0]!='.'){
236 node.name = dirp->d_name;
237 if(node.name == "." || node.name == "..")
240 int isdir = -1; // -1 means unknown
243 POSIX doesn't define d_type member of struct dirent and
244 certain filesystems on glibc/Linux will only return
245 DT_UNKNOWN for the d_type member.
247 Also we don't know whether symlinks are directories or not.
249 #ifdef _DIRENT_HAVE_D_TYPE
250 if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
251 isdir = (dirp->d_type == DT_DIR);
252 #endif /* _DIRENT_HAVE_D_TYPE */
255 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
261 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
263 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
266 listing.push_back(node);
274 bool CreateDir(std::string path)
276 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
283 // If already exists, return true
290 bool PathExists(std::string path)
293 return (stat(path.c_str(),&st) == 0);
296 bool IsDir(std::string path)
299 if(stat(path.c_str(), &statbuf))
300 return false; // Actually error; but certainly not a directory
301 return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
304 bool RecursiveDelete(std::string path)
307 Execute the 'rm' command directly, by fork() and execve()
310 infostream<<"Removing \""<<path<<"\""<<std::endl;
314 pid_t child_pid = fork();
319 char argv_data[3][10000];
320 strcpy(argv_data[0], "/bin/rm");
321 strcpy(argv_data[1], "-rf");
322 strncpy(argv_data[2], path.c_str(), 10000);
324 argv[0] = argv_data[0];
325 argv[1] = argv_data[1];
326 argv[2] = argv_data[2];
329 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
330 <<argv[2]<<"'"<<std::endl;
332 execv(argv[0], argv);
334 // Execv shouldn't return. Failed.
343 tpid = wait(&child_status);
344 //if(tpid != child_pid) process_terminated(tpid);
345 }while(tpid != child_pid);
346 return (child_status == 0);
350 bool DeleteSingleFileOrEmptyDirectory(std::string path)
353 bool did = (rmdir(path.c_str()) == 0);
355 errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
359 bool did = (unlink(path.c_str()) == 0);
361 errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
369 void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst)
371 std::vector<DirListNode> content = GetDirListing(path);
372 for(unsigned int i=0; i<content.size(); i++){
373 const DirListNode &n = content[i];
374 std::string fullpath = path + DIR_DELIM + n.name;
375 dst.push_back(fullpath);
376 GetRecursiveSubPaths(fullpath, dst);
380 bool DeletePaths(const std::vector<std::string> &paths)
383 // Go backwards to succesfully delete the output of GetRecursiveSubPaths
384 for(int i=paths.size()-1; i>=0; i--){
385 const std::string &path = paths[i];
386 bool did = DeleteSingleFileOrEmptyDirectory(path);
388 errorstream<<"Failed to delete "<<path<<std::endl;
395 bool RecursiveDeleteContent(std::string path)
397 infostream<<"Removing content of \""<<path<<"\""<<std::endl;
398 std::vector<DirListNode> list = GetDirListing(path);
399 for(unsigned int i=0; i<list.size(); i++)
401 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
403 std::string childpath = path + DIR_DELIM + list[i].name;
404 bool r = RecursiveDelete(childpath);
407 errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
414 bool CreateAllDirs(std::string path)
418 std::vector<std::string> tocreate;
419 std::string basepath = path;
420 while(!PathExists(basepath))
422 tocreate.push_back(basepath);
423 pos = basepath.rfind(DIR_DELIM_C);
424 if(pos == std::string::npos)
426 basepath = basepath.substr(0,pos);
428 for(int i=tocreate.size()-1;i>=0;i--)
429 if(!CreateDir(tocreate[i]))