Follow us on Google+ Follow us on Facebook Follow us on Twitter

Changes between Version 4 and Version 5 of FilesystemAPITutorial


Ignore:
Timestamp:
2017-04-09T08:14:42Z (8 months ago)
Author:
Jakub Jermář
Comment:

Add paragraph on unlinking

Legend:

Unmodified
Added
Removed
Modified
  • FilesystemAPITutorial

    v4 v5  
    9191== Creating a new directory with a file ==
    9292
    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:
     93Imagine 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:
    9494
    9595{{{
     
    120120}}}
    121121
    122 Finally, we can write our message. This time we use a compound literal to pass our initial position:
     122Finally, we can write our string. This time we use a compound literal to pass our initial position:
    123123
    124124{{{
    125 char greeting[] = "Hello world!";
    126 ssize_t size = vfs_write(hello, (aoff64_t []) {0}, greeting, sizeof(greeting));
     125char msg[] = "Hello world!";
     126ssize_t size = vfs_write(hello, (aoff64_t []) {0}, msg, sizeof(msg));
    127127if (size < 0) {
    128128        vfs_put(hello);
     
    140140
    141141Here 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
     145In 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{{{
     148rc = vfs_unlink(parent, "hello", hello);
     149if (rc != EOK)
     150        return rc;
     151}}}
     152
     153By 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.