Merge pull request #221 from steffen-kiess/master
[mono.git] / support / sys-stat.c
1 /*
2  * <sys/stat.h> wrapper functions.
3  *
4  * Authors:
5  *   Jonathan Pryor (jonpryor@vt.edu)
6  *
7  * Copyright (C) 2004-2006 Jonathan Pryor
8  */
9
10 #ifndef _GNU_SOURCE
11 #define _GNU_SOURCE
12 #endif /* ndef _GNU_SOURCE */
13
14 #include <sys/types.h>
15 #include <sys/stat.h>
16 #include <unistd.h>
17 #include <fcntl.h>
18 #include <errno.h>
19
20 #include "map.h"
21 #include "mph.h"
22
23 G_BEGIN_DECLS
24
25 gint32
26 Mono_Posix_Syscall_stat (const char *file_name, struct Mono_Posix_Stat *buf)
27 {
28         int r;
29         struct stat _buf;
30
31         if (buf == NULL) {
32                 errno = EFAULT;
33                 return -1;
34         }
35         r = stat (file_name, &_buf);
36         if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
37                 r = -1;
38         return r;
39 }
40
41 gint32
42 Mono_Posix_Syscall_fstat (int filedes, struct Mono_Posix_Stat *buf)
43 {
44         int r;
45         struct stat _buf;
46
47         if (buf == NULL) {
48                 errno = EFAULT;
49                 return -1;
50         }
51         r = fstat (filedes, &_buf);
52         if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
53                 r = -1;
54         return r;
55 }
56
57 gint32
58 Mono_Posix_Syscall_lstat (const char *file_name, struct Mono_Posix_Stat *buf)
59 {
60         int r;
61         struct stat _buf;
62
63         if (buf == NULL) {
64                 errno = EFAULT;
65                 return -1;
66         }
67         r = lstat (file_name, &_buf);
68         if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
69                 r = -1;
70         return r;
71 }
72
73 #ifdef HAVE_FSTATAT
74 gint32
75 Mono_Posix_Syscall_fstatat (gint32 dirfd, const char *file_name, struct Mono_Posix_Stat *buf, gint32 flags)
76 {
77         int r;
78         struct stat _buf;
79
80         if (Mono_Posix_FromAtFlags (flags, &flags) == -1)
81                 return -1;
82
83         if (buf == NULL) {
84                 errno = EFAULT;
85                 return -1;
86         }
87         r = fstatat (dirfd, file_name, &_buf, flags);
88         if (r != -1 && Mono_Posix_ToStat (&_buf, buf) == -1)
89                 r = -1;
90         return r;
91 }
92 #endif
93
94 gint32
95 Mono_Posix_Syscall_mknod (const char *pathname, guint32 mode, mph_dev_t dev)
96 {
97         if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
98                 return -1;
99         return mknod (pathname, mode, dev);
100 }
101
102 #ifdef HAVE_MKNODAT
103 gint32
104 Mono_Posix_Syscall_mknodat (int dirfd, const char *pathname, guint32 mode, mph_dev_t dev)
105 {
106         if (Mono_Posix_FromFilePermissions (mode, &mode) == -1)
107                 return -1;
108         return mknodat (dirfd, pathname, mode, dev);
109 }
110 #endif
111
112 G_END_DECLS
113
114 gint64
115 Mono_Posix_Syscall_get_utime_now ()
116 {
117 #ifdef UTIME_NOW
118         return UTIME_NOW;
119 #else
120         return -1;
121 #endif
122 }
123
124 gint64
125 Mono_Posix_Syscall_get_utime_omit ()
126 {
127 #ifdef UTIME_OMIT
128         return UTIME_OMIT;
129 #else
130         return -1;
131 #endif
132 }
133
134 static inline struct timespec*
135 copy_utimens (struct timespec* to, struct Mono_Posix_Timespec *from)
136 {
137         if (from) {
138                 to[0].tv_sec  = from[0].tv_sec;
139                 to[0].tv_nsec = from[0].tv_nsec;
140                 to[1].tv_sec  = from[1].tv_sec;
141                 to[1].tv_nsec = from[1].tv_nsec;
142                 return to;
143         }
144
145         return NULL;
146 }
147
148 #ifdef HAVE_FUTIMENS
149 gint32
150 Mono_Posix_Syscall_futimens(int fd, struct Mono_Posix_Timespec *tv)
151 {
152         struct timespec _tv[2];
153         struct timespec *ptv;
154
155         ptv = copy_utimens (_tv, tv);
156
157         return futimens (fd, ptv);
158 }
159 #endif /* def HAVE_FUTIMENS */
160
161 #ifdef HAVE_UTIMENSAT
162 gint32
163 Mono_Posix_Syscall_utimensat(int dirfd, const char *pathname, struct Mono_Posix_Timespec *tv, int flags)
164 {
165         struct timespec _tv[2];
166         struct timespec *ptv;
167
168         ptv = copy_utimens (_tv, tv);
169
170         return utimensat (dirfd, pathname, ptv, flags);
171 }
172 #endif /* def HAVE_UTIMENSAT */
173
174
175 /*
176  * vim: noexpandtab
177  */