Luanti 5.18.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// FIXME: the idea that "all filesystem on an UNIX system are case-sensitive"
22// is very wrong. Different folders on the same filesystem can even have differing
23// case-sensitivity. This seems tough to handle correctly however.
24#define FILESYS_CASE_INSENSITIVE false
25#define PATH_DELIM ":"
26#endif
27
28namespace io {
29class IFileSystem;
30}
31
32namespace fs
33{
34
36{
37 std::string name;
38 bool dir;
39};
40
41[[nodiscard]]
42std::vector<DirListNode> GetDirListing(const std::string &path);
43
44// Returns true if already exists
45bool CreateDir(const std::string &path);
46
47[[nodiscard]] bool PathExists(const std::string &path);
48
49[[nodiscard]] bool IsPathAbsolute(const std::string &path);
50
51[[nodiscard]] bool IsDir(const std::string &path);
52
53[[nodiscard]] bool IsExecutable(const std::string &path);
54
55[[nodiscard]] bool IsFile(const std::string &path);
56
57[[nodiscard]] inline bool IsDirDelimiter(char c)
58{
59 return c == '/' || c == DIR_DELIM_CHAR;
60}
61
62// Only pass full paths to this one. returns true on success.
63bool RecursiveDelete(const std::string &path);
64
65bool DeleteSingleFileOrEmptyDirectory(const std::string &path, bool log_error = false);
66
70[[nodiscard]] std::string TempPath();
71
74[[nodiscard]] std::string CreateTempFile();
75
78[[nodiscard]] std::string CreateTempDir();
79
80/* Returns a list of subdirectories, including the path itself, but excluding
81 hidden directories (whose names start with . or _)
82*/
83void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
84
85[[nodiscard]]
86std::vector<std::string> GetRecursiveDirs(const std::string &dir);
87
88/* The path itself not included, returns a list of all subpaths.
89 dst - vector that contains all the subpaths.
90 list files - include files in the list of subpaths.
91 ignore - paths that start with one of these characters will not be listed.
92*/
93void GetRecursiveSubPaths(const std::string &path,
94 std::vector<std::string> &dst,
95 bool list_files,
96 std::string_view ignore = {});
97
98// Create all directories on the given path that don't already exist.
99bool CreateAllDirs(const std::string &path);
100
101// Copy a regular file
102bool CopyFileContents(const std::string &source, const std::string &target);
103
104// Copy directory and all subdirectories
105// Omits files and subdirectories that start with a period
106bool CopyDir(const std::string &source, const std::string &target);
107
108// Move directory and all subdirectories
109// Behavior with files/subdirs that start with a period is undefined
110bool MoveDir(const std::string &source, const std::string &target);
111
112// Check if two paths are the same
113// Ignores case differences and '/' vs. '\\' on Windows
114bool PathsEqual(const std::string &p1, const std::string &p2);
115
116// Check if one path is prefix of another
117// For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
118// Ignores case differences and '/' vs. '\\' on Windows
119bool PathStartsWith(const std::string &path, const std::string &prefix);
120
121// If child is (as absolute path) inside parent (also as absolute path), returns
122// the part of child that is relative to parent.
123// Symlinks and "." and ".." components are removed.
124// If child and parent are (absolute) the same, the result is ".". (Otherwise it
125// never starts with '.'.)
126// Returns "" if child is not in parent, also returns "" on failure.
127std::string MakePathRelativeTo(const std::string &child, const std::string &parent);
128
129// Remove last path component and the dir delimiter before and/or after it.
130// If there's only one path component it will refuse to remove it (if absolute)
131// or return "" (if relative).
132// removed: If non-NULL, receives the removed components
133// count: Number of components to remove
134std::string RemoveLastPathComponent(const std::string &path,
135 std::string *removed = nullptr, int count = 1);
136
137// Remove "." and ".." path components and for every ".." removed, remove
138// the last normal path component before it. Unlike AbsolutePath,
139// this does not resolve symlinks and check for existence of directories.
140std::string RemoveRelativePathComponents(std::string path);
141
142// Returns the absolute path for the passed path, with "." and ".." path
143// components and symlinks removed. Returns "" on error.
144[[nodiscard]]
145std::string AbsolutePath(const std::string &path);
146
147// This is a combination of RemoveRelativePathComponents() and AbsolutePath()
148// It will resolve symlinks for the leading path components that exist and
149// still remove "." and ".." in the rest of the path.
150// Returns "" on error.
151[[nodiscard]]
152std::string AbsolutePathPartial(const std::string &path);
153
154// Returns the filename from a path or the entire path if no directory
155// delimiter is found.
156[[nodiscard]]
157const char *GetFilenameFromPath(const char *path);
158
159// Replace the content of a file on disk in a way that is safe from
160// corruption/truncation on a crash.
161// logs and returns false on error
162bool safeWriteToFile(const std::string &path, std::string_view content);
163
164#if IS_CLIENT_BUILD
165bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination);
166#endif
167
168bool ReadFile(const std::string &path, std::string &out, bool log_error = false);
169
170bool Rename(const std::string &from, const std::string &to);
171
182bool OpenStream(std::filebuf &stream, const char *filename,
183 std::ios::openmode mode, bool log_error, bool log_warn);
184
185} // namespace fs
186
187// outside of namespace for convenience:
188
199[[nodiscard]]
200inline std::ofstream open_ofstream(const char *name, bool log,
201 std::ios::openmode mode = std::ios::openmode())
202{
203 mode |= std::ios::out | std::ios::binary;
204 if (!(mode & std::ios::app))
205 mode |= std::ios::trunc;
206 std::ofstream ofs;
207 if (!fs::OpenStream(*ofs.rdbuf(), name, mode, log, false))
208 ofs.setstate(std::ios::failbit);
209 return ofs;
210}
211
222[[nodiscard]]
223inline std::ifstream open_ifstream(const char *name, bool log,
224 std::ios::openmode mode = std::ios::openmode())
225{
226 mode |= std::ios::in | std::ios::binary;
227 std::ifstream ifs;
228 if (!fs::OpenStream(*ifs.rdbuf(), name, mode, log, false))
229 ifs.setstate(std::ios::failbit);
230 return ifs;
231}
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:200
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:223
Definition filesys.cpp:56
void GetRecursiveDirs(std::vector< std::string > &dirs, const std::string &dir)
Definition filesys.cpp:556
bool IsPathAbsolute(const std::string &path)
Definition filesys.cpp:336
void GetRecursiveSubPaths(const std::string &path, std::vector< std::string > &dst, bool list_files, std::string_view ignore)
Definition filesys.cpp:572
std::string MakePathRelativeTo(const std::string &child, const std::string &parent)
Definition filesys.cpp:741
bool IsDirDelimiter(char c)
Definition filesys.h:57
bool CopyDir(const std::string &source, const std::string &target)
Definition filesys.cpp:606
std::string RemoveRelativePathComponents(std::string path)
Definition filesys.cpp:800
std::string AbsolutePath(const std::string &path)
Definition filesys.cpp:853
std::string AbsolutePathPartial(const std::string &path)
Definition filesys.cpp:878
std::string TempPath()
Returns path to temp directory.
Definition filesys.cpp:425
bool RecursiveDelete(const std::string &path)
Definition filesys.cpp:367
bool IsDir(const std::string &path)
Definition filesys.cpp:341
bool PathExists(const std::string &path)
Definition filesys.cpp:331
bool MoveDir(const std::string &source, const std::string &target)
Definition filesys.cpp:635
const char * GetFilenameFromPath(const char *path)
Definition filesys.cpp:907
std::string CreateTempFile()
Returns path to securely-created temporary file (will already exist when this function returns).
Definition filesys.cpp:441
bool IsFile(const std::string &path)
Definition filesys.cpp:349
bool CreateAllDirs(const std::string &path)
Definition filesys.cpp:590
bool Rename(const std::string &from, const std::string &to)
Definition filesys.cpp:1113
std::vector< DirListNode > GetDirListing(const std::string &pathstring)
Definition filesys.cpp:264
bool IsExecutable(const std::string &path)
Definition filesys.cpp:362
std::string RemoveLastPathComponent(const std::string &path, std::string *removed, int count)
Definition filesys.cpp:762
bool CopyFileContents(const std::string &source, const std::string &target)
Definition filesys.cpp:470
bool CreateDir(const std::string &path)
Definition filesys.cpp:317
bool ReadFile(const std::string &path, std::string &out, bool log_error)
Definition filesys.cpp:1099
bool PathStartsWith(const std::string &path, const std::string &prefix)
Definition filesys.cpp:727
bool safeWriteToFile(const std::string &path, std::string_view content)
Definition filesys.cpp:932
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:1118
bool PathsEqual(const std::string &p1, const std::string &p2)
Definition filesys.cpp:713
std::string CreateTempDir()
Returns path to securely-created temporary directory (will already exist when this function returns).
Definition filesys.cpp:451
bool DeleteSingleFileOrEmptyDirectory(const std::string &path, bool log_error)
Definition filesys.cpp:410
Definition filesys.h:28
Definition filesys.h:36
std::string name
Definition filesys.h:37
bool dir
Definition filesys.h:38