xref: /haiku/src/system/libroot/posix/sys/stat.c (revision 9760dcae2038d47442f4658c2575844c6cf92c40)
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  */
6 
7 
8 #include <errno.h>
9 #include <sys/stat.h>
10 
11 #include <compat/sys/stat.h>
12 
13 #include <syscalls.h>
14 #include <symbol_versioning.h>
15 #include <syscall_utils.h>
16 
17 
18 // prototypes for the compiler
19 int _stat_current(const char* path, struct stat* stat);
20 int _fstat_current(int fd, struct stat* stat);
21 int _lstat_current(const char* path, struct stat* stat);
22 int _stat_beos(const char* path, struct stat_beos* beosStat);
23 int _fstat_beos(int fd, struct stat_beos* beosStat);
24 int _lstat_beos(const char* path, struct stat_beos* beosStat);
25 
26 
27 int
28 _stat_current(const char* path, struct stat* stat)
29 {
30 	int status = _kern_read_stat(-1, path, true, stat, sizeof(struct stat));
31 
32 	RETURN_AND_SET_ERRNO(status);
33 }
34 
35 
36 int
37 _fstat_current(int fd, struct stat* stat)
38 {
39 	int status = _kern_read_stat(fd, NULL, false, stat, sizeof(struct stat));
40 
41 	RETURN_AND_SET_ERRNO(status);
42 }
43 
44 
45 int
46 _lstat_current(const char* path, struct stat* stat)
47 {
48 	int status = _kern_read_stat(-1, path, false, stat, sizeof(struct stat));
49 
50 	RETURN_AND_SET_ERRNO(status);
51 }
52 
53 
54 int
55 fstatat(int fd, const char *path, struct stat *st, int flag)
56 {
57 	int status = _kern_read_stat(fd, path, (flag & AT_SYMLINK_NOFOLLOW) != 0,
58 		st, sizeof(struct stat));
59 
60 	RETURN_AND_SET_ERRNO(status);
61 }
62 
63 
64 // #pragma mark - BeOS compatibility
65 
66 
67 void
68 convert_to_stat_beos(const struct stat* stat, struct stat_beos* beosStat)
69 {
70 	if (stat == NULL || beosStat == NULL)
71 		return;
72 
73 	beosStat->st_dev = stat->st_dev;
74 	beosStat->st_ino = stat->st_ino;
75 	beosStat->st_mode = stat->st_mode;
76 	beosStat->st_nlink = stat->st_nlink;
77 	beosStat->st_uid = stat->st_uid;
78 	beosStat->st_gid = stat->st_gid;
79 	beosStat->st_size = stat->st_size;
80 	beosStat->st_rdev = stat->st_rdev;
81 	beosStat->st_blksize = stat->st_blksize;
82 	beosStat->st_atime = stat->st_atime;
83 	beosStat->st_mtime = stat->st_mtime;
84 	beosStat->st_ctime = stat->st_ctime;
85 	beosStat->st_crtime = stat->st_crtime;
86 }
87 
88 
89 void
90 convert_from_stat_beos(const struct stat_beos* beosStat, struct stat* stat)
91 {
92 	if (stat == NULL || beosStat == NULL)
93 		return;
94 
95 	stat->st_dev = beosStat->st_dev;
96 	stat->st_ino = beosStat->st_ino;
97 	stat->st_mode = beosStat->st_mode;
98 	stat->st_nlink = beosStat->st_nlink;
99 	stat->st_uid = beosStat->st_uid;
100 	stat->st_gid = beosStat->st_gid;
101 	stat->st_size = beosStat->st_size;
102 	stat->st_rdev = beosStat->st_rdev;
103 	stat->st_blksize = beosStat->st_blksize;
104 	stat->st_atim.tv_sec = beosStat->st_atime;
105 	stat->st_atim.tv_nsec = 0;
106 	stat->st_mtim.tv_sec = beosStat->st_mtime;
107 	stat->st_mtim.tv_nsec = 0;
108 	stat->st_ctim.tv_sec = beosStat->st_ctime;
109 	stat->st_ctim.tv_nsec = 0;
110 	stat->st_crtim.tv_sec = beosStat->st_crtime;
111 	stat->st_crtim.tv_nsec = 0;
112 	stat->st_type = 0;
113 	stat->st_blocks = 0;
114 }
115 
116 
117 int
118 _stat_beos(const char* path, struct stat_beos* beosStat)
119 {
120 	struct stat stat;
121 	if (_stat_current(path, &stat) != 0)
122 		return -1;
123 
124 	convert_to_stat_beos(&stat, beosStat);
125 
126 	return 0;
127 }
128 
129 
130 int
131 _fstat_beos(int fd, struct stat_beos* beosStat)
132 {
133 	struct stat stat;
134 	if (_fstat_current(fd, &stat) != 0)
135 		return -1;
136 
137 	convert_to_stat_beos(&stat, beosStat);
138 
139 	return 0;
140 }
141 
142 
143 int
144 _lstat_beos(const char* path, struct stat_beos* beosStat)
145 {
146 	struct stat stat;
147 	if (_lstat_current(path, &stat) != 0)
148 		return -1;
149 
150 	convert_to_stat_beos(&stat, beosStat);
151 
152 	return 0;
153 }
154 
155 
156 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_stat_beos", "stat@", "BASE");
157 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_fstat_beos", "fstat@", "BASE");
158 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_lstat_beos", "lstat@", "BASE");
159 
160 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_stat_current", "stat@@", "1_ALPHA1");
161 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_fstat_current", "fstat@@", "1_ALPHA1");
162 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_lstat_current", "lstat@@", "1_ALPHA1");
163