--- BasiliskII/src/Unix/extfs_unix.cpp 1999/10/27 16:59:45 1.5 +++ BasiliskII/src/Unix/extfs_unix.cpp 2000/04/10 18:53:02 1.12 @@ -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 @@ -85,6 +85,7 @@ struct finf_struct { uint32 type; uint32 creator; uint16 flags; + uint8 pad0[22]; // total size: 32 bytes to match the size of FInfo+FXInfo }; static void make_helper_path(const char *src, char *dest, const char *add, bool only_dir = false) @@ -114,7 +115,7 @@ static int create_helper_dir(const char { char helper_dir[MAX_PATH_LENGTH]; make_helper_path(path, helper_dir, add, true); - return mkdir(helper_dir, 0755); + return mkdir(helper_dir, 0777); } static int open_helper(const char *path, const char *add, int flag) @@ -122,9 +123,9 @@ static int open_helper(const char *path, char helper_path[MAX_PATH_LENGTH]; make_helper_path(path, helper_path, add); - if ((flag & O_RDWR) || (flag && O_WRONLY)) + if ((flag & O_ACCMODE) == O_RDWR || (flag & O_ACCMODE) == O_WRONLY) flag |= O_CREAT; - int fd = open(helper_path, flag, 0644); + int fd = open(helper_path, flag, 0666); if (fd < 0) { if (errno == ENOENT && (flag & O_CREAT)) { // One path component was missing, probably the helper @@ -132,7 +133,7 @@ static int open_helper(const char *path, int ret = create_helper_dir(path, add); if (ret < 0) return ret; - fd = open(helper_path, flag, 0644); + fd = open(helper_path, flag, 0666); } } return fd; @@ -225,7 +226,7 @@ void get_finder_type(const char *path, u // Read file finf_struct finf; - if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) { + if (read(fd, &finf, sizeof(finf_struct)) >= 8) { // Type/creator are in Finder info file, return them type = ntohl(finf.type); @@ -258,7 +259,9 @@ void set_finder_type(const char *path, u return; // Read file - finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS}; + finf_struct finf; + finf.flags = DEFAULT_FINDER_FLAGS; + memset(&finf, 0, sizeof(finf_struct)); read(fd, &finf, sizeof(finf_struct)); // Set Finder flags @@ -287,7 +290,7 @@ void get_finder_flags(const char *path, // Read Finder flags finf_struct finf; - if (read(fd, &finf, sizeof(finf_struct)) == sizeof(finf_struct)) + if (read(fd, &finf, sizeof(finf_struct)) >= 10) flags = ntohs(finf.flags); // Close file @@ -302,7 +305,9 @@ void set_finder_flags(const char *path, return; // Read file - finf_struct finf = {0, 0, DEFAULT_FINDER_FLAGS}; + finf_struct finf; + memset(&finf, 0, sizeof(finf_struct)); + finf.flags = DEFAULT_FINDER_FLAGS; read(fd, &finf, sizeof(finf_struct)); // Set Finder flags @@ -347,23 +352,77 @@ void close_rfork(const char *path, int f /* * 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; +}