vfs: check userland buffers before reading them.
[haiku.git] / src / system / libroot / posix / sys / stat.c
blob2fc29662d49c0c7fbf6820e621e48c127de89b17
1 /*
2 * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3 * Copyright 2002-2009, Axel Dörfler, axeld@pinc-software.de.
4 * Distributed under the terms of the MIT License.
5 */
8 #include <errno.h>
9 #include <sys/stat.h>
11 #include <compat/sys/stat.h>
13 #include <errno_private.h>
14 #include <syscalls.h>
15 #include <symbol_versioning.h>
16 #include <syscall_utils.h>
19 // prototypes for the compiler
20 int _stat_current(const char* path, struct stat* stat);
21 int _fstat_current(int fd, struct stat* stat);
22 int _lstat_current(const char* path, struct stat* stat);
23 int _stat_beos(const char* path, struct stat_beos* beosStat);
24 int _fstat_beos(int fd, struct stat_beos* beosStat);
25 int _lstat_beos(const char* path, struct stat_beos* beosStat);
28 int
29 _stat_current(const char* path, struct stat* stat)
31 int status = _kern_read_stat(-1, path, true, stat, sizeof(struct stat));
33 RETURN_AND_SET_ERRNO(status);
37 int
38 _fstat_current(int fd, struct stat* stat)
40 int status = _kern_read_stat(fd, NULL, false, stat, sizeof(struct stat));
42 RETURN_AND_SET_ERRNO(status);
46 int
47 _lstat_current(const char* path, struct stat* stat)
49 int status = _kern_read_stat(-1, path, false, stat, sizeof(struct stat));
51 RETURN_AND_SET_ERRNO(status);
55 int
56 fstatat(int fd, const char *path, struct stat *st, int flag)
58 int status = _kern_read_stat(fd, path, (flag & AT_SYMLINK_NOFOLLOW) == 0,
59 st, sizeof(struct stat));
61 RETURN_AND_SET_ERRNO(status);
65 // #pragma mark - BeOS compatibility
68 void
69 convert_to_stat_beos(const struct stat* stat, struct stat_beos* beosStat)
71 if (stat == NULL || beosStat == NULL)
72 return;
74 beosStat->st_dev = stat->st_dev;
75 beosStat->st_ino = stat->st_ino;
76 beosStat->st_mode = stat->st_mode;
77 beosStat->st_nlink = stat->st_nlink;
78 beosStat->st_uid = stat->st_uid;
79 beosStat->st_gid = stat->st_gid;
80 beosStat->st_size = stat->st_size;
81 beosStat->st_rdev = stat->st_rdev;
82 beosStat->st_blksize = stat->st_blksize;
83 beosStat->st_atime = stat->st_atime;
84 beosStat->st_mtime = stat->st_mtime;
85 beosStat->st_ctime = stat->st_ctime;
86 beosStat->st_crtime = stat->st_crtime;
90 void
91 convert_from_stat_beos(const struct stat_beos* beosStat, struct stat* stat)
93 if (stat == NULL || beosStat == NULL)
94 return;
96 stat->st_dev = beosStat->st_dev;
97 stat->st_ino = beosStat->st_ino;
98 stat->st_mode = beosStat->st_mode;
99 stat->st_nlink = beosStat->st_nlink;
100 stat->st_uid = beosStat->st_uid;
101 stat->st_gid = beosStat->st_gid;
102 stat->st_size = beosStat->st_size;
103 stat->st_rdev = beosStat->st_rdev;
104 stat->st_blksize = beosStat->st_blksize;
105 stat->st_atim.tv_sec = beosStat->st_atime;
106 stat->st_atim.tv_nsec = 0;
107 stat->st_mtim.tv_sec = beosStat->st_mtime;
108 stat->st_mtim.tv_nsec = 0;
109 stat->st_ctim.tv_sec = beosStat->st_ctime;
110 stat->st_ctim.tv_nsec = 0;
111 stat->st_crtim.tv_sec = beosStat->st_crtime;
112 stat->st_crtim.tv_nsec = 0;
113 stat->st_type = 0;
114 stat->st_blocks = 0;
119 _stat_beos(const char* path, struct stat_beos* beosStat)
121 struct stat stat;
122 if (_stat_current(path, &stat) != 0)
123 return -1;
125 convert_to_stat_beos(&stat, beosStat);
127 return 0;
132 _fstat_beos(int fd, struct stat_beos* beosStat)
134 struct stat stat;
135 if (_fstat_current(fd, &stat) != 0)
136 return -1;
138 convert_to_stat_beos(&stat, beosStat);
140 return 0;
145 _lstat_beos(const char* path, struct stat_beos* beosStat)
147 struct stat stat;
148 if (_lstat_current(path, &stat) != 0)
149 return -1;
151 convert_to_stat_beos(&stat, beosStat);
153 return 0;
157 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_stat_beos", "stat@", "BASE");
158 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_fstat_beos", "fstat@", "BASE");
159 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_lstat_beos", "lstat@", "BASE");
161 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_stat_current", "stat@@", "1_ALPHA1");
162 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_fstat_current", "fstat@@", "1_ALPHA1");
163 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_lstat_current", "lstat@@", "1_ALPHA1");