Luanti 5.17.0-dev
Loading...
Searching...
No Matches
filesys.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "config.h"
8#include <string>
9#include <string_view>
10#include <vector>
11#include <fstream>
12
13#ifdef _WIN32
14#define DIR_DELIM "\\"
15#define DIR_DELIM_CHAR '\\'
16#define FILESYS_CASE_INSENSITIVE true
17#define PATH_DELIM ";"
18#else
19#define DIR_DELIM "/"
20#define DIR_DELIM_CHAR '/'
21#define FILESYS_CASE_INSENSITIVE false
22#define PATH_DELIM ":"
23#endif
24
25namespace io {
26class IFileSystem;
27}
28
29namespace fs
30{
31
33{
34 std::string name;
35 bool dir;
36};
37
38[[nodiscard]]
39std::vector<DirListNode> GetDirListing(const std::string &path);
40
41// Returns true if already exists
42bool CreateDir(const std::string &path);
43
44[[nodiscard]] bool PathExists(const std::string &path);
45
46[[nodiscard]] bool IsPathAbsolute(const std::string &path);
47
48[[nodiscard]] bool IsDir(const std::string &path);
49
50[[nodiscard]] bool IsExecutable(const std::string &path);
51
52[[nodiscard]] bool IsFile(const std::string &path);
53
54[[nodiscard]] inline bool IsDirDelimiter(char c)
55{
56 return c == '/' || c == DIR_DELIM_CHAR;
57}
58
59// Only pass full paths to this one. returns true on success.
60bool RecursiveDelete(const std::string &path);
61
62bool DeleteSingleFileOrEmptyDirectory(const std::string &path, bool log_error = false);
63
67[[nodiscard]] std::string TempPath();
68
71[[nodiscard]] std::string CreateTempFile();
72
75[[nodiscard]] std::string CreateTempDir();
76
77/* Returns a list of subdirectories, including the path itself, but excluding
78 hidden directories (whose names start with . or _)
79*/
80void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
81
82[[nodiscard]]
83std::vector<std::string> GetRecursiveDirs(const std::string &dir);
84
85/* The path itself not included, returns a list of all subpaths.
86 dst - vector that contains all the subpaths.
87 list files - include files in the list of subpaths.
88 ignore - paths that start with one of these characters will not be listed.
89*/
90void GetRecursiveSubPaths(const std::string &path,
91 std::vector<std::string> &dst,
92 bool list_files,
93 std::string_view ignore = {});
94
95// Create all directories on the given path that don't already exist.
96bool CreateAllDirs(const std::string &path);
97
98// Copy a regular file
99bool CopyFileContents(const std::string &source, const std::string &target);
100
101// Copy directory and all subdirectories
102// Omits files and subdirectories that start with a period
103bool CopyDir(const std::string &source, const std::string &target);
104
105// Move directory and all subdirectories
106// Behavior with files/subdirs that start with a period is undefined
107bool MoveDir(const std::string &source, const std::string &target);
108
109// Check if one path is prefix of another
110// For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
111// Ignores case differences and '/' vs. '\\' on Windows
112bool PathStartsWith(const std::string &path, const std::string &prefix);
113
114// If child is (as absolute path) inside parent (also as absolute path), returns
115// the part of child that is relative to parent.
116// Symlinks and "." and ".." components are removed.
117// If child and parent are (absolute) the same, the result is ".". (Otherwise it
118// never starts with '.'.)
119// Returns "" if child is not in parent, also returns "" on failure.
120std::string MakePathRelativeTo(const std::string &child, const std::string &parent);
121
122// Remove last path component and the dir delimiter before and/or after it.
123// If there's only one path component it will refuse to remove it (if absolute)
124// or return "" (if relative).
125// removed: If non-NULL, receives the removed components
126// count: Number of components to remove
127std::string RemoveLastPathComponent(const std::string &path,
128 std::string *removed = nullptr, 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.
133std::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[[nodiscard]]
138std::string AbsolutePath(const std::string &path);
139
140// This is a combination of RemoveRelativePathComponents() and AbsolutePath()
141// It will resolve symlinks for the leading path components that exist and
142// still remove "." and ".." in the rest of the path.
143// Returns "" on error.
144[[nodiscard]]
145std::string AbsolutePathPartial(const std::string &path);
146
147// Returns the filename from a path or the entire path if no directory
148// delimiter is found.
149[[nodiscard]]
150const char *GetFilenameFromPath(const char *path);
151
152// Replace the content of a file on disk in a way that is safe from
153// corruption/truncation on a crash.
154// logs and returns false on error
155bool safeWriteToFile(const std::string &path, std::string_view content);
156
157#if IS_CLIENT_BUILD
158bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination);
159#endif
160
161bool ReadFile(const std::string &path, std::string &out, bool log_error = false);
162
163bool Rename(const std::string &from, const std::string &to);
164
175bool OpenStream(std::filebuf &stream, const char *filename,
176 std::ios::openmode mode, bool log_error, bool log_warn);
177
178} // namespace fs
179
180// outside of namespace for convenience:
181
192[[nodiscard]]
193inline std::ofstream open_ofstream(const char *name, bool log,
194 std::ios::openmode mode = std::ios::openmode())
195{
196 mode |= std::ios::out | std::ios::binary;
197 if (!(mode & std::ios::app))
198 mode |= std::ios::trunc;
199 std::ofstream ofs;
200 if (!fs::OpenStream(*ofs.rdbuf(), name, mode, log, false))
201 ofs.setstate(std::ios::failbit);
202 return ofs;
203}
204
215[[nodiscard]]
216inline std::ifstream open_ifstream(const char *name, bool log,
217 std::ios::openmode mode = std::ios::openmode())
218{
219 mode |= std::ios::in | std::ios::binary;
220 std::ifstream ifs;
221 if (!fs::OpenStream(*ifs.rdbuf(), name, mode, log, false))
222 ifs.setstate(std::ios::failbit);
223 return ifs;
224}
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:205
#define DIR_DELIM_CHAR
Definition filesys.h:20
std::ofstream open_ofstream(const char *name, bool log, std::ios::openmode mode=std::ios::openmode())
Helper function to open an output file stream (= writing).
Definition filesys.h:193
std::ifstream open_ifstream(const char *name, bool log, std::ios::openmode mode=std::ios::openmode())
Helper function to open an input file stream (= reading).
Definition filesys.h:216
Definition filesys.cpp:55
void GetRecursiveDirs(std::vector< std::string > &dirs, const std::string &dir)
Definition filesys.cpp:555
bool IsPathAbsolute(const std::string &path)
Definition filesys.cpp:335
void GetRecursiveSubPaths(const std::string &path, std::vector< std::string > &dst, bool list_files, std::string_view ignore)
Definition filesys.cpp:571
std::string MakePathRelativeTo(const std::string &child, const std::string &parent)
Definition filesys.cpp:719
bool IsDirDelimiter(char c)
Definition filesys.h:54
bool CopyDir(const std::string &source, const std::string &target)
Definition filesys.cpp:605
std::string RemoveRelativePathComponents(std::string path)
Definition filesys.cpp:777
std::string AbsolutePath(const std::string &path)
Definition filesys.cpp:830
std::string AbsolutePathPartial(const std::string &path)
Definition filesys.cpp:849
std::string TempPath()
Returns path to temp directory.
Definition filesys.cpp:424
bool RecursiveDelete(const std::string &path)
Definition filesys.cpp:366
bool IsDir(const std::string &path)
Definition filesys.cpp:340
bool PathExists(const std::string &path)
Definition filesys.cpp:330
bool MoveDir(const std::string &source, const std::string &target)
Definition filesys.cpp:634
const char * GetFilenameFromPath(const char *path)
Definition filesys.cpp:878
std::string CreateTempFile()
Returns path to securely-created temporary file (will already exist when this function returns).
Definition filesys.cpp:440
bool IsFile(const std::string &path)
Definition filesys.cpp:348
bool CreateAllDirs(const std::string &path)
Definition filesys.cpp:589
bool Rename(const std::string &from, const std::string &to)
Definition filesys.cpp:1084
std::vector< DirListNode > GetDirListing(const std::string &pathstring)
Definition filesys.cpp:263
bool IsExecutable(const std::string &path)
Definition filesys.cpp:361
std::string RemoveLastPathComponent(const std::string &path, std::string *removed, int count)
Definition filesys.cpp:739
bool CopyFileContents(const std::string &source, const std::string &target)
Definition filesys.cpp:469
bool CreateDir(const std::string &path)
Definition filesys.cpp:316
bool ReadFile(const std::string &path, std::string &out, bool log_error)
Definition filesys.cpp:1070
bool PathStartsWith(const std::string &path, const std::string &prefix)
Definition filesys.cpp:658
bool safeWriteToFile(const std::string &path, std::string_view content)
Definition filesys.cpp:903
bool OpenStream(std::filebuf &stream, const char *filename, std::ios::openmode mode, bool log_error, bool log_warn)
Open a file buffer with error handling, commonly used with std::fstream.rdbuf().
Definition filesys.cpp:1089
std::string CreateTempDir()
Returns path to securely-created temporary directory (will already exist when this function returns).
Definition filesys.cpp:450
bool DeleteSingleFileOrEmptyDirectory(const std::string &path, bool log_error)
Definition filesys.cpp:409
Definition filesys.h:25
Definition filesys.h:33
std::string name
Definition filesys.h:34
bool dir
Definition filesys.h:35