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