Spin-off syscall-related file operations (#78)
* [file_utils] spin-off syscall-related file operations 1. Eliminate the potential circular dependency between persistent-data/block.h and persistent-data/file_utils.h, if the former one wants to include the latter. 2. Avoid namespace pollution by removing the "using namespace std" declaration in block.tcc. 3. Correct the header hierarchy: base/xml_utils.h now no longer depends on the higher-level persistent-data/file_utils.h * [file_utils] support block files in get_file_length()
This commit is contained in:
committed by
Joe Thornber
parent
8f25e1b234
commit
b7d418131d
@ -5,56 +5,26 @@
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <sstream>
|
||||
#include <unistd.h>
|
||||
|
||||
using namespace base;
|
||||
using namespace bcache;
|
||||
using namespace persistent_data;
|
||||
using namespace std;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
persistent_data::block_address
|
||||
persistent_data::get_nr_blocks(string const &path, sector_t block_size)
|
||||
persistent_data::get_nr_blocks(std::string const &path, sector_t block_size)
|
||||
{
|
||||
using namespace persistent_data;
|
||||
|
||||
struct stat info;
|
||||
block_address nr_blocks;
|
||||
|
||||
int r = ::stat(path.c_str(), &info);
|
||||
if (r) {
|
||||
ostringstream out;
|
||||
out << "Couldn't stat dev path '" << path << "': "
|
||||
<< strerror(errno);
|
||||
throw runtime_error(out.str());
|
||||
}
|
||||
|
||||
if (S_ISREG(info.st_mode) && info.st_size)
|
||||
nr_blocks = div_down<block_address>(info.st_size, block_size);
|
||||
|
||||
else if (S_ISBLK(info.st_mode)) {
|
||||
// To get the size of a block device we need to
|
||||
// open it, and then make an ioctl call.
|
||||
int fd = ::open(path.c_str(), O_RDONLY);
|
||||
if (fd < 0)
|
||||
throw runtime_error("couldn't open block device to ascertain size");
|
||||
|
||||
r = ::ioctl(fd, BLKGETSIZE64, &nr_blocks);
|
||||
if (r) {
|
||||
::close(fd);
|
||||
throw runtime_error("ioctl BLKGETSIZE64 failed");
|
||||
}
|
||||
::close(fd);
|
||||
nr_blocks = div_down<block_address>(nr_blocks, block_size);
|
||||
} else
|
||||
// FIXME: needs a better message
|
||||
throw runtime_error("bad path");
|
||||
|
||||
return nr_blocks;
|
||||
return div_down<block_address>(file_utils::get_file_length(path),
|
||||
block_size);
|
||||
}
|
||||
|
||||
block_address
|
||||
persistent_data::get_nr_metadata_blocks(string const &path)
|
||||
persistent_data::get_nr_metadata_blocks(std::string const &path)
|
||||
{
|
||||
return get_nr_blocks(path, MD_BLOCK_SIZE);
|
||||
}
|
||||
@ -66,15 +36,4 @@ persistent_data::open_bm(std::string const &dev_path, block_manager<>::mode m, b
|
||||
return block_manager<>::ptr(new block_manager<>(dev_path, nr_blocks, 1, m, excl));
|
||||
}
|
||||
|
||||
void
|
||||
persistent_data::check_file_exists(string const &file) {
|
||||
struct stat info;
|
||||
int r = ::stat(file.c_str(), &info);
|
||||
if (r)
|
||||
throw runtime_error("Couldn't stat file");
|
||||
|
||||
if (!S_ISREG(info.st_mode))
|
||||
throw runtime_error("Not a regular file");
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
Reference in New Issue
Block a user