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