scclib
Stable Cloud Computing C++ Library
fs.h
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #ifndef _SCC_UTIL_FS_H
32 #define _SCC_UTIL_FS_H
33 
34 #include <string>
35 #include <map>
36 #include <functional>
37 #include <ostream>
38 #include <system_error>
39 
40 namespace scc::util
41 {
42 
58 enum class FileType
59 {
60  unknown= 0,
61  reg= 1,
62  dir= 2,
63  link= 4,
64  sock= 8,
65  block= 16,
66  chr= 32,
67  fifo= 64
68 };
69 
74 struct FileStat
75 {
77  unsigned mode;
78  int uid;
79  int gid;
80  uint64_t size;
81  uint64_t alloc_size;
82  uint64_t access_time;
83  uint64_t mod_time;
84  uint64_t change_time;
85  uint64_t inode;
86  uint16_t num_links;
87 
88  FileStat() : type(FileType::unknown), mode(0), uid(0), gid(0), size(0), alloc_size(0),
89  access_time(0), mod_time(0), change_time(0) {}
90 };
91 
96 bool default_scan_filter(const std::string&, FileType);
97 
103 {
104  static void remove_file(const std::string&, std::system_error*);
105  static void remove_dir(const std::string&, std::system_error*);
106  static void remove_all(const std::string&, const FileType&, std::system_error*);
107 
108  Filesystem() {}
109 public:
110 
122  static std::map<std::string, FileType> scan_dir(const std::string&,
123  std::function<bool(const std::string&, FileType)> = default_scan_filter, std::system_error* = nullptr);
124 
127  static FileType file_type(const std::string&, std::system_error* = nullptr);
128 
131  static FileStat file_stat(const std::string&, std::system_error* = nullptr);
132 
153  static void set_mode(const std::string&, unsigned, std::system_error* = nullptr);
154 
166  static void set_times(const std::string&, uint64_t, uint64_t, std::system_error* = nullptr);
167 
177  static void set_ids(const std::string&, int, int, std::system_error* = nullptr);
178 
192  static void set_size(const std::string&, off_t, std::system_error* = nullptr);
193 
212  static std::map<off_t, off_t> sparse_map(const std::string&, std::system_error* = nullptr);
213 
216  static void remove(const std::string&, std::system_error* = nullptr);
217 
222  static void rename(const std::string&, const std::string&, std::system_error* = nullptr);
223 
226  static void remove_all(const std::string& fn, std::system_error* err = nullptr)
227  {
228  auto ty = file_type(fn, err);
229  if (ty == FileType::unknown) return;
230  remove_all(fn, ty, err);
231  }
232 
240  static void create_dir(const std::string&, std::system_error* = nullptr);
241 
253  static void create_reg(const std::string&, std::system_error* = nullptr);
254 
261  static std::string create_tmp_reg(const std::string&, std::system_error* = nullptr);
262 
273  static void create_symlink(const std::string&, const std::string&, std::system_error* = nullptr);
274 
285  static void create_link(const std::string&, const std::string&, std::system_error* = nullptr);
286 
291  static void create_fifo(const std::string&, std::system_error* = nullptr);
292 
295  static void change_dir(const std::string&, std::system_error* = nullptr);
296 
299  static std::string get_current_dir(std::system_error* = nullptr);
300 
311  static std::string read_symlink(const std::string&, std::system_error* = nullptr);
312 
335  static std::string norm_path(const std::string&, const std::string&) noexcept;
336 
340  static std::string norm_path(const std::string& path) noexcept
341  {
342  return norm_path("", path);
343  }
344 };
345 
350 void PrintTo(const FileStat&, std::ostream*);
352 void PrintTo(const FileType&, std::ostream*);
353 }
354 
361 std::ostream& operator<<(std::ostream&, scc::util::FileStat);
362 std::ostream& operator<<(std::ostream&, scc::util::FileType);
366 #endif
Common file system utilities.
Definition: fs.h:103
static std::string get_current_dir(std::system_error *=nullptr)
Get working directory.
Definition: fs.cc:454
static void create_reg(const std::string &, std::system_error *=nullptr)
Create a regular file.
Definition: fs.cc:298
static std::string norm_path(const std::string &, const std::string &) noexcept
Normalize a path with base directory.
Definition: fs.cc:671
static void create_symlink(const std::string &, const std::string &, std::system_error *=nullptr)
Create a symbolic link.
Definition: fs.cc:339
static void rename(const std::string &, const std::string &, std::system_error *=nullptr)
Rename the file or directory.
Definition: fs.cc:270
static void create_link(const std::string &, const std::string &, std::system_error *=nullptr)
Create a hard link.
Definition: fs.cc:372
static void set_ids(const std::string &, int, int, std::system_error *=nullptr)
Set the user id and/or group id.
Definition: fs.cc:487
static void create_fifo(const std::string &, std::system_error *=nullptr)
Create a named pipe (FIFO).
Definition: fs.cc:386
static FileType file_type(const std::string &, std::system_error *=nullptr)
Get the file type.
Definition: fs.cc:186
static std::map< std::string, FileType > scan_dir(const std::string &, std::function< bool(const std::string &, FileType)>=default_scan_filter, std::system_error *=nullptr)
Scan a directory, and return a map of names and file types.
Definition: fs.cc:128
static void remove_all(const std::string &fn, std::system_error *err=nullptr)
Recursively removes the file or directory.
Definition: fs.h:226
static FileStat file_stat(const std::string &, std::system_error *=nullptr)
Get the file stat.
Definition: fs.cc:400
static std::string norm_path(const std::string &path) noexcept
Normalize a path.
Definition: fs.h:340
static std::string read_symlink(const std::string &, std::system_error *=nullptr)
Read the location of a symbolic link target.
Definition: fs.cc:353
static void set_size(const std::string &, off_t, std::system_error *=nullptr)
Set the file size.
Definition: fs.cc:610
static std::string create_tmp_reg(const std::string &, std::system_error *=nullptr)
Create a temporary regular file.
Definition: fs.cc:314
static void change_dir(const std::string &, std::system_error *=nullptr)
Change working directory.
Definition: fs.cc:440
static void remove(const std::string &, std::system_error *=nullptr)
Remove the file or directory.
Definition: fs.cc:233
static std::map< off_t, off_t > sparse_map(const std::string &, std::system_error *=nullptr)
Map of file sparseness.
Definition: fs.cc:525
static void create_dir(const std::string &, std::system_error *=nullptr)
Create a directory.
Definition: fs.cc:284
static void set_mode(const std::string &, unsigned, std::system_error *=nullptr)
Set the file mode.
Definition: fs.cc:472
static void set_times(const std::string &, uint64_t, uint64_t, std::system_error *=nullptr)
Set the file times.
Definition: fs.cc:504
FileType
File type.
Definition: fs.h:59
bool default_scan_filter(const std::string &, FileType)
Default scan filter.
Definition: fs.cc:119
@ block
block device
@ link
symbolic link
@ reg
regular file
@ chr
character device
@ unknown
unknown or does not exist
std::ostream & operator<<(std::ostream &, const scc::net::InetAddr &)
Print the socket address details to an output stream.
Definition: inet.cc:320
File status structure.
Definition: fs.h:75
uint64_t alloc_size
Real allocated size on disk.
Definition: fs.h:81
int gid
Group id.
Definition: fs.h:79
uint64_t change_time
Last status change time; writes, permission changes, etc. (ns since epoch)
Definition: fs.h:84
uint64_t size
Size of file.
Definition: fs.h:80
uint64_t access_time
Last access time; reads, etc. (ns since epoch)
Definition: fs.h:82
int uid
User id.
Definition: fs.h:78
uint16_t num_links
Number of hard links to this inode.
Definition: fs.h:86
uint64_t inode
Inode number.
Definition: fs.h:85
uint64_t mod_time
Last modification time; writes, etc. (ns since epoch)
Definition: fs.h:83
unsigned mode
File mode 07777 format (bits/user/group/all 4=read, 2=write, 3=execute, or uid/gid/sticky)
Definition: fs.h:77
FileType type
File type.
Definition: fs.h:76