Changes between Version 2 and Version 3 of FilesystemAPITutorial
- Timestamp:
- 2017-04-09T07:40:21Z (8 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
FilesystemAPITutorial
v2 v3 6 6 == Preparing to use the API == 7 7 8 HelenOS filesystem API is implemented in the standard library, so there is no need to link against any additional library in order to access files. HelenOS requires you to only include `vfs/vfs.h` in order to get declarations of all filesystemAPIs:8 HelenOS filesystem API is implemented in the standard library, so there is no need to link against any additional library in order to be able to use it. HelenOS only requires filesystem clients to include `vfs/vfs.h` in order to get declarations of all the APIs: 9 9 10 10 {{{ … … 14 14 == Paths and File handles == 15 15 16 The API is designed to use file handles as a primary way to refer to files. One may obtain a file handle by looking up a filesytem path, creating a new file or directory or attaching a new filesystem into the filesystem hierarchy.16 The API is designed to use file handles as a primary and prefered way to refer to files. One may obtain a file handle by looking up a filesytem path, creating a new file or directory or attaching a new filesystem into the filesystem hierarchy. 17 17 18 18 == Reading an existing file == … … 100 100 }}} 101 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: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 (but directory `/foo` must already exist). 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 103 104 104 {{{ … … 108 108 if (rc != EOK) 109 109 return rc; 110 }}} 110 111 112 As in the previous scenario, the file handle needs to be opened before we can write to it: 113 114 {{{ 111 115 rc = vfs_open(hello, MODE_WRITE); 112 116 if (rc != EOK) { … … 114 118 return rc; 115 119 } 120 }}} 121 122 Finally, we can write our message. This time we use a compound literal to pass our initial position. 116 123 117 124 char greeting[] = "Hello world!"; … … 122 129 } 123 130 }}} 131 132 Finally, we put the file handle: 133 134 {{{ 135 rc = vfs_put(hello); 136 if (rc != EOK) 137 return rc; 138 }}} 139 140 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.