ViewVC Help
View File | Revision Log | Show Annotations | Revision Graph | Root Listing
root/cebix/BasiliskII/src/extfs.cpp
(Generate patch)

Comparing BasiliskII/src/extfs.cpp (file contents):
Revision 1.10 by cebix, 1999-11-03T19:08:52Z vs.
Revision 1.30 by gbeauche, 2005-01-30T21:42:13Z

# Line 1 | Line 1
1   /*
2 < *  extfs.cpp - MacOS file system for access native file system access
2 > *  extfs.cpp - MacOS file system for native file system access
3   *
4 < *  Basilisk II (C) 1997-1999 Christian Bauer
4 > *  Basilisk II (C) 1997-2005 Christian Bauer
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 40 | Line 40
40   #include <string.h>
41   #include <stdio.h>
42   #include <stdlib.h>
43 #include <unistd.h>
43   #include <fcntl.h>
45 #include <dirent.h>
44   #include <errno.h>
45  
46 + #ifndef WIN32
47 + #include <unistd.h>
48 + #include <dirent.h>
49 + #endif
50 +
51   #include "cpu_emulation.h"
49 #include "macos_util.h"
52   #include "emul_op.h"
53   #include "main.h"
54   #include "disk.h"
# Line 55 | Line 57
57   #include "extfs.h"
58   #include "extfs_defs.h"
59  
60 + #ifdef WIN32
61 + # include "posix_emu.h"
62 + #endif
63 +
64   #define DEBUG 0
65   #include "debug.h"
66  
# Line 65 | Line 71 | enum {
71          fsHFSProcStub = 6,
72          fsDrvStatus = 12,                               // Drive Status record
73          fsFSD = 42,                                             // File system descriptor
74 <        fsPB = 238,                                             // IOParam (for mounting and renaming)
74 >        fsPB = 238,                                             // IOParam (for mounting and renaming), also used for temporary storage
75          fsVMI = 288,                                    // VoumeMountInfoHeader (for mounting)
76          fsParseRec = 296,                               // ParsePathRec struct
77          fsReturn = 306,                                 // Area for return data of 68k routines
# Line 101 | Line 107 | static bool ready = false;
107   static struct stat root_stat;
108  
109   // File system ID/media type
110 < const int16 MY_FSID = 'ba';
111 < const uint32 MY_MEDIA_TYPE = 'basi';
110 > const int16 MY_FSID = EMULATOR_ID_2;
111 > const uint32 MY_MEDIA_TYPE = EMULATOR_ID_4;
112  
113   // CNID of root and root's parent
114   const uint32 ROOT_ID = 2;
# Line 111 | Line 117 | const uint32 ROOT_PARENT_ID = 1;
117   // File system stack size
118   const int STACK_SIZE = 0x10000;
119  
120 + // Allocation block and clump size as reported to MacOS (these are of course
121 + // not the real values and have no meaning on the host OS)
122 + const int AL_BLK_SIZE = 0x4000;
123 + const int CLUMP_SIZE = 0x4000;
124 +
125   // Drive number of our pseudo-drive
126   static int drive_number;
127  
# Line 223 | Line 234 | static void get_path_for_fsitem(FSItem *
234  
235  
236   /*
237 + *  Exchange parent CNIDs in all FSItems
238 + */
239 +
240 + static void swap_parent_ids(uint32 parent1, uint32 parent2)
241 + {
242 +        FSItem *p = first_fs_item;
243 +        while (p) {
244 +                if (p->parent_id == parent1)
245 +                        p->parent_id = parent2;
246 +                else if (p->parent_id == parent2)
247 +                        p->parent_id = parent1;
248 +                p = p->next;
249 +        }
250 + }
251 +
252 +
253 + /*
254   *  String handling functions
255   */
256  
# Line 240 | Line 268 | static void cstr2pstr(char *dst, const c
268          *dst++ = strlen(src);
269          char c;
270          while ((c = *src++) != 0) {
271 +                // Note: we are converting host ':' characters to Mac '/' characters here
272 +                // '/' is not a path separator as this function is only used on object names
273                  if (c == ':')
274                          c = '/';
275                  *dst++ = c;
276          }
277   }
278  
249 // Convert pascal string to C string
250 static void pstr2cstr(char *dst, const char *src)
251 {
252        int size = *src++;
253        while (size--) {
254                char c = *src++;
255                if (c == '/')
256                        c = ':';
257                *dst++ = c;
258        }
259        *dst = 0;
260 }
261
279   // Convert string (no length byte) to C string, length given separately
280   static void strn2cstr(char *dst, const char *src, int size)
281   {
282          while (size--) {
283                  char c = *src++;
284 +                // Note: we are converting Mac '/' characters to host ':' characters here
285 +                // '/' is not a path separator as this function is only used on object names
286                  if (c == '/')
287                          c = ':';
288                  *dst++ = c;
# Line 339 | Line 358 | void ExtFSInit(void)
358          p->parent_id = ROOT_PARENT_ID;
359          p->parent = first_fs_item;
360          strncpy(p->name, GetString(STR_EXTFS_VOLUME_NAME), 32);
361 +        p->name[31] = 0;
362  
363          // Find path for root
364          if ((RootPath = PrefsFindString("extfs")) != NULL) {
# Line 387 | Line 407 | void InstallExtFS(void)
407          // FSM present?
408          r.d[0] = gestaltFSAttr;
409          Execute68kTrap(0xa1ad, &r);     // Gestalt()
410 <        D(bug("FSAttr %ld, %08lx\n", r.d[0], r.a[0]));
410 >        D(bug("FSAttr %d, %08x\n", r.d[0], r.a[0]));
411          if ((r.d[0] & 0xffff) || !(r.a[0] & (1 << gestaltHasFileSystemManager))) {
412                  printf("WARNING: No FSM present, disabling ExtFS\n");
413                  return;
# Line 396 | Line 416 | void InstallExtFS(void)
416          // Yes, version >=1.2?
417          r.d[0] = gestaltFSMVersion;
418          Execute68kTrap(0xa1ad, &r);     // Gestalt()
419 <        D(bug("FSMVersion %ld, %08lx\n", r.d[0], r.a[0]));
419 >        D(bug("FSMVersion %d, %08x\n", r.d[0], r.a[0]));
420          if ((r.d[0] & 0xffff) || (r.a[0] < 0x0120)) {
421                  printf("WARNING: FSM <1.2 found, disabling ExtFS\n");
422                  return;
# Line 666 | Line 686 | int16 ExtFSComm(uint16 message, uint32 p
686                  }
687  
688                  case ffsIDDiskMessage: {                // Check if volume is handled by our FS
689 <                        if (ReadMacInt16(paramBlock + ioVRefNum) == drive_number)
689 >                        if ((int16)ReadMacInt16(paramBlock + ioVRefNum) == drive_number)
690                                  return noErr;
691                          else
692                                  return extFSErr;
# Line 710 | Line 730 | static int16 get_current_dir(uint32 pb,
730          if (no_vol_name)
731                  WriteMacInt32(pb + ioNamePtr, name_ptr);
732          int16 status = ReadMacInt16(fs_data + fsReturn);
713        int16 more_matches = ReadMacInt16(fs_data + fsReturn + 2);
714        int16 vRefNum = ReadMacInt16(fs_data + fsReturn + 4);
715        uint32 vcb = ReadMacInt32(fs_data + fsReturn + 6);
733          D(bug("  UTDetermineVol() returned %d, status %d\n", r.d[0], status));
734 <        result = r.d[0] & 0xffff;
734 >        result = (int16)(r.d[0] & 0xffff);
735  
736          if (result == noErr) {
737                  switch (status) {
# Line 739 | Line 756 | static int16 get_current_dir(uint32 pb,
756                                          Execute68k(fs_data + fsResolveWDCB, &r);
757                                          uint32 wdcb = ReadMacInt32(fs_data + fsReturn);
758                                          D(bug("  UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID)));
759 <                                        result = r.d[0] & 0xffff;
759 >                                        result = (int16)(r.d[0] & 0xffff);
760                                          if (result == noErr)
761                                                  current_dir = ReadMacInt32(wdcb + wdDirID);
762                                  }
# Line 755 | Line 772 | static int16 get_current_dir(uint32 pb,
772                                          r.a[0] = wdpb;
773                                          Execute68k(fs_data + fsGetDefaultVol, &r);
774                                          D(bug("  UTGetDefaultVol() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdpb + ioWDDirID)));
775 <                                        result = r.d[0] & 0xffff;
775 >                                        result = (int16)(r.d[0] & 0xffff);
776                                          if (result == noErr)
777                                                  current_dir = ReadMacInt32(wdpb + ioWDDirID);
778                                  }
# Line 781 | Line 798 | static int16 get_path_component_name(uin
798          r.a[0] = rec;
799          Execute68k(fs_data + fsGetPathComponentName, &r);
800   //      D(bug("  UTGetPathComponentName returned %d\n", r.d[0]));
801 <        return r.d[0] & 0xffff;
801 >        return (int16)(r.d[0] & 0xffff);
802   }
803  
804  
# Line 817 | Line 834 | static int16 get_item_and_path(uint32 pb
834          r.a[1] = ReadMacInt32(parseRec + ppNamePtr);
835          Execute68k(fs_data + fsParsePathname, &r);
836          D(bug("  UTParsePathname() returned %d, startOffset %d\n", r.d[0], ReadMacInt16(parseRec + ppStartOffset)));
837 <        result = r.d[0] & 0xffff;
837 >        result = (int16)(r.d[0] & 0xffff);
838          if (result == noErr) {
839  
840                  // Check for leading delimiter of the partial pathname
# Line 930 | Line 947 | static uint32 find_fcb(int16 refNum)
947   static int16 fs_mount_vol(uint32 pb)
948   {
949          D(bug(" fs_mount_vol(%08lx), vRefNum %d\n", pb, ReadMacInt16(pb + ioVRefNum)));
950 <        if (ReadMacInt16(pb + ioVRefNum) == drive_number)
950 >        if ((int16)ReadMacInt16(pb + ioVRefNum) == drive_number)
951                  return noErr;
952          else
953                  return extFSErr;
# Line 947 | Line 964 | static int16 fs_volume_mount(uint32 pb)
964          r.a[0] = fs_data + fsReturn;
965          r.a[1] = fs_data + fsReturn + 2;
966          Execute68k(fs_data + fsAllocateVCB, &r);
967 + #if DEBUG
968          uint16 sysVCBLength = ReadMacInt16(fs_data + fsReturn);
969 + #endif
970          uint32 vcb = ReadMacInt32(fs_data + fsReturn + 2);
971          D(bug("  UTAllocateVCB() returned %d, vcb %08lx, size %d\n", r.d[0], vcb, sysVCBLength));
972          if (r.d[0] & 0xffff)
973 <                return r.d[0];
973 >                return (int16)r.d[0];
974  
975          // Init VCB
976          WriteMacInt16(vcb + vcbSigWord, 0x4244);
977 < #ifdef __BEOS__
978 <        WriteMacInt32(vcb + vcbCrDate, root_stat.st_crtime + TIME_OFFSET);
977 > #if defined(__BEOS__) || defined(WIN32)
978 >        WriteMacInt32(vcb + vcbCrDate, TimeToMacTime(root_stat.st_crtime));
979   #else
980          WriteMacInt32(vcb + vcbCrDate, 0);
981   #endif
982 <        WriteMacInt32(vcb + vcbLsMod, root_stat.st_mtime + TIME_OFFSET);
982 >        WriteMacInt32(vcb + vcbLsMod, TimeToMacTime(root_stat.st_mtime));
983          WriteMacInt32(vcb + vcbVolBkUp, 0);
984          WriteMacInt16(vcb + vcbNmFls, 1);                       //!!
985          WriteMacInt16(vcb + vcbNmRtDirs, 1);            //!!
986          WriteMacInt16(vcb + vcbNmAlBlks, 0xffff);       //!!
987 <        WriteMacInt32(vcb + vcbAlBlkSiz, 1024);
988 <        WriteMacInt32(vcb + vcbClpSiz, 1024);
987 >        WriteMacInt32(vcb + vcbAlBlkSiz, AL_BLK_SIZE);
988 >        WriteMacInt32(vcb + vcbClpSiz, CLUMP_SIZE);
989          WriteMacInt32(vcb + vcbNxtCNID, next_cnid);
990          WriteMacInt16(vcb + vcbFreeBks, 0xffff);        //!!
991          Host2Mac_memcpy(vcb + vcbVN, VOLUME_NAME, 28);
# Line 980 | Line 999 | static int16 fs_volume_mount(uint32 pb)
999          r.a[0] = fs_data + fsReturn;
1000          r.a[1] = vcb;
1001          Execute68k(fs_data + fsAddNewVCB, &r);
1002 <        int16 vRefNum = ReadMacInt32(fs_data + fsReturn);
1002 >        int16 vRefNum = (int16)ReadMacInt32(fs_data + fsReturn);
1003          D(bug("  UTAddNewVCB() returned %d, vRefNum %d\n", r.d[0], vRefNum));
1004          if (r.d[0] & 0xffff)
1005 <                return r.d[0];
1005 >                return (int16)r.d[0];
1006  
1007          // Post diskInsertEvent
1008          D(bug("  posting diskInsertEvent\n"));
# Line 1007 | Line 1026 | static int16 fs_unmount_vol(uint32 vcb)
1026          r.a[0] = vcb;
1027          Execute68k(fs_data + fsDisposeVCB, &r);
1028          D(bug("  UTDisposeVCB() returned %d\n", r.d[0]));
1029 <        return r.d[0];
1029 >        return (int16)r.d[0];
1030   }
1031  
1032   // Get information about a volume (HVolumeParam)
# Line 1018 | Line 1037 | static int16 fs_get_vol_info(uint32 pb,
1037          // Fill in struct
1038          if (ReadMacInt32(pb + ioNamePtr))
1039                  pstrcpy((char *)Mac2HostAddr(ReadMacInt32(pb + ioNamePtr)), VOLUME_NAME);
1040 < #ifdef __BEOS__
1041 <        WriteMacInt32(pb + ioVCrDate, root_stat.st_crtime + TIME_OFFSET);
1040 > #if defined(__BEOS__) || defined(WIN32)
1041 >        WriteMacInt32(pb + ioVCrDate, TimeToMacTime(root_stat.st_crtime));
1042   #else
1043          WriteMacInt32(pb + ioVCrDate, 0);
1044   #endif
1045 <        WriteMacInt32(pb + ioVLsMod, root_stat.st_mtime + TIME_OFFSET);
1045 >        WriteMacInt32(pb + ioVLsMod, TimeToMacTime(root_stat.st_mtime));
1046          WriteMacInt16(pb + ioVAtrb, 0);
1047          WriteMacInt16(pb + ioVNmFls, 1);                        //!!
1048          WriteMacInt16(pb + ioVBitMap, 0);
1049          WriteMacInt16(pb + ioAllocPtr, 0);
1050          WriteMacInt16(pb + ioVNmAlBlks, 0xffff);        //!!
1051 <        WriteMacInt32(pb + ioVAlBlkSiz, 1024);
1052 <        WriteMacInt32(pb + ioVClpSiz, 1024);
1051 >        WriteMacInt32(pb + ioVAlBlkSiz, AL_BLK_SIZE);
1052 >        WriteMacInt32(pb + ioVClpSiz, CLUMP_SIZE);
1053          WriteMacInt16(pb + ioAlBlSt, 0);
1054          WriteMacInt32(pb + ioVNxtCNID, next_cnid);
1055          WriteMacInt16(pb + ioVFrBlk, 0xffff);           //!!
# Line 1063 | Line 1082 | static int16 fs_get_vol_parms(uint32 pb)
1082   //      D(bug(" fs_get_vol_parms(%08lx)\n", pb));
1083  
1084          // Return parameter block
1066        uint8 vol[SIZEOF_GetVolParmsInfoBuffer];
1067        WriteMacInt16((uint32)vol + vMVersion, 2);
1068        WriteMacInt32((uint32)vol + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol);
1069        WriteMacInt32((uint32)vol + vMLocalHand, 0);
1070        WriteMacInt32((uint32)vol + vMServerAdr, 0);
1071        WriteMacInt32((uint32)vol + vMVolumeGrade, 0);
1072        WriteMacInt16((uint32)vol + vMForeignPrivID, 0);
1085          uint32 actual = ReadMacInt32(pb + ioReqCount);
1086 <        if (actual > sizeof(vol))
1087 <                actual = sizeof(vol);
1076 <        Host2Mac_memcpy(ReadMacInt32(pb + ioBuffer), vol, actual);
1086 >        if (actual > SIZEOF_GetVolParmsInfoBuffer)
1087 >                actual = SIZEOF_GetVolParmsInfoBuffer;
1088          WriteMacInt32(pb + ioActCount, actual);
1089 +        uint32 p = ReadMacInt32(pb + ioBuffer);
1090 +        if (actual > vMVersion) WriteMacInt16(p + vMVersion, 2);
1091 +        if (actual > vMAttrib) WriteMacInt32(p + vMAttrib, kNoMiniFndr | kNoVNEdit | kNoLclSync | kTrshOffLine | kNoSwitchTo | kNoBootBlks | kNoSysDir | kHasExtFSVol);
1092 +        if (actual > vMLocalHand) WriteMacInt32(p + vMLocalHand, 0);
1093 +        if (actual > vMServerAdr) WriteMacInt32(p + vMServerAdr, 0);
1094 +        if (actual > vMVolumeGrade) WriteMacInt32(p + vMVolumeGrade, 0);
1095 +        if (actual > vMForeignPrivID) WriteMacInt16(p + vMForeignPrivID, 0);
1096          return noErr;
1097   }
1098  
# Line 1089 | Line 1107 | static int16 fs_get_vol(uint32 pb)
1107          r.a[0] = pb;
1108          Execute68k(fs_data + fsGetDefaultVol, &r);
1109          D(bug("  UTGetDefaultVol() returned %d\n", r.d[0]));
1110 <        return r.d[0];
1110 >        return (int16)r.d[0];
1111   }
1112  
1113   // Set default volume (WDParam)
# Line 1145 | Line 1163 | static int16 fs_set_vol(uint32 pb, bool
1163          r.d[2] = refNum;
1164          Execute68k(fs_data + fsSetDefaultVol, &r);
1165          D(bug("  UTSetDefaultVol() returned %d\n", r.d[0]));
1166 <        return r.d[0];
1166 >        return (int16)r.d[0];
1167   }
1168  
1169   // Query file attributes (HFileParam)
# Line 1187 | Line 1205 | read_next_de:
1205                                  return fnfErr;
1206                          }
1207                          if (de->d_name[0] == '.')
1208 <                                goto read_next_de;      // Suppress name beginning with '.' (MacOS could interpret these as driver names)
1208 >                                goto read_next_de;      // Suppress names beginning with '.' (MacOS could interpret these as driver names)
1209                          //!! suppress directories
1210                  }
1211                  add_path_comp(de->d_name);
# Line 1211 | Line 1229 | read_next_de:
1229          WriteMacInt8(pb + ioFlAttrib, access(full_path, W_OK) == 0 ? 0 : faLocked);
1230          WriteMacInt32(pb + ioDirID, fs_item->id);
1231  
1232 < #ifdef __BEOS__
1233 <        WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET);
1232 > #if defined(__BEOS__) || defined(WIN32)
1233 >        WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime));
1234   #else
1235          WriteMacInt32(pb + ioFlCrDat, 0);
1236   #endif
1237 <        WriteMacInt32(pb + ioFlMdDat, st.st_mtime + TIME_OFFSET);
1237 >        WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(st.st_mtime));
1238  
1239 <        Mac_memset(pb + ioFlFndrInfo, 0, SIZEOF_FInfo);
1222 <        uint32 type, creator;   // pb may point to kernel space, but stack is switched
1223 <        get_finder_type(full_path, type, creator);
1224 <        WriteMacInt32(pb + ioFlFndrInfo + fdType, type);
1225 <        WriteMacInt32(pb + ioFlFndrInfo + fdCreator, creator);
1226 <        uint16 fflags;
1227 <        get_finder_flags(full_path, fflags);
1228 <        WriteMacInt16(pb + ioFlFndrInfo + fdFlags, fflags);
1239 >        get_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false);
1240  
1241          WriteMacInt16(pb + ioFlStBlk, 0);
1242          WriteMacInt32(pb + ioFlLgLen, st.st_size);
1243 <        WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023);
1243 >        WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1244          WriteMacInt16(pb + ioFlRStBlk, 0);
1245          uint32 rf_size = get_rfork_size(full_path);
1246          WriteMacInt32(pb + ioFlRLgLen, rf_size);
1247 <        WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023);
1247 >        WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1);
1248  
1249          if (hfs) {
1250                  WriteMacInt32(pb + ioFlBkDat, 0);
1240                Mac_memset(pb + ioFlXFndrInfo, 0, SIZEOF_FXInfo);
1251                  WriteMacInt32(pb + ioFlParID, fs_item->parent_id);
1252                  WriteMacInt32(pb + ioFlClpSiz, 0);
1253          }
# Line 1262 | Line 1272 | static int16 fs_set_file_info(uint32 pb,
1272          if (S_ISDIR(st.st_mode))
1273                  return fnfErr;
1274  
1275 <        // Set attributes
1276 <        set_finder_type(full_path, ReadMacInt32(pb + ioFlFndrInfo + fdType), ReadMacInt32(pb + ioFlFndrInfo + fdCreator));
1277 <        set_finder_flags(full_path, ReadMacInt16(pb + ioFlFndrInfo + fdFlags));
1275 >        // Set Finder info
1276 >        set_finfo(full_path, pb + ioFlFndrInfo, hfs ? pb + ioFlXFndrInfo : 0, false);
1277 >
1278          //!! times
1279          return noErr;
1280   }
# Line 1316 | Line 1326 | read_next_de:
1326                                  return fnfErr;
1327                          }
1328                          if (de->d_name[0] == '.')
1329 <                                goto read_next_de;      // Suppress name beginning with '.' (MacOS could interpret these as driver names)
1329 >                                goto read_next_de;      // Suppress names beginning with '.' (MacOS could interpret these as driver names)
1330                  }
1331                  add_path_comp(de->d_name);
1332  
# Line 1341 | Line 1351 | read_next_de:
1351          WriteMacInt8(pb + ioACUser, 0);
1352          WriteMacInt32(pb + ioDirID, fs_item->id);
1353          WriteMacInt32(pb + ioFlParID, fs_item->parent_id);
1354 < #ifdef __BEOS__
1355 <        WriteMacInt32(pb + ioFlCrDat, st.st_crtime + TIME_OFFSET);
1354 > #if defined(__BEOS__) || defined(WIN32)
1355 >        WriteMacInt32(pb + ioFlCrDat, TimeToMacTime(st.st_crtime));
1356   #else
1357          WriteMacInt32(pb + ioFlCrDat, 0);
1358   #endif
# Line 1352 | Line 1362 | read_next_de:
1362                  fs_item->mtime = mtime;
1363                  cached = false;
1364          }
1365 <        WriteMacInt32(pb + ioFlMdDat, mtime);
1365 >        WriteMacInt32(pb + ioFlMdDat, TimeToMacTime(mtime));
1366          WriteMacInt32(pb + ioFlBkDat, 0);
1367 +
1368 +        get_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode));
1369 +
1370          if (S_ISDIR(st.st_mode)) {
1358                Mac_memset(pb + ioDrUsrWds, 0, SIZEOF_DInfo);
1359                Mac_memset(pb + ioDrFndrInfo, 0, SIZEOF_DXInfo);
1360                uint16 fflags;  // pb may point to kernel space, but stack is switched
1361                get_finder_flags(full_path, fflags);
1362                WriteMacInt16(pb + ioDrUsrWds + frFlags, fflags);
1371  
1372                  // Determine number of files in directory (cached)
1373                  int count;
# Line 1374 | Line 1382 | read_next_de:
1382                                          de = readdir(d);
1383                                          if (de == NULL)
1384                                                  break;
1385 +                                        if (de->d_name[0] == '.')
1386 +                                                continue;       // Suppress names beginning with '.'
1387                                          count++;
1388                                  }
1389                                  closedir(d);
# Line 1382 | Line 1392 | read_next_de:
1392                  }
1393                  WriteMacInt16(pb + ioDrNmFls, count);
1394          } else {
1385                Mac_memset(pb + ioFlFndrInfo, 0, SIZEOF_FInfo);
1386                Mac_memset(pb + ioFlXFndrInfo, 0, SIZEOF_FXInfo);
1387                uint32 type, creator;   // pb may point to kernel space, but stack is switched
1388                get_finder_type(full_path, type, creator);
1389                WriteMacInt32(pb + ioFlFndrInfo + fdType, type);
1390                WriteMacInt32(pb + ioFlFndrInfo + fdCreator, creator);
1391                uint16 fflags;
1392                get_finder_flags(full_path, fflags);
1393                WriteMacInt16(pb + ioFlFndrInfo + fdFlags, fflags);
1395                  WriteMacInt16(pb + ioFlStBlk, 0);
1396                  WriteMacInt32(pb + ioFlLgLen, st.st_size);
1397 <                WriteMacInt32(pb + ioFlPyLen, (st.st_size + 1023) & ~1023);
1397 >                WriteMacInt32(pb + ioFlPyLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1398                  WriteMacInt16(pb + ioFlRStBlk, 0);
1399                  uint32 rf_size = get_rfork_size(full_path);
1400                  WriteMacInt32(pb + ioFlRLgLen, rf_size);
1401 <                WriteMacInt32(pb + ioFlRPyLen, (rf_size + 1023) & ~1023);
1401 >                WriteMacInt32(pb + ioFlRPyLen, (rf_size | (AL_BLK_SIZE - 1)) + 1);
1402                  WriteMacInt32(pb + ioFlClpSiz, 0);
1403          }
1404          return noErr;
# Line 1419 | Line 1420 | static int16 fs_set_cat_info(uint32 pb)
1420          if (stat(full_path, &st) < 0)
1421                  return errno2oserr();
1422  
1423 <        // Set attributes
1424 <        if (S_ISDIR(st.st_mode))
1425 <                set_finder_flags(full_path, ReadMacInt16(pb + ioDrUsrWds + frFlags));
1425 <        else {
1426 <                set_finder_type(full_path, ReadMacInt32(pb + ioFlFndrInfo + fdType), ReadMacInt32(pb + ioFlFndrInfo + fdCreator));
1427 <                set_finder_flags(full_path, ReadMacInt16(pb + ioFlFndrInfo + fdFlags));
1428 <        }
1423 >        // Set Finder info
1424 >        set_finfo(full_path, pb + ioFlFndrInfo, pb + ioFlXFndrInfo, S_ISDIR(st.st_mode));
1425 >
1426          //!! times
1427          return noErr;
1428   }
# Line 1472 | Line 1469 | static int16 fs_open(uint32 pb, uint32 d
1469                  if (access(full_path, F_OK))
1470                          return fnfErr;
1471                  fd = open_rfork(full_path, flag);
1472 <                if (fd > 0) {
1473 <                        if (fstat(fd, &st) < 0)
1472 >                if (fd >= 0) {
1473 >                        if (fstat(fd, &st) < 0) {
1474 >                                close(fd);
1475                                  return errno2oserr();
1476 +                        }
1477                  } else {        // Resource fork not supported, silently ignore it ("pseudo" resource fork)
1478                          st.st_size = 0;
1479                          st.st_mode = 0;
# Line 1483 | Line 1482 | static int16 fs_open(uint32 pb, uint32 d
1482                  fd = open(full_path, flag);
1483                  if (fd < 0)
1484                          return errno2oserr();
1485 <                if (fstat(fd, &st) < 0)
1485 >                if (fstat(fd, &st) < 0) {
1486 >                        close(fd);
1487                          return errno2oserr();
1488 +                }
1489          }
1490  
1491          // File open, allocate FCB
# Line 1496 | Line 1497 | static int16 fs_open(uint32 pb, uint32 d
1497          D(bug("  UTAllocateFCB() returned %d, fRefNum %d, fcb %08lx\n", r.d[0], ReadMacInt16(pb + ioRefNum), fcb));
1498          if (r.d[0] & 0xffff) {
1499                  close(fd);
1500 <                return r.d[0];
1500 >                return (int16)r.d[0];
1501          }
1502  
1503          // Initialize FCB, fd is stored in fcbCatPos
1504          WriteMacInt32(fcb + fcbFlNm, fs_item->id);
1505          WriteMacInt8(fcb + fcbFlags, ((flag == O_WRONLY || flag == O_RDWR) ? fcbWriteMask : 0) | (resource_fork ? fcbResourceMask : 0) | (write_ok ? 0 : fcbFileLockedMask));
1506          WriteMacInt32(fcb + fcbEOF, st.st_size);
1507 <        WriteMacInt32(fcb + fcbPLen, (st.st_size + 1023) & ~1023);
1507 >        WriteMacInt32(fcb + fcbPLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1508          WriteMacInt32(fcb + fcbCrPs, 0);
1509          WriteMacInt32(fcb + fcbVPtr, vcb);
1510 <        WriteMacInt32(fcb + fcbClmpSize, 1024);
1511 <        uint32 type, creator;   // fcb may point to kernel space, but stack is switched
1512 <        get_finder_type(full_path, type, creator);
1513 <        WriteMacInt32(fcb + fcbFType, type);
1510 >        WriteMacInt32(fcb + fcbClmpSize, CLUMP_SIZE);
1511 >
1512 >        get_finfo(full_path, fs_data + fsPB, 0, false);
1513 >        WriteMacInt32(fcb + fcbFType, ReadMacInt32(fs_data + fsPB + fdType));
1514 >
1515          WriteMacInt32(fcb + fcbCatPos, fd);
1516          WriteMacInt32(fcb + fcbDirID, fs_item->parent_id);
1517          cstr2pstr((char *)Mac2HostAddr(fcb + fcbCName), fs_item->name);
# Line 1546 | Line 1548 | static int16 fs_close(uint32 pb)
1548          r.d[0] = ReadMacInt16(pb + ioRefNum);
1549          Execute68k(fs_data + fsReleaseFCB, &r);
1550          D(bug("  UTReleaseFCB() returned %d\n", r.d[0]));
1551 <        return r.d[0];
1551 >        return (int16)r.d[0];
1552   }
1553  
1554   // Query information about FCB (FCBPBRec)
# Line 1565 | Line 1567 | static int16 fs_get_fcb_info(uint32 pb,
1567  
1568                  // Find FCB by index
1569                  WriteMacInt16(pb + ioRefNum, 0);
1570 <                for (int i=0; i<ReadMacInt16(pb + ioFCBIndx); i++) {
1570 >                for (int i=0; i<(int)ReadMacInt16(pb + ioFCBIndx); i++) {
1571                          D(bug("  indexing FCBs\n"));
1572                          r.a[0] = vcb;
1573                          r.a[1] = pb + ioRefNum;
# Line 1574 | Line 1576 | static int16 fs_get_fcb_info(uint32 pb,
1576                          fcb = ReadMacInt32(fs_data + fsReturn);
1577                          D(bug("  UTIndexFCB() returned %d, fcb %p\n", r.d[0], fcb));
1578                          if (r.d[0] & 0xffff)
1579 <                                return r.d[0];
1579 >                                return (int16)r.d[0];
1580                  }
1581          }
1582          if (fcb == 0)
# Line 1622 | Line 1624 | static int16 fs_get_eof(uint32 pb)
1624  
1625          // Adjust FCBs
1626          WriteMacInt32(fcb + fcbEOF, st.st_size);
1627 <        WriteMacInt32(fcb + fcbPLen, (st.st_size + 1023) & ~1023);
1627 >        WriteMacInt32(fcb + fcbPLen, (st.st_size | (AL_BLK_SIZE - 1)) + 1);
1628          WriteMacInt32(pb + ioMisc, st.st_size);
1629          D(bug("  adjusting FCBs\n"));
1630          r.d[0] = ReadMacInt16(pb + ioRefNum);
# Line 1657 | Line 1659 | static int16 fs_set_eof(uint32 pb)
1659  
1660          // Adjust FCBs
1661          WriteMacInt32(fcb + fcbEOF, size);
1662 <        WriteMacInt32(fcb + fcbPLen, (size + 1023) & ~1023);
1662 >        WriteMacInt32(fcb + fcbPLen, (size | (AL_BLK_SIZE - 1)) + 1);
1663          D(bug("  adjusting FCBs\n"));
1664          r.d[0] = ReadMacInt16(pb + ioRefNum);
1665          Execute68k(fs_data + fsAdjustEOF, &r);
# Line 1742 | Line 1744 | static int16 fs_read(uint32 pb)
1744   {
1745          D(bug(" fs_read(%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)));
1746  
1747 +        // Check parameters
1748 +        if ((int32)ReadMacInt32(pb + ioReqCount) < 0)
1749 +                return paramErr;
1750 +
1751          // Find FCB and fd for file
1752          uint32 fcb = find_fcb(ReadMacInt16(pb + ioRefNum));
1753          if (fcb == 0)
# Line 1773 | Line 1779 | static int16 fs_read(uint32 pb)
1779          }
1780  
1781          // Read
1782 <        size_t actual = extfs_read(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1782 >        ssize_t actual = extfs_read(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1783 >        int16 read_err = errno2oserr();
1784          D(bug("  actual %d\n", actual));
1785 <        WriteMacInt32(pb + ioActCount, actual);
1785 >        WriteMacInt32(pb + ioActCount, actual >= 0 ? actual : 0);
1786          uint32 pos = lseek(fd, 0, SEEK_CUR);
1787          WriteMacInt32(fcb + fcbCrPs, pos);
1788          WriteMacInt32(pb + ioPosOffset, pos);
1789 <        if (actual != ReadMacInt32(pb + ioReqCount))
1790 <                if (errno)
1784 <                        return errno2oserr();
1785 <                else
1786 <                        return eofErr;
1789 >        if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount))
1790 >                return actual < 0 ? read_err : eofErr;
1791          else
1792                  return noErr;
1793   }
# Line 1793 | Line 1797 | static int16 fs_write(uint32 pb)
1797   {
1798          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)));
1799  
1800 +        // Check parameters
1801 +        if ((int32)ReadMacInt32(pb + ioReqCount) < 0)
1802 +                return paramErr;
1803 +
1804          // Find FCB and fd for file
1805          uint32 fcb = find_fcb(ReadMacInt16(pb + ioRefNum));
1806          if (fcb == 0)
# Line 1824 | Line 1832 | static int16 fs_write(uint32 pb)
1832          }
1833  
1834          // Write
1835 <        size_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1835 >        ssize_t actual = extfs_write(fd, Mac2HostAddr(ReadMacInt32(pb + ioBuffer)), ReadMacInt32(pb + ioReqCount));
1836 >        int16 write_err = errno2oserr();
1837          D(bug("  actual %d\n", actual));
1838 <        WriteMacInt32(pb + ioActCount, actual);
1838 >        WriteMacInt32(pb + ioActCount, actual >= 0 ? actual : 0);
1839          uint32 pos = lseek(fd, 0, SEEK_CUR);
1840          WriteMacInt32(fcb + fcbCrPs, pos);
1841          WriteMacInt32(pb + ioPosOffset, pos);
1842 <        if (actual != ReadMacInt32(pb + ioReqCount))
1843 <                return errno2oserr();
1842 >        if (actual != (ssize_t)ReadMacInt32(pb + ioReqCount))
1843 >                return write_err;
1844          else
1845                  return noErr;
1846   }
# Line 1897 | Line 1906 | static int16 fs_delete(uint32 pb, uint32
1906                  return result;
1907  
1908          // Delete file
1909 <        if (remove(full_path) < 0) {
1910 <                int16 err = errno2oserr();
1911 <                if (errno == EISDIR) {  // Workaround for BeOS bug
1903 <                        if (rmdir(full_path) < 0)
1904 <                                return errno2oserr();
1905 <                        else
1906 <                                return noErr;
1907 <                } else
1908 <                        return err;
1909 <        } else
1909 >        if (!extfs_remove(full_path))
1910 >                return errno2oserr();
1911 >        else
1912                  return noErr;
1913   }
1914  
# Line 1939 | Line 1941 | static int16 fs_rename(uint32 pb, uint32
1941  
1942          // Rename item
1943          D(bug("  renaming %s -> %s\n", old_path, full_path));
1944 <        if (rename(old_path, full_path) < 0)
1944 >        if (!extfs_rename(old_path, full_path))
1945                  return errno2oserr();
1946          else {
1947                  // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems
1948 +                swap_parent_ids(fs_item->id, new_item->id);
1949                  uint32 t = fs_item->id;
1950                  fs_item->id = new_item->id;
1951                  new_item->id = t;
# Line 1982 | Line 1985 | static int16 fs_cat_move(uint32 pb)
1985  
1986          // Move item
1987          D(bug("  moving %s -> %s\n", old_path, full_path));
1988 <        if (rename(old_path, full_path) < 0)
1988 >        if (!extfs_rename(old_path, full_path))
1989                  return errno2oserr();
1990          else {
1991                  // The ID of the old file/dir has to stay the same, so we swap the IDs of the FSItems
1992                  FSItem *new_item = find_fsitem(fs_item->name, new_dir_item);
1993                  if (new_item) {
1994 +                        swap_parent_ids(fs_item->id, new_item->id);
1995                          uint32 t = fs_item->id;
1996                          fs_item->id = new_item->id;
1997                          new_item->id = t;
# Line 2007 | Line 2011 | static int16 fs_open_wd(uint32 pb)
2011          r.a[0] = pb;
2012          Execute68k(fs_data + fsAllocateWDCB, &r);
2013          D(bug("  UTAllocateWDCB returned %d, refNum is %d\n", r.d[0], ReadMacInt16(pb + ioVRefNum)));
2014 <        return r.d[0];
2014 >        return (int16)r.d[0];
2015   }
2016  
2017   // Close working directory (WDParam)
# Line 2021 | Line 2025 | static int16 fs_close_wd(uint32 pb)
2025          r.d[0] = ReadMacInt16(pb + ioVRefNum);
2026          Execute68k(fs_data + fsReleaseWDCB, &r);
2027          D(bug("  UTReleaseWDCB returned %d\n", r.d[0]));
2028 <        return r.d[0];
2028 >        return (int16)r.d[0];
2029   }
2030  
2031   // Query information about working directory (WDParam)
# Line 2050 | Line 2054 | static int16 fs_get_wd_info(uint32 pb, u
2054          uint32 wdcb = ReadMacInt32(fs_data + fsReturn);
2055          D(bug("  UTResolveWDCB() returned %d, dirID %d\n", r.d[0], ReadMacInt32(wdcb + wdDirID)));
2056          if (r.d[0] & 0xffff)
2057 <                return r.d[0];
2057 >                return (int16)r.d[0];
2058  
2059          // Return information
2060 <        WriteMacInt16(pb + ioWDProcID, ReadMacInt32(wdcb + wdProcID));
2060 >        WriteMacInt32(pb + ioWDProcID, ReadMacInt32(wdcb + wdProcID));
2061          WriteMacInt16(pb + ioWDVRefNum, ReadMacInt16(ReadMacInt32(wdcb + wdVCBPtr) + vcbVRefNum));
2062          if (ReadMacInt32(pb + ioNamePtr))
2063                  Mac2Mac_memcpy(ReadMacInt32(pb + ioNamePtr), ReadMacInt32(wdcb + wdVCBPtr) + vcbVN, 28);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines