Luanti 5.15.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 charcters 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// Remove last path component and the dir delimiter before and/or after it.
115// If there's only one path component it will refuse to remove it (if absolute)
116// or return "" (if relative).
117// removed: If non-NULL, receives the removed components
118// count: Number of components to remove
119std::string RemoveLastPathComponent(const std::string &path,
120 std::string *removed = nullptr, int count = 1);
121
122// Remove "." and ".." path components and for every ".." removed, remove
123// the last normal path component before it. Unlike AbsolutePath,
124// this does not resolve symlinks and check for existence of directories.
125std::string RemoveRelativePathComponents(std::string path);
126
127// Returns the absolute path for the passed path, with "." and ".." path
128// components and symlinks removed. Returns "" on error.
129[[nodiscard]]
130std::string AbsolutePath(const std::string &path);
131
132// This is a combination of RemoveRelativePathComponents() and AbsolutePath()
133// It will resolve symlinks for the leading path components that exist and
134// still remove "." and ".." in the rest of the path.
135// Returns "" on error.
136[[nodiscard]]
137std::string AbsolutePathPartial(const std::string &path);
138
139// Returns the filename from a path or the entire path if no directory
140// delimiter is found.
141[[nodiscard]]
142const char *GetFilenameFromPath(const char *path);
143
144// Replace the content of a file on disk in a way that is safe from
145// corruption/truncation on a crash.
146// logs and returns false on error
147bool safeWriteToFile(const std::string &path, std::string_view content);
148
149#if IS_CLIENT_BUILD
150bool extractZipFile(io::IFileSystem *fs, const char *filename, const std::string &destination);
151#endif
152
153bool ReadFile(const std::string &path, std::string &out, bool log_error = false);
154
155bool Rename(const std::string &from, const std::string &to);
156
167bool OpenStream(std::filebuf &stream, const char *filename,
168 std::ios::openmode mode, bool log_error, bool log_warn);
169
170} // namespace fs
171
172// outside of namespace for convenience:
173
184[[nodiscard]]
185inline std::ofstream open_ofstream(const char *name, bool log,
186 std::ios::openmode mode = std::ios::openmode())
187{
188 mode |= std::ios::out | std::ios::binary;
189 if (!(mode & std::ios::app))
190 mode |= std::ios::trunc;
191 std::ofstream ofs;
192 if (!fs::OpenStream(*ofs.rdbuf(), name, mode, log, false))
193 ofs.setstate(std::ios::failbit);
194 return ofs;
195}
196
207[[nodiscard]]
208inline std::ifstream open_ifstream(const char *name, bool log,
209 std::ios::openmode mode = std::ios::openmode())
210{
211 mode |= std::ios::in | std::ios::binary;
212 std::ifstream ifs;
213 if (!fs::OpenStream(*ifs.rdbuf(), name, mode, log, false))
214 ifs.setstate(std::ios::failbit);
215 return ifs;
216}
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:207
#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:185
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:208
Definition filesys.cpp:53
void GetRecursiveDirs(std::vector< std::string > &dirs, const std::string &dir)
Definition filesys.cpp:554
bool IsPathAbsolute(const std::string &path)
Definition filesys.cpp:333
void GetRecursiveSubPaths(const std::string &path, std::vector< std::string > &dst, bool list_files, std::string_view ignore)
Definition filesys.cpp:570
bool IsDirDelimiter(char c)
Definition filesys.h:54
bool CopyDir(const std::string &source, const std::string &target)
Definition filesys.cpp:604
std::string RemoveRelativePathComponents(std::string path)
Definition filesys.cpp:756
std::string AbsolutePath(const std::string &path)
Definition filesys.cpp:809
std::string AbsolutePathPartial(const std::string &path)
Definition filesys.cpp:828
std::string TempPath()
Returns path to temp directory.
Definition filesys.cpp:423
bool RecursiveDelete(const std::string &path)
Definition filesys.cpp:364
bool IsDir(const std::string &path)
Definition filesys.cpp:338
bool PathExists(const std::string &path)
Definition filesys.cpp:328
bool MoveDir(const std::string &source, const std::string &target)
Definition filesys.cpp:633
const char * GetFilenameFromPath(const char *path)
Definition filesys.cpp:857
std::string CreateTempFile()
Returns path to securely-created temporary file (will already exist when this function returns).
Definition filesys.cpp:439
bool IsFile(const std::string &path)
Definition filesys.cpp:346
bool CreateAllDirs(const std::string &path)
Definition filesys.cpp:588
bool Rename(const std::string &from, const std::string &to)
Definition filesys.cpp:1063
std::vector< DirListNode > GetDirListing(const std::string &pathstring)
Definition filesys.cpp:261
bool IsExecutable(const std::string &path)
Definition filesys.cpp:359
std::string RemoveLastPathComponent(const std::string &path, std::string *removed, int count)
Definition filesys.cpp:718
bool CopyFileContents(const std::string &source, const std::string &target)
Definition filesys.cpp:468
bool CreateDir(const std::string &path)
Definition filesys.cpp:314
bool ReadFile(const std::string &path, std::string &out, bool log_error)
Definition filesys.cpp:1049
bool PathStartsWith(const std::string &path, const std::string &prefix)
Definition filesys.cpp:657
bool safeWriteToFile(const std::string &path, std::string_view content)
Definition filesys.cpp:882
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:1068
std::string CreateTempDir()
Returns path to securely-created temporary directory (will already exist when this function returns).
Definition filesys.cpp:449
bool DeleteSingleFileOrEmptyDirectory(const std::string &path, bool log_error)
Definition filesys.cpp:408
Definition filesys.h:25
Definition filesys.h:33
std::string name
Definition filesys.h:34
bool dir
Definition filesys.h:35