1 /* $NetBSD: pathfs.c,v 1.1 2012/01/18 23:12:21 nonaka Exp $ */
2
3 /*-
4 * Copyright (C) 2012 NONAKA Kimihiro <nonaka@netbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "boot.h"
29 #include "pathfs.h"
30 #include "unixdev.h"
31 #include "compat_linux.h"
32
33 __compactcall int
pathfs_open(const char * path,struct open_file * fd)34 pathfs_open(const char *path, struct open_file *fd)
35 {
36
37 if (strcmp(fd->f_dev->dv_name, "path"))
38 return EINVAL;
39
40 (void) ulseek((int)fd->f_devdata, 0L, SEEK_SET);
41 return 0;
42 }
43
44 __compactcall int
pathfs_read(struct open_file * fd,void * vbuf,size_t nbyte,size_t * resid)45 pathfs_read(struct open_file *fd, void *vbuf, size_t nbyte, size_t *resid)
46 {
47 char *buf = vbuf;
48 size_t off = 0;
49 ssize_t rsz;
50
51 while (off < nbyte) {
52 rsz = uread((int)fd->f_devdata, buf + off, nbyte - off);
53 if (rsz < 0)
54 return errno;
55 if (rsz == 0)
56 break;
57 off += rsz;
58 }
59
60 *resid -= off;
61 return 0;
62 }
63
64 __compactcall int
pathfs_write(struct open_file * fd,void * vbuf,size_t size,size_t * resid)65 pathfs_write(struct open_file *fd, void *vbuf, size_t size, size_t *resid)
66 {
67
68 return EROFS;
69 }
70
71 __compactcall off_t
pathfs_seek(struct open_file * fd,off_t offset,int whence)72 pathfs_seek(struct open_file *fd, off_t offset, int whence)
73 {
74
75 return ulseek((int)fd->f_devdata, offset, whence);
76 }
77
78 __compactcall int
pathfs_close(struct open_file * fd)79 pathfs_close(struct open_file *fd)
80 {
81
82 return 0;
83 }
84
85 __compactcall int
pathfs_stat(struct open_file * fd,struct stat * sb)86 pathfs_stat(struct open_file *fd, struct stat *sb)
87 {
88 struct linux_stat lsb;
89 int rv;
90
91 rv = ufstat((int)fd->f_devdata, &lsb);
92 if (rv < 0)
93 return errno;
94
95 sb->st_ino = lsb.lst_ino;
96 sb->st_mode = lsb.lst_mode;
97 sb->st_nlink = lsb.lst_nlink;
98 sb->st_uid = lsb.lst_uid;
99 sb->st_gid = lsb.lst_gid;
100 sb->st_size = lsb.lst_size;
101 sb->st_blksize = lsb.lst_blksize;
102 sb->st_blocks = lsb.lst_blocks;
103 sb->st_atime = lsb.lst_atime;
104 sb->st_mtime = lsb.lst_mtime;
105 sb->st_ctime = lsb.lst_ctime;
106 return 0;
107 }
108
109 __compactcall void
pathfs_ls(struct open_file * f,const char * pattern)110 pathfs_ls(struct open_file *f, const char *pattern)
111 {
112
113 printf("Currently ls command is unsupported by pathfs.\n");
114 }
115