-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfs_op_read.c
70 lines (61 loc) · 1.63 KB
/
fs_op_read.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
/*
* fs_read.c
*
* description: fs_read function for CS 5600 / 7600 file system
*
* CS 5600, Computer Systems, Northeastern CCIS
* Peter Desnoyers, November 2016
* Philip Gust, March 2019, March 2020
*/
#include <stdlib.h>
#include <errno.h>
#include <fuse.h>
#include <string.h>
#include "fs_util_file.h"
#include "fs_util_path.h"
#include "fs_util_vol.h"
#include "blkdev.h"
/**
* read - read data from an open file.
*
* Should return exactly the number of bytes requested, except:
* - if offset >= file len, return 0
* - if offset+len > file len, return bytes from offset to EOF
* - on error, return <0
*
* Errors:
* -ENOENT - file does not exist
* -ENOTDIR - component of path not a directory
* -EISDIR - file is a directory
* -EIO - error reading block
*
* @param path the path to the file
* @param buf the read buffer
* @param len the number of bytes to read
* @param offset to start reading at
* @param fi fuse file info
* @return number of bytes actually read if successful, or -error number
*/
int fs_read(const char* path, char* buf, size_t len, off_t offset,
struct fuse_file_info* fi)
{
int inum;
if (fi != NULL) {
// get inode stored in fi->fh by fs_open()
inum = fi->fh;
} else {
// get inode for specified path
inum = get_inode_of_file_path(path);
// report error if error
if (inum < 0) {
return inum;
}
}
/* cannot read if it is directory */
if (S_ISDIR(fs.inodes[inum].mode)) {
return -EISDIR;
}
// read bytes of inode
int nread = do_read(inum, buf, len, offset);
return nread;
}