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