pascal void FSpCreateResFileCompat(const FSSpec *spec, OSType creator, OSType fileType, ScriptCode scriptTag) { #if !__MACOSSEVENFIVEONEORLATER if ( #if !__MACOSSEVENORLATER (!FSHasFSSpecCalls() && !QTHasFSSpecCalls()) || #endif /* !__MACOSSEVENORLATER */ !HasFSpCreateScriptSupportFix() ) { OSErr result; CInfoPBRec pb; HCreateResFile(spec->vRefNum, spec->parID, spec->name); if ( ResError() == noErr ) { /* get info on created item */ pb.hFileInfo.ioVRefNum = spec->vRefNum; pb.hFileInfo.ioDirID = spec->parID; pb.hFileInfo.ioNamePtr = (StringPtr) &(spec->name); pb.hFileInfo.ioFDirIndex = 0; result = PBGetCatInfoSync(&pb); if ( result == noErr ) { /* Set fdScript in FXInfo */ /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */ /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */ /* (smRoman is 0). fdScript is valid if high bit is set (see IM-6, page 9-38) */ pb.hFileInfo.ioFlXFndrInfo.fdScript = (scriptTag >= smRoman) ? ((char)scriptTag | (char)0x80) : (smRoman); /* Set creator/fileType */ pb.hFileInfo.ioFlFndrInfo.fdCreator = creator; pb.hFileInfo.ioFlFndrInfo.fdType = fileType; /* Restore ioDirID field in pb which was changed by PBGetCatInfo */ pb.hFileInfo.ioDirID = spec->parID; result = PBSetCatInfoSync(&pb); } /* Set ResErr low memory global to result */ LMSetResErr(result); } return; } else #endif /* !__MACOSSEVENFIVEONEORLATER */ { FSpCreateResFile(spec, creator, fileType, scriptTag); return; } }
pascal OSErr FSpCreateCompat(const FSSpec *spec, OSType creator, OSType fileType, ScriptCode scriptTag) { #if !__MACOSSEVENFIVEONEORLATER OSErr result; UniversalFMPB pb; if ( #if !__MACOSSEVENORLATER (!FSHasFSSpecCalls() && !QTHasFSSpecCalls()) || #endif /* !__MACOSSEVENORLATER */ !HasFSpCreateScriptSupportFix() ) { /* If FSpCreate isn't called, this code will be executed */ pb.hPB.fileParam.ioVRefNum = spec->vRefNum; pb.hPB.fileParam.ioDirID = spec->parID; pb.hPB.fileParam.ioNamePtr = (StringPtr) &(spec->name); pb.hPB.fileParam.ioFVersNum = 0; result = PBHCreateSync(&(pb.hPB)); if ( result == noErr ) { /* get info on created item */ pb.ciPB.hFileInfo.ioFDirIndex = 0; result = PBGetCatInfoSync(&(pb.ciPB)); if ( result == noErr ) { /* Set fdScript in FXInfo */ /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */ /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */ /* (smRoman is 0). fdScript is valid if high bit is set (see IM-6, page 9-38) */ pb.ciPB.hFileInfo.ioFlXFndrInfo.fdScript = (scriptTag >= smRoman) ? ((char)scriptTag | (char)0x80) : (smRoman); /* Set creator/fileType */ pb.ciPB.hFileInfo.ioFlFndrInfo.fdCreator = creator; pb.ciPB.hFileInfo.ioFlFndrInfo.fdType = fileType; /* Restore ioDirID field in pb which was changed by PBGetCatInfo */ pb.ciPB.hFileInfo.ioDirID = spec->parID; result = PBSetCatInfoSync(&(pb.ciPB)); } } return ( result ); } else #endif /* !__MACOSSEVENFIVEONEORLATER */ { return ( FSpCreate(spec, creator, fileType, scriptTag) ); } }
static void set_file_type(FSSpec * spec, int binary_file) { CInfoPBRec pb; OSErr ioResult; pb.hFileInfo.ioNamePtr = spec->name; pb.hFileInfo.ioVRefNum = spec->vRefNum; pb.hFileInfo.ioFDirIndex = 0; pb.hFileInfo.ioDirID = spec->parID; if (!(ioResult = PBGetCatInfoSync(&pb))) { pb.hFileInfo.ioFlFndrInfo.fdType = __gettype(binary_file); /*mm-960729*/ pb.hFileInfo.ioFlFndrInfo.fdCreator = __getcreator(binary_file); /*mm-960729*/ pb.hFileInfo.ioDirID = spec->parID; ioResult = PBSetCatInfoSync(&pb); } }
pascal OSErr FSpDirCreateCompat(const FSSpec *spec, ScriptCode scriptTag, long *createdDirID) { #if !__MACOSSEVENORLATER if ( !FSHasFSSpecCalls() && !QTHasFSSpecCalls() ) { OSErr result; UniversalFMPB pb; pb.hPB.fileParam.ioVRefNum = spec->vRefNum; pb.hPB.fileParam.ioDirID = spec->parID; pb.hPB.fileParam.ioNamePtr = (StringPtr) &(spec->name); result = PBDirCreateSync(&(pb.hPB)); *createdDirID = pb.hPB.fileParam.ioDirID; if ( result == noErr ) { /* get info on created item */ pb.ciPB.dirInfo.ioFDirIndex = 0; pb.ciPB.dirInfo.ioDrDirID = spec->parID; result = PBGetCatInfoSync(&(pb.ciPB)); if ( result == noErr ) { /* Set frScript in DXInfo */ /* The negative script constants (smSystemScript, smCurrentScript, and smAllScripts) */ /* don't make sense on disk, so only use scriptTag if scriptTag >= smRoman */ /* (smRoman is 0). frScript is valid if high bit is set (see IM-6, page 9-38) */ pb.ciPB.dirInfo.ioDrFndrInfo.frScript = (scriptTag >= smRoman) ? ((char)scriptTag | (char)0x80) : (smRoman); /* Restore ioDirID field in pb which was changed by PBGetCatInfo */ pb.ciPB.dirInfo.ioDrDirID = spec->parID; result = PBSetCatInfoSync(&(pb.ciPB)); } } return ( result ); } else #endif /* !__MACOSSEVENORLATER */ { return ( FSpDirCreate(spec, scriptTag, createdDirID) ); } }
/////////////////////////////////////////////////////////////////// // Make sure file exists and we have privileges. Then set the // file Finder comment. /////////////////////////////////////////////////////////////////// static void SetFileLabel (char *path, short labelNum) { OSErr err = noErr; FSRef fileRef; FSSpec fileSpec; short isFldr; short currentLabel; FInfo finderInfo; CInfoPBRec infoRec; //see if the file in question exists and we can write it if (access(path, R_OK|W_OK|F_OK) == -1) { perror(path); return; } //get file reference from path err = FSPathMakeRef((char *)path, &fileRef, NULL); if (err != noErr) { fprintf(stderr, "FSPathMakeRef: Error %d for file %s\n", err, path); return; } //retrieve filespec from file ref err = FSGetCatalogInfo (&fileRef, NULL, NULL, NULL, &fileSpec, NULL); if (err != noErr) { fprintf(stderr, "FSGetCatalogInfo(): Error %d getting file spec for %s\n", err, path); return; } /* Check if we're dealing with a folder */ isFldr = UnixIsFolder(path); if (isFldr == -1)/* an error occurred in stat */ { perror(path); return; } ///////////////////////// IF SPECIFIED FILE IS A FOLDER ///////////////////////// if (isFldr) { //Get HFS record FSpGetPBRec(&fileSpec, &infoRec); //get current label currentLabel = GetLabelNumber(infoRec.dirInfo.ioDrUsrWds.frFlags); //set new label into record SetLabelInFlags(&infoRec.dirInfo.ioDrUsrWds.frFlags, labelNum); //fill in the requisite fields infoRec.hFileInfo.ioNamePtr = (unsigned char *)fileSpec.name; infoRec.hFileInfo.ioVRefNum = fileSpec.vRefNum; infoRec.hFileInfo.ioDirID = fileSpec.parID; //set the record PBSetCatInfoSync(&infoRec); } ///////////////////////// IF SPECIFIED FILE IS A REGULAR FILE ///////////////////////// else { /* get the finder info */ err = FSpGetFInfo (&fileSpec, &finderInfo); if (err != noErr) { if (!silentMode) fprintf(stderr, "FSpGetFInfo(): Error %d getting file Finder info from file spec for file %s", err, path); } //retrieve the label number of the file currentLabel = GetLabelNumber(finderInfo.fdFlags); //if it's already set with the desired label we return if (currentLabel == labelNum) return; //set the appropriate value in the flags field SetLabelInFlags(&finderInfo.fdFlags, labelNum); //apply the settings to the file err = FSpSetFInfo (&fileSpec, &finderInfo); if (err != noErr) { if (!silentMode) fprintf(stderr, "FSpSetFInfo(): Error %d setting Finder info for %s", err, path); return; } } //print output reporting the changes made if (!silentMode) printf("%s:\n\t%s --> %s\n", path, (char *)&labelNames[currentLabel], (char *)&labelNames[labelNum]); return; }
pascal OSErr FSpExchangeFilesCompat(const FSSpec *source, const FSSpec *dest) { #if !__MACOSSEVENFIVEORLATER if ( #if !__MACOSSEVENORLATER !FSHasFSSpecCalls() || #endif /* !__MACOSSEVENORLATER */ !HasFSpExchangeFilesCompatibilityFix() ) { HParamBlockRec pb; CInfoPBRec catInfoSource, catInfoDest; OSErr result, result2; Str31 unique1, unique2; StringPtr unique1Ptr, unique2Ptr, swapola; GetVolParmsInfoBuffer volInfo; long theSeed, temp; /* Make sure the source and destination are on the same volume */ if ( source->vRefNum != dest->vRefNum ) { result = diffVolErr; goto errorExit3; } /* Try PBExchangeFiles first since it preserves the file ID reference */ pb.fidParam.ioNamePtr = (StringPtr) &(source->name); pb.fidParam.ioVRefNum = source->vRefNum; pb.fidParam.ioDestNamePtr = (StringPtr) &(dest->name); pb.fidParam.ioDestDirID = dest->parID; pb.fidParam.ioSrcDirID = source->parID; result = PBExchangeFilesSync(&pb); /* Note: The compatibility case won't work for files with *Btree control blocks. */ /* Right now the only *Btree files are created by the system. */ if ( result != noErr ) { pb.ioParam.ioNamePtr = NULL; pb.ioParam.ioBuffer = (Ptr) &volInfo; pb.ioParam.ioReqCount = sizeof(volInfo); result2 = PBHGetVolParmsSync(&pb); /* continue if volume has no fileID support (or no GetVolParms support) */ if ( (result2 == noErr) && hasFileIDs(volInfo) ) { goto errorExit3; } /* Get the catalog information for each file */ /* and make sure both files are *really* files */ catInfoSource.hFileInfo.ioVRefNum = source->vRefNum; catInfoSource.hFileInfo.ioFDirIndex = 0; catInfoSource.hFileInfo.ioNamePtr = (StringPtr) &(source->name); catInfoSource.hFileInfo.ioDirID = source->parID; catInfoSource.hFileInfo.ioACUser = 0; /* ioACUser used to be filler2 */ result = PBGetCatInfoSync(&catInfoSource); if ( result != noErr ) { goto errorExit3; } if ( (catInfoSource.hFileInfo.ioFlAttrib & kioFlAttribDirMask) != 0 ) { result = notAFileErr; goto errorExit3; } catInfoDest.hFileInfo.ioVRefNum = dest->vRefNum; catInfoDest.hFileInfo.ioFDirIndex = 0; catInfoDest.hFileInfo.ioNamePtr = (StringPtr) &(dest->name); catInfoDest.hFileInfo.ioDirID = dest->parID; catInfoDest.hFileInfo.ioACUser = 0; /* ioACUser used to be filler2 */ result = PBGetCatInfoSync(&catInfoDest); if ( result != noErr ) { goto errorExit3; } if ( (catInfoDest.hFileInfo.ioFlAttrib & kioFlAttribDirMask) != 0 ) { result = notAFileErr; goto errorExit3; } /* generate 2 filenames that are unique in both directories */ theSeed = 0x64666A6C; /* a fine unlikely filename */ unique1Ptr = (StringPtr)&unique1; unique2Ptr = (StringPtr)&unique2; result = GenerateUniqueName(source->vRefNum, &theSeed, source->parID, dest->parID, unique1Ptr); if ( result != noErr ) { goto errorExit3; } GenerateUniqueName(source->vRefNum, &theSeed, source->parID, dest->parID, unique2Ptr); if ( result != noErr ) { goto errorExit3; } /* rename source to unique1 */ pb.fileParam.ioNamePtr = (StringPtr) &(source->name); pb.ioParam.ioMisc = (Ptr) unique1Ptr; pb.ioParam.ioVersNum = 0; result = PBHRenameSync(&pb); if ( result != noErr ) { goto errorExit3; } /* rename dest to unique2 */ pb.ioParam.ioMisc = (Ptr) unique2Ptr; pb.ioParam.ioVersNum = 0; pb.fileParam.ioNamePtr = (StringPtr) &(dest->name); pb.fileParam.ioDirID = dest->parID; result = PBHRenameSync(&pb); if ( result != noErr ) { goto errorExit2; /* back out gracefully by renaming unique1 back to source */ } /* If files are not in same directory, swap their locations */ if ( source->parID != dest->parID ) { /* move source file to dest directory */ pb.copyParam.ioNamePtr = unique1Ptr; pb.copyParam.ioNewName = NULL; pb.copyParam.ioNewDirID = dest->parID; pb.copyParam.ioDirID = source->parID; result = PBCatMoveSync((CMovePBPtr) &pb); if ( result != noErr ) { goto errorExit1; /* back out gracefully by renaming both files to original names */ } /* move dest file to source directory */ pb.copyParam.ioNamePtr = unique2Ptr; pb.copyParam.ioNewDirID = source->parID; pb.copyParam.ioDirID = dest->parID; result = PBCatMoveSync((CMovePBPtr) &pb); if ( result != noErr) { /* life is very bad. We'll at least try to move source back */ pb.copyParam.ioNamePtr = unique1Ptr; pb.copyParam.ioNewName = NULL; pb.copyParam.ioNewDirID = source->parID; pb.copyParam.ioDirID = dest->parID; (void) PBCatMoveSync((CMovePBPtr) &pb); /* ignore errors */ goto errorExit1; /* back out gracefully by renaming both files to original names */ } } /* Make unique1Ptr point to file in source->parID */ /* and unique2Ptr point to file in dest->parID */ /* This lets us fall through to the rename code below */ swapola = unique1Ptr; unique1Ptr = unique2Ptr; unique2Ptr = swapola; /* At this point, the files are in their new locations (if they were moved) */ /* Source is named Unique1 (name pointed to by unique2Ptr) and is in dest->parID */ /* Dest is named Unique2 (name pointed to by unique1Ptr) and is in source->parID */ /* Need to swap attributes except mod date and swap names */ /* swap the catalog info by re-aiming the CInfoPB's */ catInfoSource.hFileInfo.ioNamePtr = unique1Ptr; catInfoDest.hFileInfo.ioNamePtr = unique2Ptr; catInfoSource.hFileInfo.ioDirID = source->parID; catInfoDest.hFileInfo.ioDirID = dest->parID; /* Swap the original mod dates with each file */ temp = catInfoSource.hFileInfo.ioFlMdDat; catInfoSource.hFileInfo.ioFlMdDat = catInfoDest.hFileInfo.ioFlMdDat; catInfoDest.hFileInfo.ioFlMdDat = temp; /* Here's the swap (ignore errors) */ (void) PBSetCatInfoSync(&catInfoSource); (void) PBSetCatInfoSync(&catInfoDest); /* rename unique2 back to dest */ errorExit1: pb.ioParam.ioMisc = (Ptr) &(dest->name); pb.ioParam.ioVersNum = 0; pb.fileParam.ioNamePtr = unique2Ptr; pb.fileParam.ioDirID = dest->parID; (void) PBHRenameSync(&pb); /* ignore errors */ /* rename unique1 back to source */ errorExit2: pb.ioParam.ioMisc = (Ptr) &(source->name); pb.ioParam.ioVersNum = 0; pb.fileParam.ioNamePtr = unique1Ptr; pb.fileParam.ioDirID = source->parID; (void) PBHRenameSync(&pb); /* ignore errors */ } errorExit3: { /* null statement */ } return ( result ); } else #endif /* !__MACOSSEVENFIVEORLATER */ { return ( FSpExchangeFiles(source, dest) ); } }