-
Notifications
You must be signed in to change notification settings - Fork 0
/
userfs.h
142 lines (121 loc) · 3.8 KB
/
userfs.h
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
#include <stddef.h>
#include <sys/types.h>
#define NEED_OPEN_FLAGS
/**
* User-defined in-memory filesystem. It is as simple as possible.
* Each file lies in the memory as an array of blocks. A file
* has an unique file name, and there are no directories, so the
* FS is a monolithic flat contiguous folder.
*/
/**
* Here you should specify which features do you want to implement
* via macros: NEED_OPEN_FLAGS and NEED_RESIZE. If you want to
* allow advanced flags, do this here:
*
* #define NEED_OPEN_FLAGS
*
* To allow resize() functions define this:
*
* #define NEED_RESIZE
*
* It is important to define these macros here, in the header,
* because it is used by tests.
*/
/**
* Flags for ufs_open call.
*/
enum open_flags {
/** If the flag is specified and file does not exist - create it. */
UFS_CREATE = 1,
#ifdef NEED_OPEN_FLAGS
UFS_READ_ONLY = 2,
UFS_WRITE_ONLY = 4,
UFS_READ_WRITE = 8,
#endif
};
/** Possible errors from all functions. */
enum ufs_error_code {
UFS_ERR_NO_ERR = 0,
UFS_ERR_NO_FILE,
UFS_ERR_NO_MEM,
#ifdef NEED_OPEN_FLAGS
UFS_ERR_NO_PERMISSION,
#endif
};
/** Get code of the last error. */
enum ufs_error_code ufs_errno();
/**
* Open a file by filename.
* @param filename Name of a file to open.
* @param flags Bitwise combination of open_flags.
*
* @retval > 0 File descriptor.
* @retval -1 Error occurred. Check ufs_errno() for a code.
* - UFS_ERR_NO_FILE - no such file, and UFS_CREATE flag is
* not specified.
*/
int ufs_open(const char *filename, int flags);
/**
* Write data to the file.
* @param fd File descriptor from ufs_open().
* @param buf Buffer to write.
* @param size Size of @a buf.
*
* @retval > 0 How many bytes were written.
* @retval -1 Error occurred. Check ufs_errno() for a code.
* - UFS_ERR_NO_FILE - invalid file descriptor.
* - UFS_ERR_NO_MEM - not enough memory.
*/
ssize_t ufs_write(int fd, const char *buf, size_t size);
/**
* Read data from the file.
* @param fd File descriptor from ufs_open().
* @param buf Buffer to read into.
* @param size Maximum bytes to read.
*
* @retval > 0 How many bytes were read.
* @retval 0 EOF.
* @retval -1 Error occurred. Check ufs_errno() for a code.
* - UFS_ERR_NO_FILE - invalid file descriptor.
*/
ssize_t ufs_read(int fd, char *buf, size_t size);
#ifdef NEED_RESIZE
/**
* Resize a file opened by the file descriptor @a fd. If current
* file size is less than @a new_size, then new empty blocks are
* created and positions of opened file descriptors are not
* changed. If the current size is bigger than @a new_size, then
* the blocks are truncated. Opened file descriptors behind the
* new file size should proceed from the new file end.
*
* @param fd File descriptor from ufs_open().
* @param new_size New file size.
* @retval 0 Success.
* @retval -1 Error occurred.
* - UFS_ERR_NO_FILE - invalid file descriptor.
* - UFS_ERR_NO_MEM - not enough memory. Can appear only when
* @a new_size is bigger than the current size.
*/
int ufs_resize(int fd, size_t new_size);
#endif
/**
* Close a file.
* @param fd File descriptor from ufs_open().
* @retval 0 Success.
* @retval -1 Error occurred. Check ufs_errno() for a code.
* - UFS_ERR_NO_FILE - invalid file descriptor.
*/
int ufs_close(int fd);
/**
* Delete a file by its name. Note, that it is allowed to drop the
* file even if there are opened descriptors. In such a case the
* file content will live until the last descriptor is closed. If
* the file is deleted, it is allowed to create a new one with the
* same name immediately and it should not affect existing opened
* descriptors of the deleted file.
*
* @param filename Name of a file to delete.
* @retval -1 Error occurred. Check ufs_errno() for a code.
* - UFS_ERR_NO_FILE - no such file.
*/
int ufs_delete(const char *filename);