《操作系统》的实验代码。
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.

110 lines
2.1 KiB

12 years ago
  1. #include <defs.h>
  2. #include <string.h>
  3. #include <vfs.h>
  4. #include <inode.h>
  5. #include <unistd.h>
  6. #include <error.h>
  7. #include <assert.h>
  8. // open file in vfs, get/create inode for file with filename path.
  9. int
  10. vfs_open(char *path, uint32_t open_flags, struct inode **node_store) {
  11. bool can_write = 0;
  12. switch (open_flags & O_ACCMODE) {
  13. case O_RDONLY:
  14. break;
  15. case O_WRONLY:
  16. case O_RDWR:
  17. can_write = 1;
  18. break;
  19. default:
  20. return -E_INVAL;
  21. }
  22. if (open_flags & O_TRUNC) {
  23. if (!can_write) {
  24. return -E_INVAL;
  25. }
  26. }
  27. int ret;
  28. struct inode *node;
  29. bool excl = (open_flags & O_EXCL) != 0;
  30. bool create = (open_flags & O_CREAT) != 0;
  31. ret = vfs_lookup(path, &node);
  32. if (ret != 0) {
  33. if (ret == -16 && (create)) {
  34. char *name;
  35. struct inode *dir;
  36. if ((ret = vfs_lookup_parent(path, &dir, &name)) != 0) {
  37. return ret;
  38. }
  39. ret = vop_create(dir, name, excl, &node);
  40. } else return ret;
  41. } else if (excl && create) {
  42. return -E_EXISTS;
  43. }
  44. assert(node != NULL);
  45. if ((ret = vop_open(node, open_flags)) != 0) {
  46. vop_ref_dec(node);
  47. return ret;
  48. }
  49. vop_open_inc(node);
  50. if (open_flags & O_TRUNC || create) {
  51. if ((ret = vop_truncate(node, 0)) != 0) {
  52. vop_open_dec(node);
  53. vop_ref_dec(node);
  54. return ret;
  55. }
  56. }
  57. *node_store = node;
  58. return 0;
  59. }
  60. // close file in vfs
  61. int
  62. vfs_close(struct inode *node) {
  63. vop_open_dec(node);
  64. vop_ref_dec(node);
  65. return 0;
  66. }
  67. // unimplement
  68. int
  69. vfs_unlink(char *path) {
  70. return -E_UNIMP;
  71. }
  72. // unimplement
  73. int
  74. vfs_rename(char *old_path, char *new_path) {
  75. return -E_UNIMP;
  76. }
  77. // unimplement
  78. int
  79. vfs_link(char *old_path, char *new_path) {
  80. return -E_UNIMP;
  81. }
  82. // unimplement
  83. int
  84. vfs_symlink(char *old_path, char *new_path) {
  85. return -E_UNIMP;
  86. }
  87. // unimplement
  88. int
  89. vfs_readlink(char *path, struct iobuf *iob) {
  90. return -E_UNIMP;
  91. }
  92. // unimplement
  93. int
  94. vfs_mkdir(char *path){
  95. return -E_UNIMP;
  96. }