-
Notifications
You must be signed in to change notification settings - Fork 46
/
stat.c
62 lines (54 loc) · 1.81 KB
/
stat.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//
// Created by bruce on 27/04/20.
//
#include <fs/fs.h>
void set_statbuf(struct inode* inode, struct stat* statbuf){
statbuf->st_dev = inode->i_dev->dev_id;
statbuf->st_ino = inode->i_num;
statbuf->st_mode = inode->i_mode;
statbuf->st_nlink = inode->i_nlinks;
statbuf->st_uid = inode->i_uid;
statbuf->st_gid = inode->i_gid;
statbuf->st_rdev = inode->i_dev->dev_id;
statbuf->st_size = inode->i_size;
DEARCH_CHAR_SIZE(statbuf->st_size, inode->i_sb); //convert words to bytes
statbuf->st_blksize = BLOCK_SIZE;
statbuf->st_blocks = get_inode_blocks(inode);
statbuf->st_atime = inode->i_atime;
statbuf->st_mtime = inode->i_mtime;
statbuf->st_ctime = inode->i_ctime;
}
int sys_fstat(struct proc* who, int fd, struct stat* statbuf){
struct filp* file;
if(!is_fd_opened_and_valid(who, fd))
return -EBADF;
file = who->fp_filp[fd];
set_statbuf(file->filp_ino, statbuf);
return 0;
}
int sys_stat(struct proc* who, const char* pathname, struct stat *statbuf){
int ret;
inode_t *inode = NULL;
if((ret = get_inode_by_path(who, pathname, &inode)))
return ret;
if(!inode)
return -ENOENT;
set_statbuf(inode, statbuf);
put_inode(inode, false);
return 0;
}
int do_stat(struct proc* who, struct message* msg){
if(!is_vaddr_accessible(msg->m1_p1, who)
|| !is_vaddr_ok(msg->m1_p2, sizeof(struct stat), who)){
return -EFAULT;
}
return sys_stat(who, (char*)get_physical_addr(msg->m1_p1, who),
(struct stat*)get_physical_addr(msg->m1_p2, who));
}
int do_fstat(struct proc* who, struct message* msg){
if(!is_vaddr_ok(msg->m1_p1, sizeof(struct stat), who) ){
return -EFAULT;
}
return sys_fstat(who, msg->m1_i1,
(struct stat*)get_physical_addr(msg->m1_p1, who));
}