Welcome to The Fiwix Project
A UNIX-like kernel for the i386 architecture
1 /* 2 * fiwix/kernel/syscalls/truncate.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/stat.h> 11 #include <fiwix/errno.h> 12 #include <fiwix/string.h> 13 14 #ifdef __DEBUG__ 15 #include <fiwix/stdio.h> 16 #include <fiwix/process.h> 17 #endif /*__DEBUG__ */ 18 19 int sys_truncate(const char *path, __off_t length) 20 { 21 struct inode *i; 22 char *tmp_name; 23 int errno; 24 25 #ifdef __DEBUG__ 26 printk("(pid %d) sys_truncate(%s, %d)\n", current->pid, path, length); 27 #endif /*__DEBUG__ */ 28 29 if((errno = malloc_name(path, &tmp_name)) < 0) { 30 return errno; 31 } 32 if((errno = namei(tmp_name, &i, NULL, FOLLOW_LINKS))) { 33 free_name(tmp_name); 34 return errno; 35 } 36 if(S_ISDIR(i->i_mode)) { 37 iput(i); 38 free_name(tmp_name); 39 return -EISDIR; 40 } 41 if(IS_RDONLY_FS(i)) { 42 iput(i); 43 free_name(tmp_name); 44 return -EROFS; 45 } 46 if(check_permission(TO_WRITE, i) < 0) { 47 iput(i); 48 free_name(tmp_name); 49 return -EACCES; 50 } 51 if(length == i->i_size) { 52 iput(i); 53 free_name(tmp_name); 54 return 0; 55 } 56 57 errno = 0; 58 if(i->fsop && i->fsop->truncate) { 59 inode_lock(i); 60 errno = i->fsop->truncate(i, length); 61 inode_unlock(i); 62 } 63 iput(i); 64 free_name(tmp_name); 65 return errno; 66 }