1 /* $NetBSD: memfs.c,v 1.1 2011/03/06 18:22:13 phx Exp $ */
2
3 /*-
4 * Copyright (c) 2011 Frank Wille.
5 * All rights reserved.
6 *
7 * Written by Frank Wille for The NetBSD Project.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
29 */
30
31 #include <sys/param.h>
32
33 #include <lib/libsa/stand.h>
34
35 #include "globals.h"
36 #include "memfs.h"
37
38 struct memhandle {
39 char *base;
40 off_t off;
41 };
42
43 int
mem_open(const char * path,struct open_file * f)44 mem_open(const char *path, struct open_file *f)
45 {
46 struct memhandle *mh;
47
48 mh = alloc(sizeof(struct memhandle));
49 if (mh == NULL)
50 return ENOMEM;
51 mh->base = (char *)read_hex(path);
52 mh->off = 0;
53 f->f_fsdata = mh;
54 return 0;
55 }
56
57 #ifndef LIBSA_NO_FS_CLOSE
58 int
mem_close(struct open_file * f)59 mem_close(struct open_file *f)
60 {
61
62 dealloc(f->f_fsdata, sizeof(struct memhandle));
63 return 0;
64 }
65 #endif
66
67 int
mem_read(struct open_file * f,void * buf,size_t size,size_t * resid)68 mem_read(struct open_file *f, void *buf, size_t size, size_t *resid)
69 {
70 struct memhandle *mh;
71
72 mh = f->f_fsdata;
73 memcpy(buf, mh->base + mh->off, size);
74 mh->off += size;
75 if (resid)
76 *resid = 0;
77 return 0;
78 }
79
80 #ifndef LIBSA_NO_FS_WRITE
81 int
mem_write(struct open_file * f,void * buf,size_t size,size_t * resid)82 mem_write(struct open_file *f, void *buf, size_t size, size_t *resid)
83 {
84 struct memhandle *mh;
85
86 mh = f->f_fsdata;
87 memcpy(mh->base + mh->off, buf, size);
88 mh->off += size;
89 if (resid)
90 *resid = 0;
91 return 0;
92 }
93 #endif
94
95 #ifndef LIBSA_NO_FS_SEEK
96 off_t
mem_seek(struct open_file * f,off_t offset,int where)97 mem_seek(struct open_file *f, off_t offset, int where)
98 {
99 struct memhandle *mh;
100
101 mh = f->f_fsdata;
102 switch (where) {
103 case SEEK_SET:
104 mh->off = offset;
105 break;
106 case SEEK_CUR:
107 mh->off += offset;
108 break;
109 default:
110 errno = EOFFSET;
111 return -1;
112 }
113 return mh->off;
114 }
115 #endif
116
117 int
mem_stat(struct open_file * f,struct stat * sb)118 mem_stat(struct open_file *f, struct stat *sb)
119 {
120
121 return EIO;
122 }
123