scclib
Stable Cloud Computing C++ Library
fs.cc
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 #include <util/fs.h>
32 #include <gtest/gtest.h>
33 #include <cstring>
34 #include <iostream>
35 #include <sys/socket.h>
36 #include <sys/un.h>
37 #include <algorithm>
38 #include <fstream>
39 #include <fcntl.h>
40 #include <util/safe_clib.h>
41 #include <util/filedesc.h>
42 
49 using std::cout;
50 using std::endl;
51 using std::string;
52 using std::system_error;
53 using std::setw;
54 using std::ifstream;
57 
58 struct FsTest : public testing::Test
59 {
60  string m_curdir;
61  FsTest()
62  {
63  system_error err;
64  m_curdir = fs::get_current_dir();
65  fs::remove_all("sandbox", &err);
66  fs::create_dir("sandbox");
67  }
68  virtual ~FsTest()
69  {
70  fs::change_dir(m_curdir);
71  system_error err;
72  fs::remove_all("sandbox", &err);
73  }
74  void create_files()
75  {
76  fs::create_reg("sandbox/reg");
77  fs::create_symlink("reg", "sandbox/link");
78  fs::create_link("sandbox/reg", "sandbox/reg2");
79  fs::create_fifo("sandbox/fifo");
80  struct sockaddr_un addr;
81  addr.sun_family = AF_UNIX;
82  std::strcpy(addr.sun_path, "sandbox/sock");
83  int fd = socket(PF_UNIX, SOCK_STREAM, 0);
84  bind(fd, (struct sockaddr*)&addr, sizeof addr);
86  }
87 };
88 
89 TEST_F(FsTest, list_current)
90 {
91  cout << "listing current directory contents" << endl;
92  auto files = fs::scan_dir(".");
93  ASSERT_GT(files.size(), 0);
94  for (auto& f : files)
95  {
96  cout << f.first << " type: " << f.second << endl;
97  }
98 }
99 
100 TEST_F(FsTest, list_sandbox)
101 {
102  cout << "creating sandbox directory files" << endl;
103  create_files();
104  cout << "listing sandbox directory contents" << endl;
105  auto files = fs::scan_dir("sandbox");
106  ASSERT_GT(files.size(), 0);
107  for (auto& f : files)
108  {
109  cout << f.first << " type: " << f.second << endl;
110  }
111 }
112 
114 static bool reg_filt(const string& s, FileType t)
115 {
116  if (s == "reg" && t == FileType::reg)
117  return true;
118  return false;
119 }
120 
121 
122 TEST_F(FsTest, filter)
123 {
124  create_files();
125  auto files = fs::scan_dir("sandbox", reg_filt);
126  ASSERT_EQ(files.size(), 1);
127  ASSERT_EQ(files.begin()->first, "reg");
128  ASSERT_EQ(files.begin()->second, FileType::reg);
129 }
130 
132 
133 TEST_F(FsTest, create_and_delete)
134 {
135  ASSERT_THROW(fs::create_dir("sandbox"), system_error);
136  fs::create_reg("sandbox/test");
137  auto n = fs::create_tmp_reg("sandbox/test");
138  cout << "tmp file: " << n << endl;
139  auto d = fs::scan_dir("sandbox");
140  for (auto& f : d)
141  {
142  cout << f.first << " type: " << f.second << endl;
143  }
144  ASSERT_EQ(d.size(), 2);
145  ASSERT_NE(d.find("test"), d.end());
146 
147  fs::remove(n);
148  d = fs::scan_dir("sandbox");
149  ASSERT_EQ(d.size(), 1);
150  ASSERT_THROW(fs::remove(n), system_error);
151 
152  fs::remove_all("sandbox");
153  d = fs::scan_dir(".");
154  ASSERT_EQ(d.find("sandbox"), d.end());
155 
156  ASSERT_THROW(fs::remove("sandbox"), system_error);
157 }
158 
159 TEST_F(FsTest, create_types)
160 {
161  create_files();
162 
163  fs::change_dir("sandbox");
164  auto d = fs::scan_dir(".");
165  for (auto& f : d)
166  {
167  cout << "sandbox/" << f.first << " type: " << f.second << ":" << endl;
168  auto ty = fs::file_stat(f.first);
169  cout << ty << endl;
170  ASSERT_EQ(f.second, ty.type);
171  }
172 
173  ASSERT_EQ(fs::read_symlink("link"), "reg");
174 
175  auto ty = fs::file_stat("/dev/null");
176  cout << "/dev/zero " << ty << endl;
177  ASSERT_EQ(ty.type, FileType::chr);
178 
179  fs::change_dir("/dev");
180  d = fs::scan_dir(".");
181  auto i = std::find_if(d.begin(), d.end(), [](const auto& x) -> bool
182  {
183  return x.second == FileType::block;
184  });
185  ASSERT_NE(i, d.end());
186  ty = fs::file_stat(i->first);
187  cout << "/dev/" << i->first << " " << ty << endl;
188  ASSERT_EQ(ty.type, FileType::block);
189 }
190 
191 TEST_F(FsTest, attributes)
192 {
193  fs::change_dir("sandbox");
194 
195  fs::create_reg("reg");
196  fs::set_mode("reg", 0660);
197  fs::create_symlink("reg", "link");
198  fs::create_reg("reg2");
199 
200  const uint64_t NS = 1000000000;
201  const uint64_t DAY = 24*60*60*NS;
202 
203  auto st = fs::file_stat("reg");
204  fs::set_times("reg2", st.access_time-DAY, st.mod_time-2*DAY);
205  //fs::set_times("link", st.access_time-3*DAY, st.mod_time-4*DAY);
206 
207  auto d = fs::scan_dir(".");
208  for (auto& f : d)
209  {
210  cout << "sandbox/" << f.first << " type: " << f.second << ":" << endl;
211  auto ty = fs::file_stat(f.first);
212  cout << ty << endl;
213  }
214 
215  auto streg = fs::file_stat("reg");
216  ASSERT_EQ(streg.mode, 0660);
217 
218  auto streg2 = fs::file_stat("reg2");
219  ASSERT_EQ(streg2.mode, 0600);
220  ASSERT_EQ(streg2.access_time, streg.access_time-DAY);
221  ASSERT_EQ(streg2.mod_time, streg.mod_time-2*DAY);
222 
223  auto stlink = fs::file_stat("link");
224  ASSERT_EQ(stlink.mode, 0777);
225 
226  // NOTE: can't test the uid / gid settings easily...
227 
228 }
229 
231 TEST(FsExampleTest, sparse_and_trunc)
232 {
233  system_error err;
234  fs::remove_all("sandbox", &err);
235  fs::create_dir("sandbox");
236  auto curdir = fs::get_current_dir();
237  cout << "curdir: " << curdir << endl;
238 
239  fs::change_dir("sandbox");
240 
241  string s("this is a test of the emergency sizing system\n");
242  cout << "sparse test with string len=" << s.size() << endl;
243 
244  string fn("sparse_file");
245 
246  auto write_sparse = [&fn, &s](off_t loc)
247  {
248  // use clib to write data into the middle of the file
249  // if we use fstream, the system may truncate the file
250 
251  // use system wrapper FileDesc to ensure the file is always closed
252  scc::util::FileDesc fd(scc::util::safe_open_throw(fn.c_str(), O_WRONLY));
253 
254  lseek(fd, loc, SEEK_SET);
255  safe_write_throw(fd, &s[0], s.size());
256 
257  cout << "wrote " << s.size() << " bytes at loc " << loc << endl;
258  };
259 
260  fs::create_reg(fn);
261  fs::set_size(fn, 16384); // 16 K file, with a 0 at the end
262 
263  auto st = fs::file_stat(fn);
264  cout << "12288 hole at start of file:\n" << st << endl;
265 
266  ASSERT_EQ(st.size, 16384);
267  ASSERT_EQ(st.alloc_size, 4096); // the last block has data
268 
269  auto sm = fs::sparse_map(fn);
270  cout << setw(6) << "start" << setw(6) << "data" << endl;
271  for (auto& x : sm)
272  {
273  cout << setw(6) << x.first << setw(6) << x.second << endl;
274  }
275 
276  std::map<int64_t,int64_t> ver;
277 
278  ver[0] = 12287; // 12288 sized hole at 0 (beginning)
279  ASSERT_EQ(sm, ver);
280 
281  write_sparse(1024); // write the string at 1024, causing first block to be written
282 
283  st = fs::file_stat(fn);
284  cout << "8192 hole in middle of file\n" << st << endl;
285 
286  ASSERT_EQ(st.alloc_size, 8192);
287 
288  sm = fs::sparse_map(fn);
289  cout << setw(6) << "start" << setw(6) << "data" << endl;
290  for (auto& x : sm)
291  {
292  cout << setw(6) << x.first << setw(6) << x.second << endl;
293  }
294 
295  ver.clear();
296  ver[4096] = 12287; // 8192 sized hole at 4096 (middle)
297  ASSERT_EQ(sm, ver);
298 
299  fs::set_size(fn, 12288);
300 
301  st = fs::file_stat(fn);
302  cout << "8192 hole at end of file\n" << st << endl;
303 
304  ASSERT_EQ(st.alloc_size, 4096);
305 
306  sm = fs::sparse_map(fn);
307  cout << setw(6) << "start" << setw(6) << "data" << endl;
308  for (auto& x : sm)
309  {
310  cout << setw(6) << x.first << setw(6) << x.second << endl;
311  }
312 
313  ver.clear();
314  ver[4096] = 12287; // 8192 sized hole at 4096 (end)
315  ASSERT_EQ(sm, ver);
316 
317  ifstream f(fn);
318  f.seekg(1024);
319  string val;
320  val.resize(s.size(), '\x01');
321  f.read(&val[0], val.size());
322  ASSERT_EQ(val, s);
323 
324  fs::change_dir(curdir);
325  fs::remove_all("sandbox");
326 }
328 
329 TEST_F(FsTest, paths)
330 {
331  auto t = [](const std::string& b, const std::string& p) -> std::string
332  {
333  auto x = fs::norm_path(b, p);
334  cout << "base:" << ">" << b << "< path: >" << p << "< norm: >" << x << "<" << endl;
335  return x;
336  };
337 
338  ASSERT_EQ(t("", ""), ".");
339  ASSERT_EQ(t("", "."), ".");
340  ASSERT_EQ(t(".", ""), ".");
341  ASSERT_EQ(t(".", "."), ".");
342 
343  ASSERT_EQ(t("", "test/.."), ".");
344  ASSERT_EQ(t("", "test/../.."), "./..");
345 
346  ASSERT_EQ(t("", "/"), "/");
347  ASSERT_EQ(t("", "/test"), "/test");
348  ASSERT_EQ(t("", "/test/"), "/test/");
349  ASSERT_EQ(t("/", ""), "/");
350  ASSERT_EQ(t("/", "/"), "/");
351 
352  ASSERT_EQ(t("base", "../../sandbox/../../path"), "./../../path");
353 
354  ASSERT_EQ(t("/base/", ""), "/base/");
355  ASSERT_EQ(t("/base/", "."), "/base");
356  ASSERT_EQ(t("/base/", "root"), "/base/root");
357  ASSERT_EQ(t("/base", "root"), "/base/root");
358  ASSERT_EQ(t("/base/", "sandbox/rel/path"), "/base/sandbox/rel/path");
359  ASSERT_EQ(t("/base/sandbox", "../sandbox/path"), "/base/sandbox/path");
360  ASSERT_EQ(t("/base/next/../again", "sandbox/next/../path"), "/base/again/sandbox/path");
361 
362  ASSERT_EQ(t("", "sandbox/rel/path"), "./sandbox/rel/path");
363  ASSERT_EQ(t("", "sandbox/rel/../path"), "./sandbox/path");
364  ASSERT_EQ(t("", "./sandbox/rel/../path"), "./sandbox/path");
365  ASSERT_EQ(t("", "/sandbox/path"), "/sandbox/path");
366  ASSERT_EQ(t("", "/sandbox/path/"), "/sandbox/path/");
367  ASSERT_EQ(t("", "/sandbox/path"), "/sandbox/path");
368  ASSERT_EQ(t("", "/base/../sandbox/next/../../path"), "/path");
369  ASSERT_EQ(t("", "/this/../is/a/big/long/path/../../../../../"), "/");
370 
371  ASSERT_EQ(t("", "/this/../is/a/path/../../../../."), "/..");
372  ASSERT_EQ(t("../", "../../sandbox/../../"), "./../../../../");
373 }
File descriptor.
Definition: filedesc.h:66
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 create_link(const std::string &, const std::string &, std::system_error *=nullptr)
Create a hard link.
Definition: fs.cc:372
static void create_fifo(const std::string &, std::system_error *=nullptr)
Create a named pipe (FIFO).
Definition: fs.cc:386
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 FileStat file_stat(const std::string &, std::system_error *=nullptr)
Get the file stat.
Definition: fs.cc:400
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
File descriptor.
Common file system utilities.
FileType
File type.
Definition: fs.h:59
int safe_close(int fd)
Signal safe close.
Definition: safe_clib.cc:95
ssize_t safe_write_throw(int fd, const void *buf, size_t count)
Signal safe write, throws system_error on error.
Definition: safe_clib.cc:152
int safe_open_throw(const char *pathname, int flags)
Signal safe open, throws system_error on error.
Definition: safe_clib.cc:221
Signal-safe C library wrapper.
TEST_F(FsTest, create_and_delete)
[Scan directory]
Definition: fs.cc:133
TEST(FsExampleTest, sparse_and_trunc)
[Sparse file]
Definition: fs.cc:231