--- BasiliskII/src/extfs.cpp 2001/07/11 19:26:13 1.27 +++ BasiliskII/src/extfs.cpp 2008/01/01 09:40:31 1.35 @@ -1,7 +1,7 @@ /* * extfs.cpp - MacOS file system for native file system access * - * Basilisk II (C) 1997-2001 Christian Bauer + * Basilisk II (C) 1997-2008 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 @@ -48,6 +48,10 @@ #include #endif +#if defined __APPLE__ && defined __MACH__ +#include +#endif + #include "cpu_emulation.h" #include "emul_op.h" #include "main.h" @@ -154,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; // 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 }; @@ -165,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 */ @@ -179,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; + p->name = new char[strlen(name) + 1]; + strcpy(p->name, name); + 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 @@ -194,19 +257,25 @@ 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(macroman_to_host_encoding(guest_name), guest_name, parent); +} /* * Get full path (->full_path) for given FSItem @@ -347,7 +416,9 @@ void ExtFSInit(void) p->id = ROOT_PARENT_ID; p->parent_id = 0; p->parent = NULL; + p->name = new char[1]; p->name[0] = 0; + p->guest_name[0] = 0; // Create root FSItem p = new FSItem; @@ -357,8 +428,11 @@ void ExtFSInit(void) p->id = ROOT_ID; p->parent_id = ROOT_PARENT_ID; p->parent = first_fs_item; - strncpy(p->name, GetString(STR_EXTFS_VOLUME_NAME), 32); - p->name[31] = 0; + const char *volume_name = GetString(STR_EXTFS_VOLUME_NAME); + p->name = new char[strlen(volume_name) + 1]; + strcpy(p->name, volume_name); + 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) { @@ -381,6 +455,7 @@ void ExtFSExit(void) FSItem *p = first_fs_item, *next; while (p) { next = p->next; + delete[] p->name; delete p; p = next; } @@ -869,7 +944,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 @@ -892,7 +967,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); } } } @@ -976,6 +1051,8 @@ static int16 fs_volume_mount(uint32 pb) WriteMacInt16(vcb + vcbSigWord, 0x4244); #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 @@ -1039,6 +1116,8 @@ static int16 fs_get_vol_info(uint32 pb, pstrcpy((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), VOLUME_NAME); #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 @@ -1224,13 +1303,15 @@ 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); #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 @@ -1345,7 +1426,7 @@ 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); @@ -1353,6 +1434,8 @@ read_next_de: WriteMacInt32(pb + ioFlParID, fs_item->parent_id); #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 @@ -1514,7 +1597,7 @@ static int16 fs_open(uint32 pb, uint32 d 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; }