Fix filesys.cpp debug output to go into debug.txt
[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 #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           errorstream<<"GetDirListing: Invalid file handle. Error is "
78                         <<GetLastError()<<std::endl;
79           retval = (-1);
80         } 
81         else 
82         {
83                 // NOTE:
84                 // Be very sure to not include '..' in the results, it will
85                 // result in an epic failure when deleting stuff.
86
87                 DirListNode node;
88                 node.name = FindFileData.cFileName;
89                 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
90                 if(node.name != "." && node.name != "..")
91                         listing.push_back(node);
92
93                 // List all the other files in the directory.
94                 while (FindNextFile(hFind, &FindFileData) != 0) 
95                 {
96                         DirListNode node;
97                         node.name = FindFileData.cFileName;
98                         node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
99                         if(node.name != "." && node.name != "..")
100                                 listing.push_back(node);
101                 }
102
103                 dwError = GetLastError();
104                 FindClose(hFind);
105                 if (dwError != ERROR_NO_MORE_FILES) 
106                 {
107                         errorstream<<"GetDirListing: FindNextFile error. Error is "
108                                         <<dwError<<std::endl;
109                         retval = (-1);
110                         goto Cleanup;
111                 }
112         }
113         retval  = 0;
114
115 Cleanup:
116         free(DirSpec);
117
118         if(retval != 0) listing.clear();
119
120         //for(unsigned int i=0; i<listing.size(); i++){
121         //      infostream<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
122         //}
123         
124         return listing;
125 }
126
127 bool CreateDir(std::string path)
128 {
129         bool r = CreateDirectory(path.c_str(), NULL);
130         if(r == true)
131                 return true;
132         if(GetLastError() == ERROR_ALREADY_EXISTS)
133                 return true;
134         return false;
135 }
136
137 bool PathExists(std::string path)
138 {
139         return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
140 }
141
142 bool RecursiveDelete(std::string path)
143 {
144         infostream<<"Removing \""<<path<<"\""<<std::endl;
145
146         //return false;
147         
148         // This silly function needs a double-null terminated string...
149         // Well, we'll just make sure it has at least two, then.
150         path += "\0\0";
151
152         SHFILEOPSTRUCT sfo;
153         sfo.hwnd = NULL;
154         sfo.wFunc = FO_DELETE;
155         sfo.pFrom = path.c_str();
156         sfo.pTo = NULL;
157         sfo.fFlags = FOF_SILENT | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR;
158         
159         int r = SHFileOperation(&sfo);
160
161         if(r != 0)
162                 errorstream<<"SHFileOperation returned "<<r<<std::endl;
163
164         //return (r == 0);
165         return true;
166 }
167
168 #else // POSIX
169
170 #include <sys/types.h>
171 #include <dirent.h>
172 #include <errno.h>
173 #include <sys/stat.h>
174 #include <sys/wait.h>
175
176 std::vector<DirListNode> GetDirListing(std::string pathstring)
177 {
178         std::vector<DirListNode> listing;
179
180     DIR *dp;
181     struct dirent *dirp;
182     if((dp  = opendir(pathstring.c_str())) == NULL) {
183                 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
184         return listing;
185     }
186
187     while ((dirp = readdir(dp)) != NULL) {
188                 // NOTE:
189                 // Be very sure to not include '..' in the results, it will
190                 // result in an epic failure when deleting stuff.
191                 if(dirp->d_name[0]!='.'){
192                         DirListNode node;
193                         node.name = dirp->d_name;
194                         if(node.name == "." || node.name == "..")
195                                 continue;
196
197                         int isdir = -1; // -1 means unknown
198
199                         /*
200                                 POSIX doesn't define d_type member of struct dirent and
201                                 certain filesystems on glibc/Linux will only return
202                                 DT_UNKNOWN for the d_type member.
203
204                                 Also we don't know whether symlinks are directories or not.
205                         */
206 #ifdef _DIRENT_HAVE_D_TYPE
207                         if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
208                                 isdir = (dirp->d_type == DT_DIR);
209 #endif /* _DIRENT_HAVE_D_TYPE */
210
211                         /*
212                                 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
213                                 If so, try stat().
214                         */
215                         if(isdir == -1)
216                         {
217                                 struct stat statbuf;
218                                 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
219                                         continue;
220                                 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
221                         }
222                         node.dir = isdir;
223                         listing.push_back(node);
224                 }
225     }
226     closedir(dp);
227
228         return listing;
229 }
230
231 bool CreateDir(std::string path)
232 {
233         int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
234         if(r == 0)
235         {
236                 return true;
237         }
238         else
239         {
240                 // If already exists, return true
241                 if(errno == EEXIST)
242                         return true;
243                 return false;
244         }
245 }
246
247 bool PathExists(std::string path)
248 {
249         struct stat st;
250         return (stat(path.c_str(),&st) == 0);
251 }
252
253 bool RecursiveDelete(std::string path)
254 {
255         /*
256                 Execute the 'rm' command directly, by fork() and execve()
257         */
258         
259         infostream<<"Removing \""<<path<<"\""<<std::endl;
260
261         //return false;
262         
263         pid_t child_pid = fork();
264
265         if(child_pid == 0)
266         {
267                 // Child
268                 char argv_data[3][10000];
269                 strcpy(argv_data[0], "/bin/rm");
270                 strcpy(argv_data[1], "-rf");
271                 strncpy(argv_data[2], path.c_str(), 10000);
272                 char *argv[4];
273                 argv[0] = argv_data[0];
274                 argv[1] = argv_data[1];
275                 argv[2] = argv_data[2];
276                 argv[3] = NULL;
277
278                 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
279                                 <<argv[2]<<"'"<<std::endl;
280                 
281                 execv(argv[0], argv);
282                 
283                 // Execv shouldn't return. Failed.
284                 _exit(1);
285         }
286         else
287         {
288                 // Parent
289                 int child_status;
290                 pid_t tpid;
291                 do{
292                         tpid = wait(&child_status);
293                         //if(tpid != child_pid) process_terminated(tpid);
294                 }while(tpid != child_pid);
295                 return (child_status == 0);
296         }
297 }
298
299 #endif
300
301 bool RecursiveDeleteContent(std::string path)
302 {
303         infostream<<"Removing content of \""<<path<<"\""<<std::endl;
304         std::vector<DirListNode> list = GetDirListing(path);
305         for(unsigned int i=0; i<list.size(); i++)
306         {
307                 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
308                         continue;
309                 std::string childpath = path + DIR_DELIM + list[i].name;
310                 bool r = RecursiveDelete(childpath);
311                 if(r == false)
312                 {
313                         errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
314                         return false;
315                 }
316         }
317         return true;
318 }
319
320 bool CreateAllDirs(std::string path)
321 {
322
323         size_t pos;
324         std::vector<std::string> tocreate;
325         std::string basepath = path;
326         while(!PathExists(basepath))
327         {
328                 tocreate.push_back(basepath);
329                 pos = basepath.rfind(DIR_DELIM_C);
330                 if(pos == std::string::npos)
331                         break;
332                 basepath = basepath.substr(0,pos);
333         }
334         for(int i=tocreate.size()-1;i>=0;i--)
335                 if(!CreateDir(tocreate[i]))
336                         return false;
337         return true;
338 }
339
340 } // namespace fs
341