Changes between Version 4 and Version 5 of FilesystemAPITutorial
- Timestamp:
- 2017-04-09T08:14:42Z (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
FilesystemAPITutorial
v4 v5 91 91 == Creating a new directory with a file == 92 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:93 Imagine we now want to create a new directory `/foo/foobar`, then create a file named `hello.txt` inside of it and finally write the string "Hello world!" to it. Let's start with creating the directory: 94 94 95 95 {{{ … … 120 120 }}} 121 121 122 Finally, we can write our message. This time we use a compound literal to pass our initial position:122 Finally, we can write our string. This time we use a compound literal to pass our initial position: 123 123 124 124 {{{ 125 char greeting[] = "Hello world!";126 ssize_t size = vfs_write(hello, (aoff64_t []) {0}, greeting, sizeof(greeting));125 char msg[] = "Hello world!"; 126 ssize_t size = vfs_write(hello, (aoff64_t []) {0}, msg, sizeof(msg)); 127 127 if (size < 0) { 128 128 vfs_put(hello); … … 140 140 141 141 Here we are interested in the return code of `vfs_put()` because we made modifications to the file. An error at this point might indicate our data didn't get entirely through. 142 143 == Removing a file == 144 145 In general, files may have multiple names - so called hardlinks. To remove one of these hardlinks (including the last one), we must unlink it. We could either use a convenience wrapper (eg. `vfs_unlink_path()`) or, if we already have a file handle of the parent directory and optionally a file handle of the unlinked file, like in the example above, we use `vfs_unlink()`. So imagine we want to remove the file created in the previous example and that neither of `foobar` or `hello` have been put by `vfs_put()` yet: 146 147 {{{ 148 rc = vfs_unlink(parent, "hello", hello); 149 if (rc != EOK) 150 return rc; 151 }}} 152 153 By passing the third argument to `vfs_unlink()` above, we are making sure that we won't accidentally remove some other link called `hello` created by someone else after the original `hello` had been removed. Note that this argument can be -1 if we don't care.