|
|
1.1 root 1: //
2: // nono
3: // Copyright (C) 2021 nono project
4: // Licensed under nono-license.txt
5: //
6:
7: //
8: // ディスクイメージドライバ (生イメージ形式)
9: //
10:
11: #include "diskdriver_raw.h"
12: #include <fcntl.h>
13: #include <sys/stat.h>
14:
15: // コンストラクタ
16: DiskDriverRaw::DiskDriverRaw(const std::string& objname_,
17: const std::string& pathname_)
18: : inherited(objname_ + ".raw", pathname_)
19: {
20: }
21:
22: // デストラクタ
23: DiskDriverRaw::~DiskDriverRaw()
24: {
25: }
26:
27: // マッチ。
28: bool
29: DiskDriverRaw::Match()
30: {
31: // 構造を持たないので、オープンできれば常に成功する。
32: fd = open(pathname.c_str(), O_RDONLY);
33: if (fd < 0) {
34: return false;
35: }
36:
37: return true;
38: }
39:
40: // アタッチ。
41: bool
42: DiskDriverRaw::Attach(std::string& errmsg)
43: {
44: // 構造を持たないので、ここですることは何もない。
45: putmsg(1, "%s", pathname.c_str());
46: return true;
47: }
48:
49: // オープン。
50: bool
51: DiskDriverRaw::Open(bool read_only)
52: {
53: assert(pathname.empty() == false);
54: assert(fd.Valid());
55:
56: if (read_only == false) {
57: // RW 指定ならオープンし直す。
58: fd.Close();
59: fd = open(pathname.c_str(), O_RDWR);
60: if (fd < 0) {
61: warn("%s", pathname.c_str());
62: return false;
63: }
64: }
65:
66: // ファイルサイズを取得
67: struct stat st;
68: if (fstat(fd, &st) < 0) {
69: warn("%s: stat failed", pathname.c_str());
70: return false;
71: }
72: filesize = st.st_size;
73:
74: return true;
75: }
76:
77: void
78: DiskDriverRaw::Close()
79: {
80: fd.Close();
81: filesize = 0;
82: }
83:
84: bool
85: DiskDriverRaw::Read(void *buf, off_t offset, size_t size)
86: {
87: if (__predict_false(fd < 0)) {
88: return false;
89: }
90: if (__predict_false(offset + size > GetSize())) {
91: return false;
92: }
93:
94: if (readpos(fd, buf, size, offset) < 0) {
95: return false;
96: }
97: return true;
98: }
99:
100: bool
101: DiskDriverRaw::Write(const void *buf, off_t offset, size_t size)
102: {
103: if (__predict_false(fd < 0)) {
104: return false;
105: }
106: if (__predict_false(offset + size > GetSize())) {
107: return false;
108: }
109:
110: if (writepos(fd, buf, size, offset) < 0) {
111: return false;
112: }
113: return true;
114: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.