1 |
/* |
2 |
* libhfsp - library for reading and writing Macintosh HFS+ volumes |
3 |
* |
4 |
* Copyright (C) 2000-2001 Klaus Halfmann <klaus.halfmann@t-online.de> |
5 |
* Original Copyright (C) 1996-1998 Robert Leslie |
6 |
* Additional work in 2004 by Stefan Weyergraf for use in PearPC |
7 |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
9 |
* it under the terms of the GNU General Public License as published by |
10 |
* the Free Software Foundation; either version 2 of the License, or |
11 |
* (at your option) any later version. |
12 |
* |
13 |
* This program is distributed in the hope that it will be useful, |
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16 |
* GNU General Public License for more details. |
17 |
* |
18 |
* You should have received a copy of the GNU General Public License |
19 |
* along with this program; if not, write to the Free Software |
20 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
21 |
* |
22 |
*/ |
23 |
|
24 |
# ifdef HAVE_CONFIG_H |
25 |
# include "config.h" |
26 |
# endif |
27 |
|
28 |
#define _FILE_OFFSET_BITS 64 |
29 |
|
30 |
//# ifdef HAVE_FCNTL_H |
31 |
# if 1 |
32 |
# include <fcntl.h> |
33 |
# else |
34 |
int open(const char *, int, ...); |
35 |
int fcntl(int, int, ...); |
36 |
# endif |
37 |
|
38 |
//# ifdef HAVE_UNISTD_H |
39 |
# if 1 |
40 |
# include <unistd.h> |
41 |
# else |
42 |
int close(int); |
43 |
off_t lseek(int, off_t, int); |
44 |
size_t read(int, void *, size_t); |
45 |
size_t write(int, const char *, size_t); |
46 |
int stat(const char *, struct stat *); |
47 |
int fstat(int, struct stat *); |
48 |
# endif |
49 |
|
50 |
# include <stdio.h> |
51 |
|
52 |
# include <errno.h> |
53 |
# include <sys/stat.h> |
54 |
// need BLKGETSIZE ioctl call |
55 |
// # include <linux/fs.h> |
56 |
|
57 |
extern "C" { |
58 |
# include "libhfsp.h" |
59 |
# include "os.h" |
60 |
} |
61 |
#include "../hfsplusglue.h" |
62 |
#include "tools/except.h" |
63 |
|
64 |
/** Offset that is silently used by os_seek. |
65 |
* Nedded to transparaently support things like partitions */ |
66 |
/*APPLEUInt64 os_offset;*/ |
67 |
extern "C" uint64 hfsp_get_offset(void **priv) |
68 |
{ |
69 |
hfsplus_devicehandle_s *dh = (hfsplus_devicehandle_s *)*priv; |
70 |
return dh->mStart; |
71 |
} |
72 |
|
73 |
extern "C" void hfsp_set_offset(void **priv, uint64 ofs) |
74 |
{ |
75 |
hfsplus_devicehandle_s *dh = (hfsplus_devicehandle_s *)*priv; |
76 |
dh->mStart = ofs; |
77 |
} |
78 |
|
79 |
/* |
80 |
* NAME: os->open() |
81 |
* DESCRIPTION: open and lock a new descriptor from the given path and mode |
82 |
*/ |
83 |
#if 0 |
84 |
int os_open(void **priv, const char *path, int mode) |
85 |
{ |
86 |
int fd; |
87 |
struct flock lock; |
88 |
int c; |
89 |
|
90 |
switch (mode) |
91 |
{ |
92 |
case HFSP_MODE_RDONLY: |
93 |
mode = O_RDONLY; |
94 |
break; |
95 |
|
96 |
case HFSP_MODE_RDWR: |
97 |
default: |
98 |
fprintf(stderr,"*** Warning: You are about to open '%s' " |
99 |
"for writing ***\n", path); |
100 |
fprintf(stderr,"*** Do you really want to do that ? (y/n) ***\n"); |
101 |
do |
102 |
c = getchar(); |
103 |
while (c != 'y' && c != 'n'); |
104 |
if (c != 'y') |
105 |
exit(1); |
106 |
mode = O_RDWR; |
107 |
break; |
108 |
} |
109 |
|
110 |
|
111 |
fd = open(path, mode); |
112 |
if (fd == -1) |
113 |
HFSP_ERROR(errno, "error opening medium"); |
114 |
|
115 |
/* lock descriptor against concurrent access */ |
116 |
|
117 |
lock.l_type = (mode == O_RDONLY) ? F_RDLCK : F_WRLCK; |
118 |
lock.l_start = 0; |
119 |
lock.l_whence = SEEK_SET; |
120 |
lock.l_len = 0; |
121 |
|
122 |
if (fcntl(fd, F_SETLK, &lock) == -1 && |
123 |
(errno == EACCES || errno == EAGAIN)) |
124 |
HFSP_ERROR(EAGAIN, "unable to obtain lock for medium"); |
125 |
|
126 |
*priv = (void *) fd; |
127 |
|
128 |
return 0; |
129 |
|
130 |
fail: |
131 |
if (fd != -1) |
132 |
close(fd); |
133 |
|
134 |
return -1; |
135 |
} |
136 |
#endif |
137 |
extern "C" int hfsp_os_open(void **priv, const void *devicehandle, int mode) |
138 |
{ |
139 |
hfsplus_devicehandle_s *dh = (hfsplus_devicehandle_s *)devicehandle; |
140 |
*priv = dh; |
141 |
return 0; |
142 |
} |
143 |
|
144 |
/* |
145 |
* NAME: os->close() |
146 |
* DESCRIPTION: close an open descriptor |
147 |
*/ |
148 |
extern "C" int hfsp_os_close(void **priv) |
149 |
{ |
150 |
#if 0 |
151 |
int fd = (int) *priv; |
152 |
|
153 |
*priv = (void *) -1; |
154 |
|
155 |
if (close(fd) == -1) |
156 |
HFSP_ERROR(errno, "error closing medium"); |
157 |
|
158 |
return 0; |
159 |
|
160 |
fail: |
161 |
return -1; |
162 |
#endif |
163 |
return 0; |
164 |
} |
165 |
|
166 |
/* |
167 |
* NAME: os->same() |
168 |
* DESCRIPTION: return 1 iff path is same as the open descriptor |
169 |
*/ |
170 |
extern "C" int hfsp_os_same(void **priv, const char *path) |
171 |
{ |
172 |
#if 0 |
173 |
int fd = (int) *priv; |
174 |
struct stat fdev, dev; |
175 |
|
176 |
if (fstat(fd, &fdev) == -1 || |
177 |
stat(path, &dev) == -1) |
178 |
HFSP_ERROR(errno, "can't get path information"); |
179 |
|
180 |
return fdev.st_dev == dev.st_dev && |
181 |
fdev.st_ino == dev.st_ino; |
182 |
|
183 |
fail: |
184 |
return -1; |
185 |
#endif |
186 |
return 0; |
187 |
} |
188 |
|
189 |
/* |
190 |
* NAME: os->seek() |
191 |
* DESCRIPTION: set a descriptor's seek pointer (offset in blocks) and returns |
192 |
* this offset if everything went well |
193 |
*/ |
194 |
#if 0 |
195 |
unsigned long os_seek(void **priv, unsigned long offset, int blksize_bits) |
196 |
{ |
197 |
int fd = (int) *priv; |
198 |
off_t result, where = offset; |
199 |
|
200 |
where = os_offset + (where << blksize_bits); |
201 |
result = lseek(fd, where, SEEK_SET) - os_offset; |
202 |
|
203 |
result = result >> blksize_bits; |
204 |
return (unsigned long) result; |
205 |
} |
206 |
#endif |
207 |
|
208 |
extern "C" uint64 hfsp_os_seek(void **priv, uint64 offset, int blksize_bits) |
209 |
{ |
210 |
hfsplus_devicehandle_s *dh = (hfsplus_devicehandle_s *)*priv; |
211 |
try { |
212 |
FileOfs s = dh->mStart + (offset << blksize_bits); |
213 |
dh->mDevice->seek(s); |
214 |
} catch (const IOException &x) { |
215 |
return (uint64)-1; |
216 |
} |
217 |
// FileOfs t = dh->mDevice->tell(); |
218 |
uint64 result = (dh->mDevice->tell() - dh->mStart) >> blksize_bits; |
219 |
return result; |
220 |
} |
221 |
|
222 |
/* |
223 |
* NAME: os->read() |
224 |
* DESCRIPTION: read blocks from an open descriptor |
225 |
* |
226 |
* @param **priv is a pointer to the file descriptor |
227 |
* @param *buf buffer to put the content in |
228 |
* @param len number of buffers to read |
229 |
* @param blksize_bits blocksize as a power of 2 (e.g. 9 for 512) |
230 |
* |
231 |
* @return number of read blocks or -1 on error |
232 |
*/ |
233 |
#if 0 |
234 |
unsigned long os_read(void **priv, void *buf, unsigned long len, int blksize_bits) |
235 |
{ |
236 |
int fd = (int) *priv; |
237 |
size_t result= 0; |
238 |
int num= len << blksize_bits; |
239 |
|
240 |
while( result< num) { |
241 |
size_t size = read(fd, &((char *)buf)[ result], num- result); |
242 |
if (size <= 0) /* EOF == 0 should not happen, too */ |
243 |
HFSP_ERROR(errno, "error reading from medium"); |
244 |
result += size; |
245 |
} |
246 |
return (unsigned long) result >> blksize_bits; |
247 |
|
248 |
fail: |
249 |
return -1; |
250 |
} |
251 |
#endif |
252 |
|
253 |
extern "C" unsigned long hfsp_os_read(void **priv, void *buf, unsigned long len, int blksize_bits) |
254 |
{ |
255 |
hfsplus_devicehandle_s *dh = (hfsplus_devicehandle_s *)*priv; |
256 |
return dh->mDevice->read(buf, len << blksize_bits) >> blksize_bits; |
257 |
} |
258 |
|
259 |
/* |
260 |
* NAME: os->write() |
261 |
* DESCRIPTION: write blocks to an open descriptor |
262 |
*/ |
263 |
#if 0 |
264 |
unsigned long os_write(void **priv, const void *buf, unsigned long len, int blksize_bits) |
265 |
{ |
266 |
int fd = (int) *priv; |
267 |
size_t result; |
268 |
|
269 |
result = write(fd, buf, len << blksize_bits); |
270 |
|
271 |
if (result == -1) |
272 |
HFSP_ERROR(errno, "error writing to medium"); |
273 |
|
274 |
return (unsigned long) result >> blksize_bits; |
275 |
|
276 |
fail: |
277 |
return -1; |
278 |
} |
279 |
#endif |
280 |
|
281 |
extern "C" unsigned long hfsp_os_write(void **priv, const void *buf, unsigned long len, int blksize_bits) |
282 |
{ |
283 |
hfsplus_devicehandle_s *dh = (hfsplus_devicehandle_s *)*priv; |
284 |
return dh->mDevice->write(buf, len << blksize_bits) >> blksize_bits; |
285 |
} |