Changes between Version 1 and Version 2 of FilesystemAPITutorial


Ignore:
Timestamp:
2017-04-08T15:41:13Z (8 years ago)
Author:
Jakub Jermář
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • FilesystemAPITutorial

    v1 v2  
    3838}}}
    3939
    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.
     40Note 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.
    4141
    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:
     42We 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:
    4343
    4444{{{
     
    8282...
    8383}}}
     84
     85When we are finished with `file`, we must not forget to return it to the system:
     86
     87{{{
     88vfs_put(file);
     89}}}
     90
     91== Creating a new directory with a file ==
     92
     93Imagine 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{{{
     96int foobar;
     97int rc = vfs_link_path("/foo/foobar", KIND_DIRECTORY, &foobar);
     98if (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{{{
     105int hello;
     106rc = vfs_link(foobar, "hello", KIND_FILE, &hello);
     107(void) vfs_put(foobar);
     108if (rc != EOK)
     109        return rc;
     110
     111rc = vfs_open(hello, MODE_WRITE);
     112if (rc != EOK) {
     113        vfs_put(hello);
     114        return rc;
     115}
     116
     117char greeting[] = "Hello world!";
     118ssize_t size = vfs_write(hello, (aoff64_t []) {0}, greeting, sizeof(greeting));
     119if (size < 0) {
     120        vfs_put(hello);
     121        return size;
     122}
     123}}}