File descriptor.
Assumes ownership of an open file descriptor.
Wraps a file descriptor object and allows safe duplication and destruction. Safe for signal interruption.
Copy operations duplicate the file descriptor.
TEST(FsExampleTest, sparse_and_trunc)
{
system_error err;
fs::remove_all("sandbox", &err);
cout << "curdir: " << curdir << endl;
string s("this is a test of the emergency sizing system\n");
cout << "sparse test with string len=" << s.size() << endl;
string fn("sparse_file");
auto write_sparse = [&fn, &s](off_t loc)
{
lseek(
fd, loc, SEEK_SET);
cout << "wrote " << s.size() << " bytes at loc " << loc << endl;
};
cout << "12288 hole at start of file:\n" << st << endl;
ASSERT_EQ(st.size, 16384);
ASSERT_EQ(st.alloc_size, 4096);
cout << setw(6) << "start" << setw(6) << "data" << endl;
for (auto& x : sm)
{
cout << setw(6) << x.first << setw(6) << x.second << endl;
}
std::map<int64_t,int64_t> ver;
ver[0] = 12287;
ASSERT_EQ(sm, ver);
write_sparse(1024);
cout << "8192 hole in middle of file\n" << st << endl;
ASSERT_EQ(st.alloc_size, 8192);
cout << setw(6) << "start" << setw(6) << "data" << endl;
for (auto& x : sm)
{
cout << setw(6) << x.first << setw(6) << x.second << endl;
}
ver.clear();
ver[4096] = 12287;
ASSERT_EQ(sm, ver);
cout << "8192 hole at end of file\n" << st << endl;
ASSERT_EQ(st.alloc_size, 4096);
cout << setw(6) << "start" << setw(6) << "data" << endl;
for (auto& x : sm)
{
cout << setw(6) << x.first << setw(6) << x.second << endl;
}
ver.clear();
ver[4096] = 12287;
ASSERT_EQ(sm, ver);
ifstream f(fn);
f.seekg(1024);
string val;
val.resize(s.size(), '\x01');
f.read(&val[0], val.size());
ASSERT_EQ(val, s);
fs::remove_all("sandbox");
}
int fd() const
Return the file descriptor.
static std::string get_current_dir(std::system_error *=nullptr)
Get working directory.
static void create_reg(const std::string &, std::system_error *=nullptr)
Create a regular file.
static FileStat file_stat(const std::string &, std::system_error *=nullptr)
Get the file stat.
static void set_size(const std::string &, off_t, std::system_error *=nullptr)
Set the file size.
static void change_dir(const std::string &, std::system_error *=nullptr)
Change working directory.
static std::map< off_t, off_t > sparse_map(const std::string &, std::system_error *=nullptr)
Map of file sparseness.
static void create_dir(const std::string &, std::system_error *=nullptr)
Create a directory.
ssize_t safe_write_throw(int fd, const void *buf, size_t count)
Signal safe write, throws system_error on error.
int safe_open_throw(const char *pathname, int flags)
Signal safe open, throws system_error on error.
TEST(inet_example, client_server_stream_test)
[Inet client server]