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