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