24 FileDescriptor =
::open(path, O_RDWR|O_CREAT, 0600);
27 if (FileDescriptor < 0) {
29 snprintf(what, BUFSIZ,
"Unable to open %s: %s", path, strerror(errno));
30 throw std::runtime_error(what);
34 if (ftruncate(FileDescriptor, nblocks*BLOCK_SIZE) < 0) {
36 snprintf(what, BUFSIZ,
"Unable to open %s: %s", path, strerror(errno));
37 throw std::runtime_error(what);
52 if (FileDescriptor > 0) {
54 printf(
"%lu disk block reads\n",
Reads);
55 printf(
"%lu disk block writes\n",
Writes);
56 close(FileDescriptor);
70 snprintf(what, BUFSIZ,
"blocknum (%d) is negative!", blocknum);
71 throw std::invalid_argument(what);
74 if (blocknum >= (
int)
Blocks) {
75 snprintf(what, BUFSIZ,
"blocknum (%d) is too big!", blocknum);
76 throw std::invalid_argument(what);
81 snprintf(what, BUFSIZ,
"null data pointer!");
82 throw std::invalid_argument(what);
95 if (lseek(FileDescriptor, blocknum*BLOCK_SIZE, SEEK_SET) < 0) {
97 snprintf(what, BUFSIZ,
"Unable to lseek %d: %s", blocknum, strerror(errno));
98 throw std::runtime_error(what);
102 if (::
read(FileDescriptor, data, BLOCK_SIZE) != BLOCK_SIZE) {
104 snprintf(what, BUFSIZ,
"Unable to read %d: %s", blocknum, strerror(errno));
105 throw std::runtime_error(what);
121 if (lseek(FileDescriptor, blocknum*BLOCK_SIZE, SEEK_SET) < 0) {
123 snprintf(what, BUFSIZ,
"Unable to lseek %d: %s", blocknum, strerror(errno));
124 throw std::runtime_error(what);
128 if (::
write(FileDescriptor, data, BLOCK_SIZE) != BLOCK_SIZE) {
130 snprintf(what, BUFSIZ,
"Unable to write %d: %s", blocknum, strerror(errno));
131 throw std::runtime_error(what);