Performance improvement: Use std::list instead of std::vector for request_media,...
[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                 if (n.dir) {
381                         GetRecursiveSubPaths(fullpath, dst);
382                 }
383         }
384 }
385
386 bool DeletePaths(const std::vector<std::string> &paths)
387 {
388         bool success = true;
389         // Go backwards to succesfully delete the output of GetRecursiveSubPaths
390         for(int i=paths.size()-1; i>=0; i--){
391                 const std::string &path = paths[i];
392                 bool did = DeleteSingleFileOrEmptyDirectory(path);
393                 if(!did){
394                         errorstream<<"Failed to delete "<<path<<std::endl;
395                         success = false;
396                 }
397         }
398         return success;
399 }
400
401 bool RecursiveDeleteContent(std::string path)
402 {
403         infostream<<"Removing content of \""<<path<<"\""<<std::endl;
404         std::vector<DirListNode> list = GetDirListing(path);
405         for(unsigned int i=0; i<list.size(); i++)
406         {
407                 if(trim(list[i].name) == "." || trim(list[i].name) == "..")
408                         continue;
409                 std::string childpath = path + DIR_DELIM + list[i].name;
410                 bool r = RecursiveDelete(childpath);
411                 if(r == false)
412                 {
413                         errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
414                         return false;
415                 }
416         }
417         return true;
418 }
419
420 bool CreateAllDirs(std::string path)
421 {
422
423         std::vector<std::string> tocreate;
424         std::string basepath = path;
425         while(!PathExists(basepath))
426         {
427                 tocreate.push_back(basepath);
428                 basepath = RemoveLastPathComponent(basepath);
429                 if(basepath.empty())
430                         break;
431         }
432         for(int i=tocreate.size()-1;i>=0;i--)
433                 if(!CreateDir(tocreate[i]))
434                         return false;
435         return true;
436 }
437
438 bool CopyFileContents(std::string source, std::string target)
439 {
440         FILE *sourcefile = fopen(source.c_str(), "rb");
441         if(sourcefile == NULL){
442                 errorstream<<source<<": can't open for reading: "
443                         <<strerror(errno)<<std::endl;
444                 return false;
445         }
446
447         FILE *targetfile = fopen(target.c_str(), "wb");
448         if(targetfile == NULL){
449                 errorstream<<target<<": can't open for writing: "
450                         <<strerror(errno)<<std::endl;
451                 fclose(sourcefile);
452                 return false;
453         }
454
455         size_t total = 0;
456         bool retval = true;
457         bool done = false;
458         char readbuffer[BUFSIZ];
459         while(!done){
460                 size_t readbytes = fread(readbuffer, 1,
461                                 sizeof(readbuffer), sourcefile);
462                 total += readbytes;
463                 if(ferror(sourcefile)){
464                         errorstream<<source<<": IO error: "
465                                 <<strerror(errno)<<std::endl;
466                         retval = false;
467                         done = true;
468                 }
469                 if(readbytes > 0){
470                         fwrite(readbuffer, 1, readbytes, targetfile);
471                 }
472                 if(feof(sourcefile) || ferror(sourcefile)){
473                         // flush destination file to catch write errors
474                         // (e.g. disk full)
475                         fflush(targetfile);
476                         done = true;
477                 }
478                 if(ferror(targetfile)){
479                         errorstream<<target<<": IO error: "
480                                         <<strerror(errno)<<std::endl;
481                         retval = false;
482                         done = true;
483                 }
484         }
485         infostream<<"copied "<<total<<" bytes from "
486                 <<source<<" to "<<target<<std::endl;
487         fclose(sourcefile);
488         fclose(targetfile);
489         return retval;
490 }
491
492 bool CopyDir(std::string source, std::string target)
493 {
494         if(PathExists(source)){
495                 if(!PathExists(target)){
496                         fs::CreateAllDirs(target);
497                 }
498                 bool retval = true;
499                 std::vector<DirListNode> content = fs::GetDirListing(source);
500
501                 for(unsigned int i=0; i < content.size(); i++){
502                         std::string sourcechild = source + DIR_DELIM + content[i].name;
503                         std::string targetchild = target + DIR_DELIM + content[i].name;
504                         if(content[i].dir){
505                                 if(!fs::CopyDir(sourcechild, targetchild)){
506                                         retval = false;
507                                 }
508                         }
509                         else {
510                                 if(!fs::CopyFileContents(sourcechild, targetchild)){
511                                         retval = false;
512                                 }
513                         }
514                 }
515                 return retval;
516         }
517         else {
518                 return false;
519         }
520 }
521
522 bool PathStartsWith(std::string path, std::string prefix)
523 {
524         size_t pathsize = path.size();
525         size_t pathpos = 0;
526         size_t prefixsize = prefix.size();
527         size_t prefixpos = 0;
528         for(;;){
529                 bool delim1 = pathpos == pathsize
530                         || IsDirDelimiter(path[pathpos]);
531                 bool delim2 = prefixpos == prefixsize
532                         || IsDirDelimiter(prefix[prefixpos]);
533
534                 if(delim1 != delim2)
535                         return false;
536
537                 if(delim1){
538                         while(pathpos < pathsize &&
539                                         IsDirDelimiter(path[pathpos]))
540                                 ++pathpos;
541                         while(prefixpos < prefixsize &&
542                                         IsDirDelimiter(prefix[prefixpos]))
543                                 ++prefixpos;
544                         if(prefixpos == prefixsize)
545                                 return true;
546                         if(pathpos == pathsize)
547                                 return false;
548                 }
549                 else{
550                         size_t len = 0;
551                         do{
552                                 char pathchar = path[pathpos+len];
553                                 char prefixchar = prefix[prefixpos+len];
554                                 if(FILESYS_CASE_INSENSITIVE){
555                                         pathchar = tolower(pathchar);
556                                         prefixchar = tolower(prefixchar);
557                                 }
558                                 if(pathchar != prefixchar)
559                                         return false;
560                                 ++len;
561                         } while(pathpos+len < pathsize
562                                         && !IsDirDelimiter(path[pathpos+len])
563                                         && prefixpos+len < prefixsize
564                                         && !IsDirDelimiter(
565                                                 prefix[prefixpos+len]));
566                         pathpos += len;
567                         prefixpos += len;
568                 }
569         }
570 }
571
572 std::string RemoveLastPathComponent(std::string path,
573                 std::string *removed, int count)
574 {
575         if(removed)
576                 *removed = "";
577
578         size_t remaining = path.size();
579
580         for(int i = 0; i < count; ++i){
581                 // strip a dir delimiter
582                 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
583                         remaining--;
584                 // strip a path component
585                 size_t component_end = remaining;
586                 while(remaining != 0 && !IsDirDelimiter(path[remaining-1]))
587                         remaining--;
588                 size_t component_start = remaining;
589                 // strip a dir delimiter
590                 while(remaining != 0 && IsDirDelimiter(path[remaining-1]))
591                         remaining--;
592                 if(removed){
593                         std::string component = path.substr(component_start,
594                                         component_end - component_start);
595                         if(i)
596                                 *removed = component + DIR_DELIM + *removed;
597                         else
598                                 *removed = component;
599                 }
600         }
601         return path.substr(0, remaining);
602 }
603
604 std::string RemoveRelativePathComponents(std::string path)
605 {
606         size_t pos = path.size();
607         size_t dotdot_count = 0;
608         while(pos != 0){
609                 size_t component_with_delim_end = pos;
610                 // skip a dir delimiter
611                 while(pos != 0 && IsDirDelimiter(path[pos-1]))
612                         pos--;
613                 // strip a path component
614                 size_t component_end = pos;
615                 while(pos != 0 && !IsDirDelimiter(path[pos-1]))
616                         pos--;
617                 size_t component_start = pos;
618
619                 std::string component = path.substr(component_start,
620                                 component_end - component_start);
621                 bool remove_this_component = false;
622                 if(component == "."){
623                         remove_this_component = true;
624                 }
625                 else if(component == ".."){
626                         remove_this_component = true;
627                         dotdot_count += 1;
628                 }
629                 else if(dotdot_count != 0){
630                         remove_this_component = true;
631                         dotdot_count -= 1;
632                 }
633
634                 if(remove_this_component){
635                         while(pos != 0 && IsDirDelimiter(path[pos-1]))
636                                 pos--;
637                         path = path.substr(0, pos) + DIR_DELIM +
638                                 path.substr(component_with_delim_end,
639                                                 std::string::npos);
640                         pos++;
641                 }
642         }
643
644         if(dotdot_count > 0)
645                 return "";
646
647         // remove trailing dir delimiters
648         pos = path.size();
649         while(pos != 0 && IsDirDelimiter(path[pos-1]))
650                 pos--;
651         return path.substr(0, pos);
652 }
653
654 bool safeWriteToFile(const std::string &path, const std::string &content)
655 {
656         std::string tmp_file = path + ".~mt";
657
658         // Write to a tmp file
659         std::ofstream os(tmp_file.c_str(), std::ios::binary);
660         if (!os.good())
661                 return false;
662         os << content;
663         os.flush();
664         os.close();
665         if (os.fail()) {
666                 remove(tmp_file.c_str());
667                 return false;
668         }
669
670         // Copy file
671         remove(path.c_str());
672         if(rename(tmp_file.c_str(), path.c_str())) {
673                 remove(tmp_file.c_str());
674                 return false;
675         } else {
676                 return true;
677         }
678 }
679
680 } // namespace fs
681