Add support for Android 2.3+
[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 "util/string.h"
22 #include <iostream>
23 #include <stdio.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <fstream>
27 #include "log.h"
28 #include "config.h"
29
30 namespace fs
31 {
32
33 #ifdef _WIN32 // WINDOWS
34
35 #define _WIN32_WINNT 0x0501
36 #include <windows.h>
37 #include <malloc.h>
38 #include <tchar.h>
39 #include <wchar.h>
40
41 #define BUFSIZE MAX_PATH
42
43 std::vector<DirListNode> GetDirListing(std::string pathstring)
44 {
45         std::vector<DirListNode> listing;
46
47         WIN32_FIND_DATA FindFileData;
48         HANDLE hFind = INVALID_HANDLE_VALUE;
49         DWORD dwError;
50         LPTSTR DirSpec;
51         INT retval;
52
53         DirSpec = (LPTSTR) malloc (BUFSIZE);
54
55         if( DirSpec == NULL )
56         {
57           errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
58           retval = 1;
59           goto Cleanup;
60         }
61
62         // Check that the input is not larger than allowed.
63         if (pathstring.size() > (BUFSIZE - 2))
64         {
65           errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
66           retval = 3;
67           goto Cleanup;
68         }
69
70         //_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
71
72         sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
73
74         // Find the first file in the directory.
75         hFind = FindFirstFile(DirSpec, &FindFileData);
76
77         if (hFind == INVALID_HANDLE_VALUE)
78         {
79                 retval = (-1);
80                 goto Cleanup;
81         }
82         else
83         {
84                 // NOTE:
85                 // Be very sure to not include '..' in the results, it will
86                 // result in an epic failure when deleting stuff.
87
88                 DirListNode node;
89                 node.name = FindFileData.cFileName;
90                 node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
91                 if(node.name != "." && node.name != "..")
92                         listing.push_back(node);
93
94                 // List all the other files in the directory.
95                 while (FindNextFile(hFind, &FindFileData) != 0)
96                 {
97                         DirListNode node;
98                         node.name = FindFileData.cFileName;
99                         node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
100                         if(node.name != "." && node.name != "..")
101                                 listing.push_back(node);
102                 }
103
104                 dwError = GetLastError();
105                 FindClose(hFind);
106                 if (dwError != ERROR_NO_MORE_FILES)
107                 {
108                         errorstream<<"GetDirListing: FindNextFile error. Error is "
109                                         <<dwError<<std::endl;
110                         retval = (-1);
111                         goto Cleanup;
112                 }
113         }
114         retval  = 0;
115
116 Cleanup:
117         free(DirSpec);
118
119         if(retval != 0) listing.clear();
120
121         //for(unsigned int i=0; i<listing.size(); i++){
122         //      infostream<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
123         //}
124         
125         return listing;
126 }
127
128 bool CreateDir(std::string path)
129 {
130         bool r = CreateDirectory(path.c_str(), NULL);
131         if(r == true)
132                 return true;
133         if(GetLastError() == ERROR_ALREADY_EXISTS)
134                 return true;
135         return false;
136 }
137
138 bool PathExists(std::string path)
139 {
140         return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
141 }
142
143 bool IsDir(std::string path)
144 {
145         DWORD attr = GetFileAttributes(path.c_str());
146         return (attr != INVALID_FILE_ATTRIBUTES &&
147                         (attr & FILE_ATTRIBUTE_DIRECTORY));
148 }
149
150 bool IsDirDelimiter(char c)
151 {
152         return c == '/' || c == '\\';
153 }
154
155 bool RecursiveDelete(std::string path)
156 {
157         infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
158
159         DWORD attr = GetFileAttributes(path.c_str());
160         bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
161                         (attr & FILE_ATTRIBUTE_DIRECTORY));
162         if(!is_directory)
163         {
164                 infostream<<"RecursiveDelete: Deleting file "<<path<<std::endl;
165                 //bool did = DeleteFile(path.c_str());
166                 bool did = true;
167                 if(!did){
168                         errorstream<<"RecursiveDelete: Failed to delete file "
169                                         <<path<<std::endl;
170                         return false;
171                 }
172         }
173         else
174         {
175                 infostream<<"RecursiveDelete: Deleting content of directory "
176                                 <<path<<std::endl;
177                 std::vector<DirListNode> content = GetDirListing(path);
178                 for(int i=0; i<content.size(); i++){
179                         const DirListNode &n = content[i];
180                         std::string fullpath = path + DIR_DELIM + n.name;
181                         bool did = RecursiveDelete(fullpath);
182                         if(!did){
183                                 errorstream<<"RecursiveDelete: Failed to recurse to "
184                                                 <<fullpath<<std::endl;
185                                 return false;
186                         }
187                 }
188                 infostream<<"RecursiveDelete: Deleting directory "<<path<<std::endl;
189                 //bool did = RemoveDirectory(path.c_str();
190                 bool did = true;
191                 if(!did){
192                         errorstream<<"Failed to recursively delete directory "
193                                         <<path<<std::endl;
194                         return false;
195                 }
196         }
197         return true;
198 }
199
200 bool DeleteSingleFileOrEmptyDirectory(std::string path)
201 {
202         DWORD attr = GetFileAttributes(path.c_str());
203         bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
204                         (attr & FILE_ATTRIBUTE_DIRECTORY));
205         if(!is_directory)
206         {
207                 bool did = DeleteFile(path.c_str());
208                 return did;
209         }
210         else
211         {
212                 bool did = RemoveDirectory(path.c_str());
213                 return did;
214         }
215 }
216
217 std::string TempPath()
218 {
219         DWORD bufsize = GetTempPath(0, "");
220         if(bufsize == 0){
221                 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
222                 return "";
223         }
224         std::vector<char> buf(bufsize);
225         DWORD len = GetTempPath(bufsize, &buf[0]);
226         if(len == 0 || len > bufsize){
227                 errorstream<<"GetTempPath failed, error = "<<GetLastError()<<std::endl;
228                 return "";
229         }
230         return std::string(buf.begin(), buf.begin() + len);
231 }
232
233 #else // POSIX
234
235 #include <sys/types.h>
236 #include <dirent.h>
237 #include <sys/stat.h>
238 #include <sys/wait.h>
239 #include <unistd.h>
240
241 std::vector<DirListNode> GetDirListing(std::string pathstring)
242 {
243         std::vector<DirListNode> listing;
244
245     DIR *dp;
246     struct dirent *dirp;
247     if((dp  = opendir(pathstring.c_str())) == NULL) {
248                 //infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
249         return listing;
250     }
251
252     while ((dirp = readdir(dp)) != NULL) {
253                 // NOTE:
254                 // Be very sure to not include '..' in the results, it will
255                 // result in an epic failure when deleting stuff.
256                 if(dirp->d_name[0]!='.'){
257                         DirListNode node;
258                         node.name = dirp->d_name;
259                         if(node.name == "." || node.name == "..")
260                                 continue;
261
262                         int isdir = -1; // -1 means unknown
263
264                         /*
265                                 POSIX doesn't define d_type member of struct dirent and
266                                 certain filesystems on glibc/Linux will only return
267                                 DT_UNKNOWN for the d_type member.
268
269                                 Also we don't know whether symlinks are directories or not.
270                         */
271 #ifdef _DIRENT_HAVE_D_TYPE
272                         if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
273                                 isdir = (dirp->d_type == DT_DIR);
274 #endif /* _DIRENT_HAVE_D_TYPE */
275
276                         /*
277                                 Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
278                                 If so, try stat().
279                         */
280                         if(isdir == -1)
281                         {
282                                 struct stat statbuf;
283                                 if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
284                                         continue;
285                                 isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
286                         }
287                         node.dir = isdir;
288                         listing.push_back(node);
289                 }
290     }
291     closedir(dp);
292
293         return listing;
294 }
295
296 bool CreateDir(std::string path)
297 {
298         int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
299         if(r == 0)
300         {
301                 return true;
302         }
303         else
304         {
305                 // If already exists, return true
306                 if(errno == EEXIST)
307                         return true;
308                 return false;
309         }
310 }
311
312 bool PathExists(std::string path)
313 {
314         struct stat st;
315         return (stat(path.c_str(),&st) == 0);
316 }
317
318 bool IsDir(std::string path)
319 {
320         struct stat statbuf;
321         if(stat(path.c_str(), &statbuf))
322                 return false; // Actually error; but certainly not a directory
323         return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
324 }
325
326 bool IsDirDelimiter(char c)
327 {
328         return c == '/';
329 }
330
331 bool RecursiveDelete(std::string path)
332 {
333         /*
334                 Execute the 'rm' command directly, by fork() and execve()
335         */
336         
337         infostream<<"Removing \""<<path<<"\""<<std::endl;
338
339         //return false;
340         
341         pid_t child_pid = fork();
342
343         if(child_pid == 0)
344         {
345                 // Child
346                 char argv_data[3][10000];
347                 strcpy(argv_data[0], "/bin/rm");
348                 strcpy(argv_data[1], "-rf");
349                 strncpy(argv_data[2], path.c_str(), 10000);
350                 char *argv[4];
351                 argv[0] = argv_data[0];
352                 argv[1] = argv_data[1];
353                 argv[2] = argv_data[2];
354                 argv[3] = NULL;
355
356                 verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
357                                 <<argv[2]<<"'"<<std::endl;
358                 
359                 execv(argv[0], argv);
360                 
361                 // Execv shouldn't return. Failed.
362                 _exit(1);
363         }
364         else
365         {
366                 // Parent
367                 int child_status;
368                 pid_t tpid;
369                 do{
370                         tpid = wait(&child_status);
371                         //if(tpid != child_pid) process_terminated(tpid);
372                 }while(tpid != child_pid);
373                 return (child_status == 0);
374         }
375 }
376
377 bool DeleteSingleFileOrEmptyDirectory(std::string path)
378 {
379         if(IsDir(path)){
380                 bool did = (rmdir(path.c_str()) == 0);
381                 if(!did)
382                         errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
383                                         <<std::endl;
384                 return did;
385         } else {
386                 bool did = (unlink(path.c_str()) == 0);
387                 if(!did)
388                         errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
389                                         <<std::endl;
390                 return did;
391         }
392 }
393
394 std::string TempPath()
395 {
396         /*
397                 Should the environment variables TMPDIR, TMP and TEMP
398                 and the macro P_tmpdir (if defined by stdio.h) be checked
399                 before falling back on /tmp?
400
401                 Probably not, because this function is intended to be
402                 compatible with lua's os.tmpname which under the default
403                 configuration hardcodes mkstemp("/tmp/lua_XXXXXX").
404         */
405 #ifdef __ANDROID__
406         return DIR_DELIM "sdcard" DIR_DELIM PROJECT_NAME DIR_DELIM "tmp";
407 #else
408         return DIR_DELIM "tmp";
409 #endif
410 }
411
412 #endif
413
414 void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst)
415 {
416         std::vector<DirListNode> content = GetDirListing(path);
417         for(unsigned int  i=0; i<content.size(); i++){
418                 const DirListNode &n = content[i];
419                 std::string fullpath = path + DIR_DELIM + n.name;
420                 dst.push_back(fullpath);
421                 GetRecursiveSubPaths(fullpath, dst);
422         }
423 }
424
425 bool DeletePaths(const std::vector<std::string> &paths)
426 {
427         bool success = true;
428         // Go backwards to succesfully delete the output of GetRecursiveSubPaths
429         for(int i=paths.size()-1; i>=0; i--){
430                 const std::string &path = paths[i];
431                 bool did = DeleteSingleFileOrEmptyDirectory(path);
432                 if(!did){
433                         errorstream<<"Failed to delete "<<path<<std::endl;
434                         success = false;
435                 }
436         }
437         return success;
438 }
439
440 bool RecursiveDeleteContent(std::string path)
441 {
442         infostream<<"Removing content of \""<<path<<"\""<<std::endl;
443         std::vector<DirListNode> list = GetDirListing(path);
444         for(unsigned int i=0; i<list.size(); i++)
445         {
446                 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
447                         continue;
448                 std::string childpath = path + DIR_DELIM + list[i].name;
449                 bool r = RecursiveDelete(childpath);
450                 if(r == false)
451                 {
452                         errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
453                         return false;
454                 }
455         }
456         return true;
457 }
458
459 bool CreateAllDirs(std::string path)
460 {
461
462         std::vector<std::string> tocreate;
463         std::string basepath = path;
464         while(!PathExists(basepath))
465         {
466                 tocreate.push_back(basepath);
467                 basepath = RemoveLastPathComponent(basepath);
468                 if(basepath.empty())
469                         break;
470         }
471         for(int i=tocreate.size()-1;i>=0;i--)
472                 if(!CreateDir(tocreate[i]))
473                         return false;
474         return true;
475 }
476
477 bool CopyFileContents(std::string source, std::string target)
478 {
479         FILE *sourcefile = fopen(source.c_str(), "rb");
480         if(sourcefile == NULL){
481                 errorstream<<source<<": can't open for reading: "
482                         <<strerror(errno)<<std::endl;
483                 return false;
484         }
485
486         FILE *targetfile = fopen(target.c_str(), "wb");
487         if(targetfile == NULL){
488                 errorstream<<target<<": can't open for writing: "
489                         <<strerror(errno)<<std::endl;
490                 fclose(sourcefile);
491                 return false;
492         }
493
494         size_t total = 0;
495         bool retval = true;
496         bool done = false;
497         char readbuffer[BUFSIZ];
498         while(!done){
499                 size_t readbytes = fread(readbuffer, 1,
500                                 sizeof(readbuffer), sourcefile);
501                 total += readbytes;
502                 if(ferror(sourcefile)){
503                         errorstream<<source<<": IO error: "
504                                 <<strerror(errno)<<std::endl;
505                         retval = false;
506                         done = true;
507                 }
508                 if(readbytes > 0){
509                         fwrite(readbuffer, 1, readbytes, targetfile);
510                 }
511                 if(feof(sourcefile) || ferror(sourcefile)){
512                         // flush destination file to catch write errors
513                         // (e.g. disk full)
514                         fflush(targetfile);
515                         done = true;
516                 }
517                 if(ferror(targetfile)){
518                         errorstream<<target<<": IO error: "
519                                         <<strerror(errno)<<std::endl;
520                         retval = false;
521                         done = true;
522                 }
523         }
524         infostream<<"copied "<<total<<" bytes from "
525                 <<source<<" to "<<target<<std::endl;
526         fclose(sourcefile);
527         fclose(targetfile);
528         return retval;
529 }
530
531 bool CopyDir(std::string source, std::string target)
532 {
533         if(PathExists(source)){
534                 if(!PathExists(target)){
535                         fs::CreateAllDirs(target);
536                 }
537                 bool retval = true;
538                 std::vector<DirListNode> content = fs::GetDirListing(source);
539
540                 for(unsigned int i=0; i < content.size(); i++){
541                         std::string sourcechild = source + DIR_DELIM + content[i].name;
542                         std::string targetchild = target + DIR_DELIM + content[i].name;
543                         if(content[i].dir){
544                                 if(!fs::CopyDir(sourcechild, targetchild)){
545                                         retval = false;
546                                 }
547                         }
548                         else {
549                                 if(!fs::CopyFileContents(sourcechild, targetchild)){
550                                         retval = false;
551                                 }
552                         }
553                 }
554                 return retval;
555         }
556         else {
557                 return false;
558         }
559 }
560
561 bool PathStartsWith(std::string path, std::string prefix)
562 {
563         size_t pathsize = path.size();
564         size_t pathpos = 0;
565         size_t prefixsize = prefix.size();
566         size_t prefixpos = 0;
567         for(;;){
568                 bool delim1 = pathpos == pathsize
569                         || IsDirDelimiter(path[pathpos]);
570                 bool delim2 = prefixpos == prefixsize
571                         || IsDirDelimiter(prefix[prefixpos]);
572
573                 if(delim1 != delim2)
574                         return false;
575
576                 if(delim1){
577                         while(pathpos < pathsize &&
578                                         IsDirDelimiter(path[pathpos]))
579                                 ++pathpos;
580                         while(prefixpos < prefixsize &&
581                                         IsDirDelimiter(prefix[prefixpos]))
582                                 ++prefixpos;
583                         if(prefixpos == prefixsize)
584                                 return true;
585                         if(pathpos == pathsize)
586                                 return false;
587                 }
588                 else{
589                         size_t len = 0;
590                         do{
591                                 char pathchar = path[pathpos+len];
592                                 char prefixchar = prefix[prefixpos+len];
593                                 if(FILESYS_CASE_INSENSITIVE){
594                                         pathchar = tolower(pathchar);
595                                         prefixchar = tolower(prefixchar);
596                                 }
597                                 if(pathchar != prefixchar)
598                                         return false;
599                                 ++len;
600                         } while(pathpos+len < pathsize
601                                         && !IsDirDelimiter(path[pathpos+len])
602                                         && prefixpos+len < prefixsize
603                                         && !IsDirDelimiter(
604                                                 prefix[prefixpos+len]));
605                         pathpos += len;
606                         prefixpos += len;
607                 }
608         }
609 }
610
611 std::string RemoveLastPathComponent(std::string path,
612                 std::string *removed, int count)
613 {
614         if(removed)
615                 *removed = "";
616
617         size_t remaining = path.size();
618
619         for(int i = 0; i < count; ++i){
620                 // strip a dir delimiter
621                 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
622                         remaining--;
623                 // strip a path component
624                 size_t component_end = remaining;
625                 while(remaining != 0 && !IsDirDelimiter(path[remaining-1]))
626                         remaining--;
627                 size_t component_start = remaining;
628                 // strip a dir delimiter
629                 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
630                         remaining--;
631                 if(removed){
632                         std::string component = path.substr(component_start,
633                                         component_end - component_start);
634                         if(i)
635                                 *removed = component + DIR_DELIM + *removed;
636                         else
637                                 *removed = component;
638                 }
639         }
640         return path.substr(0, remaining);
641 }
642
643 std::string RemoveRelativePathComponents(std::string path)
644 {
645         size_t pos = path.size();
646         size_t dotdot_count = 0;
647         while(pos != 0){
648                 size_t component_with_delim_end = pos;
649                 // skip a dir delimiter
650                 while(pos != 0 && IsDirDelimiter(path[pos-1]))
651                         pos--;
652                 // strip a path component
653                 size_t component_end = pos;
654                 while(pos != 0 && !IsDirDelimiter(path[pos-1]))
655                         pos--;
656                 size_t component_start = pos;
657
658                 std::string component = path.substr(component_start,
659                                 component_end - component_start);
660                 bool remove_this_component = false;
661                 if(component == "."){
662                         remove_this_component = true;
663                 }
664                 else if(component == ".."){
665                         remove_this_component = true;
666                         dotdot_count += 1;
667                 }
668                 else if(dotdot_count != 0){
669                         remove_this_component = true;
670                         dotdot_count -= 1;
671                 }
672
673                 if(remove_this_component){
674                         while(pos != 0 && IsDirDelimiter(path[pos-1]))
675                                 pos--;
676                         path = path.substr(0, pos) + DIR_DELIM +
677                                 path.substr(component_with_delim_end,
678                                                 std::string::npos);
679                         pos++;
680                 }
681         }
682
683         if(dotdot_count > 0)
684                 return "";
685
686         // remove trailing dir delimiters
687         pos = path.size();
688         while(pos != 0 && IsDirDelimiter(path[pos-1]))
689                 pos--;
690         return path.substr(0, pos);
691 }
692
693 bool safeWriteToFile(const std::string &path, const std::string &content)
694 {
695         std::string tmp_file = path + ".~mt";
696
697         // Write to a tmp file
698         std::ofstream os(tmp_file.c_str(), std::ios::binary);
699         if (!os.good())
700                 return false;
701         os << content;
702         os.flush();
703         os.close();
704         if (os.fail())
705                 return false;
706
707         // Copy file
708 #ifdef _WIN32
709         remove(path.c_str());
710         return (rename(tmp_file.c_str(), path.c_str()) == 0);
711 #else
712         return (rename(tmp_file.c_str(), path.c_str()) == 0);
713 #endif
714 }
715
716 } // namespace fs
717