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.
27 #ifdef _WIN32 // WINDOWS
29 #define _WIN32_WINNT 0x0501
37 #define BUFSIZE MAX_PATH
39 std::vector<DirListNode> GetDirListing(std::string pathstring)
41 std::vector<DirListNode> listing;
43 WIN32_FIND_DATA FindFileData;
44 HANDLE hFind = INVALID_HANDLE_VALUE;
49 DirSpec = (LPTSTR) malloc (BUFSIZE);
53 printf( "Insufficient memory available\n" );
58 // Check that the input is not larger than allowed.
59 if (pathstring.size() > (BUFSIZE - 2))
61 _tprintf(TEXT("Input directory is too large.\n"));
66 //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
68 sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
70 // Find the first file in the directory.
71 hFind = FindFirstFile(DirSpec, &FindFileData);
73 if (hFind == INVALID_HANDLE_VALUE)
75 _tprintf (TEXT("Invalid file handle. Error is %u.\n"),
82 node.name = FindFileData.cFileName;
83 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
84 listing.push_back(node);
86 // List all the other files in the directory.
87 while (FindNextFile(hFind, &FindFileData) != 0)
90 node.name = FindFileData.cFileName;
91 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
92 listing.push_back(node);
95 dwError = GetLastError();
97 if (dwError != ERROR_NO_MORE_FILES)
99 _tprintf (TEXT("FindNextFile error. Error is %u.\n"),
110 if(retval != 0) listing.clear();
112 //for(unsigned int i=0; i<listing.size(); i++){
113 // std::cout<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
119 bool CreateDir(std::string path)
121 bool r = CreateDirectory(path.c_str(), NULL);
124 if(GetLastError() == ERROR_ALREADY_EXISTS)
129 bool PathExists(std::string path)
131 return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
134 bool RecursiveDelete(std::string path)
136 std::cerr<<"Removing \""<<path<<"\""<<std::endl;
140 // This silly function needs a double-null terminated string...
141 // Well, we'll just make sure it has at least two, then.
146 sfo.wFunc = FO_DELETE;
147 sfo.pFrom = path.c_str();
149 sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
151 int r = SHFileOperation(&sfo);
158 #include <sys/types.h>
161 #include <sys/stat.h>
162 #include <sys/wait.h>
164 std::vector<DirListNode> GetDirListing(std::string pathstring)
166 std::vector<DirListNode> listing;
170 if((dp = opendir(pathstring.c_str())) == NULL) {
171 //std::cout<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
175 while ((dirp = readdir(dp)) != NULL) {
176 if(dirp->d_name[0]!='.'){
178 node.name = dirp->d_name;
179 if(dirp->d_type == DT_DIR) node.dir = true;
180 else node.dir = false;
181 listing.push_back(node);
189 bool CreateDir(std::string path)
191 int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
198 // If already exists, return true
205 bool PathExists(std::string path)
208 return (stat(path.c_str(),&st) == 0);
211 bool RecursiveDelete(std::string path)
214 Execute the 'rm' command directly, by fork() and execve()
217 std::cerr<<"Removing \""<<path<<"\""<<std::endl;
221 pid_t child_pid = fork();
226 char argv_data[3][10000];
227 strcpy(argv_data[0], "/bin/rm");
228 strcpy(argv_data[1], "-rf");
229 strncpy(argv_data[2], path.c_str(), 10000);
231 argv[0] = argv_data[0];
232 argv[1] = argv_data[1];
233 argv[2] = argv_data[2];
236 std::cerr<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
237 <<argv[2]<<"'"<<std::endl;
239 execv(argv[0], argv);
241 // Execv shouldn't return. Failed.
250 tpid = wait(&child_status);
251 //if(tpid != child_pid) process_terminated(tpid);
252 }while(tpid != child_pid);
253 return (child_status == 0);
259 bool RecursiveDeleteContent(std::string path)
261 std::cerr<<"Removing content of \""<<path<<"\""<<std::endl;
262 std::vector<DirListNode> list = GetDirListing(path);
263 for(unsigned int i=0; i<list.size(); i++)
265 std::string childpath = path+"/"+list[i].name;
266 bool r = RecursiveDelete(childpath);
269 std::cerr<<"Removing \""<<childpath<<"\" failed"<<std::endl;