104 | | The ''vfs_file_t'' structures track things like how many file handles reference it, the current position in the open file and the corresponding VFS node. The transition from a file handle to a VFS node is therefore straightforward. |
| 104 | The ''vfs_file_t'' structures track things like how many file handles reference it, the current position in the open file and the corresponding VFS node. The transition from a file handle to a VFS node is therefore straightforward and is best shown on the following example: |
| 105 | |
| 106 | {{{ |
| 107 | int fd; /* file handle */ |
| 108 | ... |
| 109 | /* Lookup the file structure corresponding to the file descriptor. */ |
| 110 | vfs_file_t *file = vfs_file_get(fd); |
| 111 | ... |
| 112 | /* |
| 113 | * Lock the open file structure so that no other thread can manipulate |
| 114 | * the same open file at a time. |
| 115 | */ |
| 116 | fibril_mutex_lock(&file->lock); |
| 117 | ... |
| 118 | /* |
| 119 | * Lock the file's node so that no other client can read/write to it at |
| 120 | * the same time. |
| 121 | */ |
| 122 | if (read) |
| 123 | fibril_rwlock_read_lock(&file->node->contents_rwlock); |
| 124 | else |
| 125 | fibril_rwlock_write_lock(&file->node->contents_rwlock); |
| 126 | |
| 127 | }}} |
| 128 | |
| 129 | In the above code snippet, the ''vfs_rdwr()'' function first translates the file handle using the ''vfs_file_get()'' interface to a ''vfs_file_t'' structure and then locks it. The VFS node is directly addressed in the two RW-lock lock operations at the end of the example. |