[runtime] Switch to GC Unsafe for all GC external API functions
[mono-project.git] / support / sys-stat.c
blobaf2731cccbfd4400a3ad111c03bc26907f0ba9d4
1 /*
2 * <sys/stat.h> wrapper functions.
4 * Authors:
5 * Jonathan Pryor (jonpryor@vt.edu)
7 * Copyright (C) 2004-2006 Jonathan Pryor
8 */
10 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif /* ndef _GNU_SOURCE */
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif
19 #include <fcntl.h>
20 #include <errno.h>
22 #include "mph.h" /* Don't remove or move after map.h! Works around issues with Android SDK unified headers */
23 #include "map.h"
25 G_BEGIN_DECLS
27 int
28 Mono_Posix_FromStat (struct Mono_Posix_Stat *from, void *_to)
30 struct stat *to = _to;
31 memset (to, 0, sizeof(*to));
33 to->st_dev = from->st_dev;
34 to->st_ino = from->st_ino;
36 unsigned int to_st_mode;
37 if (Mono_Posix_FromFilePermissions (from->st_mode, &to_st_mode) != 0) {
38 return -1;
41 to->st_mode = to_st_mode;
42 to->st_nlink = from->st_nlink;
43 to->st_uid = from->st_uid;
44 to->st_gid = from->st_gid;
45 to->st_rdev = from->st_rdev;
46 to->st_size = from->st_size;
47 #ifndef HOST_WIN32
48 to->st_blksize = from->st_blksize;
49 to->st_blocks = from->st_blocks;
50 #endif
51 to->st_atime = from->st_atime_;
52 to->st_mtime = from->st_mtime_;
53 to->st_ctime = from->st_ctime_;
54 #if HAVE_STRUCT_STAT_ST_ATIMESPEC
55 to->st_atimespec.tv_sec = from->st_atime_;
56 to->st_atimespec.tv_nsec = from->st_atime_nsec;
57 to->st_mtimespec.tv_sec = from->st_mtime_;
58 to->st_mtimespec.tv_nsec = from->st_mtime_nsec;
59 to->st_ctimespec.tv_sec = from->st_ctime_;
60 to->st_ctimespec.tv_nsec = from->st_ctime_nsec;
61 #else
62 # ifdef HAVE_STRUCT_STAT_ST_ATIM
63 to->st_atim.tv_nsec = from->st_atime_nsec;
64 # endif
65 # ifdef HAVE_STRUCT_STAT_ST_MTIM
66 to->st_mtim.tv_nsec = from->st_mtime_nsec;
67 # endif
68 # ifdef HAVE_STRUCT_STAT_ST_CTIM
69 to->st_ctim.tv_nsec = from->st_ctime_nsec;
70 # endif
71 #endif
72 return 0;
75 int
76 Mono_Posix_ToStat (void *_from, struct Mono_Posix_Stat *to)
78 struct stat *from = _from;
79 memset (to, 0, sizeof(*to));
81 to->st_dev = from->st_dev;
82 to->st_ino = from->st_ino;
83 if (Mono_Posix_ToFilePermissions (from->st_mode, &to->st_mode) != 0) {
84 return -1;
86 to->st_nlink = from->st_nlink;
87 to->st_uid = from->st_uid;
88 to->st_gid = from->st_gid;
89 to->st_rdev = from->st_rdev;
90 to->st_size = from->st_size;
91 #ifndef HOST_WIN32
92 to->st_blksize = from->st_blksize;
93 to->st_blocks = from->st_blocks;
94 #endif
95 to->st_atime_ = from->st_atime;
96 to->st_mtime_ = from->st_mtime;
97 to->st_ctime_ = from->st_ctime;
98 #if HAVE_STRUCT_STAT_ST_ATIMESPEC
99 to->st_atime_nsec = from->st_atimespec.tv_nsec;
100 to->st_mtime_nsec = from->st_mtimespec.tv_nsec;
101 to->st_ctime_nsec = from->st_ctimespec.tv_nsec;
102 #else
103 # ifdef HAVE_STRUCT_STAT_ST_ATIM
104 to->st_atime_nsec = from->st_atim.tv_nsec;
105 # endif
106 # ifdef HAVE_STRUCT_STAT_ST_MTIM
107 to->st_mtime_nsec = from->st_mtim.tv_nsec;
108 # endif
109 # ifdef HAVE_STRUCT_STAT_ST_CTIM
110 to->st_ctime_nsec = from->st_ctim.tv_nsec;
111 # endif
112 #endif
113 return 0;
116 gint32
117 Mono_Posix_Syscall_stat (const char *file_name, struct Mono_Posix_Stat *buf)
119 int r;
120 struct stat _buf;
122 if (buf == NULL) {
123 errno = EFAULT;
124 return -1;
126 r = stat (file_name, &_buf);
127 if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
128 r = -1;
129 return r;
132 gint32
133 Mono_Posix_Syscall_fstat (int filedes, struct Mono_Posix_Stat *buf)
135 int r;
136 struct stat _buf;
138 if (buf == NULL) {
139 errno = EFAULT;
140 return -1;
142 r = fstat (filedes, &_buf);
143 if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
144 r = -1;
145 return r;
148 #ifndef HOST_WIN32
149 gint32
150 Mono_Posix_Syscall_lstat (const char *file_name, struct Mono_Posix_Stat *buf)
152 int r;
153 struct stat _buf;
155 if (buf == NULL) {
156 errno = EFAULT;
157 return -1;
159 r = lstat (file_name, &_buf);
160 if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
161 r = -1;
162 return r;
164 #endif
166 #ifdef HAVE_FSTATAT
167 gint32
168 Mono_Posix_Syscall_fstatat (gint32 dirfd, const char *file_name, struct Mono_Posix_Stat *buf, gint32 flags)
170 int r;
171 struct stat _buf;
173 if (Mono_Posix_FromAtFlags (flags, &flags) == -1)
174 return -1;
176 if (buf == NULL) {
177 errno = EFAULT;
178 return -1;
180 r = fstatat (dirfd, file_name, &_buf, flags);
181 if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
182 r = -1;
183 return r;
185 #endif
187 #ifndef HOST_WIN32
188 gint32
189 Mono_Posix_Syscall_mknod (const char *pathname, guint32 mode, mph_dev_t dev)
191 if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
192 return -1;
193 return mknod (pathname, mode, dev);
195 #endif
197 #ifdef HAVE_MKNODAT
198 gint32
199 Mono_Posix_Syscall_mknodat (int dirfd, const char *pathname, guint32 mode, mph_dev_t dev)
201 if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
202 return -1;
203 return mknodat (dirfd, pathname, mode, dev);
205 #endif
207 G_END_DECLS
209 gint64
210 Mono_Posix_Syscall_get_utime_now ()
212 #ifdef UTIME_NOW
213 return UTIME_NOW;
214 #else
215 return -1;
216 #endif
219 gint64
220 Mono_Posix_Syscall_get_utime_omit ()
222 #ifdef UTIME_OMIT
223 return UTIME_OMIT;
224 #else
225 return -1;
226 #endif
229 #if defined(HAVE_FUTIMENS) || defined(HAVE_UTIMENSAT)
230 static inline struct timespec*
231 copy_utimens (struct timespec* to, struct Mono_Posix_Timespec *from)
233 if (from) {
234 to[0].tv_sec = from[0].tv_sec;
235 to[0].tv_nsec = from[0].tv_nsec;
236 to[1].tv_sec = from[1].tv_sec;
237 to[1].tv_nsec = from[1].tv_nsec;
238 return to;
241 return NULL;
243 #endif
245 #ifdef HAVE_FUTIMENS
246 gint32
247 Mono_Posix_Syscall_futimens(int fd, struct Mono_Posix_Timespec *tv)
249 struct timespec _tv[2];
250 struct timespec *ptv;
252 ptv = copy_utimens (_tv, tv);
254 return futimens (fd, ptv);
256 #endif /* def HAVE_FUTIMENS */
258 #ifdef HAVE_UTIMENSAT
259 gint32
260 Mono_Posix_Syscall_utimensat(int dirfd, const char *pathname, struct Mono_Posix_Timespec *tv, int flags)
262 struct timespec _tv[2];
263 struct timespec *ptv;
265 ptv = copy_utimens (_tv, tv);
267 return utimensat (dirfd, pathname, ptv, flags);
269 #endif /* def HAVE_UTIMENSAT */
273 * vim: noexpandtab