UbixOS V2 2.0

stat.c
Go to the documentation of this file.
1 /*-
2 * Copyright (c) 2002-2018 The UbixOS Project.
3 * All rights reserved.
4 *
5 * This was developed by Christopher W. Olsen for the UbixOS Project.
6 *
7 * Redistribution and use in source and binary forms, with or without modification, are permitted
8 * provided that the following conditions are met:
9 *
10 * 1) Redistributions of source code must retain the above copyright notice, this list of
11 * conditions, the following disclaimer and the list of authors.
12 * 2) Redistributions in binary form must reproduce the above copyright notice, this list of
13 * conditions, the following disclaimer and the list of authors in the documentation and/or
14 * other materials provided with the distribution.
15 * 3) Neither the name of the UbixOS Project nor the names of its contributors may be used to
16 * endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
24 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
26 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <ubixos/errno.h>
30 #include <sys/sysproto.h>
31 #include <vfs/stat.h>
32 #include <vfs/file.h>
33 #include <lib/kprintf.h>
34 #include <sys/descrip.h>
35
36 int _sys_stat(char *path, struct stat *sb, int flags) {
37 int error = 0;
38 //struct inode *inode;
39
40 //MrOlsen kprintf("SYS_STAT {%s}", path);
41
42 /*
43 switch (flags) {
44 case STAT_LSTAT:
45 error = namei(path, NULL, STAT_NO_FOLLOW, &inode);
46 sb->st_dev = inode->i_dev;
47 sb->st_ino = inode->i_ino;
48 sb->st_mode = inode->i_mode;
49 sb->st_nlink = inode->i_nlink;
50 sb->st_uid = inode->i_uid;
51 sb->st_gid = inode->i_gid;
52 sb->st_rdev = inode->i_rdev;
53 sb->st_size = inode->i_size;
54 sb->st_atime = inode->i_atime;
55 sb->st_mtime = inode->i_mtime;
56 sb->st_ctime = inode->i_ctime;
57 //MrOlsen kprintf("LSTAT(%i)=st_ino 0x%X, st_mode=0x%X, st_uid %i, st_gid %i, st_size=0x%X", error, sb->st_ino, sb->st_mode, sb->st_uid, sb->st_gid, sb->st_size);
58 break;
59 default:
60 kprintf("STAT ERROR={%s}", path);
61 error = -1;
62 break;
63 }
64 */
65
66 fileDescriptor_t *fd = fopen(path, "rb");
67 //MrOlsen kprintf("FD=0x%X", fd);
68
69 if (fd == 0) {
70 error = -1;
71 }
72 else {
73 sb->st_dev = 0xDEADBEEF;
74 sb->st_ino = fd->ino;
75 sb->st_mode = fd->inode.u.ufs2_i.di_mode;
76 sb->st_nlink = fd->inode.u.ufs2_i.di_nlink;
77 sb->st_uid = fd->inode.u.ufs2_i.di_uid;
78 sb->st_gid = fd->inode.u.ufs2_i.di_gid;
79 sb->st_rdev = 0xBEEFDEAD;
80 sb->st_size = fd->inode.u.ufs2_i.di_size;
81 sb->st_atime = fd->inode.u.ufs2_i.di_atime;
82 sb->st_mtime = fd->inode.u.ufs2_i.di_mtime;
83 sb->st_ctime = fd->inode.u.ufs2_i.di_ctime;
84 //MrOlsen kprintf("LSTAT(%i)=st_ino 0x%X, st_mode=0x%X, st_uid %i, st_gid %i, st_size=0x%X", error, sb->st_ino, sb->st_mode, sb->st_uid, sb->st_gid, sb->st_size);
85 fclose(fd);
86 }
87
88 return (error);
89 }
90
91 int sys_fstat(struct thread *td, struct sys_fstat_args *args) {
92 int error = 0;
93
94 struct file *fdd = 0x0;
95 fileDescriptor_t *fd = 0x0;
96
97 getfd(td, &fdd, args->fd);
98
99 fd = fdd->fd;
100
101 if (fdd == 0 || fdd->fd == 0x0) {
102 error = -1;
103 }
104 else if (fd->res != 0x0) {
105 args->sb->st_dev = 0xDEADBEEF;
106 args->sb->st_ino = fd->ino;
107 args->sb->st_rdev = 0xBEEFDEAD;
108 args->sb->st_size = fd->size;
109 args->sb->st_uid = 0;
110 args->sb->st_gid = 0;
111
112 //kprintf("FSTAT DOS");
113 }
114 else {
115 args->sb->st_dev = 0xDEADBEEF;
116 args->sb->st_ino = fd->ino;
117 args->sb->st_mode = fd->inode.u.ufs2_i.di_mode;
118 args->sb->st_nlink = fd->inode.u.ufs2_i.di_nlink;
119 args->sb->st_uid = fd->inode.u.ufs2_i.di_uid;
120 args->sb->st_gid = fd->inode.u.ufs2_i.di_gid;
121 args->sb->st_rdev = 0xBEEFDEAD;
122 args->sb->st_size = fd->inode.u.ufs2_i.di_size;
123 args->sb->st_atime = fd->inode.u.ufs2_i.di_atime;
124 args->sb->st_mtime = fd->inode.u.ufs2_i.di_mtime;
125 args->sb->st_ctime = fd->inode.u.ufs2_i.di_ctime;
126 //kprintf("FSTAT(%i)=st_ino 0x%X, st_mode=0x%X, st_uid %i, st_gid %i, st_size=0x%X:0x%X", args->fd, args->sb->st_ino, args->sb->st_mode, args->sb->st_uid, args->sb->st_gid, args->sb->st_size, fd->size);
127 }
128
129 td->td_retval[0] = error;
130 return (error);
131 }
132
133 int sys_fstatat(struct thread *td, struct sys_fstatat_args *args) {
134 int error = 0;
135
136 struct file *fdd = 0x0;
137 fileDescriptor_t *fd = 0x0;
138 struct stat *sb = 0x0;
139 int uP = 0x0;
140
141 //kprintf("FSTATAT(%i:%s:%s)", args->fd, args->path, args->flag);
142
143 if (strlen(args->path) > 0) {
144 fd = fopen(args->path, "rb");
145 uP = 1;
146 }
147 else {
148 getfd(td, &fdd, args->fd);
149 if (fdd == 0 || fdd->fd == 0x0) {
150 error = -1;
151 }
152 else {
153 fd = fdd->fd;
154 }
155 }
156
157 if (fd == 0x0) {
158 error = -1;
159 }
160 else {
161 sb = args->buf;
162 sb->st_dev = 0xDEADBEEF;
163 sb->st_ino = fd->ino;
164 sb->st_mode = fd->inode.u.ufs2_i.di_mode;
165 sb->st_nlink = fd->inode.u.ufs2_i.di_nlink;
166 sb->st_uid = fd->inode.u.ufs2_i.di_uid;
167 sb->st_gid = fd->inode.u.ufs2_i.di_gid;
168 sb->st_rdev = 0xBEEFDEAD;
169 sb->st_size = fd->inode.u.ufs2_i.di_size;
170 sb->st_atime = fd->inode.u.ufs2_i.di_atime;
171 sb->st_mtime = fd->inode.u.ufs2_i.di_mtime;
172 sb->st_ctime = fd->inode.u.ufs2_i.di_ctime;
173 //kprintf("FSTAT(%i:%s:%i)=st_ino 0x%X, st_mode=0x%X, st_uid %i, st_gid %i, st_size=0x%X:0x%X", args->fd, args->path, args->flag, sb->st_ino, sb->st_mode, sb->st_uid, sb->st_gid, sb->st_size, fd->size);
174 if (uP == 1)
175 fclose(fd);
176 }
177
178 td->td_retval[0] = error;
179 return (error);
180 }
181
182 int sys_fstatfs(struct thread *td, struct sys_fstatfs_args *args) {
183 int error = 0;
184
185 struct file *fd = 0x0;
186
187 getfd(td, &fd, args->fd);
188
189 if (fd == 0) {
190 error = -1;
191 }
192 else {
193 /*
194 args->buf->st_dev = 0xDEADBEEF;
195 args->buf->st_ino = fd->fd->ino;
196 args->buf->st_mode = fd->fd->inode.u.ufs2_i.di_mode;
197 args->buf->st_nlink = fd->fd->inode.u.ufs2_i.di_nlink;
198 args->buf->st_uid = fd->fd->inode.u.ufs2_i.di_uid;
199 args->buf->st_gid = fd->fd->inode.u.ufs2_i.di_gid;
200 args->buf->st_rdev = 0xBEEFDEAD;
201 args->buf->st_size = fd->fd->inode.u.ufs2_i.di_size;
202 args->buf->st_atime = fd->fd->inode.u.ufs2_i.di_atime;
203 args->buf->st_mtime = fd->fd->inode.u.ufs2_i.di_mtime;
204 args->buf->st_ctime = fd->fd->inode.u.ufs2_i.di_ctime;
205 //MrOlsen kprintf("LSTAT(%i)=st_ino 0x%X, st_mode=0x%X, st_uid %i, st_gid %i, st_size=0x%X", error, args->buf->st_ino, args->buf->st_mode, args->buf->st_uid, args->buf->st_gid, args->buf->st_size);
206 //fclose(fd);
207 */
208
209 args->buf->f_version = 0x20030518;
210 args->buf->f_type = 0x35;
211 args->buf->f_flags = 0x1000;
212 args->buf->f_bsize = 0x1000;
213 args->buf->f_iosize = 0x8000;
214 args->buf->f_blocks = 0x7bf9d;
215 args->buf->f_bfree = 0x73c0c;
216 args->buf->f_bavail = 0x69d5c;
217 args->buf->f_files = 0x3fffe;
218 args->buf->f_ffree = 0x3d3f0;
219 args->buf->f_syncwrites = 0x75232;
220 args->buf->f_asyncwrites = 0x2b804;
221 args->buf->f_syncreads = 0x2edea;
222 args->buf->f_asyncreads = 0x6182;
223 args->buf->f_namemax = 0xFF;
224 args->buf->f_owner = 0x0;
225 args->buf->f_fsid.val[0] = 0x5A31F4F0;
226 args->buf->f_fsid.val[1] = 0x65E7C98F;
227 //args->buf->f_charspare= {""};
228 sprintf(args->buf->f_fstypename, "ufs");
229 sprintf(args->buf->f_mntfromname, "/dev/ada0s1a");
230 sprintf(args->buf->f_mntonname, "/");
231
232 }
233
234 td->td_retval[0] = error;
235 return (error);
236
237 }
238
239 int sys_statfs(struct thread *td, struct sys_statfs_args *args) {
240 int error = 0;
241 fileDescriptor_t *fd = 0x0;
242
243 fd = fopen(args->path, "rb");
244
245 if (fd == 0) {
246 error = -1;
247 }
248 else {
249 /*
250 args->buf->st_dev = 0xDEADBEEF;
251 args->buf->st_ino = fd->fd->ino;
252 args->buf->st_mode = fd->fd->inode.u.ufs2_i.di_mode;
253 args->buf->st_nlink = fd->fd->inode.u.ufs2_i.di_nlink;
254 args->buf->st_uid = fd->fd->inode.u.ufs2_i.di_uid;
255 args->buf->st_gid = fd->fd->inode.u.ufs2_i.di_gid;
256 args->buf->st_rdev = 0xBEEFDEAD;
257 args->buf->st_size = fd->fd->inode.u.ufs2_i.di_size;
258 args->buf->st_atime = fd->fd->inode.u.ufs2_i.di_atime;
259 args->buf->st_mtime = fd->fd->inode.u.ufs2_i.di_mtime;
260 args->buf->st_ctime = fd->fd->inode.u.ufs2_i.di_ctime;
261 //MrOlsen kprintf("LSTAT(%i)=st_ino 0x%X, st_mode=0x%X, st_uid %i, st_gid %i, st_size=0x%X", error, args->buf->st_ino, args->buf->st_mode, args->buf->st_uid, args->buf->st_gid, args->buf->st_size);
262 //fclose(fd);
263 */
264
265 //kprintf("sFS: %s", args->path);
266 args->buf->f_version = 0x20030518;
267 args->buf->f_type = 0x35;
268 args->buf->f_flags = 0x1000;
269 args->buf->f_bsize = 0x1000;
270 args->buf->f_iosize = 0x8000;
271 args->buf->f_blocks = 0x7bf9d;
272 args->buf->f_bfree = 0x73c0c;
273 args->buf->f_bavail = 0x69d5c;
274 args->buf->f_files = 0x3fffe;
275 args->buf->f_ffree = 0x3d3f0;
276 args->buf->f_syncwrites = 0x75232;
277 args->buf->f_asyncwrites = 0x2b804;
278 args->buf->f_syncreads = 0x2edea;
279 args->buf->f_asyncreads = 0x6182;
280 args->buf->f_namemax = 0xFF;
281 args->buf->f_owner = 0x0;
282 args->buf->f_fsid.val[0] = 0x5A31F4F0;
283 args->buf->f_fsid.val[1] = 0x65E7C98F;
284 //args->buf->f_charspare= {""};
285 sprintf(args->buf->f_fstypename, "ufs");
286 sprintf(args->buf->f_mntfromname, "/dev/ada0s1a");
287 sprintf(args->buf->f_mntonname, "/");
288
289 }
290
291 td->td_retval[0] = error;
292 return (error);
293
294 }
295
296 /* Return stat of path do not follow if link return stat of link */
297 int sys_lstat(struct thread *td, struct sys_lstat_args *args) {
298 td->td_retval[0] = _sys_stat(args->path, args->sb, STAT_LSTAT);
299 return (0x0);
300 }
301
302 int sys_stat(struct thread *td, struct sys_stat_args *args) {
303 td->td_retval[0] = _sys_stat(args->path, args->ub, STAT_LSTAT);
304 return (0x0);
305 }
sys_fstatfs_args::buf
struct statfs * buf
Definition: sysproto_posix.h:292
stat::st_size
off_t st_size
Definition: stat.h:58
statfs::f_asyncreads
u_int64_t f_asyncreads
Definition: mount.h:55
stat
Definition: stat.h:44
sys_fstat_args
Definition: sysproto_posix.h:467
stat::st_mtime
time_t st_mtime
Definition: stat.h:54
fsid::val
int32_t val[2]
Definition: mount.h:38
ufs2_dinode::di_size
u_int64_t di_size
Definition: ufs.h:110
statfs::f_blocks
u_int64_t f_blocks
Definition: mount.h:47
sysproto.h
sys_statfs_args::path
char * path
Definition: sysproto_posix.h:598
statfs::f_namemax
uint32_t f_namemax
Definition: mount.h:57
statfs::f_fstypename
char f_fstypename[16]
Definition: mount.h:61
file.h
fopen
fileDescriptor_t * fopen(const char *file, const char *flags)
Definition: file.c:388
fileDescriptor::res
void * res
Definition: file.h:82
stat::st_uid
uid_t st_uid
Definition: stat.h:49
ufs2_dinode::di_mtime
ufs_time_t di_mtime
Definition: ufs.h:113
sys_fstatat
int sys_fstatat(struct thread *td, struct sys_fstatat_args *args)
Definition: stat.c:133
statfs::f_mntfromname
char f_mntfromname[88]
Definition: mount.h:62
fileDescriptor
Definition: file.h:62
stat::st_nlink
__nlink_t st_nlink
Definition: stat.h:48
file
Definition: descrip.h:67
ufs2_dinode::di_atime
ufs_time_t di_atime
Definition: ufs.h:112
statfs::f_bavail
int64_t f_bavail
Definition: mount.h:49
thread
Definition: thread.h:40
stat::st_mode
__mode_t st_mode
Definition: stat.h:47
sys_statfs
int sys_statfs(struct thread *td, struct sys_statfs_args *args)
Definition: stat.c:239
sys_fstatfs_args::fd
int fd
Definition: sysproto_posix.h:289
statfs::f_bsize
u_int64_t f_bsize
Definition: mount.h:45
stat::st_ctime
time_t st_ctime
Definition: stat.h:56
statfs::f_syncreads
u_int64_t f_syncreads
Definition: mount.h:54
strlen
int strlen(const char *str)
Definition: strlen.c:55
sys_fstatat_args
Definition: sysproto_posix.h:605
sys_lstat
int sys_lstat(struct thread *td, struct sys_lstat_args *args)
Definition: stat.c:297
statfs::f_bfree
u_int64_t f_bfree
Definition: mount.h:48
ufs2_dinode::di_ctime
ufs_time_t di_ctime
Definition: ufs.h:114
sprintf
int sprintf(char *buf, const char *fmt,...)
Definition: kprintf.c:278
statfs::f_flags
u_int64_t f_flags
Definition: mount.h:44
sys_fstat
int sys_fstat(struct thread *td, struct sys_fstat_args *args)
Definition: stat.c:91
statfs::f_fsid
fsid_t f_fsid
Definition: mount.h:59
statfs::f_files
u_int64_t f_files
Definition: mount.h:50
statfs::f_owner
uid_t f_owner
Definition: mount.h:58
file::fd
fileDescriptor_t * fd
Definition: descrip.h:71
thread::td_retval
int td_retval[2]
Definition: thread.h:41
fclose
int fclose(fileDescriptor_t *fd)
Definition: file.c:526
kprintf.h
stat::st_rdev
__dev_t st_rdev
Definition: stat.h:51
statfs::f_iosize
u_int64_t f_iosize
Definition: mount.h:46
inode::u
union inode::@22 u
sys_stat
int sys_stat(struct thread *td, struct sys_stat_args *args)
Definition: stat.c:302
inode::ufs2_i
struct ufs2_dinode ufs2_i
Definition: inode.h:75
getfd
int getfd(struct thread *td, struct file **fp, int fd)
get pointer to file fd in specified thread
Definition: descrip.c:214
sys_statfs_args
Definition: sysproto_posix.h:596
sys_stat_args
Definition: sysproto_posix.h:399
stat::st_ino
ino_t st_ino
Definition: stat.h:46
fileDescriptor::ino
uint32_t ino
Definition: file.h:66
_sys_stat
int _sys_stat(char *path, struct stat *sb, int flags)
Definition: stat.c:36
STAT_LSTAT
#define STAT_LSTAT
Definition: stat.h:34
sys_fstat_args::sb
struct stat * sb
Definition: sysproto_posix.h:472
statfs::f_syncwrites
u_int64_t f_syncwrites
Definition: mount.h:52
sys_fstatfs
int sys_fstatfs(struct thread *td, struct sys_fstatfs_args *args)
Definition: stat.c:182
sys_lstat_args::sb
struct stat * sb
Definition: sysproto_posix.h:413
sys_fstat_args::fd
int fd
Definition: sysproto_posix.h:469
ufs2_dinode::di_nlink
int16_t di_nlink
Definition: ufs.h:106
fileDescriptor::inode
struct inode inode
Definition: file.h:80
stat::st_dev
__dev_t st_dev
Definition: stat.h:45
statfs::f_asyncwrites
u_int64_t f_asyncwrites
Definition: mount.h:53
ufs2_dinode::di_mode
u_int16_t di_mode
Definition: ufs.h:105
stat.h
statfs::f_version
uint32_t f_version
Definition: mount.h:42
sys_statfs_args::buf
struct statfs * buf
Definition: sysproto_posix.h:601
stat::st_atime
time_t st_atime
Definition: stat.h:52
statfs::f_mntonname
char f_mntonname[88]
Definition: mount.h:63
descrip.h
sys_fstatfs_args
Definition: sysproto_posix.h:287
sys_stat_args::ub
struct stat * ub
Definition: sysproto_posix.h:404
ufs2_dinode::di_gid
uint32_t di_gid
Definition: ufs.h:108
stat::st_gid
gid_t st_gid
Definition: stat.h:50
sys_fstatat_args::path
char * path
Definition: sysproto_posix.h:610
statfs::f_type
uint32_t f_type
Definition: mount.h:43
sys_lstat_args::path
char * path
Definition: sysproto_posix.h:410
sys_fstatat_args::fd
int fd
Definition: sysproto_posix.h:607
fileDescriptor::size
uint32_t size
Definition: file.h:70
errno.h
sys_stat_args::path
char * path
Definition: sysproto_posix.h:401
ufs2_dinode::di_uid
uint32_t di_uid
Definition: ufs.h:107
sys_fstatat_args::buf
struct stat * buf
Definition: sysproto_posix.h:613
sys_lstat_args
Definition: sysproto_posix.h:408
statfs::f_ffree
int64_t f_ffree
Definition: mount.h:51