--- nono/host/diskimage_raw.cpp 2026/04/29 17:04:57 1.1 +++ nono/host/diskimage_raw.cpp 2026/04/29 17:05:22 1.1.1.4 @@ -11,14 +11,12 @@ #include "diskimage_raw.h" #include #include -#include // コンストラクタ DiskImageRaw::DiskImageRaw(const std::string& pathname_) - : inherited(DiskImage::Type::Raw) + : inherited(pathname_) { - pathname = pathname_; - memlen = (off_t)-1; + filesize = (off_t)-1; } // デストラクタ @@ -29,21 +27,9 @@ DiskImageRaw::~DiskImageRaw() // このディスクイメージのサイズと書き込み可能かどうかを返す。 // Open() 前でも動作する。 -int +bool DiskImageRaw::GetInfo(off_t *sizep, bool *w_ok) { - // ファイルサイズは未取得なら取得 - if (memlen == (off_t)-1) { - struct stat st; - - if (stat(pathname.c_str(), &st) < 0) { - warn("%s: stat failed", pathname.c_str()); - return -1; - } - memlen = st.st_size; - } - *sizep = memlen; - // ファイルのパーミッション int r = access(pathname.c_str(), W_OK); if (r == 0) { @@ -51,11 +37,27 @@ DiskImageRaw::GetInfo(off_t *sizep, bool } else if (errno == EACCES) { *w_ok = false; } else { - warn("%s: access failed", pathname.c_str()); - return -1; + if (errno == ENOENT) { + warn("%s", pathname.c_str()); + } else { + warn("%s: access failed", pathname.c_str()); + } + return false; } - return 0; + // ファイルサイズは未取得なら取得 + if (filesize == (off_t)-1) { + struct stat st; + + if (stat(pathname.c_str(), &st) < 0) { + warn("%s: stat failed", pathname.c_str()); + return false; + } + filesize = st.st_size; + } + *sizep = filesize; + + return true; } // オープン @@ -63,16 +65,13 @@ bool DiskImageRaw::Open(bool read_only) { int openmode; - int prot; assert(pathname.empty() == false); if (read_only) { openmode = O_RDONLY; - prot = PROT_READ; } else { openmode = O_RDWR; - prot = PROT_READ | PROT_WRITE; } fd = open(pathname.c_str(), openmode); @@ -82,7 +81,7 @@ DiskImageRaw::Open(bool read_only) } // ファイルサイズは未取得なら取得 - if (memlen == (off_t)-1) { + if (filesize == (off_t)-1) { struct stat st; if (fstat(fd, &st) < 0) { @@ -90,29 +89,42 @@ DiskImageRaw::Open(bool read_only) fd.Close(); return false; } - memlen = st.st_size; - } - - // mmap - void *m = mmap(NULL, (size_t)memlen, prot, MAP_SHARED, fd, 0); - if (m == MAP_FAILED) { - warn("%s: mmap failed", pathname.c_str()); - fd.Close(); - return false; + filesize = st.st_size; } - mem = (uint8 *)m; return true; } +void +DiskImageRaw::Close() +{ + fd.Close(); + filesize = (off_t)-1; +} + +off_t +DiskImageRaw::GetSize() const +{ + return filesize; +} + bool DiskImageRaw::Read(void *buf, off_t offset, size_t size) { if (__predict_false(fd < 0)) { return false; } + if (__predict_false(offset + size > GetSize())) { + return false; + } - memcpy(buf, &mem[offset], size); + if (lseek(fd, offset, SEEK_SET) == (off_t)-1) { + return false; + } + + if (read(fd, buf, size) < 0) { + return false; + } return true; } @@ -122,22 +134,16 @@ DiskImageRaw::Write(const void *buf, off if (__predict_false(fd < 0)) { return false; } + if (__predict_false(offset + size > GetSize())) { + return false; + } - memcpy(&mem[offset], buf, size); - return true; -} - -void -DiskImageRaw::Close() -{ - if (fd >= 0) { - munmap(mem, (size_t)memlen); - fd.Close(); + if (lseek(fd, offset, SEEK_SET) == (off_t)-1) { + return false; } -} -off_t -DiskImageRaw::GetSize() const -{ - return memlen; + if (write(fd, buf, size) < 0) { + return false; + } + return true; }