Fork me on GitHub

root/kernel/syscalls/ftruncate.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. sys_ftruncate

   1 /*
   2  * fiwix/kernel/syscalls/ftruncate.c
   3  *
   4  * Copyright 2018, Jordi Sanfeliu. All rights reserved.
   5  * Distributed under the terms of the Fiwix License.
   6  */
   7 
   8 #include <fiwix/types.h>
   9 #include <fiwix/fs.h>
  10 #include <fiwix/fcntl.h>
  11 #include <fiwix/stat.h>
  12 #include <fiwix/errno.h>
  13 
  14 #ifdef __DEBUG__
  15 #include <fiwix/stdio.h>
  16 #include <fiwix/process.h>
  17 #endif /*__DEBUG__ */
  18 
  19 int sys_ftruncate(int ufd, __off_t length)
  20 {
  21         struct inode *i;
  22         int errno;
  23 
  24 #ifdef __DEBUG__
  25         printk("(pid %d) sys_ftruncate(%d, %d)\n", current->pid, ufd, length);
  26 #endif /*__DEBUG__ */
  27 
  28         CHECK_UFD(ufd);
  29         i = fd_table[current->fd[ufd]].inode;
  30         if((fd_table[current->fd[ufd]].flags & O_ACCMODE) == O_RDONLY) {
  31                 return -EINVAL;
  32         }
  33         if(S_ISDIR(i->i_mode)) {
  34                 return -EISDIR;
  35         }
  36         if(IS_RDONLY_FS(i)) {
  37                 return -EROFS;
  38         }
  39         if(check_permission(TO_WRITE, i) < 0) {
  40                 return -EPERM;
  41         }
  42         if(length == i->i_size) {
  43                 return 0;
  44         }
  45 
  46         errno = 0;
  47         if(i->fsop && i->fsop->truncate) {
  48                 inode_lock(i);
  49                 errno = i->fsop->truncate(i, length);
  50                 inode_unlock(i);
  51         }
  52         return errno;
  53 }

/* [previous][next][first][last][top][bottom][index][help] */