1 |
|
/* |
2 |
|
* extfs.cpp - MacOS file system for native file system access |
3 |
|
* |
4 |
< |
* Basilisk II (C) 1997-2000 Christian Bauer |
4 |
> |
* Basilisk II (C) 1997-2005 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 |
48 |
|
#include <dirent.h> |
49 |
|
#endif |
50 |
|
|
51 |
+ |
#if defined __APPLE__ && defined __MACH__ |
52 |
+ |
#include <sys/attr.h> |
53 |
+ |
#endif |
54 |
+ |
|
55 |
|
#include "cpu_emulation.h" |
52 |
– |
#include "macos_util.h" |
56 |
|
#include "emul_op.h" |
57 |
|
#include "main.h" |
58 |
|
#include "disk.h" |
111 |
|
static struct stat root_stat; |
112 |
|
|
113 |
|
// File system ID/media type |
114 |
< |
const int16 MY_FSID = 'ba'; |
115 |
< |
const uint32 MY_MEDIA_TYPE = 'basi'; |
114 |
> |
const int16 MY_FSID = EMULATOR_ID_2; |
115 |
> |
const uint32 MY_MEDIA_TYPE = EMULATOR_ID_4; |
116 |
|
|
117 |
|
// CNID of root and root's parent |
118 |
|
const uint32 ROOT_ID = 2; |
158 |
|
uint32 id; // CNID of this file/dir |
159 |
|
uint32 parent_id; // CNID of parent file/dir |
160 |
|
FSItem *parent; // Pointer to parent |
161 |
< |
char name[32]; // Object name (C string) |
161 |
> |
char name[32]; // Object name (C string) - Host OS |
162 |
> |
char guest_name[32]; // Object name (C string) - Guest OS |
163 |
|
time_t mtime; // Modification time for get_cat_info caching |
164 |
|
int cache_dircount; // Cached number of files in directory |
165 |
|
}; |
170 |
|
|
171 |
|
|
172 |
|
/* |
173 |
+ |
* Get object creation time |
174 |
+ |
*/ |
175 |
+ |
|
176 |
+ |
#if defined __APPLE__ && defined __MACH__ |
177 |
+ |
struct crtimebuf { |
178 |
+ |
unsigned long length; |
179 |
+ |
struct timespec crtime; |
180 |
+ |
}; |
181 |
+ |
|
182 |
+ |
static uint32 do_get_creation_time(const char *path) |
183 |
+ |
{ |
184 |
+ |
struct attrlist attr; |
185 |
+ |
memset(&attr, 0, sizeof(attr)); |
186 |
+ |
attr.bitmapcount = ATTR_BIT_MAP_COUNT; |
187 |
+ |
attr.commonattr = ATTR_CMN_CRTIME; |
188 |
+ |
|
189 |
+ |
crtimebuf buf; |
190 |
+ |
if (getattrlist(path, &attr, &buf, sizeof(buf), FSOPT_NOFOLLOW) < 0) |
191 |
+ |
return 0; |
192 |
+ |
return TimeToMacTime(buf.crtime.tv_sec); |
193 |
+ |
} |
194 |
+ |
|
195 |
+ |
static uint32 get_creation_time(const char *path) |
196 |
+ |
{ |
197 |
+ |
if (path == NULL) |
198 |
+ |
return 0; |
199 |
+ |
if (path == RootPath) { |
200 |
+ |
static uint32 root_crtime = UINT_MAX; |
201 |
+ |
if (root_crtime == UINT_MAX) |
202 |
+ |
root_crtime = do_get_creation_time(path); |
203 |
+ |
return root_crtime; |
204 |
+ |
} |
205 |
+ |
return do_get_creation_time(path); |
206 |
+ |
} |
207 |
+ |
#endif |
208 |
+ |
|
209 |
+ |
|
210 |
+ |
/* |
211 |
|
* Find FSItem for given CNID |
212 |
|
*/ |
213 |
|
|
222 |
|
return NULL; |
223 |
|
} |
224 |
|
|
225 |
+ |
/* |
226 |
+ |
* Create FSItem with the given parameters |
227 |
+ |
*/ |
228 |
+ |
|
229 |
+ |
static FSItem *create_fsitem(const char *name, const char *guest_name, FSItem *parent) |
230 |
+ |
{ |
231 |
+ |
FSItem *p = new FSItem; |
232 |
+ |
last_fs_item->next = p; |
233 |
+ |
p->next = NULL; |
234 |
+ |
last_fs_item = p; |
235 |
+ |
p->id = next_cnid++; |
236 |
+ |
p->parent_id = parent->id; |
237 |
+ |
p->parent = parent; |
238 |
+ |
strncpy(p->name, name, 31); |
239 |
+ |
p->name[31] = 0; |
240 |
+ |
strncpy(p->guest_name, guest_name, 31); |
241 |
+ |
p->guest_name[31] = 0; |
242 |
+ |
p->mtime = 0; |
243 |
+ |
return p; |
244 |
+ |
} |
245 |
|
|
246 |
|
/* |
247 |
|
* Find FSItem for given name and parent, construct new FSItem if not found |
257 |
|
} |
258 |
|
|
259 |
|
// Not found, construct new FSItem |
260 |
< |
p = new FSItem; |
199 |
< |
last_fs_item->next = p; |
200 |
< |
p->next = NULL; |
201 |
< |
last_fs_item = p; |
202 |
< |
p->id = next_cnid++; |
203 |
< |
p->parent_id = parent->id; |
204 |
< |
p->parent = parent; |
205 |
< |
strncpy(p->name, name, 31); |
206 |
< |
p->name[31] = 0; |
207 |
< |
p->mtime = 0; |
208 |
< |
return p; |
260 |
> |
return create_fsitem(name, host_encoding_to_macroman(name), parent); |
261 |
|
} |
262 |
|
|
263 |
+ |
/* |
264 |
+ |
* Find FSItem for given guest_name and parent, construct new FSItem if not found |
265 |
+ |
*/ |
266 |
+ |
|
267 |
+ |
static FSItem *find_fsitem_guest(const char *guest_name, FSItem *parent) |
268 |
+ |
{ |
269 |
+ |
FSItem *p = first_fs_item; |
270 |
+ |
while (p) { |
271 |
+ |
if (p->parent == parent && !strcmp(p->guest_name, guest_name)) |
272 |
+ |
return p; |
273 |
+ |
p = p->next; |
274 |
+ |
} |
275 |
+ |
|
276 |
+ |
// Not found, construct new FSItem |
277 |
+ |
return create_fsitem(macroman_to_host_encoding(guest_name), guest_name, parent); |
278 |
+ |
} |
279 |
|
|
280 |
|
/* |
281 |
|
* Get full path (->full_path) for given FSItem |
345 |
|
} |
346 |
|
} |
347 |
|
|
280 |
– |
// Convert pascal string to C string |
281 |
– |
static void pstr2cstr(char *dst, const char *src) |
282 |
– |
{ |
283 |
– |
int size = *src++; |
284 |
– |
while (size--) { |
285 |
– |
char c = *src++; |
286 |
– |
// Note: we are converting Mac '/' characters to host ':' characters here |
287 |
– |
// '/' is not a path separator as this function is only used on object names |
288 |
– |
if (c == '/') |
289 |
– |
c = ':'; |
290 |
– |
*dst++ = c; |
291 |
– |
} |
292 |
– |
*dst = 0; |
293 |
– |
} |
294 |
– |
|
348 |
|
// Convert string (no length byte) to C string, length given separately |
349 |
|
static void strn2cstr(char *dst, const char *src, int size) |
350 |
|
{ |
417 |
|
p->parent_id = 0; |
418 |
|
p->parent = NULL; |
419 |
|
p->name[0] = 0; |
420 |
+ |
p->guest_name[0] = 0; |
421 |
|
|
422 |
|
// Create root FSItem |
423 |
|
p = new FSItem; |
429 |
|
p->parent = first_fs_item; |
430 |
|
strncpy(p->name, GetString(STR_EXTFS_VOLUME_NAME), 32); |
431 |
|
p->name[31] = 0; |
432 |
+ |
strncpy(p->guest_name, host_encoding_to_macroman(p->name), 32); |
433 |
+ |
p->guest_name[31] = 0; |
434 |
|
|
435 |
|
// Find path for root |
436 |
|
if ((RootPath = PrefsFindString("extfs")) != NULL) { |
479 |
|
// FSM present? |
480 |
|
r.d[0] = gestaltFSAttr; |
481 |
|
Execute68kTrap(0xa1ad, &r); // Gestalt() |
482 |
< |
D(bug("FSAttr %ld, %08lx\n", r.d[0], r.a[0])); |
482 |
> |
D(bug("FSAttr %d, %08x\n", r.d[0], r.a[0])); |
483 |
|
if ((r.d[0] & 0xffff) || !(r.a[0] & (1 << gestaltHasFileSystemManager))) { |
484 |
|
printf("WARNING: No FSM present, disabling ExtFS\n"); |
485 |
|
return; |
488 |
|
// Yes, version >=1.2? |
489 |
|
r.d[0] = gestaltFSMVersion; |
490 |
|
Execute68kTrap(0xa1ad, &r); // Gestalt() |
491 |
< |
D(bug("FSMVersion %ld, %08lx\n", r.d[0], r.a[0])); |
491 |
> |
D(bug("FSMVersion %d, %08x\n", r.d[0], r.a[0])); |
492 |
|
if ((r.d[0] & 0xffff) || (r.a[0] < 0x0120)) { |
493 |
|
printf("WARNING: FSM <1.2 found, disabling ExtFS\n"); |
494 |
|
return; |
758 |
|
} |
759 |
|
|
760 |
|
case ffsIDDiskMessage: { // Check if volume is handled by our FS |
761 |
< |
if (ReadMacInt16(paramBlock + ioVRefNum) == drive_number) |
761 |
> |
if ((int16)ReadMacInt16(paramBlock + ioVRefNum) == drive_number) |
762 |
|
return noErr; |
763 |
|
else |
764 |
|
return extFSErr; |
802 |
|
if (no_vol_name) |
803 |
|
WriteMacInt32(pb + ioNamePtr, name_ptr); |
804 |
|
int16 status = ReadMacInt16(fs_data + fsReturn); |
749 |
– |
int16 more_matches = ReadMacInt16(fs_data + fsReturn + 2); |
750 |
– |
int16 vRefNum = ReadMacInt16(fs_data + fsReturn + 4); |
751 |
– |
uint32 vcb = ReadMacInt32(fs_data + fsReturn + 6); |
805 |
|
D(bug(" UTDetermineVol() returned %d, status %d\n", r.d[0], status)); |
806 |
|
result = (int16)(r.d[0] & 0xffff); |
807 |
|
|
941 |
|
char name[32]; |
942 |
|
strn2cstr(name, (char *)Mac2HostAddr(ReadMacInt32(parseRec + ppNamePtr)) + ReadMacInt16(parseRec + ppStartOffset) + 1, ReadMacInt16(parseRec + ppComponentLength)); |
943 |
|
D(bug(" entering %s\n", name)); |
944 |
< |
p = find_fsitem(name, p); |
944 |
> |
p = find_fsitem_guest(name, p); |
945 |
|
current_dir = p->id; |
946 |
|
|
947 |
|
// startOffset = start of next component |
964 |
|
char name[32]; |
965 |
|
strn2cstr(name, (char *)Mac2HostAddr(ReadMacInt32(parseRec + ppNamePtr)) + ReadMacInt16(parseRec + ppStartOffset) + 1, ReadMacInt16(parseRec + ppComponentLength)); |
966 |
|
D(bug(" object is %s\n", name)); |
967 |
< |
item = find_fsitem(name, p); |
967 |
> |
item = find_fsitem_guest(name, p); |
968 |
|
} |
969 |
|
} |
970 |
|
} |
1019 |
|
static int16 fs_mount_vol(uint32 pb) |
1020 |
|
{ |
1021 |
|
D(bug(" fs_mount_vol(%08lx), vRefNum %d\n", pb, ReadMacInt16(pb + ioVRefNum))); |
1022 |
< |
if (ReadMacInt16(pb + ioVRefNum) == drive_number) |
1022 |
> |
if ((int16)ReadMacInt16(pb + ioVRefNum) == drive_number) |
1023 |
|
return noErr; |
1024 |
|
else |
1025 |
|
return extFSErr; |
1036 |
|
r.a[0] = fs_data + fsReturn; |
1037 |
|
r.a[1] = fs_data + fsReturn + 2; |
1038 |
|
Execute68k(fs_data + fsAllocateVCB, &r); |
1039 |
+ |
#if DEBUG |
1040 |
|
uint16 sysVCBLength = ReadMacInt16(fs_data + fsReturn); |
1041 |
+ |
#endif |
1042 |
|
uint32 vcb = ReadMacInt32(fs_data + fsReturn + 2); |
1043 |
|
D(bug(" UTAllocateVCB() returned %d, vcb %08lx, size %d\n", r.d[0], vcb, sysVCBLength)); |
1044 |
|
if (r.d[0] & 0xffff) |
1047 |
|
// Init VCB |
1048 |
|
WriteMacInt16(vcb + vcbSigWord, 0x4244); |
1049 |
|
#if defined(__BEOS__) || defined(WIN32) |
1050 |
< |
WriteMacInt32(vcb + vcbCrDate, root_stat.st_crtime + TIME_OFFSET); |
1050 |
> |
WriteMacInt32(vcb + vcbCrDate, TimeToMacTime(root_stat.st_crtime)); |
1051 |
> |
#elif defined __APPLE__ && defined __MACH__ |
1052 |
> |
WriteMacInt32(vcb + vcbCrDate, get_creation_time(RootPath)); |
1053 |
|
#else |
1054 |
|
WriteMacInt32(vcb + vcbCrDate, 0); |
1055 |
|
#endif |
1056 |
< |
WriteMacInt32(vcb + vcbLsMod, root_stat.st_mtime + TIME_OFFSET); |
1056 |
> |
WriteMacInt32(vcb + vcbLsMod, TimeToMacTime(root_stat.st_mtime)); |
1057 |
|
WriteMacInt32(vcb + vcbVolBkUp, 0); |
1058 |
|
WriteMacInt16(vcb + vcbNmFls, 1); //!! |
1059 |
|
WriteMacInt16(vcb + vcbNmRtDirs, 1); //!! |
1112 |
|
if (ReadMacInt32(pb + ioNamePtr)) |
1113 |
|
pstrcpy((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), VOLUME_NAME); |
1114 |
|
#if defined(__BEOS__) || defined(WIN32) |
1115 |
< |
WriteMacInt32(pb + ioVCrDate, root_stat.st_crtime + TIME_OFFSET); |
1115 |
> |
WriteMacInt32(pb + ioVCrDate, TimeToMacTime(root_stat.st_crtime)); |
1116 |
> |
#elif defined __APPLE__ && defined __MACH__ |
1117 |
> |
WriteMacInt32(pb + ioVCrDate, get_creation_time(RootPath)); |
1118 |
|
#else |
1119 |
|
WriteMacInt32(pb + ioVCrDate, 0); |
1120 |
|
#endif |
1121 |
< |
WriteMacInt32(pb + ioVLsMod, root_stat.st_mtime + TIME_OFFSET); |
1121 |
> |
WriteMacInt32(pb + ioVLsMod, TimeToMacTime(root_stat.st_mtime)); |
1122 |
|
WriteMacInt16(pb + ioVAtrb, 0); |
1123 |
|
WriteMacInt16(pb + ioVNmFls, 1); //!! |
1124 |
|
WriteMacInt16(pb + ioVBitMap, 0); |
1300 |
|
|
1301 |
|
// Fill in struct from fs_item and stats |
1302 |
|
if (ReadMacInt32(pb + ioNamePtr)) |
1303 |
< |
cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->name); |
1303 |
> |
cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->guest_name); |
1304 |
|
WriteMacInt16(pb + ioFRefNum, 0); |
1305 |
|
WriteMacInt8(pb + ioFlAttrib, access(full_path, W_OK) == 0 ? 0 : faLocked); |
1306 |
|
WriteMacInt32(pb + ioDirID, fs_item->id); |
1307 |
|
|
1308 |
|
#if defined(__BEOS__) || defined(WIN32) |
1309 |
< |
WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET); |
1309 |
> |
WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime)); |
1310 |
> |
#elif defined __APPLE__ && defined __MACH__ |
1311 |
> |
WriteMacInt32(pb + ioFlCrDat, get_creation_time(full_path)); |
1312 |
|
#else |
1313 |
|
WriteMacInt32(pb + ioFlCrDat, 0); |
1314 |
|
#endif |
1315 |
< |
WriteMacInt32(pb + ioFlMdDat, st.st_mtime + TIME_OFFSET); |
1315 |
> |
WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(st.st_mtime)); |
1316 |
|
|
1317 |
< |
get_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0); |
1317 |
> |
get_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false); |
1318 |
|
|
1319 |
|
WriteMacInt16(pb + ioFlStBlk, 0); |
1320 |
|
WriteMacInt32(pb + ioFlLgLen, st.st_size); |
1351 |
|
return fnfErr; |
1352 |
|
|
1353 |
|
// Set Finder info |
1354 |
< |
set_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0); |
1354 |
> |
set_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false); |
1355 |
|
|
1356 |
|
//!! times |
1357 |
|
return noErr; |
1423 |
|
|
1424 |
|
// Fill in struct from fs_item and stats |
1425 |
|
if (ReadMacInt32(pb + ioNamePtr)) |
1426 |
< |
cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->name); |
1426 |
> |
cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->guest_name); |
1427 |
|
WriteMacInt16(pb + ioFRefNum, 0); |
1428 |
|
WriteMacInt8(pb + ioFlAttrib, (S_ISDIR(st.st_mode) ? faIsDir : 0) | (access(full_path, W_OK) == 0 ? 0 : faLocked)); |
1429 |
|
WriteMacInt8(pb + ioACUser, 0); |
1430 |
|
WriteMacInt32(pb + ioDirID, fs_item->id); |
1431 |
|
WriteMacInt32(pb + ioFlParID, fs_item->parent_id); |
1432 |
|
#if defined(__BEOS__) || defined(WIN32) |
1433 |
< |
WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET); |
1433 |
> |
WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime)); |
1434 |
> |
#elif defined __APPLE__ && defined __MACH__ |
1435 |
> |
WriteMacInt32(pb + ioFlCrDat, get_creation_time(full_path)); |
1436 |
|
#else |
1437 |
|
WriteMacInt32(pb + ioFlCrDat, 0); |
1438 |
|
#endif |
1442 |
|
fs_item->mtime = mtime; |
1443 |
|
cached = false; |
1444 |
|
} |
1445 |
< |
WriteMacInt32(pb + ioFlMdDat, mtime + TIME_OFFSET); |
1445 |
> |
WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(mtime)); |
1446 |
|
WriteMacInt32(pb + ioFlBkDat, 0); |
1447 |
|
|
1448 |
< |
get_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo); |
1448 |
> |
get_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode)); |
1449 |
|
|
1450 |
|
if (S_ISDIR(st.st_mode)) { |
1451 |
|
|
1501 |
|
return errno2oserr(); |
1502 |
|
|
1503 |
|
// Set Finder info |
1504 |
< |
set_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo); |
1504 |
> |
set_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode)); |
1505 |
|
|
1506 |
|
//!! times |
1507 |
|
return noErr; |
1589 |
|
WriteMacInt32(fcb + fcbVPtr, vcb); |
1590 |
|
WriteMacInt32(fcb + fcbClmpSize, CLUMP_SIZE); |
1591 |
|
|
1592 |
< |
get_finfo(full_path, fs_data + fsPB, 0); |
1592 |
> |
get_finfo(full_path, fs_data + fsPB, 0, false); |
1593 |
|
WriteMacInt32(fcb + fcbFType, ReadMacInt32(fs_data + fsPB + fdType)); |
1594 |
|
|
1595 |
|
WriteMacInt32(fcb + fcbCatPos, fd); |
1596 |
|
WriteMacInt32(fcb + fcbDirID, fs_item->parent_id); |
1597 |
< |
cstr2pstr((char *)Mac2HostAddr(fcb + fcbCName), fs_item->name); |
1597 |
> |
cstr2pstr((char *)Mac2HostAddr(fcb + fcbCName), fs_item->guest_name); |
1598 |
|
return noErr; |
1599 |
|
} |
1600 |
|
|
1647 |
|
|
1648 |
|
// Find FCB by index |
1649 |
|
WriteMacInt16(pb + ioRefNum, 0); |
1650 |
< |
for (int i=0; i<ReadMacInt16(pb + ioFCBIndx); i++) { |
1650 |
> |
for (int i=0; i<(int)ReadMacInt16(pb + ioFCBIndx); i++) { |
1651 |
|
D(bug(" indexing FCBs\n")); |
1652 |
|
r.a[0] = vcb; |
1653 |
|
r.a[1] = pb + ioRefNum; |
1866 |
|
uint32 pos = lseek(fd, 0, SEEK_CUR); |
1867 |
|
WriteMacInt32(fcb + fcbCrPs, pos); |
1868 |
|
WriteMacInt32(pb + ioPosOffset, pos); |
1869 |
< |
if (actual != ReadMacInt32(pb + ioReqCount)) |
1869 |
> |
if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount)) |
1870 |
|
return actual < 0 ? read_err : eofErr; |
1871 |
|
else |
1872 |
|
return noErr; |
1919 |
|
uint32 pos = lseek(fd, 0, SEEK_CUR); |
1920 |
|
WriteMacInt32(fcb + fcbCrPs, pos); |
1921 |
|
WriteMacInt32(pb + ioPosOffset, pos); |
1922 |
< |
if (actual != ReadMacInt32(pb + ioReqCount)) |
1922 |
> |
if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount)) |
1923 |
|
return write_err; |
1924 |
|
else |
1925 |
|
return noErr; |