1
2
3
4
5
6
7
8
9
10
11
12
13#ifndef QEMU_9P_UTIL_H
14#define QEMU_9P_UTIL_H
15
16#ifdef O_PATH
17#define O_PATH_9P_UTIL O_PATH
18#else
19#define O_PATH_9P_UTIL 0
20#endif
21
22#if !defined(CONFIG_LINUX)
23
24
25
26
27
28
29
30
31
32static inline uint64_t makedev_dotl(uint32_t dev_major, uint32_t dev_minor)
33{
34 uint64_t dev;
35
36
37 dev = (((uint64_t) (dev_major & 0x00000fffu)) << 8);
38 dev |= (((uint64_t) (dev_major & 0xfffff000u)) << 32);
39 dev |= (((uint64_t) (dev_minor & 0x000000ffu)) << 0);
40 dev |= (((uint64_t) (dev_minor & 0xffffff00u)) << 12);
41 return dev;
42}
43
44#endif
45
46
47
48
49
50
51
52static inline uint64_t host_dev_to_dotl_dev(dev_t dev)
53{
54#ifdef CONFIG_LINUX
55 return dev;
56#else
57 return makedev_dotl(major(dev), minor(dev));
58#endif
59}
60
61
62static inline int errno_to_dotl(int err) {
63#if defined(CONFIG_LINUX)
64
65#elif defined(CONFIG_DARWIN)
66
67
68
69
70
71
72 if (err == ENAMETOOLONG) {
73 err = 36;
74 } else if (err == ENOTEMPTY) {
75 err = 39;
76 } else if (err == ELOOP) {
77 err = 40;
78 } else if (err == ENOATTR) {
79 err = 61;
80 } else if (err == ENOTSUP) {
81 err = 95;
82 } else if (err == EOPNOTSUPP) {
83 err = 95;
84 }
85#else
86#error Missing errno translation to Linux for this host system
87#endif
88 return err;
89}
90
91#ifdef CONFIG_DARWIN
92#define qemu_fgetxattr(...) fgetxattr(__VA_ARGS__, 0, 0)
93#define qemu_lgetxattr(...) getxattr(__VA_ARGS__, 0, XATTR_NOFOLLOW)
94#define qemu_llistxattr(...) listxattr(__VA_ARGS__, XATTR_NOFOLLOW)
95#define qemu_lremovexattr(...) removexattr(__VA_ARGS__, XATTR_NOFOLLOW)
96static inline int qemu_lsetxattr(const char *path, const char *name,
97 const void *value, size_t size, int flags) {
98 return setxattr(path, name, value, size, 0, flags | XATTR_NOFOLLOW);
99}
100#else
101#define qemu_fgetxattr fgetxattr
102#define qemu_lgetxattr lgetxattr
103#define qemu_llistxattr llistxattr
104#define qemu_lremovexattr lremovexattr
105#define qemu_lsetxattr lsetxattr
106#endif
107
108static inline void close_preserve_errno(int fd)
109{
110 int serrno = errno;
111 close(fd);
112 errno = serrno;
113}
114
115static inline int openat_dir(int dirfd, const char *name)
116{
117 return openat(dirfd, name,
118 O_DIRECTORY | O_RDONLY | O_NOFOLLOW | O_PATH_9P_UTIL);
119}
120
121static inline int openat_file(int dirfd, const char *name, int flags,
122 mode_t mode)
123{
124 int fd, serrno, ret;
125
126#ifndef CONFIG_DARWIN
127again:
128#endif
129 fd = openat(dirfd, name, flags | O_NOFOLLOW | O_NOCTTY | O_NONBLOCK,
130 mode);
131 if (fd == -1) {
132#ifndef CONFIG_DARWIN
133 if (errno == EPERM && (flags & O_NOATIME)) {
134
135
136
137
138
139
140
141
142 flags &= ~O_NOATIME;
143 goto again;
144 }
145#endif
146 return -1;
147 }
148
149 serrno = errno;
150
151
152
153
154 if (!(flags & O_PATH_9P_UTIL)) {
155 ret = fcntl(fd, F_SETFL, flags);
156 assert(!ret);
157 }
158 errno = serrno;
159 return fd;
160}
161
162ssize_t fgetxattrat_nofollow(int dirfd, const char *path, const char *name,
163 void *value, size_t size);
164int fsetxattrat_nofollow(int dirfd, const char *path, const char *name,
165 void *value, size_t size, int flags);
166ssize_t flistxattrat_nofollow(int dirfd, const char *filename,
167 char *list, size_t size);
168ssize_t fremovexattrat_nofollow(int dirfd, const char *filename,
169 const char *name);
170
171
172
173
174
175
176
177static inline off_t qemu_dirent_off(struct dirent *dent)
178{
179#ifdef CONFIG_DARWIN
180 return dent->d_seekoff;
181#else
182 return dent->d_off;
183#endif
184}
185
186
187
188
189
190
191
192
193
194
195
196
197static inline struct dirent *qemu_dirent_dup(struct dirent *dent)
198{
199 size_t sz = 0;
200#if defined _DIRENT_HAVE_D_RECLEN
201
202 sz = dent->d_reclen;
203#endif
204
205
206
207
208 if (sz == 0) {
209
210 sz = offsetof(struct dirent, d_name) +
211 strlen(dent->d_name) + 1;
212 }
213 return g_memdup(dent, sz);
214}
215
216
217
218
219
220
221
222
223#if defined CONFIG_DARWIN && defined CONFIG_PTHREAD_FCHDIR_NP
224int pthread_fchdir_np(int fd) __attribute__((weak_import));
225#endif
226int qemu_mknodat(int dirfd, const char *filename, mode_t mode, dev_t dev);
227
228#endif
229