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.
28 #ifdef _WIN32 // WINDOWS
30 #define _WIN32_WINNT 0x0501
38 #define BUFSIZE MAX_PATH
40 std::vector<DirListNode> GetDirListing(std::string pathstring)
42 std::vector<DirListNode> listing;
44 WIN32_FIND_DATA FindFileData;
45 HANDLE hFind = INVALID_HANDLE_VALUE;
50 DirSpec = (LPTSTR) malloc (BUFSIZE);
54 printf( "Insufficient memory available\n" );
59 // Check that the input is not larger than allowed.
60 if (pathstring.size() > (BUFSIZE - 2))
62 _tprintf(TEXT("Input directory is too large.\n"));
67 //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
69 sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
71 // Find the first file in the directory.
72 hFind = FindFirstFile(DirSpec, &FindFileData);
74 if (hFind == INVALID_HANDLE_VALUE)
76 _tprintf (TEXT("Invalid file handle. Error is %u.\n"),
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 _tprintf (TEXT("FindNextFile error. Error is %u.\n"),
117 if(retval != 0) listing.clear();
119 //for(unsigned int i=0; i<listing.size(); i++){
120 // std::cout<<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 RecursiveDelete(std::string path)
143 std::cerr<<"Removing \""<<path<<"\""<<std::endl;
147 // This silly function needs a double-null terminated string...
148 // Well, we'll just make sure it has at least two, then.
153 sfo.wFunc = FO_DELETE;
154 sfo.pFrom = path.c_str();
156 sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
158 int r = SHFileOperation(&sfo);
161 std::cerr<<"SHFileOperation returned "<<r<<std::endl;
169 #include <sys/types.h>
172 #include <sys/stat.h>
173 #include <sys/wait.h>
175 std::vector<DirListNode> GetDirListing(std::string pathstring)
177 std::vector<DirListNode> listing;
181 if((dp = opendir(pathstring.c_str())) == NULL) {
182 //std::cout<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
186 while ((dirp = readdir(dp)) != NULL) {
188 // Be very sure to not include '..' in the results, it will
189 // result in an epic failure when deleting stuff.
190 if(dirp->d_name[0]!='.'){
192 node.name = dirp->d_name;
193 if(node.name == "." || node.name == "..")
196 int isdir = -1; // -1 means unknown
199 POSIX doesn't define d_type member of
200 struct dirent and certain filesystems on
201 glibc/Linux will only return DT_UNKNOWN for
204 #ifdef _DIRENT_HAVE_D_TYPE
205 if(dirp->d_type != DT_UNKNOWN)
206 isdir = (dirp->d_type == DT_DIR);
207 #endif /* _DIRENT_HAVE_D_TYPE */
210 Was d_type DT_UNKNOWN (or nonexistent)?
216 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
218 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
221 listing.push_back(node);
229 bool CreateDir(std::string path)
231 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
238 // If already exists, return true
245 bool PathExists(std::string path)
248 return (stat(path.c_str(),&st) == 0);
251 bool RecursiveDelete(std::string path)
254 Execute the 'rm' command directly, by fork() and execve()
257 std::cerr<<"Removing \""<<path<<"\""<<std::endl;
261 pid_t child_pid = fork();
266 char argv_data[3][10000];
267 strcpy(argv_data[0], "/bin/rm");
268 strcpy(argv_data[1], "-rf");
269 strncpy(argv_data[2], path.c_str(), 10000);
271 argv[0] = argv_data[0];
272 argv[1] = argv_data[1];
273 argv[2] = argv_data[2];
276 std::cerr<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
277 <<argv[2]<<"'"<<std::endl;
279 execv(argv[0], argv);
281 // Execv shouldn't return. Failed.
290 tpid = wait(&child_status);
291 //if(tpid != child_pid) process_terminated(tpid);
292 }while(tpid != child_pid);
293 return (child_status == 0);
299 bool RecursiveDeleteContent(std::string path)
301 std::cerr<<"Removing content of \""<<path<<"\""<<std::endl;
302 std::vector<DirListNode> list = GetDirListing(path);
303 for(unsigned int i=0; i<list.size(); i++)
305 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
307 std::string childpath = path + DIR_DELIM + list[i].name;
308 bool r = RecursiveDelete(childpath);
311 std::cerr<<"Removing \""<<childpath<<"\" failed"<<std::endl;
318 bool CreateAllDirs(std::string path)
322 std::vector<std::string> tocreate;
323 std::string basepath = path;
324 while(!PathExists(basepath))
326 tocreate.push_back(basepath);
327 pos = basepath.rfind(DIR_DELIM_C);
328 if(pos == std::string::npos)
330 basepath = basepath.substr(0,pos);
332 for(int i=tocreate.size()-1;i>=0;i--)
333 CreateDir(tocreate[i]);