OSErr FSMakePath_FSRef( SInt16 volRefNum, SInt32 dirID, ConstStr255Param name, char* path, vuint32 maxPathSize) { OSStatus result; FSRef ref; // convert the inputs to an FSRef result = FSMakeFSRef(volRefNum, dirID, name, &ref); // and then convert the FSRef to a path result = FSRefMakePath(&ref, (UInt8*) path, maxPathSize); return (OSErr) result; }
/* Sets itself to current directory. */ HXBOOL CHXDirectory::SetCurrentDir() { OSErr err; long dirID; short vRefNum; FSRef currDir; err = HGetVol(NULL, &vRefNum, &dirID); if (err == noErr) { err = FSMakeFSRef(vRefNum, dirID, NULL, &currDir); if (err == noErr) { CHXDirSpecifier dirSpec(currDir); SetPath(dirSpec.GetPathName()); } } return (err == noErr); }
UINT32 CHXDirectory::Rename(const char* szOldName, const char* szNewName) { UINT32 err; // Unfortunately, the semantics of the parameters for this call aren't clear // // presumably, szOldName is a full path, or a partial path in the current directory // presumably, szNewName is a full path, or just a name CHXString oldFileStr(szOldName); CHXString newFileStr(szNewName); CHXFileSpecifier oldFileSpec; CHXFileSpecifier newFileSpec; CHXDirSpecifier destDirSpec; if (oldFileStr.Find(':') >= 0) { // the old name has a colon; convert it to a file spec oldFileSpec = oldFileStr; } if (!oldFileSpec.IsSet()) { // we couldn't get a valid FSSpec for the old name, // so assume it's relative to the current directory, // and make a file spec for it CHXDirSpecifier currPathSpec(m_strPath); oldFileSpec = currPathSpec.SpecifyChildFile((const char *) oldFileStr); } require_action(oldFileSpec.IsSet(), CantGetSourceForRename, err = fnfErr); if (newFileStr.Find(':') >= 0) { // the new name has a colon; try to convert it to a file spec newFileSpec = newFileStr; } // make a filespec for the destination folder // // use the directory of the new file if it was specified, otherwise use // the directory of the old file FSRef destFSRef; if (newFileSpec.IsSet()) { CHXDirSpecifier newParentDir = newFileSpec.GetParentDirectory(); err = FSMakeFSRef(newFileSpec.GetVRefNum(), newParentDir.GetDirID(), NULL, &destFSRef); } else { CHXDirSpecifier oldParentDir = oldFileSpec.GetParentDirectory(); err = FSMakeFSRef(oldFileSpec.GetVRefNum(), oldParentDir.GetDirID(), NULL, &destFSRef); } check_noerr(err); destDirSpec = destFSRef; // make sure we're not trying to move to another volume require_action(destDirSpec.GetVRefNum() == oldFileSpec.GetVRefNum(), CantChangeVolumes, err = HXR_FAILED); // they're on the same drive; possibly in different folders // use the name from the new file spec, if we have one, or else from the parameter HFSUniStr255 uniName; if (newFileSpec.IsSet()) { uniName = newFileSpec.GetNameHFSUniStr255(); } else { newFileStr.MakeHFSUniStr255(uniName, CFStringGetSystemEncoding()); } FSRef newFSRef; err = FSMoveRenameObjectUnicode(oldFileSpec, destDirSpec, uniName.length, uniName.unicode, kTextEncodingUnknown, &newFSRef); if (err == dupFNErr) { err = FSDeleteObject(newFileSpec); if (err == noErr) { err = FSMoveRenameObjectUnicode(oldFileSpec, destDirSpec, uniName.length, uniName.unicode, kTextEncodingUnknown, &newFSRef); } } CantChangeVolumes: CantGetSourceForRename: if (err == noErr) err = HXR_OK; else err = HXR_FAILED; return err; }