Merge pull request #482 from proller/liquid
[oweals/minetest.git] / src / filesys.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 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 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.
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 Lesser General Public License for more details.
14
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.
18 */
19
20 #include "filesys.h"
21 #include "strfnd.h"
22 #include <iostream>
23 #include <string.h>
24 #include "log.h"
25
26 namespace fs
27 {
28
29 #ifdef _WIN32 // WINDOWS
30
31 #define _WIN32_WINNT 0x0501
32 #include <windows.h>
33 #include <stdio.h>
34 #include <malloc.h>
35 #include <tchar.h> 
36 #include <wchar.h> 
37 #include <stdio.h>
38
39 #define BUFSIZE MAX_PATH
40
41 std::vector<DirListNode> GetDirListing(std::string pathstring)
42 {
43         std::vector<DirListNode> listing;
44
45         WIN32_FIND_DATA FindFileData;
46         HANDLE hFind = INVALID_HANDLE_VALUE;
47         DWORD dwError;
48         LPTSTR DirSpec;
49         INT retval;
50
51         DirSpec = (LPTSTR) malloc (BUFSIZE);
52
53         if( DirSpec == NULL )
54         {
55           errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
56           retval = 1;
57           goto Cleanup;
58         }
59
60         // Check that the input is not larger than allowed.
61         if (pathstring.size() > (BUFSIZE - 2))
62         {
63           errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
64           retval = 3;
65           goto Cleanup;
66         }
67
68         //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
69
70         sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
71
72         // Find the first file in the directory.
73         hFind = FindFirstFile(DirSpec, &FindFileData);
74
75         if (hFind == INVALID_HANDLE_VALUE) 
76         {
77                 retval = (-1);
78                 goto Cleanup;
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                         errorstream<<"GetDirListing: FindNextFile error. Error is "
107                                         <<dwError<<std::endl;
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         //      infostream<<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 IsDir(std::string path)
142 {
143         DWORD attr = GetFileAttributes(path.c_str());
144         return (attr != INVALID_FILE_ATTRIBUTES &&
145                         (attr & FILE_ATTRIBUTE_DIRECTORY));
146 }
147
148 bool RecursiveDelete(std::string path)
149 {
150         infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
151
152         DWORD attr = GetFileAttributes(path.c_str());
153         bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
154                         (attr & FILE_ATTRIBUTE_DIRECTORY));
155         if(!is_directory)
156         {
157                 infostream<<"RecursiveDelete: Deleting file "<<path<<std::endl;
158                 //bool did = DeleteFile(path.c_str());
159                 bool did = true;
160                 if(!did){
161                         errorstream<<"RecursiveDelete: Failed to delete file "
162                                         <<path<<std::endl;
163                         return false;
164                 }
165         }
166         else
167         {
168                 infostream<<"RecursiveDelete: Deleting content of directory "
169                                 <<path<<std::endl;
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);
175                         if(!did){
176                                 errorstream<<"RecursiveDelete: Failed to recurse to "
177                                                 <<fullpath<<std::endl;
178                                 return false;
179                         }
180                 }
181                 infostream<<"RecursiveDelete: Deleting directory "<<path<<std::endl;
182                 //bool did = RemoveDirectory(path.c_str();
183                 bool did = true;
184                 if(!did){
185                         errorstream<<"Failed to recursively delete directory "
186                                         <<path<<std::endl;
187                         return false;
188                 }
189         }
190         return true;
191 }
192
193 bool DeleteSingleFileOrEmptyDirectory(std::string path)
194 {
195         DWORD attr = GetFileAttributes(path.c_str());
196         bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
197                         (attr & FILE_ATTRIBUTE_DIRECTORY));
198         if(!is_directory)
199         {
200                 bool did = DeleteFile(path.c_str());
201                 return did;
202         }
203         else
204         {
205                 bool did = RemoveDirectory(path.c_str());
206                 return did;
207         }
208 }
209
210 #else // POSIX
211
212 #include <sys/types.h>
213 #include <dirent.h>
214 #include <errno.h>
215 #include <sys/stat.h>
216 #include <sys/wait.h>
217 #include <unistd.h>
218
219 std::vector<DirListNode> GetDirListing(std::string pathstring)
220 {
221         std::vector<DirListNode> listing;
222
223     DIR *dp;
224     struct dirent *dirp;
225     if((dp  = opendir(pathstring.c_str())) == NULL) {
226                 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
227         return listing;
228     }
229
230     while ((dirp = readdir(dp)) != NULL) {
231                 // NOTE:
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]!='.'){
235                         DirListNode node;
236                         node.name = dirp->d_name;
237                         if(node.name == "." || node.name == "..")
238                                 continue;
239
240                         int isdir = -1; // -1 means unknown
241
242                         /*
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.
246
247                                 Also we don't know whether symlinks are directories or not.
248                         */
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 */
253
254                         /*
255                                 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
256                                 If so, try stat().
257                         */
258                         if(isdir == -1)
259                         {
260                                 struct stat statbuf;
261                                 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
262                                         continue;
263                                 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
264                         }
265                         node.dir = isdir;
266                         listing.push_back(node);
267                 }
268     }
269     closedir(dp);
270
271         return listing;
272 }
273
274 bool CreateDir(std::string path)
275 {
276         int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
277         if(r == 0)
278         {
279                 return true;
280         }
281         else
282         {
283                 // If already exists, return true
284                 if(errno == EEXIST)
285                         return true;
286                 return false;
287         }
288 }
289
290 bool PathExists(std::string path)
291 {
292         struct stat st;
293         return (stat(path.c_str(),&st) == 0);
294 }
295
296 bool IsDir(std::string path)
297 {
298         struct stat statbuf;
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);
302 }
303
304 bool RecursiveDelete(std::string path)
305 {
306         /*
307                 Execute the 'rm' command directly, by fork() and execve()
308         */
309         
310         infostream<<"Removing \""<<path<<"\""<<std::endl;
311
312         //return false;
313         
314         pid_t child_pid = fork();
315
316         if(child_pid == 0)
317         {
318                 // Child
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);
323                 char *argv[4];
324                 argv[0] = argv_data[0];
325                 argv[1] = argv_data[1];
326                 argv[2] = argv_data[2];
327                 argv[3] = NULL;
328
329                 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
330                                 <<argv[2]<<"'"<<std::endl;
331                 
332                 execv(argv[0], argv);
333                 
334                 // Execv shouldn't return. Failed.
335                 _exit(1);
336         }
337         else
338         {
339                 // Parent
340                 int child_status;
341                 pid_t tpid;
342                 do{
343                         tpid = wait(&child_status);
344                         //if(tpid != child_pid) process_terminated(tpid);
345                 }while(tpid != child_pid);
346                 return (child_status == 0);
347         }
348 }
349
350 bool DeleteSingleFileOrEmptyDirectory(std::string path)
351 {
352         if(IsDir(path)){
353                 bool did = (rmdir(path.c_str()) == 0);
354                 if(!did)
355                         errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
356                                         <<std::endl;
357                 return did;
358         } else {
359                 bool did = (unlink(path.c_str()) == 0);
360                 if(!did)
361                         errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
362                                         <<std::endl;
363                 return did;
364         }
365 }
366
367 #endif
368
369 void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst)
370 {
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);
377         }
378 }
379
380 bool DeletePaths(const std::vector<std::string> &paths)
381 {
382         bool success = true;
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);
387                 if(!did){
388                         errorstream<<"Failed to delete "<<path<<std::endl;
389                         success = false;
390                 }
391         }
392         return success;
393 }
394
395 bool RecursiveDeleteContent(std::string path)
396 {
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++)
400         {
401                 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
402                         continue;
403                 std::string childpath = path + DIR_DELIM + list[i].name;
404                 bool r = RecursiveDelete(childpath);
405                 if(r == false)
406                 {
407                         errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
408                         return false;
409                 }
410         }
411         return true;
412 }
413
414 bool CreateAllDirs(std::string path)
415 {
416
417         size_t pos;
418         std::vector<std::string> tocreate;
419         std::string basepath = path;
420         while(!PathExists(basepath))
421         {
422                 tocreate.push_back(basepath);
423                 pos = basepath.rfind(DIR_DELIM_C);
424                 if(pos == std::string::npos)
425                         break;
426                 basepath = basepath.substr(0,pos);
427         }
428         for(int i=tocreate.size()-1;i>=0;i--)
429                 if(!CreateDir(tocreate[i]))
430                         return false;
431         return true;
432 }
433
434 } // namespace fs
435