Repository navigation
Expand file tree
/
Copy pathdiskio-heap.cc
More file actions
173 lines (144 loc) · 3.7 KB
/
Copy pathdiskio-heap.cc
File metadata and controls
173 lines (144 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (C) 2020 The Android Open Source Project
*
* This software is licensed under the terms of the GNU General Public
* License version 2, as published by the Free Software Foundation, and
* may be copied, distributed, and modified under those terms.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include "diskio.h"
using namespace std;
int DiskIO::OpenForRead(const unsigned char* data, size_t size) {
this->data = data;
this->size = size;
this->off = 0;
this->isOpen = 1;
this->openForWrite = 0;
return 1;
}
void DiskIO::MakeRealName(void) { this->realFilename = this->userFilename; }
int DiskIO::OpenForRead(void) {
struct stat64 st;
if (this->isOpen) {
if (this->openForWrite) {
Close();
} else {
return 1;
}
}
this->fd = open(realFilename.c_str(), O_RDONLY | O_CREAT, S_IRUSR | S_IRGRP | S_IROTH);
if (this->fd == -1) {
this->realFilename = this->userFilename = "";
} else {
if (fstat64(fd, &st) == 0) {
if (!(S_ISDIR(st.st_mode) || S_ISFIFO(st.st_mode) ||
S_ISSOCK(st.st_mode))) {
this->isOpen = 1;
}
}
}
return this->isOpen;
}
int DiskIO::OpenForWrite(void) {
if ((this->isOpen) && (this->openForWrite)) {
return 1;
}
Close();
this->fd = open(realFilename.c_str(), O_WRONLY | O_CREAT,
S_IWUSR | S_IRUSR | S_IRGRP | S_IROTH);
if (fd >= 0) {
this->isOpen = 1;
this->openForWrite = 1;
}
return this->isOpen;
}
void DiskIO::Close(void) {
if (this->isOpen) {
close(this->fd);
}
this->isOpen = 0;
this->openForWrite = 0;
}
int DiskIO::GetBlockSize(void) {
return 512;
}
int DiskIO::GetPhysBlockSize(void) {
return 512;
}
uint32_t DiskIO::GetNumHeads(void) {
return 255;
}
uint32_t DiskIO::GetNumSecsPerTrack(void) {
return 63;
}
int DiskIO::DiskSync(void) {
return 1;
}
int DiskIO::Seek(uint64_t sector) {
int retval = 1;
off_t seekTo = sector * static_cast<uint64_t>(GetBlockSize());
if (!isOpen) {
if (OpenForRead() != 1) {
retval = 0;
}
}
if (isOpen && seekTo < this->size) {
off_t sought = lseek64(fd, seekTo, SEEK_SET);
if (sought != seekTo) {
retval = 0;
}
}
if (retval) {
this->off = seekTo;
}
return retval;
}
int DiskIO::Read(void* buffer, int numBytes) {
int actualBytes = 0;
if (this->size > this->off) {
actualBytes = std::min(static_cast<int>(this->size - this->off), numBytes);
memcpy(buffer, this->data + this->off, actualBytes);
}
return actualBytes;
}
int DiskIO::Write(void *buffer, int numBytes) {
int blockSize, i, numBlocks, retval = 0;
char *tempSpace;
if ((!this->isOpen) || (!this->openForWrite)) {
OpenForWrite();
}
if (this->isOpen) {
blockSize = GetBlockSize();
if (numBytes <= blockSize) {
numBlocks = 1;
tempSpace = new char[blockSize];
} else {
numBlocks = numBytes / blockSize;
if ((numBytes % blockSize) != 0)
numBlocks++;
tempSpace = new char[numBlocks * blockSize];
}
if (tempSpace == NULL) {
return 0;
}
memcpy(tempSpace, buffer, numBytes);
for (i = numBytes; i < numBlocks * blockSize; i++) {
tempSpace[i] = 0;
}
retval = write(fd, tempSpace, numBlocks * blockSize);
if (((numBlocks * blockSize) != numBytes) && (retval > 0))
retval = numBytes;
delete[] tempSpace;
}
return retval;
}
uint64_t DiskIO::DiskSize(int *) {
return this->size / GetBlockSize();
}