Merge branch 'master' of github.com:jthornber/thin-provisioning-tools

This commit is contained in:
Joe Thornber
2020-04-08 12:30:26 +01:00
23 changed files with 493 additions and 868 deletions

View File

@@ -24,12 +24,12 @@ command::die(string const &msg)
exit(1);
}
uint64_t
::uint64_t
command::parse_uint64(string const &str, string const &desc)
{
try {
// FIXME: check trailing garbage is handled
return lexical_cast<uint64_t>(str);
return lexical_cast<::uint64_t>(str);
} catch (...) {
ostringstream out;

View File

@@ -39,14 +39,16 @@ namespace {
//----------------------------------------------------------------
int
file_utils::open_file(string const &path, int flags) {
int fd = ::open(path.c_str(), OPEN_FLAGS | flags, DEFAULT_MODE);
if (fd < 0)
file_utils::file_descriptor::file_descriptor(string const &path, int flags) {
fd_ = ::open(path.c_str(), OPEN_FLAGS | flags, DEFAULT_MODE);
if (fd_ < 0)
syscall_failed("open",
"Note: you cannot run this tool with these options on live metadata.");
}
return fd;
file_utils::file_descriptor::~file_descriptor() {
close(fd_);
fd_ = -1;
}
bool
@@ -76,7 +78,7 @@ file_utils::check_file_exists(string const &file, bool must_be_regular_file) {
throw runtime_error("Not a regular file");
}
int
file_utils::file_descriptor
file_utils::create_block_file(string const &path, off_t file_size) {
if (file_exists(path)) {
ostringstream out;
@@ -84,16 +86,16 @@ file_utils::create_block_file(string const &path, off_t file_size) {
throw runtime_error(out.str());
}
int fd = open_file(path, O_CREAT | O_EXCL | O_RDWR);
file_descriptor fd(path, O_CREAT | O_EXCL | O_RDWR);
int r = ::ftruncate(fd, file_size);
int r = ::ftruncate(fd.fd_, file_size);
if (r < 0)
syscall_failed("ftruncate");
return fd;
}
int
file_utils::file_descriptor
file_utils::open_block_file(string const &path, off_t min_size, bool writeable, bool excl) {
if (!file_exists(path)) {
ostringstream out;
@@ -105,7 +107,7 @@ file_utils::open_block_file(string const &path, off_t min_size, bool writeable,
if (excl)
flags |= O_EXCL;
return open_file(path, flags);
return file_descriptor(path, flags);
}
uint64_t
@@ -146,17 +148,15 @@ file_utils::zero_superblock(std::string const &path)
{
char *buffer;
unsigned const SUPERBLOCK_SIZE = 4096;
int fd = open_block_file(path, SUPERBLOCK_SIZE, true, true);
file_descriptor fd = open_block_file(path, SUPERBLOCK_SIZE, true, true);
buffer = reinterpret_cast<char *>(aligned_alloc(SUPERBLOCK_SIZE, SUPERBLOCK_SIZE));
if (!buffer)
throw runtime_error("out of memory");
memset(buffer, 0, SUPERBLOCK_SIZE);
if (::write(fd, buffer, SUPERBLOCK_SIZE) != SUPERBLOCK_SIZE)
if (::write(fd.fd_, buffer, SUPERBLOCK_SIZE) != SUPERBLOCK_SIZE)
throw runtime_error("couldn't zero superblock");
::close(fd);
}
//----------------------------------------------------------------

View File

@@ -8,11 +8,20 @@
//----------------------------------------------------------------
namespace file_utils {
int open_file(std::string const &path, int flags);
struct file_descriptor {
// file_descriptor is movable but not copyable
file_descriptor(file_descriptor &&) = default;
file_descriptor& operator=(file_descriptor &&) = default;
file_descriptor(std::string const &path, int flags);
virtual ~file_descriptor();
int fd_;
};
bool file_exists(std::string const &path);
void check_file_exists(std::string const &file, bool must_be_regular_file = true);
int create_block_file(std::string const &path, off_t file_size);
int open_block_file(std::string const &path, off_t min_size, bool writeable, bool excl = true);
file_descriptor create_block_file(std::string const &path, off_t file_size);
file_descriptor open_block_file(std::string const &path, off_t min_size, bool writeable, bool excl = true);
uint64_t get_file_length(std::string const &file);
void zero_superblock(std::string const &path);
}