scclib
Stable Cloud Computing C++ Library
Public Member Functions | List of all members
scc::util::FileDesc Class Reference

File descriptor. More...

#include <filedesc.h>

Public Member Functions

 FileDesc (int fd)
 Wrap an open file descriptor.
 
 FileDesc (const FileDesc &other)
 Copy construct by duplication.
 
FileDescoperator= (const FileDesc &other)
 Copy assign by duplication.
 
 FileDesc (FileDesc &&other)
 Move construct.
 
FileDescoperator= (FileDesc &&other)
 Move assign.
 
 operator int () const
 Cast const file descriptor to int.
 
int fd () const
 Return the file descriptor.
 
void close ()
 Signal safe close.
 
void dup (int)
 Duplicate a file descriptor. More...
 

Detailed Description

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.

Example of using FileDesc to protect a file descriptor opened with the clib open api, from scclib/util/unittest/fs.cc

TEST(FsExampleTest, sparse_and_trunc)
{
system_error err;
fs::remove_all("sandbox", &err);
fs::create_dir("sandbox");
auto curdir = fs::get_current_dir();
cout << "curdir: " << curdir << endl;
fs::change_dir("sandbox");
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)
{
// use clib to write data into the middle of the file
// if we use fstream, the system may truncate the file
// use system wrapper FileDesc to ensure the file is always closed
lseek(fd, loc, SEEK_SET);
safe_write_throw(fd, &s[0], s.size());
cout << "wrote " << s.size() << " bytes at loc " << loc << endl;
};
fs::set_size(fn, 16384); // 16 K file, with a 0 at the end
auto st = fs::file_stat(fn);
cout << "12288 hole at start of file:\n" << st << endl;
ASSERT_EQ(st.size, 16384);
ASSERT_EQ(st.alloc_size, 4096); // the last block has data
auto sm = fs::sparse_map(fn);
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; // 12288 sized hole at 0 (beginning)
ASSERT_EQ(sm, ver);
write_sparse(1024); // write the string at 1024, causing first block to be written
st = fs::file_stat(fn);
cout << "8192 hole in middle of file\n" << st << endl;
ASSERT_EQ(st.alloc_size, 8192);
sm = fs::sparse_map(fn);
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; // 8192 sized hole at 4096 (middle)
ASSERT_EQ(sm, ver);
fs::set_size(fn, 12288);
st = fs::file_stat(fn);
cout << "8192 hole at end of file\n" << st << endl;
ASSERT_EQ(st.alloc_size, 4096);
sm = fs::sparse_map(fn);
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; // 8192 sized hole at 4096 (end)
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::change_dir(curdir);
fs::remove_all("sandbox");
}
File descriptor.
Definition: filedesc.h:66
int fd() const
Return the file descriptor.
Definition: filedesc.h:105
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 FileStat file_stat(const std::string &, std::system_error *=nullptr)
Get the file stat.
Definition: fs.cc:400
static void set_size(const std::string &, off_t, std::system_error *=nullptr)
Set the file size.
Definition: fs.cc:610
static void change_dir(const std::string &, std::system_error *=nullptr)
Change working directory.
Definition: fs.cc:440
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
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
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521
Examples
util/unittest/filedesc.cc, and util/unittest/fs.cc.

Definition at line 65 of file filedesc.h.

Member Function Documentation

◆ dup()

void FileDesc::dup ( int  fd)

Duplicate a file descriptor.

This descriptor is now a duplicate of the input descriptor.

If the input file descriptor is invalid, this descriptor will also be invalid.

Definition at line 50 of file filedesc.cc.


The documentation for this class was generated from the following files: