xref: /haiku/src/system/libroot/posix/sys/stat.c (revision bcfe344c537cb0dcee183c5a9afb4c362f5abe86)
1 /*
2  * Copyright 2009, Ingo Weinhold, ingo_weinhold@gmx.de. All rights reserved.
3  * Copyright 2002-2008, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
4  * Distributed under the terms of the MIT License.
5  */
6 
7 #include <syscalls.h>
8 
9 #include <sys/stat.h>
10 #include <errno.h>
11 
12 #include <compat/sys/stat.h>
13 
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 // #pragma mark - BeOS compatibility
55 
56 
57 void
58 convert_to_stat_beos(const struct stat* stat, struct stat_beos* beosStat)
59 {
60 	if (stat == NULL || beosStat == NULL)
61 		return;
62 
63 	beosStat->st_dev = stat->st_dev;
64 	beosStat->st_ino = stat->st_ino;
65 	beosStat->st_mode = stat->st_mode;
66 	beosStat->st_nlink = stat->st_nlink;
67 	beosStat->st_uid = stat->st_uid;
68 	beosStat->st_gid = stat->st_gid;
69 	beosStat->st_size = stat->st_size;
70 	beosStat->st_rdev = stat->st_rdev;
71 	beosStat->st_blksize = stat->st_blksize;
72 	beosStat->st_atime = stat->st_atime;
73 	beosStat->st_mtime = stat->st_mtime;
74 	beosStat->st_ctime = stat->st_ctime;
75 	beosStat->st_crtime = stat->st_crtime;
76 }
77 
78 
79 void
80 convert_from_stat_beos(const struct stat_beos* beosStat, struct stat* stat)
81 {
82 	if (stat == NULL || beosStat == NULL)
83 		return;
84 
85 	stat->st_dev = beosStat->st_dev;
86 	stat->st_ino = beosStat->st_ino;
87 	stat->st_mode = beosStat->st_mode;
88 	stat->st_nlink = beosStat->st_nlink;
89 	stat->st_uid = beosStat->st_uid;
90 	stat->st_gid = beosStat->st_gid;
91 	stat->st_size = beosStat->st_size;
92 	stat->st_rdev = beosStat->st_rdev;
93 	stat->st_blksize = beosStat->st_blksize;
94 	stat->st_atim.tv_sec = beosStat->st_atime;
95 	stat->st_atim.tv_nsec = 0;
96 	stat->st_mtim.tv_sec = beosStat->st_mtime;
97 	stat->st_mtim.tv_nsec = 0;
98 	stat->st_ctim.tv_sec = beosStat->st_ctime;
99 	stat->st_ctim.tv_nsec = 0;
100 	stat->st_crtim.tv_sec = beosStat->st_crtime;
101 	stat->st_crtim.tv_nsec = 0;
102 	stat->st_type = 0;
103 	stat->st_blocks = 0;
104 }
105 
106 
107 int
108 _stat_beos(const char* path, struct stat_beos* beosStat)
109 {
110 	struct stat stat;
111 	if (_stat_current(path, &stat) != 0)
112 		return -1;
113 
114 	convert_to_stat_beos(&stat, beosStat);
115 
116 	return 0;
117 }
118 
119 
120 int
121 _fstat_beos(int fd, struct stat_beos* beosStat)
122 {
123 	struct stat stat;
124 	if (_fstat_current(fd, &stat) != 0)
125 		return -1;
126 
127 	convert_to_stat_beos(&stat, beosStat);
128 
129 	return 0;
130 }
131 
132 
133 int
134 _lstat_beos(const char* path, struct stat_beos* beosStat)
135 {
136 	struct stat stat;
137 	if (_lstat_current(path, &stat) != 0)
138 		return -1;
139 
140 	convert_to_stat_beos(&stat, beosStat);
141 
142 	return 0;
143 }
144 
145 
146 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_stat_beos", "stat@", "BASE");
147 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_fstat_beos", "fstat@", "BASE");
148 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_lstat_beos", "lstat@", "BASE");
149 
150 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_stat_current", "stat@@", "1_ALPHA1");
151 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_fstat_current", "fstat@@", "1_ALPHA1");
152 DEFINE_LIBROOT_KERNEL_SYMBOL_VERSION("_lstat_current", "lstat@@", "1_ALPHA1");
153