static OSErr FT_FSPathMakeRes( const UInt8* pathname, ResFileRefNum* res ) { #if HAVE_FSREF OSErr err; FSRef ref; if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) ) return FT_THROW( Cannot_Open_Resource ); /* at present, no support for dfont format */ err = FSOpenResourceFile( &ref, 0, NULL, fsRdPerm, res ); if ( noErr == err ) return err; /* fallback to original resource-fork font */ *res = FSOpenResFile( &ref, fsRdPerm ); err = ResError(); #else OSErr err; FSSpec spec; if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) ) return FT_THROW( Cannot_Open_Resource ); /* at present, no support for dfont format without FSRef */ /* (see above), try original resource-fork font */ *res = FSpOpenResFile( &spec, fsRdPerm ); err = ResError(); #endif /* HAVE_FSREF */ return err; }
void CResourceFile::LoadFile( const std::string& fpath ) { FSRef fileRef; mResRefNum = -1; OSStatus resErr = FSPathMakeRef( (const UInt8*) fpath.c_str(), &fileRef, NULL ); if( resErr == noErr ) { mResRefNum = FSOpenResFile( &fileRef, fsRdPerm ); if( mResRefNum < 0 ) { fprintf( stderr, "Warning: No Mac resource fork to import.\n" ); resErr = fnfErr; } } else { fprintf( stderr, "Error: Error %d locating input file's resource fork.\n", (int)resErr ); mResRefNum = -1; } }
static OSErr FT_FSPathMakeRes( const UInt8* pathname, ResFileRefNum* res ) { OSErr err; FSRef ref; if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) ) return FT_Err_Cannot_Open_Resource; /* at present, no support for dfont format */ err = FSOpenResourceFile( &ref, 0, NULL, fsRdPerm, res ); if ( noErr == err ) return err; /* fallback to original resource-fork font */ *res = FSOpenResFile( &ref, fsRdPerm ); err = ResError(); return err; }
static PyObject *Res_FSOpenResFile(PyObject *_self, PyObject *_args) { PyObject *_res = NULL; short _rv; FSRef ref; SignedByte permission; #ifndef FSOpenResFile PyMac_PRECHECK(FSOpenResFile); #endif if (!PyArg_ParseTuple(_args, "O&b", PyMac_GetFSRef, &ref, &permission)) return NULL; _rv = FSOpenResFile(&ref, permission); { OSErr _err = ResError(); if (_err != noErr) return PyMac_Error(_err); } _res = Py_BuildValue("h", _rv); return _res; }
////////////////////////////////////////////////////////////////////////////////////////////////// // // 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 {
/* alias ----- Will create a Macintosh HFS specific alias at the path pointed to by dest. The alias will point to the file located at target. The target must exist. The return value is zero upon success. If unsuccessful, a non-zero value will be returned and both errno and mac_errno should be consulted. */ int alias (const char *target, const char *dest) { char *rtarget; char *destparentdir, *destname; #ifdef HAVE_CORESERVICES char *targetsuffix; UniChar *udestname; FSRef *fstarget, *fsdest, *fsnewfile; FSCatalogInfo *fscdestinfo, *fsctargetinfo; FileInfo destinfo, targetinfo; AliasHandle aliasrec; u_int32_t infomap = 0; int resfrk; u_int8_t targetisdir = 0; #endif u_int8_t destisdir = 0; size_t len = 0; #ifndef HAVE_CORESERVICES /* this function is for creating macintosh aliases. if we don't have the CoreServices API it's impossible, so why fool ourselves, let's just return error now and save the cpu cycles for something else */ errno = EIO; /* returning a generic I/O error */ return errno; #endif /* As of this writing, this function is not complete. Things left to complete are to check the target to see if it has a custom icon (if so, copy the custom to the alias file so that they appear the same. Also, copy custom icons of bundles to the alias. Another very important thing left to complete is to retire the process of making the alias manually to a last resort if we cannot create the alias by sending an apple event to the finder. */ /* check for null parameters and return accordingly */ if (!target || !dest) return 0; #ifdef HAVE_CORESERVICES rtarget = (char *)malloc(PATH_MAX); if (!rtarget) return errno; /* ENOMEM */ if (!resolve_alias(target, rtarget) || mac_errno) return errno; /* convert the target path into an FSRef */ fstarget = (FSRef *)malloc(sizeof(FSRef)); if (!fstarget) return errno; /* ENOMEM */ mac_errno = FSPathMakeRef(rtarget, fstarget, &targetisdir); if (mac_errno) return 0; fsdest = (FSRef *)malloc(sizeof(FSRef)); if (!fsdest) return errno; /* ENOMEM */ /* convert the destination into an FSRef so that we can test if it exists and if it is a directory (in which we should save the new alias file inside the directory specified) */ mac_errno = FSPathMakeRef(dest, fsdest, &destisdir); #endif /* if the destination is not a directory, we'll disassemble the path we were given for the destination into a parent directory and a name */ if (!destisdir) { /* obtain the name of the destination file */ len = strlen(dest); destname = (char *)dest + len; while (destname > dest && *(destname - 1) != '/') destname--, len--; /* obtain the parent directory of the destination file */ destparentdir = (char *)malloc(len); snprintf(destparentdir, len ? len + 1 : 3, "%s", len ? dest : "./"); } else { /* the parent directory should be the destination we were passed */ destparentdir = (char *)dest; /* and the name should be the real name of the target */ destname = (char *)rtarget + strlen(rtarget); while (destname > rtarget && *(destname - 1) != '/') destname--; } #ifdef HAVE_CORESERVICES /* attempt to convert the parent directory into an FSRef */ mac_errno = FSPathMakeRef(destparentdir, fsdest, 0); if (mac_errno) return 0; /* infomap is an integer telling FSCreateResFile which settings from the FSCatalogInfo paramater you want to set */ infomap |= kFSCatInfoFinderInfo; /* if the target is not a directory, get its file type/creator */ if (!targetisdir) { fsctargetinfo = (FSCatalogInfo *)malloc(sizeof(FSCatalogInfo)); if (!fsctargetinfo) return errno; /* ENOMEM */ /* get the target files information */ mac_errno = FSGetCatalogInfo(fstarget, infomap, fsctargetinfo, 0,0,0); if (mac_errno) return 0; /* I don't know why Apple did not declare the structure member 'finderInfo' as an FileInfo type (it is an array of 16 single bytes). This makes it impossible to address elements of the finderInfo directly. So, we move it into an FileInfo data type we allocated */ memmove(&targetinfo, fsctargetinfo->finderInfo, sizeof(FileInfo)); /* this is not really necessary for the creation of the file, since a null type or creator will be transposed to '????', but should any functions try to display the type/creator, it is much friendlier to display '????' than an empty string (which hardcore mac people will understand better */ if (!targetinfo.fileType) { targetinfo.fileType = '\?\?\?\?'; targetinfo.fileCreator = '\?\?\?\?'; } } else { /* folders natively return null as their file type and creator but to make an alias appear as a folder, we use the following settings */ targetinfo.fileType = 'fldr'; targetinfo.fileCreator = 'MACS'; /* there is one exception, when the folder ends in .app it is an application package and should get the following settings */ targetsuffix = suffix(rtarget); if (!strcmp(targetsuffix, "app")) { targetinfo.fileType = 'fapa'; targetinfo.fileCreator = '\?\?\?\?'; *--targetsuffix = '\0'; /* this will cut off the .app from the name */ } } fscdestinfo = (FSCatalogInfo *)malloc(sizeof(FSCatalogInfo)); if (!fscdestinfo) return errno; /* ENOMEM */ /* set the file type, creator, and alias flag for the file */ destinfo.fileType = targetinfo.fileType; destinfo.fileCreator = targetinfo.fileCreator; destinfo.finderFlags = kIsAlias; memcpy(fscdestinfo->finderInfo, &destinfo, sizeof(FileInfo)); fsnewfile = (FSRef *)malloc(sizeof(FSRef)); if (!fsnewfile) return errno; /* ENOMEM */ udestname = (UniChar *)malloc(PATH_MAX); len = utf8to16(destname, udestname, PATH_MAX); if (len < 0) return errno; /* caller: consult both errno and mac_errno */ /* attempt to create the resource file (fails if it already exists) */ FSCreateResFile(fsdest, len, udestname, infomap, fscdestinfo, fsnewfile, 0); free(udestname); /* test for error creating the file */ mac_errno = ResError(); if (mac_errno) return 0; /* attempt to open the resource fork of the file now */ resfrk = FSOpenResFile(fsnewfile, fsRdWrPerm); /* test for errors */ mac_errno = ResError(); if (mac_errno) return 0; /* attempt to create an alias record to save in the resource file */ mac_errno = FSNewAlias(0, fstarget, &aliasrec); if (mac_errno) return CloseResFile(resfrk), 0; /* add the alias record to the resource fork */ AddResource((Handle)aliasrec, rAliasType, 0, 0); /* test for errors */ mac_errno = ResError(); if (mac_errno) return CloseResFile(resfrk), 0; /* write the resource fork data */ WriteResource((Handle)aliasrec); /* test for errors */ mac_errno = ResError(); if (mac_errno) return CloseResFile(resfrk), 0; /* clean up */ CloseResFile(resfrk); #endif /* HAVE_CORESERVICES */ /* return success */ return 0; }