xref: /netbsd/sys/arch/hpcmips/stand/libsa/winfs.c (revision bf9ec67e)
1 /*	$NetBSD: winfs.c,v 1.2 2000/01/16 03:07:27 takemura Exp $	*/
2 
3 /*-
4  * Copyright (c) 1999 Shin Takemura.
5  * All rights reserved.
6  *
7  * This software is part of the PocketBSD.
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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *	This product includes software developed by the PocketBSD project
20  *	and its contributors.
21  * 4. Neither the name of the project nor the names of its contributors
22  *    may be used to endorse or promote products derived from this software
23  *    without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35  * SUCH DAMAGE.
36  *
37  */
38 #define STANDALONE_WINDOWS_SIDE
39 #include <stand.h>
40 #include <winfs.h>
41 
42 #define MAXPATHLEN 1024
43 
44 /*
45  *  file system specific context.
46  */
47 struct winfs {
48 	HANDLE	hDevice;
49 };
50 
51 
52 int
53 win_open(path, f)
54 	char           *path;
55 	struct open_file *f;
56 {
57 	TCHAR *wpath = (TCHAR*)path;
58 	struct winfs *fsdata;
59 
60 	fsdata = (struct winfs *)alloc(sizeof(*fsdata));
61 	if (!fsdata) {
62 		return (ENOMEM);
63 	}
64 
65 	win_printf(TEXT("open(%s)\n"), wpath);
66 	fsdata->hDevice = CreateFile(wpath, GENERIC_READ, 0, NULL,
67 				    OPEN_EXISTING, 0, NULL);
68 	if (fsdata->hDevice == INVALID_HANDLE_VALUE) {
69 		win_printf(TEXT("can't open %s.\n"), wpath);
70 		free(fsdata, sizeof(*fsdata));
71 		return (EIO);	/* XXX, We shuld check GetLastError(). */
72 	}
73 
74 	f->f_fsdata = (void *)fsdata;
75 
76 	return (0);
77 }
78 
79 
80 int
81 win_close(f)
82 	struct open_file *f;
83 {
84 	struct winfs *fsdata = (struct winfs *) f->f_fsdata;
85 
86 	if (fsdata->hDevice != INVALID_HANDLE_VALUE) {
87 		CloseHandle(fsdata->hDevice);
88 	}
89 	free(fsdata, sizeof(*fsdata));
90 
91 	return (0);
92 }
93 
94 
95 int
96 win_read(f, addr, size, resid)
97 	struct open_file *f;
98 	void           *addr;
99 	size_t         size;
100 	size_t         *resid;	/* out */
101 {
102 	struct winfs *fsdata = (struct winfs *) f->f_fsdata;
103 	DWORD read_len;
104 
105 	while (size > 0) {
106 		if (!ReadFile(fsdata->hDevice,
107 			      (u_char*)addr, size,
108 			      &read_len, NULL)) {
109 			win_printf(TEXT("ReadFile() failed.\n"));
110 		}
111 
112 		if (read_len == 0)
113 			break;	/* EOF */
114 
115 		(unsigned long)addr += read_len;
116 		size -= read_len;
117 	}
118 
119 	if (resid)
120 		*resid = size;
121 	return (0);
122 }
123 
124 int
125 win_write(f, start, size, resid)
126 	struct open_file *f;
127 	void           *start;
128 	size_t          size;
129 	size_t         *resid;	/* out */
130 {
131 	return (EROFS);	/* XXX */
132 }
133 
134 
135 int
136 win_stat(f, sb)
137 	struct open_file *f;
138 	struct stat    *sb;
139 {
140 	sb->st_mode = 0444;
141 	sb->st_nlink = 1;
142 	sb->st_uid = 0;
143 	sb->st_gid = 0;
144 	sb->st_size = -1;
145 	return (0);
146 }
147 
148 off_t
149 win_seek(f, offset, whence)
150 	struct open_file *f;
151 	off_t           offset;
152 	int             whence;
153 {
154 	struct winfs *fsdata = (struct winfs *) f->f_fsdata;
155 	DWORD dwPointer;
156 	int winwhence;
157 
158 	switch (whence) {
159 	case SEEK_SET:
160 		winwhence = FILE_BEGIN;
161 		break;
162 	case SEEK_CUR:
163 		winwhence = FILE_CURRENT;
164 		break;
165 	case SEEK_END:
166 		winwhence = FILE_END;
167 		break;
168 	default:
169 		errno = EOFFSET;
170 		return (-1);
171 	}
172 
173 	dwPointer = SetFilePointer(fsdata->hDevice, offset, NULL, winwhence);
174 	if (dwPointer == 0xffffffff) {
175 		errno = EINVAL;	/* XXX, We shuld check GetLastError(). */
176 		return (-1);
177 	}
178 
179 	return (0);
180 }
181