《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

356 lines
8.4 KiB

12 years ago
  1. #include <defs.h>
  2. #include <string.h>
  3. #include <vfs.h>
  4. #include <proc.h>
  5. #include <file.h>
  6. #include <unistd.h>
  7. #include <iobuf.h>
  8. #include <inode.h>
  9. #include <stat.h>
  10. #include <dirent.h>
  11. #include <error.h>
  12. #include <assert.h>
  13. #define testfd(fd) ((fd) >= 0 && (fd) < FILES_STRUCT_NENTRY)
  14. // get_fd_array - get current process's open files table
  15. static struct file *
  16. get_fd_array(void) {
  17. struct files_struct *filesp = current->filesp;
  18. assert(filesp != NULL && files_count(filesp) > 0);
  19. return filesp->fd_array;
  20. }
  21. // fd_array_init - initialize the open files table
  22. void
  23. fd_array_init(struct file *fd_array) {
  24. int fd;
  25. struct file *file = fd_array;
  26. for (fd = 0; fd < FILES_STRUCT_NENTRY; fd ++, file ++) {
  27. atomic_set(&(file->open_count), 0);
  28. file->status = FD_NONE, file->fd = fd;
  29. }
  30. }
  31. // fs_array_alloc - allocate a free file item (with FD_NONE status) in open files table
  32. static int
  33. fd_array_alloc(int fd, struct file **file_store) {
  34. // panic("debug");
  35. struct file *file = get_fd_array();
  36. if (fd == NO_FD) {
  37. for (fd = 0; fd < FILES_STRUCT_NENTRY; fd ++, file ++) {
  38. if (file->status == FD_NONE) {
  39. goto found;
  40. }
  41. }
  42. return -E_MAX_OPEN;
  43. }
  44. else {
  45. if (testfd(fd)) {
  46. file += fd;
  47. if (file->status == FD_NONE) {
  48. goto found;
  49. }
  50. return -E_BUSY;
  51. }
  52. return -E_INVAL;
  53. }
  54. found:
  55. assert(fopen_count(file) == 0);
  56. file->status = FD_INIT, file->node = NULL;
  57. *file_store = file;
  58. return 0;
  59. }
  60. // fd_array_free - free a file item in open files table
  61. static void
  62. fd_array_free(struct file *file) {
  63. assert(file->status == FD_INIT || file->status == FD_CLOSED);
  64. assert(fopen_count(file) == 0);
  65. if (file->status == FD_CLOSED) {
  66. vfs_close(file->node);
  67. }
  68. file->status = FD_NONE;
  69. }
  70. static void
  71. fd_array_acquire(struct file *file) {
  72. assert(file->status == FD_OPENED);
  73. fopen_count_inc(file);
  74. }
  75. // fd_array_release - file's open_count--; if file's open_count-- == 0 , then call fd_array_free to free this file item
  76. static void
  77. fd_array_release(struct file *file) {
  78. assert(file->status == FD_OPENED || file->status == FD_CLOSED);
  79. assert(fopen_count(file) > 0);
  80. if (fopen_count_dec(file) == 0) {
  81. fd_array_free(file);
  82. }
  83. }
  84. // fd_array_open - file's open_count++, set status to FD_OPENED
  85. void
  86. fd_array_open(struct file *file) {
  87. assert(file->status == FD_INIT && file->node != NULL);
  88. file->status = FD_OPENED;
  89. fopen_count_inc(file);
  90. }
  91. // fd_array_close - file's open_count--; if file's open_count-- == 0 , then call fd_array_free to free this file item
  92. void
  93. fd_array_close(struct file *file) {
  94. assert(file->status == FD_OPENED);
  95. assert(fopen_count(file) > 0);
  96. file->status = FD_CLOSED;
  97. if (fopen_count_dec(file) == 0) {
  98. fd_array_free(file);
  99. }
  100. }
  101. //fs_array_dup - duplicate file 'from' to file 'to'
  102. void
  103. fd_array_dup(struct file *to, struct file *from) {
  104. //cprintf("[fd_array_dup]from fd=%d, to fd=%d\n",from->fd, to->fd);
  105. assert(to->status == FD_INIT && from->status == FD_OPENED);
  106. to->pos = from->pos;
  107. to->readable = from->readable;
  108. to->writable = from->writable;
  109. struct inode *node = from->node;
  110. vop_ref_inc(node), vop_open_inc(node);
  111. to->node = node;
  112. fd_array_open(to);
  113. }
  114. // fd2file - use fd as index of fd_array, return the array item (file)
  115. static inline int
  116. fd2file(int fd, struct file **file_store) {
  117. if (testfd(fd)) {
  118. struct file *file = get_fd_array() + fd;
  119. if (file->status == FD_OPENED && file->fd == fd) {
  120. *file_store = file;
  121. return 0;
  122. }
  123. }
  124. return -E_INVAL;
  125. }
  126. // file_testfd - test file is readble or writable?
  127. bool
  128. file_testfd(int fd, bool readable, bool writable) {
  129. int ret;
  130. struct file *file;
  131. if ((ret = fd2file(fd, &file)) != 0) {
  132. return 0;
  133. }
  134. if (readable && !file->readable) {
  135. return 0;
  136. }
  137. if (writable && !file->writable) {
  138. return 0;
  139. }
  140. return 1;
  141. }
  142. // open file
  143. int
  144. file_open(char *path, uint32_t open_flags) {
  145. bool readable = 0, writable = 0;
  146. switch (open_flags & O_ACCMODE) {
  147. case O_RDONLY: readable = 1; break;
  148. case O_WRONLY: writable = 1; break;
  149. case O_RDWR:
  150. readable = writable = 1;
  151. break;
  152. default:
  153. return -E_INVAL;
  154. }
  155. int ret;
  156. struct file *file;
  157. if ((ret = fd_array_alloc(NO_FD, &file)) != 0) {
  158. return ret;
  159. }
  160. struct inode *node;
  161. if ((ret = vfs_open(path, open_flags, &node)) != 0) {
  162. fd_array_free(file);
  163. return ret;
  164. }
  165. file->pos = 0;
  166. if (open_flags & O_APPEND) {
  167. struct stat __stat, *stat = &__stat;
  168. if ((ret = vop_fstat(node, stat)) != 0) {
  169. vfs_close(node);
  170. fd_array_free(file);
  171. return ret;
  172. }
  173. file->pos = stat->st_size;
  174. }
  175. file->node = node;
  176. file->readable = readable;
  177. file->writable = writable;
  178. fd_array_open(file);
  179. return file->fd;
  180. }
  181. // close file
  182. int
  183. file_close(int fd) {
  184. int ret;
  185. struct file *file;
  186. if ((ret = fd2file(fd, &file)) != 0) {
  187. return ret;
  188. }
  189. fd_array_close(file);
  190. return 0;
  191. }
  192. // read file
  193. int
  194. file_read(int fd, void *base, size_t len, size_t *copied_store) {
  195. int ret;
  196. struct file *file;
  197. *copied_store = 0;
  198. if ((ret = fd2file(fd, &file)) != 0) {
  199. return ret;
  200. }
  201. if (!file->readable) {
  202. return -E_INVAL;
  203. }
  204. fd_array_acquire(file);
  205. struct iobuf __iob, *iob = iobuf_init(&__iob, base, len, file->pos);
  206. ret = vop_read(file->node, iob);
  207. size_t copied = iobuf_used(iob);
  208. if (file->status == FD_OPENED) {
  209. file->pos += copied;
  210. }
  211. *copied_store = copied;
  212. fd_array_release(file);
  213. return ret;
  214. }
  215. // write file
  216. int
  217. file_write(int fd, void *base, size_t len, size_t *copied_store) {
  218. int ret;
  219. struct file *file;
  220. *copied_store = 0;
  221. if ((ret = fd2file(fd, &file)) != 0) {
  222. return ret;
  223. }
  224. if (!file->writable) {
  225. return -E_INVAL;
  226. }
  227. fd_array_acquire(file);
  228. struct iobuf __iob, *iob = iobuf_init(&__iob, base, len, file->pos);
  229. ret = vop_write(file->node, iob);
  230. size_t copied = iobuf_used(iob);
  231. if (file->status == FD_OPENED) {
  232. file->pos += copied;
  233. }
  234. *copied_store = copied;
  235. fd_array_release(file);
  236. return ret;
  237. }
  238. // seek file
  239. int
  240. file_seek(int fd, off_t pos, int whence) {
  241. struct stat __stat, *stat = &__stat;
  242. int ret;
  243. struct file *file;
  244. if ((ret = fd2file(fd, &file)) != 0) {
  245. return ret;
  246. }
  247. fd_array_acquire(file);
  248. switch (whence) {
  249. case LSEEK_SET: break;
  250. case LSEEK_CUR: pos += file->pos; break;
  251. case LSEEK_END:
  252. if ((ret = vop_fstat(file->node, stat)) == 0) {
  253. pos += stat->st_size;
  254. }
  255. break;
  256. default: ret = -E_INVAL;
  257. }
  258. if (ret == 0) {
  259. if ((ret = vop_tryseek(file->node, pos)) == 0) {
  260. file->pos = pos;
  261. }
  262. // cprintf("file_seek, pos=%d, whence=%d, ret=%d\n", pos, whence, ret);
  263. }
  264. fd_array_release(file);
  265. return ret;
  266. }
  267. // stat file
  268. int
  269. file_fstat(int fd, struct stat *stat) {
  270. int ret;
  271. struct file *file;
  272. if ((ret = fd2file(fd, &file)) != 0) {
  273. return ret;
  274. }
  275. fd_array_acquire(file);
  276. ret = vop_fstat(file->node, stat);
  277. fd_array_release(file);
  278. return ret;
  279. }
  280. // sync file
  281. int
  282. file_fsync(int fd) {
  283. int ret;
  284. struct file *file;
  285. if ((ret = fd2file(fd, &file)) != 0) {
  286. return ret;
  287. }
  288. fd_array_acquire(file);
  289. ret = vop_fsync(file->node);
  290. fd_array_release(file);
  291. return ret;
  292. }
  293. // get file entry in DIR
  294. int
  295. file_getdirentry(int fd, struct dirent *direntp) {
  296. int ret;
  297. struct file *file;
  298. if ((ret = fd2file(fd, &file)) != 0) {
  299. return ret;
  300. }
  301. fd_array_acquire(file);
  302. struct iobuf __iob, *iob = iobuf_init(&__iob, direntp->name, sizeof(direntp->name), direntp->offset);
  303. if ((ret = vop_getdirentry(file->node, iob)) == 0) {
  304. direntp->offset += iobuf_used(iob);
  305. }
  306. fd_array_release(file);
  307. return ret;
  308. }
  309. // duplicate file
  310. int
  311. file_dup(int fd1, int fd2) {
  312. int ret;
  313. struct file *file1, *file2;
  314. if ((ret = fd2file(fd1, &file1)) != 0) {
  315. return ret;
  316. }
  317. if ((ret = fd_array_alloc(fd2, &file2)) != 0) {
  318. return ret;
  319. }
  320. fd_array_dup(file2, file1);
  321. return file2->fd;
  322. }