static OSErr PrintLabelName (FSRef *fileRef) { static char labelNames[8][8] = { "None", "Red", "Orange", "Yellow", "Green", "Blue", "Purple", "Gray" }; FSSpec fileSpec; FInfo finderInfo; DInfo dInfo; OSErr err = noErr; int labelNum = 0; /* retrieve filespec from file ref */ err = FSGetCatalogInfo (fileRef, NULL, NULL, NULL, &fileSpec, NULL); if (err != noErr) { fprintf(stderr, "FSGetCatalogInfo(): Error %d getting file spec from file reference\n", err); return err; } if (IsFolder(fileRef)) { err = FSGetDInfo (fileRef, &dInfo); if (err != noErr) { fprintf(stderr, "FSpGetFInfo(): Error %d getting file Finder Directory Info from file spec\n", err); return err; } labelNum = GetLabelNumber(dInfo.frFlags); } else { err = FSGetFInfo (fileRef, &finderInfo); if (err != noErr) { fprintf(stderr, "FSpGetFInfo(): Error %d getting file Finder File Info from file spec\n", err); return err; } labelNum = GetLabelNumber(finderInfo.fdFlags); } printf("%s\n", (char *)&labelNames[labelNum]); return noErr; }
static OSErr PrintLabelNumber (FSRef *fileRef) { FSSpec fileSpec; FInfo finderInfo; DInfo dInfo; OSErr err = noErr; int labelNum = 0; /* retrieve filespec from file ref */ err = FSGetCatalogInfo (fileRef, NULL, NULL, NULL, &fileSpec, NULL); if (err != noErr) { fprintf(stderr, "FSGetCatalogInfo(): Error %d getting file spec from file reference\n", err); return err; } if (IsFolder(fileRef)) { err = FSGetDInfo (fileRef, &dInfo); if (err != noErr) { fprintf(stderr, "FSpGetFInfo(): Error %d getting file Finder Directory Info from file spec\n", err); return err; } labelNum = GetLabelNumber(dInfo.frFlags); } else { err = FSGetFInfo (fileRef, &finderInfo); if (err != noErr) { fprintf(stderr, "FSpGetFInfo(): Error %d getting file Finder File Info from file spec\n", err); return err; } labelNum = GetLabelNumber(finderInfo.fdFlags); } printf("%d\n", labelNum); return noErr; }
/////////////////////////////////////////////////////////////////// // 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; }