Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should be aware that that you should avoid file I/O when possible. The main idea is to go "one level deeper" and call <a href="http://en.wikipedia.org/wiki/Virtual_file_system" rel="noreferrer">VFS level functions</a> instead of the syscall handler directly:</p> <p>Includes:</p> <pre><code>#include &lt;linux/fs.h&gt; #include &lt;asm/segment.h&gt; #include &lt;asm/uaccess.h&gt; #include &lt;linux/buffer_head.h&gt; </code></pre> <p>Opening a file (similar to open):</p> <pre><code>struct file *file_open(const char *path, int flags, int rights) { struct file *filp = NULL; mm_segment_t oldfs; int err = 0; oldfs = get_fs(); set_fs(get_ds()); filp = filp_open(path, flags, rights); set_fs(oldfs); if (IS_ERR(filp)) { err = PTR_ERR(filp); return NULL; } return filp; } </code></pre> <p>Close a file (similar to close):</p> <pre><code>void file_close(struct file *file) { filp_close(file, NULL); } </code></pre> <p>Reading data from a file (similar to pread):</p> <pre><code>int file_read(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) { mm_segment_t oldfs; int ret; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_read(file, data, size, &amp;offset); set_fs(oldfs); return ret; } </code></pre> <p>Writing data to a file (similar to pwrite):</p> <pre><code>int file_write(struct file *file, unsigned long long offset, unsigned char *data, unsigned int size) { mm_segment_t oldfs; int ret; oldfs = get_fs(); set_fs(get_ds()); ret = vfs_write(file, data, size, &amp;offset); set_fs(oldfs); return ret; } </code></pre> <p>Syncing changes a file (similar to fsync):</p> <pre><code>int file_sync(struct file *file) { vfs_fsync(file, 0); return 0; } </code></pre> <p>[Edit] Originally, I proposed using file_fsync, which is gone in newer kernel versions. Thanks to the poor guy suggesting the change, but whose change was rejected. The edit was rejected before I could review it.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload