Example #1
0
File: main.c Project: Airr/osxutils
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;
}
Example #2
0
File: main.c Project: Airr/osxutils
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;
}
Example #3
0
File: main.c Project: Airr/osxutils
static OSErr PrintCreatorCode (FSRef *fileRef)
{
	FSSpec		fileSpec;
    FInfo 		finderInfo;
	OSErr		err = noErr;
	char		creatorType[5] = "\0";
	
	//if it's a folder
	if (IsFolder(fileRef))
	{
		printf("MACS\n");
		return 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;
    }
	// get Finder File Info
	err = FSGetFInfo (fileRef, &finderInfo);
	if (err != noErr)
	{
		fprintf(stderr, "FSpGetFInfo(): Error %d getting file Finder File Info from file spec\n", err);
		return err;
	}
	
	/* get creator type string */
	OSTypeToStr(finderInfo.fdCreator, creatorType);
	
	//print it
	if (strlen(creatorType) != 0)
		printf("%s\n", creatorType);

	return 0;
}
Example #4
0
static OSErr myFSCreateResFile(const char *path, OSType creator, OSType fileType, FSRef *outRef) {
  int fd = open(path, O_CREAT | O_WRONLY, 0666);
  if (fd == -1) {
    perror("opening destination:");
    return bdNamErr;
  }
  close(fd);

  FSRef ref;

  OSErr err = FSPathMakeRef((const UInt8*)path, &ref, NULL);
  if (err != noErr)
    return err;

  HFSUniStr255 rname;
  FSGetResourceForkName(&rname);

  err = FSCreateResourceFork(&ref, rname.length, rname.unicode, 0);
  if (err != noErr)
    return err;

  FInfo finfo;

  err = FSGetFInfo(&ref, &finfo);
  if (err != noErr)
    return err;

  finfo.fdCreator = creator;
  finfo.fdType = fileType;

  err = FSSetFInfo(&ref, &finfo);
  if (err != noErr)
      return err;

  *outRef = ref;

  return noErr;
}
Example #5
0
//////////////////////////////////////////////////////////////////////////////////////////////////
//
//  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
  {
Example #6
0
///////////////////////////////////////////////////////////////////
// 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;

  //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)
  {
    DInfo dInfo;
    err = FSGetDInfo (&fileRef, &dInfo);

    if (err != noErr) {
      fprintf(stderr, "Error %d getting file Finder Directory Info\n", err);

      return;
    }

    //get current label
    currentLabel = GetLabelNumber(dInfo.frFlags);

    //set new label into record
    SetLabelInFlags(&dInfo.frFlags, labelNum);

    err = FSSetDInfo(&fileRef, &dInfo);
    if (err != noErr) {
      fprintf(stderr, "Error %d setting file Finder Directory Info\n", err);
      return;
    }
  }

  ///////////////////////// IF SPECIFIED FILE IS A REGULAR FILE /////////////////////////

  else
  {
    /* get the finder info */
    err = FSGetFInfo (&fileRef, &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 = FSSetFInfo (&fileRef, &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;
}