--- nono/host/diskimage_raw.cpp 2026/04/29 17:05:11 1.1.1.3 +++ nono/host/diskimage_raw.cpp 2026/04/29 17:05:30 1.1.1.5 @@ -16,44 +16,36 @@ DiskImageRaw::DiskImageRaw(const std::string& pathname_) : inherited(pathname_) { - filesize = (off_t)-1; } // デストラクタ DiskImageRaw::~DiskImageRaw() { - Close(); } -// このディスクイメージのサイズと書き込み可能かどうかを返す。 +// このディスクイメージが書き込み可能かどうかを返す。 // Open() 前でも動作する。 -bool -DiskImageRaw::GetInfo(off_t *sizep, bool *w_ok) +int +DiskImageRaw::IsWriteable() const { - // ファイルサイズは未取得なら取得 - 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; + int rv; // ファイルのパーミッション int r = access(pathname.c_str(), W_OK); if (r == 0) { - *w_ok = true; + rv = 1; } else if (errno == EACCES) { - *w_ok = false; + rv = 0; } else { - warn("%s: access failed", pathname.c_str()); - return false; + if (errno == ENOENT) { + warn("%s", pathname.c_str()); + } else { + warn("%s: access failed", pathname.c_str()); + } + rv = -1; } - return true; + return rv; } // オープン @@ -76,17 +68,14 @@ DiskImageRaw::Open(bool read_only) return false; } - // ファイルサイズは未取得なら取得 - if (filesize == (off_t)-1) { - struct stat st; - - if (fstat(fd, &st) < 0) { - warn("%s: stat failed", pathname.c_str()); - fd.Close(); - return false; - } - filesize = st.st_size; + // ファイルサイズを取得 + struct stat st; + if (fstat(fd, &st) < 0) { + warn("%s: stat failed", pathname.c_str()); + fd.Close(); + return false; } + filesize = st.st_size; return true; } @@ -95,13 +84,7 @@ void DiskImageRaw::Close() { fd.Close(); - filesize = (off_t)-1; -} - -off_t -DiskImageRaw::GetSize() const -{ - return filesize; + filesize = 0; } bool