/
fileops.c
281 lines (231 loc) · 5.46 KB
/
fileops.c
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* fileops.c - This module provides an Unix like file absraction
* on the prog1 file system access code
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <inttypes.h>
#include "fileops.h"
#include "proj1/pathname.h"
#include "proj1/unixfilesystem.h"
#include "diskimg.h"
#include "proj1/inode.h"
#include "proj1/file.h"
#include "proj1/chksumfile.h"
#define MAX_FILES 64
static uint64_t numopens = 0;
static uint64_t numreads = 0;
static uint64_t numgetchars = 0;
static uint64_t numisfiles = 0;
/*
* Table of open files.
*/
static struct {
char *pathname; // absolute pathname NULL if slot is not used.
int cursor; // Current position in the file
} openFileTable[MAX_FILES];
static struct unixfilesystem *unixfs;
/*
* Initialize the fileops module for the specified disk.
*/
void *
Fileops_init(char *diskpath)
{
memset(openFileTable, 0, sizeof(openFileTable));
int fd = diskimg_open(diskpath, 1);
if (fd < 0) {
fprintf(stderr, "Can't open diskimagePath %s\n", diskpath);
return NULL;
}
unixfs = unixfilesystem_init(fd);
if (unixfs == NULL) {
diskimg_close(fd);
return NULL;
}
return unixfs;
}
/*
* Open the specified absolute pathname for reading. Returns -1 on error;
*/
int
Fileops_open(char *pathname)
{
int fd;
int inumber;
numopens++;
inumber = pathname_lookup(unixfs,pathname);
if (inumber < 0) {
return -1; // File not found
}
for (fd = 0; fd < MAX_FILES; fd++) {
if (openFileTable[fd].pathname == NULL) break;
}
if (fd >= MAX_FILES) {
return -1; // No open file slots
}
openFileTable[fd].pathname = strdup(pathname); // Save our own copy
openFileTable[fd].cursor = 0;
return fd;
}
/*
* Get the inumber for the given file. Also makes sure that the cursor
* is within the file.
*/
int
Fileops_getinumber(int fd, int *size)
{
int inumber;
struct inode in;
int err;
inumber = pathname_lookup(unixfs, openFileTable[fd].pathname);
if (inumber < 0) {
return inumber; // Can't find file
}
err = inode_iget(unixfs, inumber,&in);
if (err < 0) {
return err;
}
if (!(in.i_mode & IALLOC)) {
return -1;
}
*size = inode_getsize(&in);
//if (openFileTable[fd].cursor >= size) return -1; // Finished with file
return inumber;
}
/*
* Returns the block number for the given part of the desired file.
*/
int
Fileops_getblockno(int fd)
{
return openFileTable[fd].cursor / DISKIMG_SECTOR_SIZE;
}
/*
* Updates the file block to the correct block.
*/
int
Fileops_getblock(int inum, int blockNo, unsigned char *buf)
{
return file_getblock(unixfs, inum,blockNo,buf);
}
/*
* Fetch the next character from the file. Return -1 if at end of file.
*/
int
Fileops_getchar(int fd, int inum, int size, unsigned char *buf, int *bm)
{
//int inumber;
//struct inode in;
//unsigned char buf[DISKIMG_SECTOR_SIZE];
int bytesMoved;
//int err, size;
int blockNo, blockOffset;
numgetchars++;
/*if (openFileTable[fd].pathname == NULL)
return -1; // fd not opened.
inumber = pathname_lookup(unixfs, openFileTable[fd].pathname);
if (inumber < 0) {
return inumber; // Can't find file
}
err = inode_iget(unixfs, inumber,&in);
if (err < 0) {
return err;
}
if (!(in.i_mode & IALLOC)) {
return -1;
}
size = inode_getsize(&in);
*/
if (openFileTable[fd].cursor >= size) return -1; // Finished with file
blockNo = openFileTable[fd].cursor / DISKIMG_SECTOR_SIZE;
blockOffset = openFileTable[fd].cursor % DISKIMG_SECTOR_SIZE;
if(blockOffset == 0)
{
bytesMoved = Fileops_getblock(inum, blockNo, buf);
*bm = bytesMoved;
}
else bytesMoved = *bm;
if (bytesMoved < 0) {
return -1;
}
assert(bytesMoved > blockOffset);
openFileTable[fd].cursor += 1;
return (int)(buf[blockOffset]);
}
/*
* Implement the Unix read system call. Number of bytes returned. Return -1 on
* err.
*/
int
Fileops_read(int fd, char *buffer, int length)
{
int i;
int ch;
int size = 0;
numreads++;
int inumber = Fileops_getinumber(fd, &size);
printf ("Size: %d\n", size);
if(inumber < 0) return 0;
int blockNo = Fileops_getblockno(fd);
unsigned char buf[DISKIMG_SECTOR_SIZE];
int bytesMoved = Fileops_getblock(inumber, blockNo, buf);
for (i = 0; i < length; i++) {
ch = Fileops_getchar(fd, inumber, size, buf, &bytesMoved);
if (ch == -1) break;
buffer[i] = ch;
}
return i;
}
/*
* Return the current position in the file.
*/
int
Fileops_tell(int fd)
{
if (openFileTable[fd].pathname == NULL)
return -1; // fd not opened.
return openFileTable[fd].cursor;
}
/*
* Close the files - return the resources
*/
int
Fileops_close(int fd)
{
if (openFileTable[fd].pathname == NULL)
return -1; // fd not opened.
free(openFileTable[fd].pathname);
openFileTable[fd].pathname = NULL;
return 0;
}
/*
* Return true if specified pathname is a regular file.
*/
int
Fileops_isfile(char *pathname)
{
numisfiles++;
int inumber = pathname_lookup(unixfs, pathname);
if (inumber < 0) {
return 0;
}
struct inode in;
int err = inode_iget(unixfs, inumber, &in);
if (err < 0) return 0;
if (!(in.i_mode & IALLOC) || ((in.i_mode & IFMT) != 0)) {
/* Not allocated or not a file */
return 0;
}
return 1; /* Must be a file */
}
void
Fileops_dumpstats(FILE *file)
{
fprintf(file,
"Fileops: %"PRIu64" opens, %"PRIu64" reads, "
"%"PRIu64" getchars, %"PRIu64 " isfiles\n",
numopens, numreads, numgetchars, numisfiles);
}