1 |
dpavlin |
1 |
/* |
2 |
|
|
* libhfs - library for reading and writing Macintosh HFS volumes |
3 |
|
|
* Copyright (C) 1996-1998 Robert Leslie |
4 |
|
|
* Modified for use with PearPC (c) 2004 Stefan Weyergraf |
5 |
|
|
* |
6 |
|
|
* This program is free software; you can redistribute it and/or modify |
7 |
|
|
* it under the terms of the GNU General Public License as published by |
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
9 |
|
|
* (at your option) any later version. |
10 |
|
|
* |
11 |
|
|
* This program is distributed in the hope that it will be useful, |
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
|
|
* GNU General Public License for more details. |
15 |
|
|
* |
16 |
|
|
* You should have received a copy of the GNU General Public License |
17 |
|
|
* along with this program; if not, write to the Free Software |
18 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
19 |
|
|
* |
20 |
|
|
*/ |
21 |
|
|
|
22 |
|
|
# ifdef HAVE_CONFIG_H |
23 |
|
|
# include "config.h" |
24 |
|
|
# endif |
25 |
|
|
|
26 |
|
|
//# ifdef HAVE_FCNTL_H |
27 |
|
|
# if 1 |
28 |
|
|
# include <fcntl.h> |
29 |
|
|
# else |
30 |
|
|
int open(const char *, int, ...); |
31 |
|
|
int fcntl(int, int, ...); |
32 |
|
|
# endif |
33 |
|
|
|
34 |
|
|
//# ifdef HAVE_UNISTD_H |
35 |
|
|
# if 1 |
36 |
|
|
# include <unistd.h> |
37 |
|
|
# else |
38 |
|
|
int close(int); |
39 |
|
|
off_t lseek(int, off_t, int); |
40 |
|
|
ssize_t read(int, void *, size_t); |
41 |
|
|
ssize_t write(int, const char *, size_t); |
42 |
|
|
int stat(const char *, struct stat *); |
43 |
|
|
int fstat(int, struct stat *); |
44 |
|
|
# endif |
45 |
|
|
|
46 |
|
|
# include <errno.h> |
47 |
|
|
# include <sys/stat.h> |
48 |
|
|
|
49 |
|
|
extern "C" { |
50 |
|
|
# include "libhfs.h" |
51 |
|
|
# include "os.h" |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
#include "../hfsglue.h" |
55 |
|
|
#include "tools/except.h" |
56 |
|
|
|
57 |
|
|
/* |
58 |
|
|
* NAME: os->open() |
59 |
|
|
* DESCRIPTION: open and lock a new descriptor from the given path and mode |
60 |
|
|
*/ |
61 |
|
|
#if 0 |
62 |
|
|
int os_open(void **priv, const char *path, int mode) |
63 |
|
|
{ |
64 |
|
|
int fd; |
65 |
|
|
struct flock lock; |
66 |
|
|
|
67 |
|
|
switch (mode) |
68 |
|
|
{ |
69 |
|
|
case HFS_MODE_RDONLY: |
70 |
|
|
mode = O_RDONLY; |
71 |
|
|
break; |
72 |
|
|
|
73 |
|
|
case HFS_MODE_RDWR: |
74 |
|
|
default: |
75 |
|
|
mode = O_RDWR; |
76 |
|
|
break; |
77 |
|
|
} |
78 |
|
|
|
79 |
|
|
fd = open(path, mode); |
80 |
|
|
if (fd == -1) |
81 |
|
|
ERROR(errno, "error opening medium"); |
82 |
|
|
|
83 |
|
|
/* lock descriptor against concurrent access */ |
84 |
|
|
|
85 |
|
|
lock.l_type = (mode == O_RDONLY) ? F_RDLCK : F_WRLCK; |
86 |
|
|
lock.l_start = 0; |
87 |
|
|
lock.l_whence = SEEK_SET; |
88 |
|
|
lock.l_len = 0; |
89 |
|
|
|
90 |
|
|
if (fcntl(fd, F_SETLK, &lock) == -1 && |
91 |
|
|
(errno == EACCES || errno == EAGAIN)) |
92 |
|
|
ERROR(EAGAIN, "unable to obtain lock for medium"); |
93 |
|
|
|
94 |
|
|
*priv = (void *) fd; |
95 |
|
|
|
96 |
|
|
return 0; |
97 |
|
|
|
98 |
|
|
fail: |
99 |
|
|
if (fd != -1) |
100 |
|
|
close(fd); |
101 |
|
|
|
102 |
|
|
return -1; |
103 |
|
|
} |
104 |
|
|
#endif |
105 |
|
|
|
106 |
|
|
int hfs_os_open(void **priv, const void *devicehandle, int mode) |
107 |
|
|
{ |
108 |
|
|
hfs_devicehandle_s *dh = (hfs_devicehandle_s *)devicehandle; |
109 |
|
|
*priv = dh; |
110 |
|
|
return 0; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
/* |
114 |
|
|
* NAME: os->close() |
115 |
|
|
* DESCRIPTION: close an open descriptor |
116 |
|
|
*/ |
117 |
|
|
int hfs_os_close(void **priv) |
118 |
|
|
{ |
119 |
|
|
#if 0 |
120 |
|
|
int fd = (int) *priv; |
121 |
|
|
|
122 |
|
|
*priv = (void *) -1; |
123 |
|
|
|
124 |
|
|
if (close(fd) == -1) |
125 |
|
|
ERROR(errno, "error closing medium"); |
126 |
|
|
|
127 |
|
|
return 0; |
128 |
|
|
|
129 |
|
|
fail: |
130 |
|
|
return -1; |
131 |
|
|
#endif |
132 |
|
|
return 0; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
/* |
136 |
|
|
* NAME: os->same() |
137 |
|
|
* DESCRIPTION: return 1 iff path is same as the open descriptor |
138 |
|
|
*/ |
139 |
|
|
#if 0 |
140 |
|
|
int os_same(void **priv, const char *path) |
141 |
|
|
{ |
142 |
|
|
int fd = (int) *priv; |
143 |
|
|
struct stat fdev, dev; |
144 |
|
|
|
145 |
|
|
if (fstat(fd, &fdev) == -1 || |
146 |
|
|
stat(path, &dev) == -1) |
147 |
|
|
ERROR(errno, "can't get path information"); |
148 |
|
|
|
149 |
|
|
return fdev.st_dev == dev.st_dev && |
150 |
|
|
fdev.st_ino == dev.st_ino; |
151 |
|
|
|
152 |
|
|
fail: |
153 |
|
|
return -1; |
154 |
|
|
} |
155 |
|
|
#endif |
156 |
|
|
|
157 |
|
|
int hfs_os_same(void **priv, const void *devicehandle) |
158 |
|
|
{ |
159 |
|
|
return 0; |
160 |
|
|
} |
161 |
|
|
|
162 |
|
|
/* |
163 |
|
|
* NAME: os->seek() |
164 |
|
|
* DESCRIPTION: set a descriptor's seek pointer (offset in blocks) |
165 |
|
|
*/ |
166 |
|
|
#if 0 |
167 |
|
|
unsigned long os_seek(void **priv, unsigned long offset) |
168 |
|
|
{ |
169 |
|
|
int fd = (int) *priv; |
170 |
|
|
off_t result; |
171 |
|
|
|
172 |
|
|
/* offset == -1 special; seek to last block of device */ |
173 |
|
|
|
174 |
|
|
if (offset == (unsigned long) -1) |
175 |
|
|
result = lseek(fd, 0, SEEK_END); |
176 |
|
|
else |
177 |
|
|
result = lseek(fd, offset << HFS_BLOCKSZ_BITS, SEEK_SET); |
178 |
|
|
|
179 |
|
|
if (result == -1) |
180 |
|
|
ERROR(errno, "error seeking medium"); |
181 |
|
|
|
182 |
|
|
return (unsigned long) result >> HFS_BLOCKSZ_BITS; |
183 |
|
|
|
184 |
|
|
fail: |
185 |
|
|
return -1; |
186 |
|
|
} |
187 |
|
|
#endif |
188 |
|
|
|
189 |
|
|
unsigned long hfs_os_seek(void **priv, unsigned long offset) |
190 |
|
|
{ |
191 |
|
|
hfs_devicehandle_s *dh = (hfs_devicehandle_s *)*priv; |
192 |
|
|
try { |
193 |
|
|
FileOfs s = dh->mStart + (offset << HFS_BLOCKSZ_BITS); |
194 |
|
|
dh->mDevice->seek(s); |
195 |
|
|
} catch (const IOException &x) { |
196 |
|
|
return (unsigned long)-1; |
197 |
|
|
} |
198 |
|
|
/* FileOfs t = dh->mDevice->tell();*/ |
199 |
|
|
long result = (dh->mDevice->tell()-dh->mStart) >> HFS_BLOCKSZ_BITS; |
200 |
|
|
return result; |
201 |
|
|
} |
202 |
|
|
|
203 |
|
|
/* |
204 |
|
|
* NAME: os->read() |
205 |
|
|
* DESCRIPTION: read blocks from an open descriptor |
206 |
|
|
*/ |
207 |
|
|
#if 0 |
208 |
|
|
unsigned long os_read(void **priv, void *buf, unsigned long len) |
209 |
|
|
{ |
210 |
|
|
int fd = (int) *priv; |
211 |
|
|
ssize_t result; |
212 |
|
|
|
213 |
|
|
result = read(fd, buf, len << HFS_BLOCKSZ_BITS); |
214 |
|
|
|
215 |
|
|
if (result == -1) |
216 |
|
|
ERROR(errno, "error reading from medium"); |
217 |
|
|
|
218 |
|
|
return (unsigned long) result >> HFS_BLOCKSZ_BITS; |
219 |
|
|
|
220 |
|
|
fail: |
221 |
|
|
return -1; |
222 |
|
|
} |
223 |
|
|
#endif |
224 |
|
|
|
225 |
|
|
unsigned long hfs_os_read(void **priv, void *buf, unsigned long len) |
226 |
|
|
{ |
227 |
|
|
hfs_devicehandle_s *dh = (hfs_devicehandle_s *)*priv; |
228 |
|
|
return dh->mDevice->read(buf, len << HFS_BLOCKSZ_BITS) >> HFS_BLOCKSZ_BITS; |
229 |
|
|
} |
230 |
|
|
|
231 |
|
|
/* |
232 |
|
|
* NAME: os->write() |
233 |
|
|
* DESCRIPTION: write blocks to an open descriptor |
234 |
|
|
*/ |
235 |
|
|
#if 0 |
236 |
|
|
unsigned long os_write(void **priv, const void *buf, unsigned long len) |
237 |
|
|
{ |
238 |
|
|
int fd = (int) *priv; |
239 |
|
|
ssize_t result; |
240 |
|
|
|
241 |
|
|
result = write(fd, buf, len << HFS_BLOCKSZ_BITS); |
242 |
|
|
|
243 |
|
|
if (result == -1) |
244 |
|
|
ERROR(errno, "error writing to medium"); |
245 |
|
|
|
246 |
|
|
return (unsigned long) result >> HFS_BLOCKSZ_BITS; |
247 |
|
|
|
248 |
|
|
fail: |
249 |
|
|
return -1; |
250 |
|
|
} |
251 |
|
|
#endif |
252 |
|
|
|
253 |
|
|
unsigned long hfs_os_write(void **priv, const void *buf, unsigned long len) |
254 |
|
|
{ |
255 |
|
|
hfs_devicehandle_s *dh = (hfs_devicehandle_s *)*priv; |
256 |
|
|
return dh->mDevice->write(buf, len << HFS_BLOCKSZ_BITS) >> HFS_BLOCKSZ_BITS; |
257 |
|
|
} |