--- BasiliskII/src/extfs.cpp 1999/10/19 21:33:56 1.2 +++ BasiliskII/src/extfs.cpp 2007/01/22 17:14:06 1.32 @@ -1,7 +1,7 @@ /* - * extfs.cpp - MacOS file system for access native file system access + * extfs.cpp - MacOS file system for native file system access * - * Basilisk II (C) 1997-1999 Christian Bauer + * Basilisk II (C) 1997-2005 Christian Bauer * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,16 +19,19 @@ */ /* -TODO: -LockRng -UnlockRng -(CatSearch) -(MakeFSSpec) -(GetVolMountInfoSize) -(GetVolMountInfo) -(GetForeignPrivs) -(SetForeignPrivs) -*/ + * SEE ALSO + * Guide to the File System Manager (from FSM 1.2 SDK) + * + * TODO + * LockRng + * UnlockRng + * (CatSearch) + * (MakeFSSpec) + * (GetVolMountInfoSize) + * (GetVolMountInfo) + * (GetForeignPrivs) + * (SetForeignPrivs) + */ #include "sysdeps.h" @@ -37,13 +40,19 @@ UnlockRng #include #include #include -#include #include -#include #include +#ifndef WIN32 +#include +#include +#endif + +#if defined __APPLE__ && defined __MACH__ +#include +#endif + #include "cpu_emulation.h" -#include "macos_util.h" #include "emul_op.h" #include "main.h" #include "disk.h" @@ -52,6 +61,10 @@ UnlockRng #include "extfs.h" #include "extfs_defs.h" +#ifdef WIN32 +# include "posix_emu.h" +#endif + #define DEBUG 0 #include "debug.h" @@ -62,14 +75,14 @@ enum { fsHFSProcStub = 6, fsDrvStatus = 12, // Drive Status record fsFSD = 42, // File system descriptor - fsPB = 238, // IOParam (for mounting) + fsPB = 238, // IOParam (for mounting and renaming), also used for temporary storage fsVMI = 288, // VoumeMountInfoHeader (for mounting) fsParseRec = 296, // ParsePathRec struct fsReturn = 306, // Area for return data of 68k routines fsAllocateVCB = 562, // UTAllocateVCB(uint16 *sysVCBLength{a0}, uint32 *vcb{a1}) fsAddNewVCB = 578, // UTAddNewVCB(int drive_number{d0}, int16 *vRefNum{a1}, uint32 vcb{a1}) fsDetermineVol = 594, // UTDetermineVol(uint32 pb{a0}, int16 *status{a1}, int16 *more_matches{a2}, int16 *vRefNum{a3}, uint32 *vcb{a4}) - fsResolveWDCB = 614, // UTResolveWDCB(int16 vRefNum{d0}, uint32 *wdcb{a0}) + fsResolveWDCB = 614, // UTResolveWDCB(uint32 procID{d0}, int16 index{d1}, int16 vRefNum{d0}, uint32 *wdcb{a0}) fsGetDefaultVol = 632, // UTGetDefaultVol(uint32 wdpb{a0}) fsGetPathComponentName = 644, // UTGetPathComponentName(uint32 rec{a0}) fsParsePathname = 656, // UTParsePathname(uint32 *start{a0}, uint32 name{a1}) @@ -98,8 +111,8 @@ static bool ready = false; static struct stat root_stat; // File system ID/media type -const int16 MY_FSID = 'ba'; -const uint32 MY_MEDIA_TYPE = 'basi'; +const int16 MY_FSID = EMULATOR_ID_2; +const uint32 MY_MEDIA_TYPE = EMULATOR_ID_4; // CNID of root and root's parent const uint32 ROOT_ID = 2; @@ -108,6 +121,11 @@ const uint32 ROOT_PARENT_ID = 1; // File system stack size const int STACK_SIZE = 0x10000; +// Allocation block and clump size as reported to MacOS (these are of course +// not the real values and have no meaning on the host OS) +const int AL_BLK_SIZE = 0x4000; +const int CLUMP_SIZE = 0x4000; + // Drive number of our pseudo-drive static int drive_number; @@ -140,7 +158,8 @@ struct FSItem { uint32 id; // CNID of this file/dir uint32 parent_id; // CNID of parent file/dir FSItem *parent; // Pointer to parent - char name[32]; // Object name (C string) + char name[32]; // Object name (C string) - Host OS + char guest_name[32]; // Object name (C string) - Guest OS time_t mtime; // Modification time for get_cat_info caching int cache_dircount; // Cached number of files in directory }; @@ -151,6 +170,44 @@ static uint32 next_cnid = fsUsrCNID; // /* + * Get object creation time + */ + +#if defined __APPLE__ && defined __MACH__ +struct crtimebuf { + unsigned long length; + struct timespec crtime; +}; + +static uint32 do_get_creation_time(const char *path) +{ + struct attrlist attr; + memset(&attr, 0, sizeof(attr)); + attr.bitmapcount = ATTR_BIT_MAP_COUNT; + attr.commonattr = ATTR_CMN_CRTIME; + + crtimebuf buf; + if (getattrlist(path, &attr, &buf, sizeof(buf), FSOPT_NOFOLLOW) < 0) + return 0; + return TimeToMacTime(buf.crtime.tv_sec); +} + +static uint32 get_creation_time(const char *path) +{ + if (path == NULL) + return 0; + if (path == RootPath) { + static uint32 root_crtime = UINT_MAX; + if (root_crtime == UINT_MAX) + root_crtime = do_get_creation_time(path); + return root_crtime; + } + return do_get_creation_time(path); +} +#endif + + +/* * Find FSItem for given CNID */ @@ -165,6 +222,26 @@ static FSItem *find_fsitem_by_id(uint32 return NULL; } +/* + * Create FSItem with the given parameters + */ + +static FSItem *create_fsitem(const char *name, const char *guest_name, FSItem *parent) +{ + FSItem *p = new FSItem; + last_fs_item->next = p; + p->next = NULL; + last_fs_item = p; + p->id = next_cnid++; + p->parent_id = parent->id; + p->parent = parent; + strncpy(p->name, name, 31); + p->name[31] = 0; + strncpy(p->guest_name, guest_name, 31); + p->guest_name[31] = 0; + p->mtime = 0; + return p; +} /* * Find FSItem for given name and parent, construct new FSItem if not found @@ -180,45 +257,64 @@ static FSItem *find_fsitem(const char *n } // Not found, construct new FSItem - p = new FSItem; - last_fs_item->next = p; - p->next = NULL; - last_fs_item = p; - p->id = next_cnid++; - p->parent_id = parent->id; - p->parent = parent; - strncpy(p->name, name, 31); - p->name[31] = 0; - p->mtime = 0; - return p; + return create_fsitem(name, host_encoding_to_macroman(name), parent); } +/* + * Find FSItem for given guest_name and parent, construct new FSItem if not found + */ + +static FSItem *find_fsitem_guest(const char *guest_name, FSItem *parent) +{ + FSItem *p = first_fs_item; + while (p) { + if (p->parent == parent && !strcmp(p->guest_name, guest_name)) + return p; + p = p->next; + } + + // Not found, construct new FSItem + return create_fsitem(guest_name, guest_name, parent); +} /* * Get full path (->full_path) for given FSItem */ -const int MAX_PATH_LENGTH = 1024; static char full_path[MAX_PATH_LENGTH]; -static void add_path_component(const char *s) +static void add_path_comp(const char *s) { - int l = strlen(full_path); - if (l < MAX_PATH_LENGTH-1 && full_path[l-1] != '/') { - full_path[l] = '/'; - full_path[l+1] = 0; - } - strncat(full_path, s, MAX_PATH_LENGTH-1); + add_path_component(full_path, s); } static void get_path_for_fsitem(FSItem *p) { - if (p->id == ROOT_ID) { + if (p->id == ROOT_PARENT_ID) { + full_path[0] = 0; + } else if (p->id == ROOT_ID) { strncpy(full_path, RootPath, MAX_PATH_LENGTH-1); full_path[MAX_PATH_LENGTH-1] = 0; } else { get_path_for_fsitem(p->parent); - add_path_component(p->name); + add_path_comp(p->name); + } +} + + +/* + * Exchange parent CNIDs in all FSItems + */ + +static void swap_parent_ids(uint32 parent1, uint32 parent2) +{ + FSItem *p = first_fs_item; + while (p) { + if (p->parent_id == parent1) + p->parent_id = parent2; + else if (p->parent_id == parent2) + p->parent_id = parent1; + p = p->next; } } @@ -241,30 +337,21 @@ static void cstr2pstr(char *dst, const c *dst++ = strlen(src); char c; while ((c = *src++) != 0) { + // Note: we are converting host ':' characters to Mac '/' characters here + // '/' is not a path separator as this function is only used on object names if (c == ':') c = '/'; *dst++ = c; } } -// Convert pascal string to C string -static void pstr2cstr(char *dst, const char *src) -{ - int size = *src++; - while (size--) { - char c = *src++; - if (c == '/') - c = ':'; - *dst++ = c; - } - *dst = 0; -} - // Convert string (no length byte) to C string, length given separately static void strn2cstr(char *dst, const char *src, int size) { while (size--) { char c = *src++; + // Note: we are converting Mac '/' characters to host ':' characters here + // '/' is not a path separator as this function is only used on object names if (c == '/') c = ':'; *dst++ = c; @@ -322,14 +409,28 @@ void ExtFSInit(void) cstr2pstr(FS_NAME, GetString(STR_EXTFS_NAME)); cstr2pstr(VOLUME_NAME, GetString(STR_EXTFS_VOLUME_NAME)); - // Create root FSItem + // Create root's parent FSItem FSItem *p = new FSItem; first_fs_item = last_fs_item = p; p->next = NULL; + p->id = ROOT_PARENT_ID; + p->parent_id = 0; + p->parent = NULL; + p->name[0] = 0; + p->guest_name[0] = 0; + + // Create root FSItem + p = new FSItem; + last_fs_item->next = p; + p->next = NULL; + last_fs_item = p; p->id = ROOT_ID; p->parent_id = ROOT_PARENT_ID; - p->parent = NULL; + p->parent = first_fs_item; strncpy(p->name, GetString(STR_EXTFS_VOLUME_NAME), 32); + p->name[31] = 0; + strncpy(p->guest_name, host_encoding_to_macroman(p->name), 32); + p->guest_name[31] = 0; // Find path for root if ((RootPath = PrefsFindString("extfs")) != NULL) { @@ -378,16 +479,20 @@ void InstallExtFS(void) // FSM present? r.d[0] = gestaltFSAttr; Execute68kTrap(0xa1ad, &r); // Gestalt() - D(bug("FSAttr %ld, %08lx\n", r.d[0], r.a[0])); - if ((r.d[0] & 0xffff) || !(r.a[0] & (1 << gestaltHasFileSystemManager))) + D(bug("FSAttr %d, %08x\n", r.d[0], r.a[0])); + if ((r.d[0] & 0xffff) || !(r.a[0] & (1 << gestaltHasFileSystemManager))) { + printf("WARNING: No FSM present, disabling ExtFS\n"); return; + } // Yes, version >=1.2? r.d[0] = gestaltFSMVersion; Execute68kTrap(0xa1ad, &r); // Gestalt() - D(bug("FSMVersion %ld, %08lx\n", r.d[0], r.a[0])); - if ((r.d[0] & 0xffff) || (r.a[0] < 0x0120)) + D(bug("FSMVersion %d, %08x\n", r.d[0], r.a[0])); + if ((r.d[0] & 0xffff) || (r.a[0] < 0x0120)) { + printf("WARNING: FSM <1.2 found, disabling ExtFS\n"); return; + } D(bug("FSM present\n")); @@ -423,7 +528,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7006); p+= 2; // UTAllocateVCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsAddNewVCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -433,7 +538,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7007); p+= 2; // UTAddNewVCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsDetermineVol) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -445,18 +550,18 @@ void InstallExtFS(void) WriteMacInt16(p, 0x701d); p+= 2; // UTDetermineVol WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsResolveWDCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) - WriteMacInt16(p, 0x42a7); p+= 2; // clr.l -(sp) - WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) - WriteMacInt16(p, 0x3f00); p+= 2; // move.w d0,-(sp) + WriteMacInt16(p, 0x2f00); p+= 2; // move.l d0,-(sp) + WriteMacInt16(p, 0x3f01); p+= 2; // move.w d1,-(sp) + WriteMacInt16(p, 0x3f02); p+= 2; // move.w d2,-(sp) WriteMacInt16(p, 0x2f08); p+= 2; // move.l a0,-(sp) WriteMacInt16(p, 0x700e); p+= 2; // UTResolveWDCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsGetDefaultVol) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -464,7 +569,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7012); p+= 2; // UTGetDefaultVol WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsGetPathComponentName) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -472,7 +577,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x701c); p+= 2; // UTGetPathComponentName WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsParsePathname) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -481,7 +586,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x701b); p+= 2; // UTParsePathname WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsDisposeVCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -489,7 +594,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7008); p+= 2; // UTDisposeVCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsCheckWDRefNum) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -497,7 +602,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7013); p+= 2; // UTCheckWDRefNum WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsSetDefaultVol) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -507,7 +612,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7011); p+= 2; // UTSetDefaultVol WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsAllocateFCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -516,7 +621,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7000); p+= 2; // UTAllocateFCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsReleaseFCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -524,7 +629,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7001); p+= 2; // UTReleaseFCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsIndexFCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -534,7 +639,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7004); p+= 2; // UTIndexFCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsResolveFCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -543,7 +648,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7005); p+= 2; // UTResolveFCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsAdjustEOF) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -551,7 +656,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x7010); p+= 2; // UTAdjustEOF WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsAllocateWDCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -559,7 +664,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x700c); p+= 2; // UTAllocateWDCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != fsReleaseWDCB) goto fsdat_error; WriteMacInt16(p, 0x4267); p+= 2; // clr.w -(sp) @@ -567,7 +672,7 @@ void InstallExtFS(void) WriteMacInt16(p, 0x700d); p+= 2; // UTReleaseWDCB WriteMacInt16(p, 0xa824); p+= 2; // FSMgr WriteMacInt16(p, 0x301f); p+= 2; // move.w (sp)+,d0 - WriteMacInt16(p, M68K_EXEC_RETURN); p+= 2; + WriteMacInt16(p, M68K_RTS); p+= 2; if (p - fs_data != SIZEOF_fsdat) goto fsdat_error; @@ -591,7 +696,7 @@ void InstallExtFS(void) WriteMacInt16(fs_data + fsFSD + fsdLength, SIZEOF_FSDRec); WriteMacInt16(fs_data + fsFSD + fsdVersion, fsdVersion1); WriteMacInt16(fs_data + fsFSD + fileSystemFSID, MY_FSID); - memcpy(Mac2HostAddr(fs_data + fsFSD + fileSystemName), FS_NAME, 32); + Host2Mac_memcpy(fs_data + fsFSD + fileSystemName, FS_NAME, 32); WriteMacInt32(fs_data + fsFSD + fileSystemCommProc, fs_data + fsCommProcStub); WriteMacInt32(fs_data + fsFSD + fsdHFSCI + compInterfProc, fs_data + fsHFSProcStub); WriteMacInt32(fs_data + fsFSD + fsdHFSCI + stackTop, fs_stack + STACK_SIZE); @@ -645,7 +750,7 @@ int16 ExtFSComm(uint16 message, uint32 p case ffsGetIconMessage: { // Get disk/drive icon if (ReadMacInt8(paramBlock + iconType) == kLargeIcon && ReadMacInt32(paramBlock + requestSize) >= sizeof(ExtFSIcon)) { - memcpy(Mac2HostAddr(ReadMacInt32(paramBlock + iconBufferPtr)), ExtFSIcon, sizeof(ExtFSIcon)); + Host2Mac_memcpy(ReadMacInt32(paramBlock + iconBufferPtr), ExtFSIcon, sizeof(ExtFSIcon)); WriteMacInt32(paramBlock + actualSize, sizeof(ExtFSIcon)); return noErr; } else @@ -653,7 +758,7 @@ int16 ExtFSComm(uint16 message, uint32 p } case ffsIDDiskMessage: { // Check if volume is handled by our FS - if (ReadMacInt16(paramBlock + ioVRefNum) == drive_number) + if ((int16)ReadMacInt16(paramBlock + ioVRefNum) == drive_number) return noErr; else return extFSErr; @@ -682,7 +787,7 @@ static int16 get_current_dir(uint32 pb, int16 result; // Determine volume -// D(bug(" determining volume\n")); + D(bug(" determining volume, dirID %d\n", dirID)); r.a[0] = pb; r.a[1] = fs_data + fsReturn; r.a[2] = fs_data + fsReturn + 2; @@ -697,11 +802,8 @@ static int16 get_current_dir(uint32 pb, if (no_vol_name) WriteMacInt32(pb + ioNamePtr, name_ptr); int16 status = ReadMacInt16(fs_data + fsReturn); - int16 more_matches = ReadMacInt16(fs_data + fsReturn + 2); - int16 vRefNum = ReadMacInt16(fs_data + fsReturn + 4); - uint32 vcb = ReadMacInt32(fs_data + fsReturn + 6); -// D(bug(" UTDetermineVol() returned %d, status %d\n", r.d[0], status)); - result = r.d[0] & 0xffff; + D(bug(" UTDetermineVol() returned %d, status %d\n", r.d[0], status)); + result = (int16)(r.d[0] & 0xffff); if (result == noErr) { switch (status) { @@ -719,12 +821,14 @@ static int16 get_current_dir(uint32 pb, current_dir = dirID; else { D(bug(" resolving WDCB\n")); - r.d[0] = ReadMacInt16(pb + ioVRefNum); + r.d[0] = 0; + r.d[1] = 0; + r.d[2] = ReadMacInt16(pb + ioVRefNum); r.a[0] = fs_data + fsReturn; Execute68k(fs_data + fsResolveWDCB, &r); uint32 wdcb = ReadMacInt32(fs_data + fsReturn); D(bug(" UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID))); - result = r.d[0] & 0xffff; + result = (int16)(r.d[0] & 0xffff); if (result == noErr) current_dir = ReadMacInt32(wdcb + wdDirID); } @@ -740,7 +844,7 @@ static int16 get_current_dir(uint32 pb, r.a[0] = wdpb; Execute68k(fs_data + fsGetDefaultVol, &r); D(bug(" UTGetDefaultVol() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdpb + ioWDDirID))); - result = r.d[0] & 0xffff; + result = (int16)(r.d[0] & 0xffff); if (result == noErr) current_dir = ReadMacInt32(wdpb + ioWDDirID); } @@ -766,7 +870,7 @@ static int16 get_path_component_name(uin r.a[0] = rec; Execute68k(fs_data + fsGetPathComponentName, &r); // D(bug(" UTGetPathComponentName returned %d\n", r.d[0])); - return r.d[0] & 0xffff; + return (int16)(r.d[0] & 0xffff); } @@ -783,6 +887,7 @@ static int16 get_item_and_path(uint32 pb uint32 current_dir; if ((result = get_current_dir(pb, dirID, current_dir, no_vol_name)) != noErr) return result; + D(bug(" current dir %08x\n", current_dir)); FSItem *p = find_fsitem_by_id(current_dir); if (p == NULL) return dirNFErr; @@ -796,12 +901,12 @@ static int16 get_item_and_path(uint32 pb WriteMacInt8(parseRec + ppFoundDelimiter, false); // Get length of volume name -// D(bug(" parsing pathname\n")); + D(bug(" parsing pathname\n")); r.a[0] = parseRec + ppStartOffset; r.a[1] = ReadMacInt32(parseRec + ppNamePtr); Execute68k(fs_data + fsParsePathname, &r); -// D(bug(" UTParsePathname() returned %d, startOffset %d\n", r.d[0], ReadMacInt16(parseRec + ppStartOffset))); - result = r.d[0] & 0xffff; + D(bug(" UTParsePathname() returned %d, startOffset %d\n", r.d[0], ReadMacInt16(parseRec + ppStartOffset))); + result = (int16)(r.d[0] & 0xffff); if (result == noErr) { // Check for leading delimiter of the partial pathname @@ -836,7 +941,7 @@ static int16 get_item_and_path(uint32 pb char name[32]; strn2cstr(name, (char *)Mac2HostAddr(ReadMacInt32(parseRec + ppNamePtr)) + ReadMacInt16(parseRec + ppStartOffset) + 1, ReadMacInt16(parseRec + ppComponentLength)); D(bug(" entering %s\n", name)); - p = find_fsitem(name, p); + p = find_fsitem_guest(name, p); current_dir = p->id; // startOffset = start of next component @@ -859,7 +964,7 @@ static int16 get_item_and_path(uint32 pb char name[32]; strn2cstr(name, (char *)Mac2HostAddr(ReadMacInt32(parseRec + ppNamePtr)) + ReadMacInt16(parseRec + ppStartOffset) + 1, ReadMacInt16(parseRec + ppComponentLength)); D(bug(" object is %s\n", name)); - item = find_fsitem(name, p); + item = find_fsitem_guest(name, p); } } } @@ -914,7 +1019,7 @@ static uint32 find_fcb(int16 refNum) static int16 fs_mount_vol(uint32 pb) { D(bug(" fs_mount_vol(%08lx), vRefNum %d\n", pb, ReadMacInt16(pb + ioVRefNum))); - if (ReadMacInt16(pb + ioVRefNum) == drive_number) + if ((int16)ReadMacInt16(pb + ioVRefNum) == drive_number) return noErr; else return extFSErr; @@ -931,29 +1036,33 @@ static int16 fs_volume_mount(uint32 pb) r.a[0] = fs_data + fsReturn; r.a[1] = fs_data + fsReturn + 2; Execute68k(fs_data + fsAllocateVCB, &r); +#if DEBUG uint16 sysVCBLength = ReadMacInt16(fs_data + fsReturn); +#endif uint32 vcb = ReadMacInt32(fs_data + fsReturn + 2); D(bug(" UTAllocateVCB() returned %d, vcb %08lx, size %d\n", r.d[0], vcb, sysVCBLength)); if (r.d[0] & 0xffff) - return r.d[0]; + return (int16)r.d[0]; // Init VCB WriteMacInt16(vcb + vcbSigWord, 0x4244); -#ifdef __BEOS__ - WriteMacInt32(vcb + vcbCrDate, root_stat.st_crtime + TIME_OFFSET); +#if defined(__BEOS__) || defined(WIN32) + WriteMacInt32(vcb + vcbCrDate, TimeToMacTime(root_stat.st_crtime)); +#elif defined __APPLE__ && defined __MACH__ + WriteMacInt32(vcb + vcbCrDate, get_creation_time(RootPath)); #else WriteMacInt32(vcb + vcbCrDate, 0); #endif - WriteMacInt32(vcb + vcbLsMod, root_stat.st_mtime + TIME_OFFSET); + WriteMacInt32(vcb + vcbLsMod, TimeToMacTime(root_stat.st_mtime)); WriteMacInt32(vcb + vcbVolBkUp, 0); WriteMacInt16(vcb + vcbNmFls, 1); //!! WriteMacInt16(vcb + vcbNmRtDirs, 1); //!! WriteMacInt16(vcb + vcbNmAlBlks, 0xffff); //!! - WriteMacInt32(vcb + vcbAlBlkSiz, 1024); - WriteMacInt32(vcb + vcbClpSiz, 1024); + WriteMacInt32(vcb + vcbAlBlkSiz, AL_BLK_SIZE); + WriteMacInt32(vcb + vcbClpSiz, CLUMP_SIZE); WriteMacInt32(vcb + vcbNxtCNID, next_cnid); WriteMacInt16(vcb + vcbFreeBks, 0xffff); //!! - memcpy(Mac2HostAddr(vcb + vcbVN), VOLUME_NAME, 28); + Host2Mac_memcpy(vcb + vcbVN, VOLUME_NAME, 28); WriteMacInt16(vcb + vcbFSID, MY_FSID); WriteMacInt32(vcb + vcbFilCnt, 1); //!! WriteMacInt32(vcb + vcbDirCnt, 1); //!! @@ -964,10 +1073,10 @@ static int16 fs_volume_mount(uint32 pb) r.a[0] = fs_data + fsReturn; r.a[1] = vcb; Execute68k(fs_data + fsAddNewVCB, &r); - int16 vRefNum = ReadMacInt32(fs_data + fsReturn); + int16 vRefNum = (int16)ReadMacInt32(fs_data + fsReturn); D(bug(" UTAddNewVCB() returned %d, vRefNum %d\n", r.d[0], vRefNum)); if (r.d[0] & 0xffff) - return r.d[0]; + return (int16)r.d[0]; // Post diskInsertEvent D(bug(" posting diskInsertEvent\n")); @@ -991,7 +1100,7 @@ static int16 fs_unmount_vol(uint32 vcb) r.a[0] = vcb; Execute68k(fs_data + fsDisposeVCB, &r); D(bug(" UTDisposeVCB() returned %d\n", r.d[0])); - return r.d[0]; + return (int16)r.d[0]; } // Get information about a volume (HVolumeParam) @@ -1002,19 +1111,21 @@ static int16 fs_get_vol_info(uint32 pb, // Fill in struct if (ReadMacInt32(pb + ioNamePtr)) pstrcpy((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), VOLUME_NAME); -#ifdef __BEOS__ - WriteMacInt32(pb + ioVCrDate, root_stat.st_crtime + TIME_OFFSET); +#if defined(__BEOS__) || defined(WIN32) + WriteMacInt32(pb + ioVCrDate, TimeToMacTime(root_stat.st_crtime)); +#elif defined __APPLE__ && defined __MACH__ + WriteMacInt32(pb + ioVCrDate, get_creation_time(RootPath)); #else WriteMacInt32(pb + ioVCrDate, 0); #endif - WriteMacInt32(pb + ioVLsMod, root_stat.st_mtime + TIME_OFFSET); + WriteMacInt32(pb + ioVLsMod, TimeToMacTime(root_stat.st_mtime)); WriteMacInt16(pb + ioVAtrb, 0); WriteMacInt16(pb + ioVNmFls, 1); //!! WriteMacInt16(pb + ioVBitMap, 0); WriteMacInt16(pb + ioAllocPtr, 0); WriteMacInt16(pb + ioVNmAlBlks, 0xffff); //!! - WriteMacInt32(pb + ioVAlBlkSiz, 1024); - WriteMacInt32(pb + ioVClpSiz, 1024); + WriteMacInt32(pb + ioVAlBlkSiz, AL_BLK_SIZE); + WriteMacInt32(pb + ioVClpSiz, CLUMP_SIZE); WriteMacInt16(pb + ioAlBlSt, 0); WriteMacInt32(pb + ioVNxtCNID, next_cnid); WriteMacInt16(pb + ioVFrBlk, 0xffff); //!! @@ -1027,7 +1138,7 @@ static int16 fs_get_vol_info(uint32 pb, WriteMacInt32(pb + ioVWrCnt, 0); WriteMacInt32(pb + ioVFilCnt, 1); //!! WriteMacInt32(pb + ioVDirCnt, 1); //!! - memset(Mac2HostAddr(pb + ioVFndrInfo), 0, 32); + Mac_memset(pb + ioVFndrInfo, 0, 32); } return noErr; } @@ -1047,18 +1158,17 @@ static int16 fs_get_vol_parms(uint32 pb) // D(bug(" fs_get_vol_parms(%08lx)\n", pb)); // Return parameter block - uint8 vol[SIZEOF_GetVolParmsInfoBuffer]; - WriteMacInt16((uint32)vol + vMVersion, 2); - WriteMacInt32((uint32)vol + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol); - WriteMacInt32((uint32)vol + vMLocalHand, 0); - WriteMacInt32((uint32)vol + vMServerAdr, 0); - WriteMacInt32((uint32)vol + vMVolumeGrade, 0); - WriteMacInt16((uint32)vol + vMForeignPrivID, 0); uint32 actual = ReadMacInt32(pb + ioReqCount); - if (actual > sizeof(vol)) - actual = sizeof(vol); - memcpy(Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), vol, actual); + if (actual > SIZEOF_GetVolParmsInfoBuffer) + actual = SIZEOF_GetVolParmsInfoBuffer; WriteMacInt32(pb + ioActCount, actual); + uint32 p = ReadMacInt32(pb + ioBuffer); + if (actual > vMVersion) WriteMacInt16(p + vMVersion, 2); + if (actual > vMAttrib) WriteMacInt32(p + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol); + if (actual > vMLocalHand) WriteMacInt32(p + vMLocalHand, 0); + if (actual > vMServerAdr) WriteMacInt32(p + vMServerAdr, 0); + if (actual > vMVolumeGrade) WriteMacInt32(p + vMVolumeGrade, 0); + if (actual > vMForeignPrivID) WriteMacInt16(p + vMForeignPrivID, 0); return noErr; } @@ -1073,13 +1183,13 @@ static int16 fs_get_vol(uint32 pb) r.a[0] = pb; Execute68k(fs_data + fsGetDefaultVol, &r); D(bug(" UTGetDefaultVol() returned %d\n", r.d[0])); - return r.d[0]; + return (int16)r.d[0]; } // Set default volume (WDParam) static int16 fs_set_vol(uint32 pb, bool hfs, uint32 vcb) { - D(bug(" fs_set_vol(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioWDDirID))); + D(bug(" fs_set_vol(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioWDDirID))); M68kRegisters r; // Determine parameters @@ -1129,17 +1239,17 @@ static int16 fs_set_vol(uint32 pb, bool r.d[2] = refNum; Execute68k(fs_data + fsSetDefaultVol, &r); D(bug(" UTSetDefaultVol() returned %d\n", r.d[0])); - return r.d[0]; + return (int16)r.d[0]; } // Query file attributes (HFileParam) static int16 fs_get_file_info(uint32 pb, bool hfs, uint32 dirID) { - D(bug(" fs_get_file_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), dirID)); + D(bug(" fs_get_file_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), dirID)); FSItem *fs_item; int16 dir_index = ReadMacInt16(pb + ioFDirIndex); - if (dir_index == 0) { // Query item specified by ioDirID and ioNamePtr + if (dir_index <= 0) { // Query item specified by ioDirID and ioNamePtr // Find FSItem for given file int16 result = get_item_and_path(pb, dirID, fs_item); @@ -1171,10 +1281,10 @@ read_next_de: return fnfErr; } if (de->d_name[0] == '.') - goto read_next_de; // Suppress name beginning with '.' (MacOS could interpret these as driver names) + goto read_next_de; // Suppress names beginning with '.' (MacOS could interpret these as driver names) //!! suppress directories } - add_path_component(de->d_name); + add_path_comp(de->d_name); // Get FSItem for queried item fs_item = find_fsitem(de->d_name, p); @@ -1190,38 +1300,32 @@ read_next_de: // Fill in struct from fs_item and stats if (ReadMacInt32(pb + ioNamePtr)) - cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->name); + cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->guest_name); WriteMacInt16(pb + ioFRefNum, 0); WriteMacInt8(pb + ioFlAttrib, access(full_path, W_OK) == 0 ? 0 : faLocked); WriteMacInt32(pb + ioDirID, fs_item->id); -#ifdef __BEOS__ - WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET); +#if defined(__BEOS__) || defined(WIN32) + WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime)); +#elif defined __APPLE__ && defined __MACH__ + WriteMacInt32(pb + ioFlCrDat, get_creation_time(full_path)); #else WriteMacInt32(pb + ioFlCrDat, 0); #endif - WriteMacInt32(pb + ioFlMdDat, st.st_mtime + TIME_OFFSET); + WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(st.st_mtime)); - memset(Mac2HostAddr(pb + ioFlFndrInfo), 0, SIZEOF_FInfo); - uint32 type, creator; // pb may point to kernel space, but stack is switched - get_finder_type(full_path, type, creator); - WriteMacInt32(pb + ioFlFndrInfo + fdType, type); - WriteMacInt32(pb + ioFlFndrInfo + fdCreator, creator); - uint16 fflags; - get_finder_flags(full_path, fflags); - WriteMacInt16(pb + ioFlFndrInfo + fdFlags, fflags); + get_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false); WriteMacInt16(pb + ioFlStBlk, 0); WriteMacInt32(pb + ioFlLgLen, st.st_size); - WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023); + WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1); WriteMacInt16(pb + ioFlRStBlk, 0); uint32 rf_size = get_rfork_size(full_path); WriteMacInt32(pb + ioFlRLgLen, rf_size); - WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023); + WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1); if (hfs) { WriteMacInt32(pb + ioFlBkDat, 0); - memset(Mac2HostAddr(pb + ioFlXFndrInfo), 0, SIZEOF_FXInfo); WriteMacInt32(pb + ioFlParID, fs_item->parent_id); WriteMacInt32(pb + ioFlClpSiz, 0); } @@ -1231,7 +1335,7 @@ read_next_de: // Set file attributes (HFileParam) static int16 fs_set_file_info(uint32 pb, bool hfs, uint32 dirID) { - D(bug(" fs_set_file_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), dirID)); + D(bug(" fs_set_file_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), dirID)); // Find FSItem for given file/dir FSItem *fs_item; @@ -1246,9 +1350,9 @@ static int16 fs_set_file_info(uint32 pb, if (S_ISDIR(st.st_mode)) return fnfErr; - // Set attributes - set_finder_type(full_path, ReadMacInt32(pb + ioFlFndrInfo + fdType), ReadMacInt32(pb + ioFlFndrInfo + fdCreator)); - set_finder_flags(full_path, ReadMacInt16(pb + ioFlFndrInfo + fdFlags)); + // Set Finder info + set_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false); + //!! times return noErr; } @@ -1256,11 +1360,11 @@ static int16 fs_set_file_info(uint32 pb, // Query file/directory attributes static int16 fs_get_cat_info(uint32 pb) { - D(bug(" fs_get_cat_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID))); + D(bug(" fs_get_cat_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID))); FSItem *fs_item; int16 dir_index = ReadMacInt16(pb + ioFDirIndex); - if (dir_index == -1) { // Query directory specified by ioDirID + if (dir_index < 0) { // Query directory specified by ioDirID // Find FSItem for directory fs_item = find_fsitem_by_id(ReadMacInt32(pb + ioDrDirID)); @@ -1300,9 +1404,9 @@ read_next_de: return fnfErr; } if (de->d_name[0] == '.') - goto read_next_de; // Suppress name beginning with '.' (MacOS could interpret these as driver names) + goto read_next_de; // Suppress names beginning with '.' (MacOS could interpret these as driver names) } - add_path_component(de->d_name); + add_path_comp(de->d_name); // Get FSItem for queried item fs_item = find_fsitem(de->d_name, p); @@ -1319,14 +1423,16 @@ read_next_de: // Fill in struct from fs_item and stats if (ReadMacInt32(pb + ioNamePtr)) - cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->name); + cstr2pstr((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), fs_item->guest_name); WriteMacInt16(pb + ioFRefNum, 0); WriteMacInt8(pb + ioFlAttrib, (S_ISDIR(st.st_mode) ? faIsDir : 0) | (access(full_path, W_OK) == 0 ? 0 : faLocked)); WriteMacInt8(pb + ioACUser, 0); WriteMacInt32(pb + ioDirID, fs_item->id); WriteMacInt32(pb + ioFlParID, fs_item->parent_id); -#ifdef __BEOS__ - WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET); +#if defined(__BEOS__) || defined(WIN32) + WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime)); +#elif defined __APPLE__ && defined __MACH__ + WriteMacInt32(pb + ioFlCrDat, get_creation_time(full_path)); #else WriteMacInt32(pb + ioFlCrDat, 0); #endif @@ -1336,14 +1442,12 @@ read_next_de: fs_item->mtime = mtime; cached = false; } - WriteMacInt32(pb + ioFlMdDat, mtime); + WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(mtime)); WriteMacInt32(pb + ioFlBkDat, 0); + + get_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode)); + if (S_ISDIR(st.st_mode)) { - memset(Mac2HostAddr(pb + ioDrUsrWds), 0, SIZEOF_DInfo); - memset(Mac2HostAddr(pb + ioDrFndrInfo), 0, SIZEOF_DXInfo); - uint16 fflags; // pb may point to kernel space, but stack is switched - get_finder_flags(full_path, fflags); - WriteMacInt16(pb + ioDrUsrWds + frFlags, fflags); // Determine number of files in directory (cached) int count; @@ -1358,6 +1462,8 @@ read_next_de: de = readdir(d); if (de == NULL) break; + if (de->d_name[0] == '.') + continue; // Suppress names beginning with '.' count++; } closedir(d); @@ -1366,22 +1472,13 @@ read_next_de: } WriteMacInt16(pb + ioDrNmFls, count); } else { - memset(Mac2HostAddr(pb + ioFlFndrInfo), 0, SIZEOF_FInfo); - memset(Mac2HostAddr(pb + ioFlXFndrInfo), 0, SIZEOF_FXInfo); - uint32 type, creator; // pb may point to kernel space, but stack is switched - get_finder_type(full_path, type, creator); - WriteMacInt32(pb + ioFlFndrInfo + fdType, type); - WriteMacInt32(pb + ioFlFndrInfo + fdCreator, creator); - uint16 fflags; - get_finder_flags(full_path, fflags); - WriteMacInt16(pb + ioFlFndrInfo + fdFlags, fflags); WriteMacInt16(pb + ioFlStBlk, 0); WriteMacInt32(pb + ioFlLgLen, st.st_size); - WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023); + WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1); WriteMacInt16(pb + ioFlRStBlk, 0); uint32 rf_size = get_rfork_size(full_path); WriteMacInt32(pb + ioFlRLgLen, rf_size); - WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023); + WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1); WriteMacInt32(pb + ioFlClpSiz, 0); } return noErr; @@ -1390,7 +1487,7 @@ read_next_de: // Set file/directory attributes static int16 fs_set_cat_info(uint32 pb) { - D(bug(" fs_set_cat_info(%08lx), vRefNum %d, name %#s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID))); + D(bug(" fs_set_cat_info(%08lx), vRefNum %d, name %.31s, idx %d, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt16(pb + ioFDirIndex), ReadMacInt32(pb + ioDirID))); // Find FSItem for given file/dir FSItem *fs_item; @@ -1403,13 +1500,9 @@ static int16 fs_set_cat_info(uint32 pb) if (stat(full_path, &st) < 0) return errno2oserr(); - // Set attributes - if (S_ISDIR(st.st_mode)) - set_finder_flags(full_path, ReadMacInt16(pb + ioDrUsrWds + frFlags)); - else { - set_finder_type(full_path, ReadMacInt32(pb + ioFlFndrInfo + fdType), ReadMacInt32(pb + ioFlFndrInfo + fdCreator)); - set_finder_flags(full_path, ReadMacInt16(pb + ioFlFndrInfo + fdFlags)); - } + // Set Finder info + set_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode)); + //!! times return noErr; } @@ -1417,7 +1510,7 @@ static int16 fs_set_cat_info(uint32 pb) // Open file static int16 fs_open(uint32 pb, uint32 dirID, uint32 vcb, bool resource_fork) { - D(bug(" fs_open(%08lx), %s, vRefNum %d, name %#s, dirID %d, perm %d\n", pb, resource_fork ? "rsrc" : "data", ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID, ReadMacInt8(pb + ioPermssn))); + D(bug(" fs_open(%08lx), %s, vRefNum %d, name %.31s, dirID %d, perm %d\n", pb, resource_fork ? "rsrc" : "data", ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID, ReadMacInt8(pb + ioPermssn))); M68kRegisters r; // Find FSItem for given file @@ -1456,9 +1549,11 @@ static int16 fs_open(uint32 pb, uint32 d if (access(full_path, F_OK)) return fnfErr; fd = open_rfork(full_path, flag); - if (fd > 0) { - if (fstat(fd, &st) < 0) + if (fd >= 0) { + if (fstat(fd, &st) < 0) { + close(fd); return errno2oserr(); + } } else { // Resource fork not supported, silently ignore it ("pseudo" resource fork) st.st_size = 0; st.st_mode = 0; @@ -1467,8 +1562,10 @@ static int16 fs_open(uint32 pb, uint32 d fd = open(full_path, flag); if (fd < 0) return errno2oserr(); - if (fstat(fd, &st) < 0) + if (fstat(fd, &st) < 0) { + close(fd); return errno2oserr(); + } } // File open, allocate FCB @@ -1480,23 +1577,24 @@ static int16 fs_open(uint32 pb, uint32 d D(bug(" UTAllocateFCB() returned %d, fRefNum %d, fcb %08lx\n", r.d[0], ReadMacInt16(pb + ioRefNum), fcb)); if (r.d[0] & 0xffff) { close(fd); - return r.d[0]; + return (int16)r.d[0]; } // Initialize FCB, fd is stored in fcbCatPos WriteMacInt32(fcb + fcbFlNm, fs_item->id); WriteMacInt8(fcb + fcbFlags, ((flag == O_WRONLY || flag == O_RDWR) ? fcbWriteMask : 0) | (resource_fork ? fcbResourceMask : 0) | (write_ok ? 0 : fcbFileLockedMask)); WriteMacInt32(fcb + fcbEOF, st.st_size); - WriteMacInt32(fcb + fcbPLen, (st.st_size + 1023) & ~1023); + WriteMacInt32(fcb + fcbPLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1); WriteMacInt32(fcb + fcbCrPs, 0); WriteMacInt32(fcb + fcbVPtr, vcb); - WriteMacInt32(fcb + fcbClmpSize, 1024); - uint32 type, creator; // fcb may point to kernel space, but stack is switched - get_finder_type(full_path, type, creator); - WriteMacInt32(fcb + fcbFType, type); + WriteMacInt32(fcb + fcbClmpSize, CLUMP_SIZE); + + get_finfo(full_path, fs_data + fsPB, 0, false); + WriteMacInt32(fcb + fcbFType, ReadMacInt32(fs_data + fsPB + fdType)); + WriteMacInt32(fcb + fcbCatPos, fd); WriteMacInt32(fcb + fcbDirID, fs_item->parent_id); - cstr2pstr((char *)Mac2HostAddr(fcb + fcbCName), fs_item->name); + cstr2pstr((char *)Mac2HostAddr(fcb + fcbCName), fs_item->guest_name); return noErr; } @@ -1530,7 +1628,7 @@ static int16 fs_close(uint32 pb) r.d[0] = ReadMacInt16(pb + ioRefNum); Execute68k(fs_data + fsReleaseFCB, &r); D(bug(" UTReleaseFCB() returned %d\n", r.d[0])); - return r.d[0]; + return (int16)r.d[0]; } // Query information about FCB (FCBPBRec) @@ -1549,7 +1647,7 @@ static int16 fs_get_fcb_info(uint32 pb, // Find FCB by index WriteMacInt16(pb + ioRefNum, 0); - for (int i=0; i= 0 ? actual : 0); uint32 pos = lseek(fd, 0, SEEK_CUR); WriteMacInt32(fcb + fcbCrPs, pos); WriteMacInt32(pb + ioPosOffset, pos); - if (actual != ReadMacInt32(pb + ioReqCount)) - if (errno) - return errno2oserr(); - else - return eofErr; + if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount)) + return actual < 0 ? read_err : eofErr; else return noErr; } @@ -1772,6 +1877,10 @@ static int16 fs_write(uint32 pb) { D(bug(" fs_write(%08lx), refNum %d, buffer %p, count %d, posMode %d, posOffset %d\n", pb, ReadMacInt16(pb + ioRefNum), ReadMacInt32(pb + ioBuffer), ReadMacInt32(pb + ioReqCount), ReadMacInt16(pb + ioPosMode), ReadMacInt32(pb + ioPosOffset))); + // Check parameters + if ((int32)ReadMacInt32(pb + ioReqCount) < 0) + return paramErr; + // Find FCB and fd for file uint32 fcb = find_fcb(ReadMacInt16(pb + ioRefNum)); if (fcb == 0) @@ -1803,14 +1912,15 @@ static int16 fs_write(uint32 pb) } // Write - size_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount)); + ssize_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount)); + int16 write_err = errno2oserr(); D(bug(" actual %d\n", actual)); - WriteMacInt32(pb + ioActCount, actual); + WriteMacInt32(pb + ioActCount, actual >= 0 ? actual : 0); uint32 pos = lseek(fd, 0, SEEK_CUR); WriteMacInt32(fcb + fcbCrPs, pos); WriteMacInt32(pb + ioPosOffset, pos); - if (actual != ReadMacInt32(pb + ioReqCount)) - return errno2oserr(); + if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount)) + return write_err; else return noErr; } @@ -1818,7 +1928,7 @@ static int16 fs_write(uint32 pb) // Create file static int16 fs_create(uint32 pb, uint32 dirID) { - D(bug(" fs_create(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID)); + D(bug(" fs_create(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID)); // Find FSItem for given file FSItem *fs_item; @@ -1831,7 +1941,7 @@ static int16 fs_create(uint32 pb, uint32 return dupFNErr; // Create file - int fd = creat(full_path, 0664); + int fd = creat(full_path, 0666); if (fd < 0) return errno2oserr(); else { @@ -1843,7 +1953,7 @@ static int16 fs_create(uint32 pb, uint32 // Create directory static int16 fs_dir_create(uint32 pb) { - D(bug(" fs_dir_create(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioDirID))); + D(bug(" fs_dir_create(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioDirID))); // Find FSItem for given directory FSItem *fs_item; @@ -1856,7 +1966,7 @@ static int16 fs_dir_create(uint32 pb) return dupFNErr; // Create directory - if (mkdir(full_path, 0775) < 0) + if (mkdir(full_path, 0777) < 0) return errno2oserr(); else { WriteMacInt32(pb + ioDirID, fs_item->id); @@ -1867,7 +1977,7 @@ static int16 fs_dir_create(uint32 pb) // Delete file/directory static int16 fs_delete(uint32 pb, uint32 dirID) { - D(bug(" fs_delete(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID)); + D(bug(" fs_delete(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID)); // Find FSItem for given file/dir FSItem *fs_item; @@ -1876,23 +1986,16 @@ static int16 fs_delete(uint32 pb, uint32 return result; // Delete file - if (remove(full_path) < 0) { - int16 err = errno2oserr(); - if (errno == EISDIR) { // Workaround for BeOS bug - if (rmdir(full_path) < 0) - return errno2oserr(); - else - return noErr; - } else - return err; - } else + if (!extfs_remove(full_path)) + return errno2oserr(); + else return noErr; } // Rename file/directory static int16 fs_rename(uint32 pb, uint32 dirID) { - D(bug(" fs_rename(%08lx), vRefNum %d, name %#s, dirID %d, new name %#s\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), dirID, Mac2HostAddr(ReadMacInt32(pb + ioMisc)))); + D(bug(" fs_rename(%08lx), vRefNum %d, name %.31s, dirID %d, new name %.31s\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), dirID, Mac2HostAddr(ReadMacInt32(pb + ioMisc) + 1))); // Find path of given file/dir FSItem *fs_item; @@ -1905,11 +2008,10 @@ static int16 fs_rename(uint32 pb, uint32 strcpy(old_path, full_path); // Find path for new name - uint8 new_pb[SIZEOF_IOParam]; - memcpy(new_pb, Mac2HostAddr(pb), SIZEOF_IOParam); - WriteMacInt32((uint32)new_pb + ioNamePtr, ReadMacInt32(pb + ioMisc)); + Mac2Mac_memcpy(fs_data + fsPB, pb, SIZEOF_IOParam); + WriteMacInt32(fs_data + fsPB + ioNamePtr, ReadMacInt32(pb + ioMisc)); FSItem *new_item; - result = get_item_and_path((uint32)new_pb, dirID, new_item); + result = get_item_and_path(fs_data + fsPB, dirID, new_item); if (result != noErr) return result; @@ -1919,10 +2021,11 @@ static int16 fs_rename(uint32 pb, uint32 // Rename item D(bug(" renaming %s -> %s\n", old_path, full_path)); - if (rename(old_path, full_path) < 0) + if (!extfs_rename(old_path, full_path)) return errno2oserr(); else { // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems + swap_parent_ids(fs_item->id, new_item->id); uint32 t = fs_item->id; fs_item->id = new_item->id; new_item->id = t; @@ -1933,7 +2036,7 @@ static int16 fs_rename(uint32 pb, uint32 // Move file/directory (CMovePBRec) static int16 fs_cat_move(uint32 pb) { - D(bug(" fs_cat_move(%08lx), vRefNum %d, name %#s, dirID %d, new name %#s, new dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioDirID), Mac2HostAddr(ReadMacInt32(pb + ioNewName)), ReadMacInt32(pb + ioNewDirID))); + D(bug(" fs_cat_move(%08lx), vRefNum %d, name %.31s, dirID %d, new name %.31s, new dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioDirID), Mac2HostAddr(ReadMacInt32(pb + ioNewName) + 1), ReadMacInt32(pb + ioNewDirID))); // Find path of given file/dir FSItem *fs_item; @@ -1946,16 +2049,15 @@ static int16 fs_cat_move(uint32 pb) strcpy(old_path, full_path); // Find path for new directory - uint8 new_pb[SIZEOF_IOParam]; - memcpy(new_pb, Mac2HostAddr(pb), SIZEOF_IOParam); - WriteMacInt32((uint32)new_pb + ioNamePtr, ReadMacInt32(pb + ioNewName)); + Mac2Mac_memcpy(fs_data + fsPB, pb, SIZEOF_IOParam); + WriteMacInt32(fs_data + fsPB + ioNamePtr, ReadMacInt32(pb + ioNewName)); FSItem *new_dir_item; - result = get_item_and_path((uint32)new_pb, ReadMacInt32(pb + ioNewDirID), new_dir_item); + result = get_item_and_path(fs_data + fsPB, ReadMacInt32(pb + ioNewDirID), new_dir_item); if (result != noErr) return result; // Append old file/dir name - add_path_component(fs_item->name); + add_path_comp(fs_item->name); // Does the new name already exist? if (access(full_path, F_OK) == 0) @@ -1963,12 +2065,13 @@ static int16 fs_cat_move(uint32 pb) // Move item D(bug(" moving %s -> %s\n", old_path, full_path)); - if (rename(old_path, full_path) < 0) + if (!extfs_rename(old_path, full_path)) return errno2oserr(); else { // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems FSItem *new_item = find_fsitem(fs_item->name, new_dir_item); if (new_item) { + swap_parent_ids(fs_item->id, new_item->id); uint32 t = fs_item->id; fs_item->id = new_item->id; new_item->id = t; @@ -1980,15 +2083,15 @@ static int16 fs_cat_move(uint32 pb) // Open working directory (WDParam) static int16 fs_open_wd(uint32 pb) { - D(bug(" fs_open_wd(%08lx), vRefNum %d, name %#s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), ReadMacInt32(pb + ioWDDirID))); + D(bug(" fs_open_wd(%08lx), vRefNum %d, name %.31s, dirID %d\n", pb, ReadMacInt16(pb + ioVRefNum), Mac2HostAddr(ReadMacInt32(pb + ioNamePtr) + 1), ReadMacInt32(pb + ioWDDirID))); M68kRegisters r; // Allocate WDCB D(bug(" allocating WDCB\n")); r.a[0] = pb; Execute68k(fs_data + fsAllocateWDCB, &r); - D(bug(" UTAllocateWDCB returned %d\n", r.d[0])); - return r.d[0]; + D(bug(" UTAllocateWDCB returned %d, refNum is %d\n", r.d[0], ReadMacInt16(pb + ioVRefNum))); + return (int16)r.d[0]; } // Close working directory (WDParam) @@ -2002,7 +2105,7 @@ static int16 fs_close_wd(uint32 pb) r.d[0] = ReadMacInt16(pb + ioVRefNum); Execute68k(fs_data + fsReleaseWDCB, &r); D(bug(" UTReleaseWDCB returned %d\n", r.d[0])); - return r.d[0]; + return (int16)r.d[0]; } // Query information about working directory (WDParam) @@ -2016,7 +2119,7 @@ static int16 fs_get_wd_info(uint32 pb, u WriteMacInt32(pb + ioWDProcID, 0); WriteMacInt16(pb + ioWDVRefNum, ReadMacInt16(vcb + vcbVRefNum)); if (ReadMacInt32(pb + ioNamePtr)) - memcpy(Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), Mac2HostAddr(vcb + vcbVN), 28); + Mac2Mac_memcpy(ReadMacInt32(pb + ioNamePtr), vcb + vcbVN, 28); WriteMacInt32(pb + ioWDDirID, ROOT_ID); return noErr; } @@ -2031,13 +2134,13 @@ static int16 fs_get_wd_info(uint32 pb, u uint32 wdcb = ReadMacInt32(fs_data + fsReturn); D(bug(" UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID))); if (r.d[0] & 0xffff) - return r.d[0]; + return (int16)r.d[0]; // Return information - WriteMacInt16(pb + ioWDProcID, ReadMacInt32(wdcb + wdProcID)); + WriteMacInt32(pb + ioWDProcID, ReadMacInt32(wdcb + wdProcID)); WriteMacInt16(pb + ioWDVRefNum, ReadMacInt16(ReadMacInt32(wdcb + wdVCBPtr) + vcbVRefNum)); if (ReadMacInt32(pb + ioNamePtr)) - memcpy(Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), Mac2HostAddr(ReadMacInt32(wdcb + wdVCBPtr) + vcbVN), 28); + Mac2Mac_memcpy(ReadMacInt32(pb + ioNamePtr), ReadMacInt32(wdcb + wdVCBPtr) + vcbVN, 28); WriteMacInt32(pb + ioWDDirID, ReadMacInt32(wdcb + wdDirID)); return noErr; }