--- BasiliskII/src/Unix/extfs_unix.cpp 1999/10/22 13:57:05 1.4 +++ BasiliskII/src/Unix/extfs_unix.cpp 2000/07/21 18:01:07 1.13 @@ -1,7 +1,7 @@ /* * extfs_unix.cpp - MacOS file system for access native file system access, Unix specific stuff * - * Basilisk II (C) 1997-1999 Christian Bauer + * Basilisk II (C) 1997-2000 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 @@ -60,19 +60,95 @@ void extfs_exit(void) * Add component to path name */ -void add_path_component(char *path, const char *component, int max_len) +void add_path_component(char *path, const char *component) { int l = strlen(path); - if (l < max_len-1 && path[l-1] != '/') { + if (l < MAX_PATH_LENGTH-1 && path[l-1] != '/') { path[l] = '/'; path[l+1] = 0; } - strncat(path, component, max_len-1); + strncat(path, component, MAX_PATH_LENGTH-1); } /* - * Get/set finder type/creator for file specified by full path + * Finder info and resource forks are kept in helper files + * + * Finder info: + * /path/.finf/file + * Resource fork: + * /path/.rsrc/file + * + * The .finf files store a FInfo/DInfo, followed by a FXInfo/DXInfo + * (16+16 bytes) + */ + +static void make_helper_path(const char *src, char *dest, const char *add, bool only_dir = false) +{ + dest[0] = 0; + + // Get pointer to last component of path + const char *last_part = strrchr(src, '/'); + if (last_part) + last_part++; + else + last_part = src; + + // Copy everything before + strncpy(dest, src, last_part-src); + dest[last_part-src] = 0; + + // Add additional component + strncat(dest, add, MAX_PATH_LENGTH-1); + + // Add last component + if (!only_dir) + strncat(dest, last_part, MAX_PATH_LENGTH-1); +} + +static int create_helper_dir(const char *path, const char *add) +{ + char helper_dir[MAX_PATH_LENGTH]; + make_helper_path(path, helper_dir, add, true); + if (helper_dir[strlen(helper_dir) - 1] == '/') // Remove trailing "/" + helper_dir[strlen(helper_dir) - 1] = 0; + return mkdir(helper_dir, 0777); +} + +static int open_helper(const char *path, const char *add, int flag) +{ + char helper_path[MAX_PATH_LENGTH]; + make_helper_path(path, helper_path, add); + + if ((flag & O_ACCMODE) == O_RDWR || (flag & O_ACCMODE) == O_WRONLY) + flag |= O_CREAT; + int fd = open(helper_path, flag, 0666); + if (fd < 0) { + if (errno == ENOENT && (flag & O_CREAT)) { + // One path component was missing, probably the helper + // directory. Try to create it and re-open the file. + int ret = create_helper_dir(path, add); + if (ret < 0) + return ret; + fd = open(helper_path, flag, 0666); + } + } + return fd; +} + +static int open_finf(const char *path, int flag) +{ + return open_helper(path, ".finf/", flag); +} + +static int open_rsrc(const char *path, int flag) +{ + return open_helper(path, ".rsrc/", flag); +} + + +/* + * Get/set finder info for file/directory specified by full path */ struct ext2type { @@ -113,6 +189,7 @@ static const ext2type e2t_translation[] {".tga", 'TPIC', 'ogle'}, {".tif", 'TIFF', 'ogle'}, {".tiff", 'TIFF', 'ogle'}, + {".htm", 'TEXT', 'MOSS'}, {".html", 'TEXT', 'MOSS'}, {".txt", 'TEXT', 'ttxt'}, {".rtf", 'TEXT', 'MSWD'}, @@ -133,44 +210,62 @@ static const ext2type e2t_translation[] {".mov", 'MooV', 'TVOD'}, {".fli", 'FLI ', 'TVOD'}, {".avi", 'VfW ', 'TVOD'}, + {".qxd", 'XDOC', 'XPR3'}, + {".hfv", 'DDim', 'ddsk'}, + {".dsk", 'DDim', 'ddsk'}, + {".img", 'rohd', 'ddsk'}, {NULL, 0, 0} // End marker }; -void get_finder_type(const char *path, uint32 &type, uint32 &creator) +void get_finfo(const char *path, uint32 finfo, uint32 fxinfo) { - type = 0; - creator = 0; + // Set default finder info + Mac_memset(finfo, 0, SIZEOF_FInfo); + if (fxinfo) + Mac_memset(fxinfo, 0, SIZEOF_FXInfo); + WriteMacInt16(finfo + fdFlags, DEFAULT_FINDER_FLAGS); + WriteMacInt32(finfo + fdLocation, (uint32)-1); + + // Read Finder info file + int fd = open_finf(path, O_RDONLY); + if (fd >= 0) { + ssize_t actual = read(fd, Mac2HostAddr(finfo), SIZEOF_FInfo); + if (fxinfo) + actual += read(fd, Mac2HostAddr(fxinfo), SIZEOF_FXInfo); + close(fd); + if (actual >= SIZEOF_FInfo) + return; + } - // Translate file name extension to MacOS type/creator - int path_len = strlen(path); - for (int i=0; e2t_translation[i].ext; i++) { - int ext_len = strlen(e2t_translation[i].ext); - if (path_len < ext_len) - continue; - if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) { - type = e2t_translation[i].type; - creator = e2t_translation[i].creator; - break; + // No Finder info file, translate file name extension to MacOS type/creator + struct stat st; + if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) { + int path_len = strlen(path); + for (int i=0; e2t_translation[i].ext; i++) { + int ext_len = strlen(e2t_translation[i].ext); + if (path_len < ext_len) + continue; + if (!strcmp(path + path_len - ext_len, e2t_translation[i].ext)) { + WriteMacInt32(finfo + fdType, e2t_translation[i].type); + WriteMacInt32(finfo + fdCreator, e2t_translation[i].creator); + break; + } } } } -void set_finder_type(const char *path, uint32 type, uint32 creator) -{ -} - - -/* - * Get/set finder flags for file/dir specified by full path (MACOS:HFS_FLAGS attribute) - */ - -void get_finder_flags(const char *path, uint16 &flags) -{ - flags = DEFAULT_FINDER_FLAGS; // Default -} - -void set_finder_flags(const char *path, uint16 flags) +void set_finfo(const char *path, uint32 finfo, uint32 fxinfo) { + // Open Finder info file + int fd = open_finf(path, O_RDWR); + if (fd < 0) + return; + + // Write file + write(fd, Mac2HostAddr(finfo), SIZEOF_FInfo); + if (fxinfo) + write(fd, Mac2HostAddr(fxinfo), SIZEOF_FXInfo); + close(fd); } @@ -180,38 +275,103 @@ void set_finder_flags(const char *path, uint32 get_rfork_size(const char *path) { - return 0; + // Open resource file + int fd = open_rsrc(path, O_RDONLY); + if (fd < 0) + return 0; + + // Get size + off_t size = lseek(fd, 0, SEEK_END); + + // Close file and return size + close(fd); + return size < 0 ? 0 : size; } int open_rfork(const char *path, int flag) { - return -1; + return open_rsrc(path, flag); } void close_rfork(const char *path, int fd) { + close(fd); } /* * Read "length" bytes from file to "buffer", - * returns number of bytes read (or 0) + * returns number of bytes read (or -1 on error) */ -size_t extfs_read(int fd, void *buffer, size_t length) +ssize_t extfs_read(int fd, void *buffer, size_t length) { - errno = 0; return read(fd, buffer, length); } /* * Write "length" bytes from "buffer" to file, - * returns number of bytes written (or 0) + * returns number of bytes written (or -1 on error) */ -size_t extfs_write(int fd, void *buffer, size_t length) +ssize_t extfs_write(int fd, void *buffer, size_t length) { - errno = 0; return write(fd, buffer, length); } + + +/* + * Remove file/directory (and associated helper files), + * returns false on error (and sets errno) + */ + +bool extfs_remove(const char *path) +{ + // Remove helpers first, don't complain if this fails + char helper_path[MAX_PATH_LENGTH]; + make_helper_path(path, helper_path, ".finf/", false); + remove(helper_path); + make_helper_path(path, helper_path, ".rsrc/", false); + remove(helper_path); + + // Now remove file or directory (and helper directories in the directory) + if (remove(path) < 0) { + if (errno == EISDIR || errno == ENOTEMPTY) { + helper_path[0] = 0; + strncpy(helper_path, path, MAX_PATH_LENGTH-1); + add_path_component(helper_path, ".finf"); + rmdir(helper_path); + helper_path[0] = 0; + strncpy(helper_path, path, MAX_PATH_LENGTH-1); + add_path_component(helper_path, ".rsrc"); + rmdir(helper_path); + return rmdir(path) == 0; + } else + return false; + } + return true; +} + + +/* + * Rename/move file/directory (and associated helper files), + * returns false on error (and sets errno) + */ + +bool extfs_rename(const char *old_path, const char *new_path) +{ + // Rename helpers first, don't complain if this fails + char old_helper_path[MAX_PATH_LENGTH], new_helper_path[MAX_PATH_LENGTH]; + make_helper_path(old_path, old_helper_path, ".finf/", false); + make_helper_path(new_path, new_helper_path, ".finf/", false); + create_helper_dir(new_path, ".finf/"); + rename(old_helper_path, new_helper_path); + make_helper_path(old_path, old_helper_path, ".rsrc/", false); + make_helper_path(new_path, new_helper_path, ".rsrc/", false); + create_helper_dir(new_path, ".rsrc/"); + rename(old_helper_path, new_helper_path); + + // Now rename file + return rename(old_path, new_path) == 0; +}