| 1 | /* | 
|---|
| 2 |  * kern/sys_stat.c - stats a file or directory | 
|---|
| 3 |  *  | 
|---|
| 4 |  * Copyright (c) 2008,2009,2010,2011,2012 Ghassan Almaless | 
|---|
| 5 |  * Copyright (c) 2011,2012 UPMC Sorbonne Universites | 
|---|
| 6 |  * | 
|---|
| 7 |  * This file is part of ALMOS-kernel. | 
|---|
| 8 |  * | 
|---|
| 9 |  * ALMOS-kernel is free software; you can redistribute it and/or modify it | 
|---|
| 10 |  * under the terms of the GNU General Public License as published by | 
|---|
| 11 |  * the Free Software Foundation; version 2.0 of the License. | 
|---|
| 12 |  * | 
|---|
| 13 |  * ALMOS-kernel is distributed in the hope that it will be useful, but | 
|---|
| 14 |  * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 15 |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 16 |  * General Public License for more details. | 
|---|
| 17 |  * | 
|---|
| 18 |  * You should have received a copy of the GNU General Public License | 
|---|
| 19 |  * along with ALMOS-kernel; if not, write to the Free Software Foundation, | 
|---|
| 20 |  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | 
|---|
| 21 |  */ | 
|---|
| 22 |  | 
|---|
| 23 | #include <errno.h> | 
|---|
| 24 | #include <thread.h> | 
|---|
| 25 | #include <vfs.h> | 
|---|
| 26 | #include <sys-vfs.h> | 
|---|
| 27 | #include <task.h> | 
|---|
| 28 | #include <spinlock.h> | 
|---|
| 29 | #include <cpu-trace.h> | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 | int sys_stat(char *pathname, struct vfs_stat_s *buff, int fd) | 
|---|
| 33 | { | 
|---|
| 34 |         CPU_HW_TRACE(sys_stat); | 
|---|
| 35 |         struct thread_s *this; | 
|---|
| 36 |         register error_t err = 0; | 
|---|
| 37 |         struct vfs_file_s *file; | 
|---|
| 38 |         struct ku_obj ku_path; | 
|---|
| 39 |         struct task_s *task; | 
|---|
| 40 |  | 
|---|
| 41 |         file = NULL; | 
|---|
| 42 |         this = current_thread; | 
|---|
| 43 |         task = current_task; | 
|---|
| 44 |  | 
|---|
| 45 |         if((buff == NULL) || ((pathname == NULL) && (fd == -1))) | 
|---|
| 46 |         { | 
|---|
| 47 |                 this->info.errno = EINVAL; | 
|---|
| 48 |                 CPU_HW_TRACE(sys_stat); | 
|---|
| 49 |                 return -1; | 
|---|
| 50 |         } | 
|---|
| 51 |  | 
|---|
| 52 |         if(NOT_IN_USPACE((uint_t)buff)) | 
|---|
| 53 |         { | 
|---|
| 54 |                 this->info.errno = EPERM; | 
|---|
| 55 |                 CPU_HW_TRACE(sys_stat); | 
|---|
| 56 |                 return -1; | 
|---|
| 57 |         } | 
|---|
| 58 |  | 
|---|
| 59 |         if(pathname == NULL) | 
|---|
| 60 |         { | 
|---|
| 61 |                 if((fd >= CONFIG_TASK_FILE_MAX_NR) || (task_fd_lookup(task, fd, &file))) | 
|---|
| 62 |                 { | 
|---|
| 63 |                         CPU_HW_TRACE(sys_stat); | 
|---|
| 64 |                         return EBADFD; | 
|---|
| 65 |                 } | 
|---|
| 66 |   | 
|---|
| 67 |                 err = vfs_stat(&task->vfs_cwd, NULL, buff, file); | 
|---|
| 68 |         } | 
|---|
| 69 |         else | 
|---|
| 70 |         { | 
|---|
| 71 |                 KU_BUFF(ku_path, pathname); | 
|---|
| 72 |                 rwlock_rdlock(&task->cwd_lock); | 
|---|
| 73 |                 err = vfs_stat(&task->vfs_cwd, &ku_path, buff, NULL); | 
|---|
| 74 |                 rwlock_unlock(&task->cwd_lock); | 
|---|
| 75 |         } | 
|---|
| 76 |   | 
|---|
| 77 |         this->info.errno = err; | 
|---|
| 78 |         CPU_HW_TRACE(sys_stat); | 
|---|
| 79 |         return 0; | 
|---|
| 80 | } | 
|---|