Changes between Version 1 and Version 2 of FilesystemAPITutorial
- Timestamp:
- 2017-04-08T15:41:13Z (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
FilesystemAPITutorial
v1 v2 38 38 }}} 39 39 40 Note that `vfs_open()` does not take a path as an argument and does not return a file handle. Instead it takes a pre-existing file handle as an argument, internally marks the file as open for reading and returns a result code, which is EOKon success and negative on error. Also note that in case of error, the file handle we are using needs to be returned to the system before bailing out.40 Note that `vfs_open()` does not take a path as an argument and does not return a file handle. Instead it takes a pre-existing file handle as an argument, internally marks the file as open for reading and returns a result code, which is `EOK` on success and negative on error. Also note that in case of error, the file handle we are using needs to be returned to the system before bailing out. 41 41 42 We are now ready to do a couple of synchronous read from the file. Let's say we first want to read a 4KiB-buffer from offset 0x1000:42 We are now ready to do a couple of synchronous reads from the file. Let's say we first want to read a 4KiB-buffer from offset 0x1000: 43 43 44 44 {{{ … … 82 82 ... 83 83 }}} 84 85 When we are finished with `file`, we must not forget to return it to the system: 86 87 {{{ 88 vfs_put(file); 89 }}} 90 91 == Creating a new directory with a file == 92 93 Imagine we now want to create a new directory `/foo/foobar`, create a file named `hello.txt` and write "Hello world!" to it. Let's start with creating the directory: 94 95 {{{ 96 int foobar; 97 int rc = vfs_link_path("/foo/foobar", KIND_DIRECTORY, &foobar); 98 if (rc != EOK) 99 return rc; 100 }}} 101 102 `vfs_link_path()` used here is a convenience wrapper around `vfs_link()`, which we will shortly use too. It allows us not to care about `/foo`'s handle. If the return code is `EOK`, we can be sure that a new directory named `/foo/foobar` was created and also that there was no file or directory of the same name standing in its way. Moreover, the variable `foobar` now contains a file handle of the newly created directory. We will use it now to create the file: 103 104 {{{ 105 int hello; 106 rc = vfs_link(foobar, "hello", KIND_FILE, &hello); 107 (void) vfs_put(foobar); 108 if (rc != EOK) 109 return rc; 110 111 rc = vfs_open(hello, MODE_WRITE); 112 if (rc != EOK) { 113 vfs_put(hello); 114 return rc; 115 } 116 117 char greeting[] = "Hello world!"; 118 ssize_t size = vfs_write(hello, (aoff64_t []) {0}, greeting, sizeof(greeting)); 119 if (size < 0) { 120 vfs_put(hello); 121 return size; 122 } 123 }}}