xref: /haiku/src/system/libroot/posix/sys/stat.c (revision 21258e2674226d6aa732321b6f8494841895af5f)
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 <errno_private.h>
14 #include <syscalls.h>
15 #include <symbol_versioning.h>
16 #include <syscall_utils.h>
17 
18 
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);
26 
27 
28 int
29 _stat_current(const char* path, struct stat* stat)
30 {
31 	int status = _kern_read_stat(-1, path, true, stat, sizeof(struct stat));
32 
33 	RETURN_AND_SET_ERRNO(status);
34 }
35 
36 
37 int
38 _fstat_current(int fd, struct stat* stat)
39 {
40 	int status = _kern_read_stat(fd, NULL, false, stat, sizeof(struct stat));
41 
42 	RETURN_AND_SET_ERRNO(status);
43 }
44 
45 
46 int
47 _lstat_current(const char* path, struct stat* stat)
48 {
49 	int status = _kern_read_stat(-1, path, false, stat, sizeof(struct stat));
50 
51 	RETURN_AND_SET_ERRNO(status);
52 }
53 
54 
55 int
56 fstatat(int fd, const char *path, struct stat *st, int flag)
57 {
58 	int status = _kern_read_stat(fd, path, (flag & AT_SYMLINK_NOFOLLOW) == 0,
59 		st, sizeof(struct stat));
60 
61 	RETURN_AND_SET_ERRNO(status);
62 }
63 
64 
65 // #pragma mark - BeOS compatibility
66 
67 
68 void
69 convert_to_stat_beos(const struct stat* stat, struct stat_beos* beosStat)
70 {
71 	if (stat == NULL || beosStat == NULL)
72 		return;
73 
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;
87 }
88 
89 
90 void
91 convert_from_stat_beos(const struct stat_beos* beosStat, struct stat* stat)
92 {
93 	if (stat == NULL || beosStat == NULL)
94 		return;
95 
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;
115 }
116 
117 
118 int
119 _stat_beos(const char* path, struct stat_beos* beosStat)
120 {
121 	struct stat stat;
122 	if (_stat_current(path, &stat) != 0)
123 		return -1;
124 
125 	convert_to_stat_beos(&stat, beosStat);
126 
127 	return 0;
128 }
129 
130 
131 int
132 _fstat_beos(int fd, struct stat_beos* beosStat)
133 {
134 	struct stat stat;
135 	if (_fstat_current(fd, &stat) != 0)
136 		return -1;
137 
138 	convert_to_stat_beos(&stat, beosStat);
139 
140 	return 0;
141 }
142 
143 
144 int
145 _lstat_beos(const char* path, struct stat_beos* beosStat)
146 {
147 	struct stat stat;
148 	if (_lstat_current(path, &stat) != 0)
149 		return -1;
150 
151 	convert_to_stat_beos(&stat, beosStat);
152 
153 	return 0;
154 }
155 
156 
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");
160 
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");
164