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