Home | History | Annotate | Line # | Download | only in libsa
      1  1.5       dsl /*	$NetBSD: winfs.c,v 1.5 2009/03/14 21:04:09 dsl 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.4       dsl win_open(char *path, struct open_file *f)
     54  1.2  takemura {
     55  1.2  takemura 	TCHAR *wpath = (TCHAR*)path;
     56  1.2  takemura 	struct winfs *fsdata;
     57  1.2  takemura 
     58  1.2  takemura 	fsdata = (struct winfs *)alloc(sizeof(*fsdata));
     59  1.2  takemura 	if (!fsdata) {
     60  1.2  takemura 		return (ENOMEM);
     61  1.2  takemura 	}
     62  1.2  takemura 
     63  1.2  takemura 	win_printf(TEXT("open(%s)\n"), wpath);
     64  1.2  takemura 	fsdata->hDevice = CreateFile(wpath, GENERIC_READ, 0, NULL,
     65  1.2  takemura 				    OPEN_EXISTING, 0, NULL);
     66  1.2  takemura 	if (fsdata->hDevice == INVALID_HANDLE_VALUE) {
     67  1.2  takemura 		win_printf(TEXT("can't open %s.\n"), wpath);
     68  1.3  christos 		dealloc(fsdata, sizeof(*fsdata));
     69  1.2  takemura 		return (EIO);	/* XXX, We shuld check GetLastError(). */
     70  1.2  takemura 	}
     71  1.2  takemura 
     72  1.2  takemura 	f->f_fsdata = (void *)fsdata;
     73  1.2  takemura 
     74  1.2  takemura 	return (0);
     75  1.2  takemura }
     76  1.2  takemura 
     77  1.2  takemura 
     78  1.2  takemura int
     79  1.4       dsl win_close(struct open_file *f)
     80  1.2  takemura {
     81  1.2  takemura 	struct winfs *fsdata = (struct winfs *) f->f_fsdata;
     82  1.2  takemura 
     83  1.2  takemura 	if (fsdata->hDevice != INVALID_HANDLE_VALUE) {
     84  1.2  takemura 		CloseHandle(fsdata->hDevice);
     85  1.2  takemura 	}
     86  1.3  christos 	dealloc(fsdata, sizeof(*fsdata));
     87  1.2  takemura 
     88  1.2  takemura 	return (0);
     89  1.2  takemura }
     90  1.2  takemura 
     91  1.2  takemura 
     92  1.2  takemura int
     93  1.5       dsl win_read(struct open_file *f, void *addr, size_t size, size_t *resid)
     94  1.5       dsl 	/* resid:	 out */
     95  1.2  takemura {
     96  1.2  takemura 	struct winfs *fsdata = (struct winfs *) f->f_fsdata;
     97  1.2  takemura 	DWORD read_len;
     98  1.2  takemura 
     99  1.2  takemura 	while (size > 0) {
    100  1.2  takemura 		if (!ReadFile(fsdata->hDevice,
    101  1.2  takemura 			      (u_char*)addr, size,
    102  1.2  takemura 			      &read_len, NULL)) {
    103  1.2  takemura 			win_printf(TEXT("ReadFile() failed.\n"));
    104  1.2  takemura 		}
    105  1.2  takemura 
    106  1.2  takemura 		if (read_len == 0)
    107  1.2  takemura 			break;	/* EOF */
    108  1.2  takemura 
    109  1.2  takemura 		(unsigned long)addr += read_len;
    110  1.2  takemura 		size -= read_len;
    111  1.2  takemura 	}
    112  1.2  takemura 
    113  1.2  takemura 	if (resid)
    114  1.2  takemura 		*resid = size;
    115  1.2  takemura 	return (0);
    116  1.2  takemura }
    117  1.2  takemura 
    118  1.2  takemura int
    119  1.5       dsl win_write(struct open_file *f, void *start, size_t size, size_t *resid)
    120  1.5       dsl 	/* resid:	 out */
    121  1.2  takemura {
    122  1.2  takemura 	return (EROFS);	/* XXX */
    123  1.2  takemura }
    124  1.2  takemura 
    125  1.2  takemura 
    126  1.2  takemura int
    127  1.4       dsl win_stat(struct open_file *f, struct stat *sb)
    128  1.2  takemura {
    129  1.2  takemura 	sb->st_mode = 0444;
    130  1.2  takemura 	sb->st_nlink = 1;
    131  1.2  takemura 	sb->st_uid = 0;
    132  1.2  takemura 	sb->st_gid = 0;
    133  1.2  takemura 	sb->st_size = -1;
    134  1.2  takemura 	return (0);
    135  1.2  takemura }
    136  1.2  takemura 
    137  1.2  takemura off_t
    138  1.4       dsl win_seek(struct open_file *f, off_t offset, int whence)
    139  1.2  takemura {
    140  1.2  takemura 	struct winfs *fsdata = (struct winfs *) f->f_fsdata;
    141  1.2  takemura 	DWORD dwPointer;
    142  1.2  takemura 	int winwhence;
    143  1.2  takemura 
    144  1.2  takemura 	switch (whence) {
    145  1.2  takemura 	case SEEK_SET:
    146  1.2  takemura 		winwhence = FILE_BEGIN;
    147  1.2  takemura 		break;
    148  1.2  takemura 	case SEEK_CUR:
    149  1.2  takemura 		winwhence = FILE_CURRENT;
    150  1.2  takemura 		break;
    151  1.2  takemura 	case SEEK_END:
    152  1.2  takemura 		winwhence = FILE_END;
    153  1.2  takemura 		break;
    154  1.2  takemura 	default:
    155  1.2  takemura 		errno = EOFFSET;
    156  1.2  takemura 		return (-1);
    157  1.2  takemura 	}
    158  1.2  takemura 
    159  1.2  takemura 	dwPointer = SetFilePointer(fsdata->hDevice, offset, NULL, winwhence);
    160  1.2  takemura 	if (dwPointer == 0xffffffff) {
    161  1.2  takemura 		errno = EINVAL;	/* XXX, We shuld check GetLastError(). */
    162  1.2  takemura 		return (-1);
    163  1.2  takemura 	}
    164  1.2  takemura 
    165  1.2  takemura 	return (0);
    166  1.2  takemura }
    167