]> git.lizzy.rs Git - minetest.git/blob - src/filesys.h
a26fe28d6f21f41c795e3118e20a20e7c2b08dd7
[minetest.git] / src / filesys.h
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 #pragma once
21
22 #include <set>
23 #include <string>
24 #include <vector>
25 #include "exceptions.h"
26
27 #ifdef _WIN32
28 #define DIR_DELIM "\\"
29 #define DIR_DELIM_CHAR '\\'
30 #define FILESYS_CASE_INSENSITIVE true
31 #define PATH_DELIM ";"
32 #else
33 #define DIR_DELIM "/"
34 #define DIR_DELIM_CHAR '/'
35 #define FILESYS_CASE_INSENSITIVE false
36 #define PATH_DELIM ":"
37 #endif
38
39 namespace irr { namespace io {
40 class IFileSystem;
41 }}
42
43 namespace fs
44 {
45
46 struct DirListNode
47 {
48         std::string name;
49         bool dir;
50 };
51
52 std::vector<DirListNode> GetDirListing(const std::string &path);
53
54 // Returns true if already exists
55 bool CreateDir(const std::string &path);
56
57 bool PathExists(const std::string &path);
58
59 bool IsPathAbsolute(const std::string &path);
60
61 bool IsDir(const std::string &path);
62
63 inline bool IsFile(const std::string &path)
64 {
65         return PathExists(path) && !IsDir(path);
66 }
67
68 bool IsDirDelimiter(char c);
69
70 // Only pass full paths to this one. True on success.
71 // NOTE: The WIN32 version returns always true.
72 bool RecursiveDelete(const std::string &path);
73
74 bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
75
76 // Returns path to temp directory, can return "" on error
77 std::string TempPath();
78
79 // Returns path to securely-created temporary file (will already exist when this function returns)
80 // can return "" on error
81 std::string CreateTempFile();
82
83 /* Returns a list of subdirectories, including the path itself, but excluding
84        hidden directories (whose names start with . or _)
85 */
86 void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
87 std::vector<std::string> GetRecursiveDirs(const std::string &dir);
88
89 /* Multiplatform */
90
91 /* The path itself not included, returns a list of all subpaths.
92    dst - vector that contains all the subpaths.
93    list files - include files in the list of subpaths.
94    ignore - paths that start with these charcters will not be listed.
95 */
96 void GetRecursiveSubPaths(const std::string &path,
97                   std::vector<std::string> &dst,
98                   bool list_files,
99                   const std::set<char> &ignore = {});
100
101 // Only pass full paths to this one. True on success.
102 bool RecursiveDeleteContent(const std::string &path);
103
104 // Create all directories on the given path that don't already exist.
105 bool CreateAllDirs(const std::string &path);
106
107 // Copy a regular file
108 bool CopyFileContents(const std::string &source, const std::string &target);
109
110 // Copy directory and all subdirectories
111 // Omits files and subdirectories that start with a period
112 bool CopyDir(const std::string &source, const std::string &target);
113
114 // Move directory and all subdirectories
115 // Behavior with files/subdirs that start with a period is undefined
116 bool MoveDir(const std::string &source, const std::string &target);
117
118 // Check if one path is prefix of another
119 // For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
120 // Ignores case differences and '/' vs. '\\' on Windows
121 bool PathStartsWith(const std::string &path, const std::string &prefix);
122
123 // Remove last path component and the dir delimiter before and/or after it,
124 // returns "" if there is only one path component.
125 // removed: If non-NULL, receives the removed component(s).
126 // count: Number of components to remove
127 std::string RemoveLastPathComponent(const std::string &path,
128                std::string *removed = NULL, int count = 1);
129
130 // Remove "." and ".." path components and for every ".." removed, remove
131 // the last normal path component before it. Unlike AbsolutePath,
132 // this does not resolve symlinks and check for existence of directories.
133 std::string RemoveRelativePathComponents(std::string path);
134
135 // Returns the absolute path for the passed path, with "." and ".." path
136 // components and symlinks removed.  Returns "" on error.
137 std::string AbsolutePath(const std::string &path);
138
139 // Returns the filename from a path or the entire path if no directory
140 // delimiter is found.
141 const char *GetFilenameFromPath(const char *path);
142
143 bool safeWriteToFile(const std::string &path, const std::string &content);
144
145 #ifndef SERVER
146 bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination);
147 #endif
148
149 bool ReadFile(const std::string &path, std::string &out);
150
151 bool Rename(const std::string &from, const std::string &to);
152
153 } // namespace fs