--- BasiliskII/src/Unix/extfs_unix.cpp 1999/10/21 22:40:01 1.3 +++ BasiliskII/src/Unix/extfs_unix.cpp 1999/11/01 16:24:15 1.6 @@ -60,14 +60,93 @@ 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, s, max_len-1); + strncat(path, component, MAX_PATH_LENGTH-1); +} + + +/* + * Finder info and resource forks are kept in helper files + * + * Finder info: + * /path/.finf/file + * Resource fork: + * /path/.rsrc/file + */ + +// Layout of Finder info helper files (all fields big-endian) +struct finf_struct { + uint32 type; + uint32 creator; + uint16 flags; + uint16 pad0; +}; + +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); + 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_RDWR) || (flag && 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); } @@ -141,7 +220,24 @@ void get_finder_type(const char *path, u type = 0; creator = 0; - // Translate file name extension to MacOS type/creator + // Open Finder info file + int fd = open_finf(path, O_RDONLY); + if (fd >= 0) { + + // Read file + finf_struct finf; + if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) { + + // Type/creator are in Finder info file, return them + type = ntohl(finf.type); + creator = ntohl(finf.creator); + close(fd); + return; + } + close(fd); + } + + // No Finder info file, 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); @@ -157,20 +253,66 @@ void get_finder_type(const char *path, u void set_finder_type(const char *path, uint32 type, uint32 creator) { + // Open Finder info file + int fd = open_finf(path, O_RDWR); + if (fd < 0) + return; + + // Read file + finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS, 0}; + read(fd, &finf, sizeof(finf_struct)); + + // Set Finder flags + finf.type = htonl(type); + finf.creator = htonl(creator); + + // Update file + lseek(fd, 0, SEEK_SET); + write(fd, &finf, sizeof(finf_struct)); + close(fd); } /* - * Get/set finder flags for file/dir specified by full path (MACOS:HFS_FLAGS attribute) + * Get/set finder flags for file/dir specified by full path */ void get_finder_flags(const char *path, uint16 &flags) { flags = DEFAULT_FINDER_FLAGS; // Default + + // Open Finder info file + int fd = open_finf(path, O_RDONLY); + if (fd < 0) + return; + + // Read Finder flags + finf_struct finf; + if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) + flags = ntohs(finf.flags); + + // Close file + close(fd); } void set_finder_flags(const char *path, uint16 flags) { + // Open Finder info file + int fd = open_finf(path, O_RDWR); + if (fd < 0) + return; + + // Read file + finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS, 0}; + read(fd, &finf, sizeof(finf_struct)); + + // Set Finder flags + finf.flags = htons(flags); + + // Update file + lseek(fd, 0, SEEK_SET); + write(fd, &finf, sizeof(finf_struct)); + close(fd); } @@ -180,16 +322,27 @@ 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); }