/////////////////////////////////////////////////////////////////// // 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; }
////////////////////////////////////////////////////////////////////////////////////////////////// // // Given two path strings, srcPath and destPath, creates a MacOS Finder alias from file in srcPath // in the destPath, complete with custom icon and all. Pretty neat. // ////////////////////////////////////////////////////////////////////////////////////////////////// static void CreateAlias (char *srcPath, char *destPath) { OSErr err; FSSpec sourceFSSpec; FSRef srcRef, destRef; OSType srcFileType = (OSType)NULL; OSType srcCreatorType = (OSType)NULL; FInfo srcFinderInfo, destFinderInfo; int fd; SInt16 rsrcRefNum; IconRef srcIconRef; IconFamilyHandle srcIconFamily; SInt16 theLabel; AliasHandle alias; short isSrcFolder; //find out if we're dealing with a folder alias isSrcFolder = UnixIsFolder(srcPath); if (isSrcFolder == -1)//error { fprintf(stderr, "UnixIsFolder(): Error doing a stat on %s\n", srcPath); exit(EX_IOERR); } ///////////////////// Get the FSRef's and FSSpec's for source and dest /////////////////// //get file ref to src err = FSPathMakeRef(srcPath, &srcRef, NULL); if (err != noErr) { fprintf(stderr, "FSPathMakeRef: Error %d getting file ref for source \"%s\"\n", err, srcPath); exit(EX_IOERR); } //retrieve source filespec from source file ref err = FSGetCatalogInfo (&srcRef, NULL, NULL, NULL, &sourceFSSpec, NULL); if (err != noErr) { fprintf(stderr, "FSGetCatalogInfo(): Error %d getting file spec from source FSRef\n", err); exit(EX_IOERR); } //get the finder info for the source if it's a folder if (!isSrcFolder) { err = FSGetFInfo (&srcRef, &srcFinderInfo); if (err != noErr) { fprintf(stderr, "FSpGetFInfo(): Error %d getting Finder info for source \"%s\"\n", err, srcPath); exit(EX_IOERR); } srcFileType = srcFinderInfo.fdType; srcCreatorType = srcFinderInfo.fdCreator; } //////////////// Get the source file's icon /////////////////////// if (!noCustomIconCopy) { err = GetIconRefFromFileInfo( &srcRef, 0, NULL, 0, NULL, kIconServicesNormalUsageFlag, &srcIconRef, &theLabel ); if (err != noErr) fprintf(stderr, "GetIconRefFromFile(): Error getting source file's icon.\n"); IconRefToIconFamily (srcIconRef, kSelectorAllAvailableData, &srcIconFamily); } ///////////////////// Create the relevant alias record /////////////////// if (makeRelativeAlias) { // The following code for making relative aliases was borrowed from Apple. See the following technote: // // http://developer.apple.com/technotes/tn/tn1188.html // // create the new file err = myFSCreateResFile(destPath, 'TEMP', 'TEMP', &destRef); if (err != noErr) { fprintf(stderr, "FSpCreateResFile(): Error %d while creating file\n", err); exit(EX_CANTCREAT); } //create the alias record, relative to the new alias file err = FSNewAlias(&destRef, &srcRef, &alias); if (err != noErr) { fprintf(stderr, "NewAlias(): Error %d while creating relative alias\n", err); exit(EX_CANTCREAT); } // save the resource rsrcRefNum = FSOpenResFile(&destRef, fsRdWrPerm); if (rsrcRefNum == -1) { err = ResError(); fprintf(stderr, "Error %d while opening resource fork for %s\n", err, (char *)&destPath); exit(EX_IOERR); } UseResFile(rsrcRefNum); Str255 rname; AddResource((Handle) alias, rAliasType, 0, NULL); if ((err = ResError()) != noErr) { fprintf(stderr, "Error %d while adding alias resource for %s", err, (char *)&destPath); exit(EX_IOERR); } if (!noCustomIconCopy) { //write the custom icon data AddResource( (Handle)srcIconFamily, kIconFamilyType, kCustomIconResource, "\p"); } CloseResFile(rsrcRefNum); } else {