|
|
nono 1.7.0
//
// nono
// Copyright (C) 2021 nono project
// Licensed under nono-license.txt
//
//
// ディスクイメージドライバ (生イメージ形式)
//
#include "diskdriver_raw.h"
#include <fcntl.h>
#include <sys/stat.h>
// コンストラクタ
DiskDriverRaw::DiskDriverRaw(const std::string& objname_,
const std::string& pathname_)
: inherited(objname_ + ".raw", pathname_)
{
}
// デストラクタ
DiskDriverRaw::~DiskDriverRaw()
{
}
// マッチ。
bool
DiskDriverRaw::Match()
{
// 構造を持たないので、オープンできれば常に成功する。
fd = open(pathname.c_str(), O_RDONLY);
if (fd < 0) {
return false;
}
return true;
}
// アタッチ。
bool
DiskDriverRaw::Attach(std::string& errmsg)
{
// 構造を持たないので、ここですることは何もない。
putmsg(1, "%s", pathname.c_str());
return true;
}
// オープン。
bool
DiskDriverRaw::Open(bool read_only)
{
assert(pathname.empty() == false);
assert(fd.Valid());
if (read_only == false) {
// RW 指定ならオープンし直す。
fd.Close();
fd = open(pathname.c_str(), O_RDWR);
if (fd < 0) {
warn("%s", pathname.c_str());
return false;
}
}
// ファイルサイズを取得
struct stat st;
if (fstat(fd, &st) < 0) {
warn("%s: stat failed", pathname.c_str());
return false;
}
filesize = st.st_size;
return true;
}
void
DiskDriverRaw::Close()
{
fd.Close();
filesize = 0;
}
bool
DiskDriverRaw::Read(void *buf, off_t offset, size_t size)
{
if (__predict_false(fd < 0)) {
return false;
}
if (__predict_false(offset + size > GetSize())) {
return false;
}
if (readpos(fd, buf, size, offset) < 0) {
return false;
}
return true;
}
bool
DiskDriverRaw::Write(const void *buf, off_t offset, size_t size)
{
if (__predict_false(fd < 0)) {
return false;
}
if (__predict_false(offset + size > GetSize())) {
return false;
}
if (writepos(fd, buf, size, offset) < 0) {
return false;
}
return true;
}
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.