Fix CreateAllDirs() (failed for relative fully non-existing path)
[oweals/minetest.git] / src / filesys.cpp
1 /*
2 Minetest-c55
3 Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "filesys.h"
21 #include "strfnd.h"
22 #include <iostream>
23 #include <string.h>
24
25 namespace fs
26 {
27
28 #ifdef _WIN32 // WINDOWS
29
30 #define _WIN32_WINNT 0x0501
31 #include <windows.h>
32 #include <stdio.h>
33 #include <malloc.h>
34 #include <tchar.h> 
35 #include <wchar.h> 
36 #include <stdio.h>
37
38 #define BUFSIZE MAX_PATH
39
40 std::vector<DirListNode> GetDirListing(std::string pathstring)
41 {
42         std::vector<DirListNode> listing;
43
44         WIN32_FIND_DATA FindFileData;
45         HANDLE hFind = INVALID_HANDLE_VALUE;
46         DWORD dwError;
47         LPTSTR DirSpec;
48         INT retval;
49
50         DirSpec = (LPTSTR) malloc (BUFSIZE);
51
52         if( DirSpec == NULL )
53         {
54           printf( "Insufficient memory available\n" );
55           retval = 1;
56           goto Cleanup;
57         }
58
59         // Check that the input is not larger than allowed.
60         if (pathstring.size() > (BUFSIZE - 2))
61         {
62           _tprintf(TEXT("Input directory is too large.\n"));
63           retval = 3;
64           goto Cleanup;
65         }
66
67         //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
68
69         sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
70
71         // Find the first file in the directory.
72         hFind = FindFirstFile(DirSpec, &FindFileData);
73
74         if (hFind == INVALID_HANDLE_VALUE) 
75         {
76           _tprintf (TEXT("Invalid file handle. Error is %u.\n"), 
77                                 GetLastError());
78           retval = (-1);
79         } 
80         else 
81         {
82                 // NOTE:
83                 // Be very sure to not include '..' in the results, it will
84                 // result in an epic failure when deleting stuff.
85
86                 DirListNode node;
87                 node.name = FindFileData.cFileName;
88                 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
89                 if(node.name != "." && node.name != "..")
90                         listing.push_back(node);
91
92                 // List all the other files in the directory.
93                 while (FindNextFile(hFind, &FindFileData) != 0) 
94                 {
95                         DirListNode node;
96                         node.name = FindFileData.cFileName;
97                         node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
98                         if(node.name != "." && node.name != "..")
99                                 listing.push_back(node);
100                 }
101
102                 dwError = GetLastError();
103                 FindClose(hFind);
104                 if (dwError != ERROR_NO_MORE_FILES) 
105                 {
106                  _tprintf (TEXT("FindNextFile error. Error is %u.\n"), 
107                                    dwError);
108                 retval = (-1);
109                 goto Cleanup;
110                 }
111         }
112         retval  = 0;
113
114 Cleanup:
115         free(DirSpec);
116
117         if(retval != 0) listing.clear();
118
119         //for(unsigned int i=0; i<listing.size(); i++){
120         //      std::cout<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
121         //}
122         
123         return listing;
124 }
125
126 bool CreateDir(std::string path)
127 {
128         bool r = CreateDirectory(path.c_str(), NULL);
129         if(r == true)
130                 return true;
131         if(GetLastError() == ERROR_ALREADY_EXISTS)
132                 return true;
133         return false;
134 }
135
136 bool PathExists(std::string path)
137 {
138         return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
139 }
140
141 bool RecursiveDelete(std::string path)
142 {
143         std::cerr<<"Removing \""<<path<<"\""<<std::endl;
144
145         //return false;
146         
147         // This silly function needs a double-null terminated string...
148         // Well, we'll just make sure it has at least two, then.
149         path += "\0\0";
150
151         SHFILEOPSTRUCT sfo;
152         sfo.hwnd = NULL;
153         sfo.wFunc = FO_DELETE;
154         sfo.pFrom = path.c_str();
155         sfo.pTo = NULL;
156         sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
157         
158         int r = SHFileOperation(&sfo);
159
160         if(r != 0)
161                 std::cerr<<"SHFileOperation returned "<<r<<std::endl;
162
163         //return (r == 0);
164         return true;
165 }
166
167 #else // POSIX
168
169 #include <sys/types.h>
170 #include <dirent.h>
171 #include <errno.h>
172 #include <sys/stat.h>
173 #include <sys/wait.h>
174
175 std::vector<DirListNode> GetDirListing(std::string pathstring)
176 {
177         std::vector<DirListNode> listing;
178
179     DIR *dp;
180     struct dirent *dirp;
181     if((dp  = opendir(pathstring.c_str())) == NULL) {
182                 //std::cout<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
183         return listing;
184     }
185
186     while ((dirp = readdir(dp)) != NULL) {
187                 // NOTE:
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]!='.'){
191                         DirListNode node;
192                         node.name = dirp->d_name;
193                         if(node.name == "." || node.name == "..")
194                                 continue;
195
196                         int isdir = -1; // -1 means unknown
197
198                         /*
199                                 POSIX doesn't define d_type member of struct dirent and
200                                 certain filesystems on glibc/Linux will only return
201                                 DT_UNKNOWN for the d_type member.
202
203                                 Also we don't know whether symlinks are directories or not.
204                         */
205 #ifdef _DIRENT_HAVE_D_TYPE
206                         if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
207                                 isdir = (dirp->d_type == DT_DIR);
208 #endif /* _DIRENT_HAVE_D_TYPE */
209
210                         /*
211                                 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
212                                 If so, try stat().
213                         */
214                         if(isdir == -1)
215                         {
216                                 struct stat statbuf;
217                                 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
218                                         continue;
219                                 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
220                         }
221                         node.dir = isdir;
222                         listing.push_back(node);
223                 }
224     }
225     closedir(dp);
226
227         return listing;
228 }
229
230 bool CreateDir(std::string path)
231 {
232         int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
233         if(r == 0)
234         {
235                 return true;
236         }
237         else
238         {
239                 // If already exists, return true
240                 if(errno == EEXIST)
241                         return true;
242                 return false;
243         }
244 }
245
246 bool PathExists(std::string path)
247 {
248         struct stat st;
249         return (stat(path.c_str(),&st) == 0);
250 }
251
252 bool RecursiveDelete(std::string path)
253 {
254         /*
255                 Execute the 'rm' command directly, by fork() and execve()
256         */
257         
258         std::cerr<<"Removing \""<<path<<"\""<<std::endl;
259
260         //return false;
261         
262         pid_t child_pid = fork();
263
264         if(child_pid == 0)
265         {
266                 // Child
267                 char argv_data[3][10000];
268                 strcpy(argv_data[0], "/bin/rm");
269                 strcpy(argv_data[1], "-rf");
270                 strncpy(argv_data[2], path.c_str(), 10000);
271                 char *argv[4];
272                 argv[0] = argv_data[0];
273                 argv[1] = argv_data[1];
274                 argv[2] = argv_data[2];
275                 argv[3] = NULL;
276
277                 std::cerr<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
278                                 <<argv[2]<<"'"<<std::endl;
279                 
280                 execv(argv[0], argv);
281                 
282                 // Execv shouldn't return. Failed.
283                 _exit(1);
284         }
285         else
286         {
287                 // Parent
288                 int child_status;
289                 pid_t tpid;
290                 do{
291                         tpid = wait(&child_status);
292                         //if(tpid != child_pid) process_terminated(tpid);
293                 }while(tpid != child_pid);
294                 return (child_status == 0);
295         }
296 }
297
298 #endif
299
300 bool RecursiveDeleteContent(std::string path)
301 {
302         std::cerr<<"Removing content of \""<<path<<"\""<<std::endl;
303         std::vector<DirListNode> list = GetDirListing(path);
304         for(unsigned int i=0; i<list.size(); i++)
305         {
306                 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
307                         continue;
308                 std::string childpath = path + DIR_DELIM + list[i].name;
309                 bool r = RecursiveDelete(childpath);
310                 if(r == false)
311                 {
312                         std::cerr<<"Removing \""<<childpath<<"\" failed"<<std::endl;
313                         return false;
314                 }
315         }
316         return true;
317 }
318
319 bool CreateAllDirs(std::string path)
320 {
321
322         size_t pos;
323         std::vector<std::string> tocreate;
324         std::string basepath = path;
325         while(!PathExists(basepath))
326         {
327                 tocreate.push_back(basepath);
328                 pos = basepath.rfind(DIR_DELIM_C);
329                 if(pos == std::string::npos)
330                         break;
331                 basepath = basepath.substr(0,pos);
332         }
333         for(int i=tocreate.size()-1;i>=0;i--)
334                 if(!CreateDir(tocreate[i]))
335                         return false;
336         return true;
337 }
338
339 } // namespace fs
340