Merge branch 'docs-move' of git://git.kernel.org/pub/scm/linux/kernel/git/rdunlap...
[linux/fpc-iii.git] / fs / hostfs / hostfs_user.c
blobdd7bc38a38251e9c123b08e87d42477f44f302b5
1 /*
2 * Copyright (C) 2000 - 2007 Jeff Dike (jdike@{addtoit,linux.intel}.com)
3 * Licensed under the GPL
4 */
6 #include <stdio.h>
7 #include <stddef.h>
8 #include <unistd.h>
9 #include <dirent.h>
10 #include <errno.h>
11 #include <fcntl.h>
12 #include <string.h>
13 #include <sys/stat.h>
14 #include <sys/time.h>
15 #include <sys/types.h>
16 #include <sys/vfs.h>
17 #include "hostfs.h"
18 #include "os.h"
19 #include <utime.h>
21 static void stat64_to_hostfs(const struct stat64 *buf, struct hostfs_stat *p)
23 p->ino = buf->st_ino;
24 p->mode = buf->st_mode;
25 p->nlink = buf->st_nlink;
26 p->uid = buf->st_uid;
27 p->gid = buf->st_gid;
28 p->size = buf->st_size;
29 p->atime.tv_sec = buf->st_atime;
30 p->atime.tv_nsec = 0;
31 p->ctime.tv_sec = buf->st_ctime;
32 p->ctime.tv_nsec = 0;
33 p->mtime.tv_sec = buf->st_mtime;
34 p->mtime.tv_nsec = 0;
35 p->blksize = buf->st_blksize;
36 p->blocks = buf->st_blocks;
37 p->maj = os_major(buf->st_rdev);
38 p->min = os_minor(buf->st_rdev);
41 int stat_file(const char *path, struct hostfs_stat *p, int fd)
43 struct stat64 buf;
45 if (fd >= 0) {
46 if (fstat64(fd, &buf) < 0)
47 return -errno;
48 } else if (lstat64(path, &buf) < 0) {
49 return -errno;
51 stat64_to_hostfs(&buf, p);
52 return 0;
55 int access_file(char *path, int r, int w, int x)
57 int mode = 0;
59 if (r)
60 mode = R_OK;
61 if (w)
62 mode |= W_OK;
63 if (x)
64 mode |= X_OK;
65 if (access(path, mode) != 0)
66 return -errno;
67 else return 0;
70 int open_file(char *path, int r, int w, int append)
72 int mode = 0, fd;
74 if (r && !w)
75 mode = O_RDONLY;
76 else if (!r && w)
77 mode = O_WRONLY;
78 else if (r && w)
79 mode = O_RDWR;
80 else panic("Impossible mode in open_file");
82 if (append)
83 mode |= O_APPEND;
84 fd = open64(path, mode);
85 if (fd < 0)
86 return -errno;
87 else return fd;
90 void *open_dir(char *path, int *err_out)
92 DIR *dir;
94 dir = opendir(path);
95 *err_out = errno;
97 return dir;
100 char *read_dir(void *stream, unsigned long long *pos,
101 unsigned long long *ino_out, int *len_out)
103 DIR *dir = stream;
104 struct dirent *ent;
106 seekdir(dir, *pos);
107 ent = readdir(dir);
108 if (ent == NULL)
109 return NULL;
110 *len_out = strlen(ent->d_name);
111 *ino_out = ent->d_ino;
112 *pos = telldir(dir);
113 return ent->d_name;
116 int read_file(int fd, unsigned long long *offset, char *buf, int len)
118 int n;
120 n = pread64(fd, buf, len, *offset);
121 if (n < 0)
122 return -errno;
123 *offset += n;
124 return n;
127 int write_file(int fd, unsigned long long *offset, const char *buf, int len)
129 int n;
131 n = pwrite64(fd, buf, len, *offset);
132 if (n < 0)
133 return -errno;
134 *offset += n;
135 return n;
138 int lseek_file(int fd, long long offset, int whence)
140 int ret;
142 ret = lseek64(fd, offset, whence);
143 if (ret < 0)
144 return -errno;
145 return 0;
148 int fsync_file(int fd, int datasync)
150 int ret;
151 if (datasync)
152 ret = fdatasync(fd);
153 else
154 ret = fsync(fd);
156 if (ret < 0)
157 return -errno;
158 return 0;
161 int replace_file(int oldfd, int fd)
163 return dup2(oldfd, fd);
166 void close_file(void *stream)
168 close(*((int *) stream));
171 void close_dir(void *stream)
173 closedir(stream);
176 int file_create(char *name, int ur, int uw, int ux, int gr,
177 int gw, int gx, int or, int ow, int ox)
179 int mode, fd;
181 mode = 0;
182 mode |= ur ? S_IRUSR : 0;
183 mode |= uw ? S_IWUSR : 0;
184 mode |= ux ? S_IXUSR : 0;
185 mode |= gr ? S_IRGRP : 0;
186 mode |= gw ? S_IWGRP : 0;
187 mode |= gx ? S_IXGRP : 0;
188 mode |= or ? S_IROTH : 0;
189 mode |= ow ? S_IWOTH : 0;
190 mode |= ox ? S_IXOTH : 0;
191 fd = open64(name, O_CREAT | O_RDWR, mode);
192 if (fd < 0)
193 return -errno;
194 return fd;
197 int set_attr(const char *file, struct hostfs_iattr *attrs, int fd)
199 struct hostfs_stat st;
200 struct timeval times[2];
201 int err, ma;
203 if (attrs->ia_valid & HOSTFS_ATTR_MODE) {
204 if (fd >= 0) {
205 if (fchmod(fd, attrs->ia_mode) != 0)
206 return -errno;
207 } else if (chmod(file, attrs->ia_mode) != 0) {
208 return -errno;
211 if (attrs->ia_valid & HOSTFS_ATTR_UID) {
212 if (fd >= 0) {
213 if (fchown(fd, attrs->ia_uid, -1))
214 return -errno;
215 } else if (chown(file, attrs->ia_uid, -1)) {
216 return -errno;
219 if (attrs->ia_valid & HOSTFS_ATTR_GID) {
220 if (fd >= 0) {
221 if (fchown(fd, -1, attrs->ia_gid))
222 return -errno;
223 } else if (chown(file, -1, attrs->ia_gid)) {
224 return -errno;
227 if (attrs->ia_valid & HOSTFS_ATTR_SIZE) {
228 if (fd >= 0) {
229 if (ftruncate(fd, attrs->ia_size))
230 return -errno;
231 } else if (truncate(file, attrs->ia_size)) {
232 return -errno;
237 * Update accessed and/or modified time, in two parts: first set
238 * times according to the changes to perform, and then call futimes()
239 * or utimes() to apply them.
241 ma = (HOSTFS_ATTR_ATIME_SET | HOSTFS_ATTR_MTIME_SET);
242 if (attrs->ia_valid & ma) {
243 err = stat_file(file, &st, fd);
244 if (err != 0)
245 return err;
247 times[0].tv_sec = st.atime.tv_sec;
248 times[0].tv_usec = st.atime.tv_nsec / 1000;
249 times[1].tv_sec = st.mtime.tv_sec;
250 times[1].tv_usec = st.mtime.tv_nsec / 1000;
252 if (attrs->ia_valid & HOSTFS_ATTR_ATIME_SET) {
253 times[0].tv_sec = attrs->ia_atime.tv_sec;
254 times[0].tv_usec = attrs->ia_atime.tv_nsec / 1000;
256 if (attrs->ia_valid & HOSTFS_ATTR_MTIME_SET) {
257 times[1].tv_sec = attrs->ia_mtime.tv_sec;
258 times[1].tv_usec = attrs->ia_mtime.tv_nsec / 1000;
261 if (fd >= 0) {
262 if (futimes(fd, times) != 0)
263 return -errno;
264 } else if (utimes(file, times) != 0) {
265 return -errno;
269 /* Note: ctime is not handled */
270 if (attrs->ia_valid & (HOSTFS_ATTR_ATIME | HOSTFS_ATTR_MTIME)) {
271 err = stat_file(file, &st, fd);
272 attrs->ia_atime = st.atime;
273 attrs->ia_mtime = st.mtime;
274 if (err != 0)
275 return err;
277 return 0;
280 int make_symlink(const char *from, const char *to)
282 int err;
284 err = symlink(to, from);
285 if (err)
286 return -errno;
287 return 0;
290 int unlink_file(const char *file)
292 int err;
294 err = unlink(file);
295 if (err)
296 return -errno;
297 return 0;
300 int do_mkdir(const char *file, int mode)
302 int err;
304 err = mkdir(file, mode);
305 if (err)
306 return -errno;
307 return 0;
310 int do_rmdir(const char *file)
312 int err;
314 err = rmdir(file);
315 if (err)
316 return -errno;
317 return 0;
320 int do_mknod(const char *file, int mode, unsigned int major, unsigned int minor)
322 int err;
324 err = mknod(file, mode, os_makedev(major, minor));
325 if (err)
326 return -errno;
327 return 0;
330 int link_file(const char *to, const char *from)
332 int err;
334 err = link(to, from);
335 if (err)
336 return -errno;
337 return 0;
340 int hostfs_do_readlink(char *file, char *buf, int size)
342 int n;
344 n = readlink(file, buf, size);
345 if (n < 0)
346 return -errno;
347 if (n < size)
348 buf[n] = '\0';
349 return n;
352 int rename_file(char *from, char *to)
354 int err;
356 err = rename(from, to);
357 if (err < 0)
358 return -errno;
359 return 0;
362 int do_statfs(char *root, long *bsize_out, long long *blocks_out,
363 long long *bfree_out, long long *bavail_out,
364 long long *files_out, long long *ffree_out,
365 void *fsid_out, int fsid_size, long *namelen_out)
367 struct statfs64 buf;
368 int err;
370 err = statfs64(root, &buf);
371 if (err < 0)
372 return -errno;
374 *bsize_out = buf.f_bsize;
375 *blocks_out = buf.f_blocks;
376 *bfree_out = buf.f_bfree;
377 *bavail_out = buf.f_bavail;
378 *files_out = buf.f_files;
379 *ffree_out = buf.f_ffree;
380 memcpy(fsid_out, &buf.f_fsid,
381 sizeof(buf.f_fsid) > fsid_size ? fsid_size :
382 sizeof(buf.f_fsid));
383 *namelen_out = buf.f_namelen;
385 return 0;