1 /* $NetBSD: file_ufs.cpp,v 1.2 2002/02/04 17:32:02 uch Exp $ */ 2 3 /*- 4 * Copyright (c) 2001, 2002 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by UCHIYAMA Yasushi. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 #include <machine/cdefs.h> 40 #include <machine/types.h> 41 #include <machine/int_types.h> 42 43 #include <file.h> 44 #include <file_ufs.h> 45 #include <console.h> 46 47 __BEGIN_DECLS 48 #include <stand.h> 49 #include <lib/libsa/ufs.h> 50 #include <winblk.h> 51 __END_DECLS 52 53 UfsFile::UfsFile(Console *&cons) 54 : File(cons) 55 { 56 static struct devsw devsw[] = { 57 {"winblk", winblkstrategy, winblkopen, 58 winblkclose, winblkioctl }, 59 }; 60 61 _f = static_cast<struct open_file *>(malloc(sizeof(struct open_file))); 62 _f->f_dev = devsw; 63 _debug = TRUE; 64 65 DPRINTF((TEXT("FileManager: UFS\n"))); 66 } 67 68 UfsFile::~UfsFile(void) 69 { 70 free(_f); 71 } 72 73 BOOL 74 UfsFile::setRoot(TCHAR *drive) 75 { 76 char name[MAX_PATH]; 77 int unit; 78 79 _to_ascii(name, drive, MAX_PATH); 80 if ('1' <= name[0] && name[0] <= '9' && name[1] == ':') 81 unit = name[0] - '0'; 82 else 83 unit = 1; 84 85 winblkopen(_f, "DSK", unit, 0); 86 87 return TRUE; 88 } 89 90 BOOL 91 UfsFile::open(const TCHAR *name, u_int32_t flags) 92 { 93 int error; 94 95 if (!_to_ascii(_ascii_filename, name, MAX_PATH)) 96 return FALSE; 97 98 error = ufs_open(_ascii_filename, _f); 99 DPRINTF((TEXT("open file \"%s\" "), name)); 100 101 if (!error) 102 DPRINTF((TEXT("(%d byte).\n"), size())); 103 else 104 DPRINTF((TEXT("failed.\n"))); 105 106 return !error; 107 } 108 109 size_t 110 UfsFile::read(void *buf, size_t bytes, off_t ofs) 111 { 112 size_t sz; 113 114 if (ofs != -1) 115 ufs_seek(_f, ofs, SEEK_SET); 116 ufs_read(_f, buf, bytes, &sz); 117 118 return bytes - sz; 119 } 120 121 size_t 122 UfsFile::write(const void *buf, size_t bytes, off_t ofs) 123 { 124 size_t sz; 125 126 if (ofs != -1) 127 ufs_seek(_f, ofs, SEEK_SET); 128 ufs_write(_f,(void *) buf, bytes, &sz); 129 130 return bytes - sz; 131 } 132 133 BOOL 134 UfsFile::seek(off_t ofs) 135 { 136 ufs_seek(_f, ofs, SEEK_SET); 137 138 return TRUE; 139 } 140 141 size_t 142 UfsFile::size() 143 { 144 struct stat st; 145 146 ufs_stat(_f, &st); 147 148 return st.st_size; 149 } 150 151 BOOL 152 UfsFile::close() 153 { 154 return ufs_close(_f); 155 } 156