| | 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 | }}} |