1 |
|
/* |
2 |
|
* extfs_amiga.cpp - MacOS file system for access native file system access, AmigaOS specific stuff |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-1999 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2001 Christian Bauer |
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 |
19 |
|
*/ |
20 |
|
|
21 |
|
#include <exec/types.h> |
22 |
+ |
#define __USE_SYSBASE |
23 |
|
#include <proto/dos.h> |
24 |
+ |
#include <inline/dos.h> |
25 |
|
|
26 |
|
#include <sys/types.h> |
27 |
|
#include <sys/stat.h> |
79 |
|
* /path/.finf/file |
80 |
|
* Resource fork: |
81 |
|
* /path/.rsrc/file |
82 |
+ |
* |
83 |
+ |
* The .finf files store a FInfo/DInfo, followed by a FXInfo/DXInfo |
84 |
+ |
* (16+16 bytes) |
85 |
|
*/ |
86 |
|
|
82 |
– |
// Layout of Finder info helper files (all fields big-endian) |
83 |
– |
struct finf_struct { |
84 |
– |
uint32 type; |
85 |
– |
uint32 creator; |
86 |
– |
uint16 flags; |
87 |
– |
uint16 pad0; |
88 |
– |
}; |
89 |
– |
|
87 |
|
static void make_helper_path(const char *src, char *dest, const char *add, bool only_dir = false) |
88 |
|
{ |
89 |
|
dest[0] = 0; |
107 |
|
{ |
108 |
|
char helper_dir[MAX_PATH_LENGTH]; |
109 |
|
make_helper_path(path, helper_dir, add, true); |
110 |
+ |
if (helper_dir[strlen(helper_dir) - 1] == '/') // Remove trailing "/" |
111 |
+ |
helper_dir[strlen(helper_dir) - 1] = 0; |
112 |
|
return mkdir(helper_dir, 0777); |
113 |
|
} |
114 |
|
|
117 |
|
char helper_path[MAX_PATH_LENGTH]; |
118 |
|
make_helper_path(path, helper_path, add); |
119 |
|
|
120 |
< |
if ((flag & O_RDWR) || (flag && O_WRONLY)) |
120 |
> |
if ((flag & O_ACCMODE) == O_RDWR || (flag & O_ACCMODE) == O_WRONLY) |
121 |
|
flag |= O_CREAT; |
122 |
|
int fd = open(helper_path, flag, 0666); |
123 |
|
if (fd < 0) { |
155 |
|
}; |
156 |
|
|
157 |
|
static const ext2type e2t_translation[] = { |
158 |
< |
{".Z", 'ZIVM', 'LZIV'}, |
159 |
< |
{".gz", 'Gzip', 'Gzip'}, |
160 |
< |
{".hqx", 'TEXT', 'SITx'}, |
161 |
< |
{".pdf", 'PDF ', 'CARO'}, |
162 |
< |
{".ps", 'TEXT', 'ttxt'}, |
163 |
< |
{".sit", 'SIT!', 'SITx'}, |
164 |
< |
{".tar", 'TARF', 'TAR '}, |
165 |
< |
{".uu", 'TEXT', 'SITx'}, |
166 |
< |
{".uue", 'TEXT', 'SITx'}, |
167 |
< |
{".zip", 'ZIP ', 'ZIP '}, |
168 |
< |
{".8svx", '8SVX', 'SNDM'}, |
169 |
< |
{".aifc", 'AIFC', 'TVOD'}, |
170 |
< |
{".aiff", 'AIFF', 'TVOD'}, |
171 |
< |
{".au", 'ULAW', 'TVOD'}, |
172 |
< |
{".mid", 'MIDI', 'TVOD'}, |
173 |
< |
{".midi", 'MIDI', 'TVOD'}, |
174 |
< |
{".mp2", 'MPG ', 'TVOD'}, |
175 |
< |
{".mp3", 'MPG ', 'TVOD'}, |
176 |
< |
{".wav", 'WAVE', 'TVOD'}, |
177 |
< |
{".bmp", 'BMPf', 'ogle'}, |
178 |
< |
{".gif", 'GIFf', 'ogle'}, |
179 |
< |
{".lbm", 'ILBM', 'GKON'}, |
180 |
< |
{".ilbm", 'ILBM', 'GKON'}, |
181 |
< |
{".jpg", 'JPEG', 'ogle'}, |
182 |
< |
{".jpeg", 'JPEG', 'ogle'}, |
183 |
< |
{".pict", 'PICT', 'ogle'}, |
184 |
< |
{".png", 'PNGf', 'ogle'}, |
185 |
< |
{".sgi", '.SGI', 'ogle'}, |
186 |
< |
{".tga", 'TPIC', 'ogle'}, |
187 |
< |
{".tif", 'TIFF', 'ogle'}, |
188 |
< |
{".tiff", 'TIFF', 'ogle'}, |
189 |
< |
{".html", 'TEXT', 'MOSS'}, |
190 |
< |
{".txt", 'TEXT', 'ttxt'}, |
191 |
< |
{".rtf", 'TEXT', 'MSWD'}, |
192 |
< |
{".c", 'TEXT', 'R*ch'}, |
193 |
< |
{".C", 'TEXT', 'R*ch'}, |
194 |
< |
{".cc", 'TEXT', 'R*ch'}, |
195 |
< |
{".cpp", 'TEXT', 'R*ch'}, |
196 |
< |
{".cxx", 'TEXT', 'R*ch'}, |
197 |
< |
{".h", 'TEXT', 'R*ch'}, |
198 |
< |
{".hh", 'TEXT', 'R*ch'}, |
199 |
< |
{".hpp", 'TEXT', 'R*ch'}, |
200 |
< |
{".hxx", 'TEXT', 'R*ch'}, |
201 |
< |
{".s", 'TEXT', 'R*ch'}, |
202 |
< |
{".S", 'TEXT', 'R*ch'}, |
203 |
< |
{".i", 'TEXT', 'R*ch'}, |
204 |
< |
{".mpg", 'MPEG', 'TVOD'}, |
205 |
< |
{".mpeg", 'MPEG', 'TVOD'}, |
206 |
< |
{".mov", 'MooV', 'TVOD'}, |
207 |
< |
{".fli", 'FLI ', 'TVOD'}, |
208 |
< |
{".avi", 'VfW ', 'TVOD'}, |
158 |
> |
{".z", FOURCC('Z','I','V','M'), FOURCC('L','Z','I','V')}, |
159 |
> |
{".gz", FOURCC('G','z','i','p'), FOURCC('G','z','i','p')}, |
160 |
> |
{".hqx", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')}, |
161 |
> |
{".bin", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')}, |
162 |
> |
{".pdf", FOURCC('P','D','F',' '), FOURCC('C','A','R','O')}, |
163 |
> |
{".ps", FOURCC('T','E','X','T'), FOURCC('t','t','x','t')}, |
164 |
> |
{".sit", FOURCC('S','I','T','!'), FOURCC('S','I','T','x')}, |
165 |
> |
{".tar", FOURCC('T','A','R','F'), FOURCC('T','A','R',' ')}, |
166 |
> |
{".uu", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')}, |
167 |
> |
{".uue", FOURCC('T','E','X','T'), FOURCC('S','I','T','x')}, |
168 |
> |
{".zip", FOURCC('Z','I','P',' '), FOURCC('Z','I','P',' ')}, |
169 |
> |
{".8svx", FOURCC('8','S','V','X'), FOURCC('S','N','D','M')}, |
170 |
> |
{".aifc", FOURCC('A','I','F','C'), FOURCC('T','V','O','D')}, |
171 |
> |
{".aiff", FOURCC('A','I','F','F'), FOURCC('T','V','O','D')}, |
172 |
> |
{".au", FOURCC('U','L','A','W'), FOURCC('T','V','O','D')}, |
173 |
> |
{".mid", FOURCC('M','I','D','I'), FOURCC('T','V','O','D')}, |
174 |
> |
{".midi", FOURCC('M','I','D','I'), FOURCC('T','V','O','D')}, |
175 |
> |
{".mp2", FOURCC('M','P','G',' '), FOURCC('T','V','O','D')}, |
176 |
> |
{".mp3", FOURCC('M','P','G',' '), FOURCC('T','V','O','D')}, |
177 |
> |
{".wav", FOURCC('W','A','V','E'), FOURCC('T','V','O','D')}, |
178 |
> |
{".bmp", FOURCC('B','M','P','f'), FOURCC('o','g','l','e')}, |
179 |
> |
{".gif", FOURCC('G','I','F','f'), FOURCC('o','g','l','e')}, |
180 |
> |
{".lbm", FOURCC('I','L','B','M'), FOURCC('G','K','O','N')}, |
181 |
> |
{".ilbm", FOURCC('I','L','B','M'), FOURCC('G','K','O','N')}, |
182 |
> |
{".jpg", FOURCC('J','P','E','G'), FOURCC('o','g','l','e')}, |
183 |
> |
{".jpeg", FOURCC('J','P','E','G'), FOURCC('o','g','l','e')}, |
184 |
> |
{".pict", FOURCC('P','I','C','T'), FOURCC('o','g','l','e')}, |
185 |
> |
{".png", FOURCC('P','N','G','f'), FOURCC('o','g','l','e')}, |
186 |
> |
{".sgi", FOURCC('.','S','G','I'), FOURCC('o','g','l','e')}, |
187 |
> |
{".tga", FOURCC('T','P','I','C'), FOURCC('o','g','l','e')}, |
188 |
> |
{".tif", FOURCC('T','I','F','F'), FOURCC('o','g','l','e')}, |
189 |
> |
{".tiff", FOURCC('T','I','F','F'), FOURCC('o','g','l','e')}, |
190 |
> |
{".htm", FOURCC('T','E','X','T'), FOURCC('M','O','S','S')}, |
191 |
> |
{".html", FOURCC('T','E','X','T'), FOURCC('M','O','S','S')}, |
192 |
> |
{".txt", FOURCC('T','E','X','T'), FOURCC('t','t','x','t')}, |
193 |
> |
{".rtf", FOURCC('T','E','X','T'), FOURCC('M','S','W','D')}, |
194 |
> |
{".c", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
195 |
> |
{".cc", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
196 |
> |
{".cpp", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
197 |
> |
{".cxx", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
198 |
> |
{".h", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
199 |
> |
{".hh", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
200 |
> |
{".hpp", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
201 |
> |
{".hxx", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
202 |
> |
{".s", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
203 |
> |
{".i", FOURCC('T','E','X','T'), FOURCC('R','*','c','h')}, |
204 |
> |
{".mpg", FOURCC('M','P','E','G'), FOURCC('T','V','O','D')}, |
205 |
> |
{".mpeg", FOURCC('M','P','E','G'), FOURCC('T','V','O','D')}, |
206 |
> |
{".mov", FOURCC('M','o','o','V'), FOURCC('T','V','O','D')}, |
207 |
> |
{".fli", FOURCC('F','L','I',' '), FOURCC('T','V','O','D')}, |
208 |
> |
{".avi", FOURCC('V','f','W',' '), FOURCC('T','V','O','D')}, |
209 |
> |
{".qxd", FOURCC('X','D','O','C'), FOURCC('X','P','R','3')}, |
210 |
> |
{".hfv", FOURCC('D','D','i','m'), FOURCC('d','d','s','k')}, |
211 |
> |
{".dsk", FOURCC('D','D','i','m'), FOURCC('d','d','s','k')}, |
212 |
> |
{".img", FOURCC('r','o','h','d'), FOURCC('d','d','s','k')}, |
213 |
|
{NULL, 0, 0} // End marker |
214 |
|
}; |
215 |
|
|
216 |
< |
void get_finder_type(const char *path, uint32 &type, uint32 &creator) |
216 |
> |
void get_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir) |
217 |
|
{ |
218 |
< |
type = 0; |
219 |
< |
creator = 0; |
218 |
> |
// Set default finder info |
219 |
> |
Mac_memset(finfo, 0, SIZEOF_FInfo); |
220 |
> |
if (fxinfo) |
221 |
> |
Mac_memset(fxinfo, 0, SIZEOF_FXInfo); |
222 |
> |
WriteMacInt16(finfo + fdFlags, DEFAULT_FINDER_FLAGS); |
223 |
> |
WriteMacInt32(finfo + fdLocation, (uint32)-1); |
224 |
|
|
225 |
< |
// Open Finder info file |
225 |
> |
// Read Finder info file |
226 |
|
int fd = open_finf(path, O_RDONLY); |
227 |
|
if (fd >= 0) { |
228 |
< |
|
229 |
< |
// Read file |
230 |
< |
finf_struct finf; |
224 |
< |
if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) { |
225 |
< |
|
226 |
< |
// Type/creator are in Finder info file, return them |
227 |
< |
type = ntohl(finf.type); |
228 |
< |
creator = ntohl(finf.creator); |
229 |
< |
close(fd); |
230 |
< |
return; |
231 |
< |
} |
228 |
> |
ssize_t actual = read(fd, Mac2HostAddr(finfo), SIZEOF_FInfo); |
229 |
> |
if (fxinfo) |
230 |
> |
actual += read(fd, Mac2HostAddr(fxinfo), SIZEOF_FXInfo); |
231 |
|
close(fd); |
232 |
+ |
if (actual >= SIZEOF_FInfo) |
233 |
+ |
return; |
234 |
|
} |
235 |
|
|
236 |
|
// No Finder info file, translate file name extension to MacOS type/creator |
237 |
< |
int path_len = strlen(path); |
238 |
< |
for (int i=0; e2t_translation[i].ext; i++) { |
239 |
< |
int ext_len = strlen(e2t_translation[i].ext); |
240 |
< |
if (path_len < ext_len) |
241 |
< |
continue; |
242 |
< |
if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) { |
243 |
< |
type = e2t_translation[i].type; |
244 |
< |
creator = e2t_translation[i].creator; |
245 |
< |
break; |
237 |
> |
if (!is_dir) { |
238 |
> |
int path_len = strlen(path); |
239 |
> |
for (int i=0; e2t_translation[i].ext; i++) { |
240 |
> |
int ext_len = strlen(e2t_translation[i].ext); |
241 |
> |
if (path_len < ext_len) |
242 |
> |
continue; |
243 |
> |
if (!strcasecmp(path + path_len - ext_len, e2t_translation[i].ext)) { |
244 |
> |
WriteMacInt32(finfo + fdType, e2t_translation[i].type); |
245 |
> |
WriteMacInt32(finfo + fdCreator, e2t_translation[i].creator); |
246 |
> |
break; |
247 |
> |
} |
248 |
|
} |
249 |
|
} |
250 |
|
} |
251 |
|
|
252 |
< |
void set_finder_type(const char *path, uint32 type, uint32 creator) |
252 |
> |
void set_finfo(const char *path, uint32 finfo, uint32 fxinfo, bool is_dir) |
253 |
|
{ |
254 |
|
// Open Finder info file |
255 |
|
int fd = open_finf(path, O_RDWR); |
256 |
|
if (fd < 0) |
257 |
|
return; |
258 |
|
|
259 |
< |
// Read file |
260 |
< |
finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS, 0}; |
261 |
< |
read(fd, &finf, sizeof(finf_struct)); |
262 |
< |
|
260 |
< |
// Set Finder flags |
261 |
< |
finf.type = htonl(type); |
262 |
< |
finf.creator = htonl(creator); |
263 |
< |
|
264 |
< |
// Update file |
265 |
< |
lseek(fd, 0, SEEK_SET); |
266 |
< |
write(fd, &finf, sizeof(finf_struct)); |
267 |
< |
close(fd); |
268 |
< |
} |
269 |
< |
|
270 |
< |
|
271 |
< |
/* |
272 |
< |
* Get/set finder flags for file/dir specified by full path |
273 |
< |
*/ |
274 |
< |
|
275 |
< |
void get_finder_flags(const char *path, uint16 &flags) |
276 |
< |
{ |
277 |
< |
flags = DEFAULT_FINDER_FLAGS; // Default |
278 |
< |
|
279 |
< |
// Open Finder info file |
280 |
< |
int fd = open_finf(path, O_RDONLY); |
281 |
< |
if (fd < 0) |
282 |
< |
return; |
283 |
< |
|
284 |
< |
// Read Finder flags |
285 |
< |
finf_struct finf; |
286 |
< |
if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) |
287 |
< |
flags = ntohs(finf.flags); |
288 |
< |
|
289 |
< |
// Close file |
290 |
< |
close(fd); |
291 |
< |
} |
292 |
< |
|
293 |
< |
void set_finder_flags(const char *path, uint16 flags) |
294 |
< |
{ |
295 |
< |
// Open Finder info file |
296 |
< |
int fd = open_finf(path, O_RDWR); |
297 |
< |
if (fd < 0) |
298 |
< |
return; |
299 |
< |
|
300 |
< |
// Read file |
301 |
< |
finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS, 0}; |
302 |
< |
read(fd, &finf, sizeof(finf_struct)); |
303 |
< |
|
304 |
< |
// Set Finder flags |
305 |
< |
finf.flags = htons(flags); |
306 |
< |
|
307 |
< |
// Update file |
308 |
< |
lseek(fd, 0, SEEK_SET); |
309 |
< |
write(fd, &finf, sizeof(finf_struct)); |
259 |
> |
// Write file |
260 |
> |
write(fd, Mac2HostAddr(finfo), SIZEOF_FInfo); |
261 |
> |
if (fxinfo) |
262 |
> |
write(fd, Mac2HostAddr(fxinfo), SIZEOF_FXInfo); |
263 |
|
close(fd); |
264 |
|
} |
265 |
|
|
296 |
|
|
297 |
|
/* |
298 |
|
* Read "length" bytes from file to "buffer", |
299 |
< |
* returns number of bytes read (or 0) |
299 |
> |
* returns number of bytes read (or -1 on error) |
300 |
|
*/ |
301 |
|
|
302 |
< |
size_t extfs_read(int fd, void *buffer, size_t length) |
302 |
> |
ssize_t extfs_read(int fd, void *buffer, size_t length) |
303 |
|
{ |
351 |
– |
errno = 0; |
304 |
|
return read(fd, buffer, length); |
305 |
|
} |
306 |
|
|
307 |
|
|
308 |
|
/* |
309 |
|
* Write "length" bytes from "buffer" to file, |
310 |
< |
* returns number of bytes written (or 0) |
310 |
> |
* returns number of bytes written (or -1 on error) |
311 |
|
*/ |
312 |
|
|
313 |
< |
size_t extfs_write(int fd, void *buffer, size_t length) |
313 |
> |
ssize_t extfs_write(int fd, void *buffer, size_t length) |
314 |
|
{ |
363 |
– |
errno = 0; |
315 |
|
return write(fd, buffer, length); |
316 |
|
} |
317 |
|
|
318 |
|
|
319 |
+ |
/* |
320 |
+ |
* Remove file/directory (and associated helper files), |
321 |
+ |
* returns false on error (and sets errno) |
322 |
+ |
*/ |
323 |
+ |
|
324 |
+ |
bool extfs_remove(const char *path) |
325 |
+ |
{ |
326 |
+ |
// Remove helpers first, don't complain if this fails |
327 |
+ |
char helper_path[MAX_PATH_LENGTH]; |
328 |
+ |
make_helper_path(path, helper_path, ".finf/", false); |
329 |
+ |
remove(helper_path); |
330 |
+ |
make_helper_path(path, helper_path, ".rsrc/", false); |
331 |
+ |
remove(helper_path); |
332 |
+ |
|
333 |
+ |
// Now remove file or directory (and helper directories in the directory) |
334 |
+ |
if (remove(path) < 0) { |
335 |
+ |
if (errno == EISDIR || errno == ENOTEMPTY) { |
336 |
+ |
helper_path[0] = 0; |
337 |
+ |
strncpy(helper_path, path, MAX_PATH_LENGTH-1); |
338 |
+ |
add_path_component(helper_path, ".finf"); |
339 |
+ |
rmdir(helper_path); |
340 |
+ |
helper_path[0] = 0; |
341 |
+ |
strncpy(helper_path, path, MAX_PATH_LENGTH-1); |
342 |
+ |
add_path_component(helper_path, ".rsrc"); |
343 |
+ |
rmdir(helper_path); |
344 |
+ |
return rmdir(path) == 0; |
345 |
+ |
} else |
346 |
+ |
return false; |
347 |
+ |
} |
348 |
+ |
return true; |
349 |
+ |
} |
350 |
+ |
|
351 |
+ |
|
352 |
+ |
/* |
353 |
+ |
* Rename/move file/directory (and associated helper files), |
354 |
+ |
* returns false on error (and sets errno) |
355 |
+ |
*/ |
356 |
+ |
|
357 |
+ |
bool extfs_rename(const char *old_path, const char *new_path) |
358 |
+ |
{ |
359 |
+ |
// Rename helpers first, don't complain if this fails |
360 |
+ |
char old_helper_path[MAX_PATH_LENGTH], new_helper_path[MAX_PATH_LENGTH]; |
361 |
+ |
make_helper_path(old_path, old_helper_path, ".finf/", false); |
362 |
+ |
make_helper_path(new_path, new_helper_path, ".finf/", false); |
363 |
+ |
create_helper_dir(new_path, ".finf/"); |
364 |
+ |
rename(old_helper_path, new_helper_path); |
365 |
+ |
make_helper_path(old_path, old_helper_path, ".rsrc/", false); |
366 |
+ |
make_helper_path(new_path, new_helper_path, ".rsrc/", false); |
367 |
+ |
create_helper_dir(new_path, ".rsrc/"); |
368 |
+ |
rename(old_helper_path, new_helper_path); |
369 |
+ |
|
370 |
+ |
// Now rename file |
371 |
+ |
return rename(old_path, new_path) == 0; |
372 |
+ |
} |
373 |
+ |
|
374 |
+ |
|
375 |
|
/* |
376 |
|
* ftruncate() is missing from libnix |
377 |
|
*/ |