Luanti 5.11.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 <set>
9#include <string>
10#include <string_view>
11#include <vector>
12#include <fstream>
13
14#ifdef _WIN32
15#define DIR_DELIM "\\"
16#define DIR_DELIM_CHAR '\\'
17#define FILESYS_CASE_INSENSITIVE true
18#define PATH_DELIM ";"
19#else
20#define DIR_DELIM "/"
21#define DIR_DELIM_CHAR '/'
22#define FILESYS_CASE_INSENSITIVE false
23#define PATH_DELIM ":"
24#endif
25
26namespace irr::io {
27class IFileSystem;
28}
29
30namespace fs
31{
32
34{
35 std::string name;
36 bool dir;
37};
38
39std::vector<DirListNode> GetDirListing(const std::string &path);
40
41// Returns true if already exists
42bool CreateDir(const std::string &path);
43
44bool PathExists(const std::string &path);
45
46bool IsPathAbsolute(const std::string &path);
47
48bool IsDir(const std::string &path);
49
50bool IsExecutable(const std::string &path);
51
52inline bool IsFile(const std::string &path)
53{
54 return PathExists(path) && !IsDir(path);
55}
56
57bool IsDirDelimiter(char c);
58
59// Only pass full paths to this one. True on success.
60// NOTE: The WIN32 version returns always true.
61bool RecursiveDelete(const std::string &path);
62
63bool DeleteSingleFileOrEmptyDirectory(const std::string &path);
64
68std::string TempPath();
69
72std::string CreateTempFile();
73
76std::string CreateTempDir();
77
78/* Returns a list of subdirectories, including the path itself, but excluding
79 hidden directories (whose names start with . or _)
80*/
81void GetRecursiveDirs(std::vector<std::string> &dirs, const std::string &dir);
82std::vector<std::string> GetRecursiveDirs(const std::string &dir);
83
84/* Multiplatform */
85
86/* The path itself not included, returns a list of all subpaths.
87 dst - vector that contains all the subpaths.
88 list files - include files in the list of subpaths.
89 ignore - paths that start with these charcters will not be listed.
90*/
91void GetRecursiveSubPaths(const std::string &path,
92 std::vector<std::string> &dst,
93 bool list_files,
94 const std::set<char> &ignore = {});
95
96// Only pass full paths to this one. True on success.
97bool RecursiveDeleteContent(const std::string &path);
98
99// Create all directories on the given path that don't already exist.
100bool CreateAllDirs(const std::string &path);
101
102// Copy a regular file
103bool CopyFileContents(const std::string &source, const std::string &target);
104
105// Copy directory and all subdirectories
106// Omits files and subdirectories that start with a period
107bool CopyDir(const std::string &source, const std::string &target);
108
109// Move directory and all subdirectories
110// Behavior with files/subdirs that start with a period is undefined
111bool MoveDir(const std::string &source, const std::string &target);
112
113// Check if one path is prefix of another
114// For example, "/tmp" is a prefix of "/tmp" and "/tmp/file" but not "/tmp2"
115// Ignores case differences and '/' vs. '\\' on Windows
116bool PathStartsWith(const std::string &path, const std::string &prefix);
117
118// Remove last path component and the dir delimiter before and/or after it,
119// returns "" if there is only one path component.
120// removed: If non-NULL, receives the removed component(s).
121// count: Number of components to remove
122std::string RemoveLastPathComponent(const std::string &path,
123 std::string *removed = NULL, int count = 1);
124
125// Remove "." and ".." path components and for every ".." removed, remove
126// the last normal path component before it. Unlike AbsolutePath,
127// this does not resolve symlinks and check for existence of directories.
128std::string RemoveRelativePathComponents(std::string path);
129
130// Returns the absolute path for the passed path, with "." and ".." path
131// components and symlinks removed. Returns "" on error.
132std::string AbsolutePath(const std::string &path);
133
134// Returns the filename from a path or the entire path if no directory
135// delimiter is found.
136const char *GetFilenameFromPath(const char *path);
137
138// Replace the content of a file on disk in a way that is safe from
139// corruption/truncation on a crash.
140// logs and returns false on error
141bool safeWriteToFile(const std::string &path, std::string_view content);
142
143#if IS_CLIENT_BUILD
144bool extractZipFile(irr::io::IFileSystem *fs, const char *filename, const std::string &destination);
145#endif
146
147bool ReadFile(const std::string &path, std::string &out, bool log_error = false);
148
149bool Rename(const std::string &from, const std::string &to);
150
161bool OpenStream(std::filebuf &stream, const char *filename,
162 std::ios::openmode mode, bool log_error, bool log_warn);
163
164} // namespace fs
165
166// outside of namespace for convenience:
167
178inline std::ofstream open_ofstream(const char *name, bool log,
179 std::ios::openmode mode = std::ios::openmode())
180{
181 mode |= std::ios::out | std::ios::binary;
182 if (!(mode & std::ios::app))
183 mode |= std::ios::trunc;
184 std::ofstream ofs;
185 if (!fs::OpenStream(*ofs.rdbuf(), name, mode, log, false))
186 ofs.setstate(std::ios::failbit);
187 return ofs;
188}
189
200inline std::ifstream open_ifstream(const char *name, bool log,
201 std::ios::openmode mode = std::ios::openmode())
202{
203 mode |= std::ios::in | std::ios::binary;
204 std::ifstream ifs;
205 if (!fs::OpenStream(*ifs.rdbuf(), name, mode, log, false))
206 ifs.setstate(std::ios::failbit);
207 return ifs;
208}
static v2f dir(const v2f &pos_dist)
Definition camera.cpp:191
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:178
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:200
Definition filesys.cpp:53
void GetRecursiveDirs(std::vector< std::string > &dirs, const std::string &dir)
Definition filesys.cpp:566
bool IsPathAbsolute(const std::string &path)
Definition filesys.cpp:343
bool IsDirDelimiter(char c)
Definition filesys.cpp:361
bool CopyDir(const std::string &source, const std::string &target)
Definition filesys.cpp:634
std::string RemoveRelativePathComponents(std::string path)
Definition filesys.cpp:780
std::string AbsolutePath(const std::string &path)
Definition filesys.cpp:833
bool DeleteSingleFileOrEmptyDirectory(const std::string &path)
Definition filesys.cpp:410
void GetRecursiveSubPaths(const std::string &path, std::vector< std::string > &dst, bool list_files, const std::set< char > &ignore)
Definition filesys.cpp:582
bool RecursiveDeleteContent(const std::string &path)
Definition filesys.cpp:599
std::string TempPath()
Returns path to temp directory.
Definition filesys.cpp:427
bool RecursiveDelete(const std::string &path)
Definition filesys.cpp:366
bool IsDir(const std::string &path)
Definition filesys.cpp:348
bool PathExists(const std::string &path)
Definition filesys.cpp:337
bool MoveDir(const std::string &source, const std::string &target)
Definition filesys.cpp:663
const char * GetFilenameFromPath(const char *path)
Definition filesys.cpp:846
std::string CreateTempFile()
Returns path to securely-created temporary file (will already exist when this function returns).
Definition filesys.cpp:446
bool IsFile(const std::string &path)
Definition filesys.h:52
bool CreateAllDirs(const std::string &path)
Definition filesys.cpp:616
bool Rename(const std::string &from, const std::string &to)
Definition filesys.cpp:1019
std::vector< DirListNode > GetDirListing(const std::string &pathstring)
Definition filesys.cpp:270
bool IsExecutable(const std::string &path)
Definition filesys.cpp:356
std::string RemoveLastPathComponent(const std::string &path, std::string *removed, int count)
Definition filesys.cpp:748
bool CopyFileContents(const std::string &source, const std::string &target)
Definition filesys.cpp:475
bool CreateDir(const std::string &path)
Definition filesys.cpp:323
bool ReadFile(const std::string &path, std::string &out, bool log_error)
Definition filesys.cpp:1005
bool PathStartsWith(const std::string &path, const std::string &prefix)
Definition filesys.cpp:687
bool safeWriteToFile(const std::string &path, std::string_view content)
Definition filesys.cpp:859
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:1024
std::string CreateTempDir()
Returns path to securely-created temporary directory (will already exist when this function returns).
Definition filesys.cpp:456
Definition filesys.h:26
Definition filesys.h:34
std::string name
Definition filesys.h:35
bool dir
Definition filesys.h:36