FSCopyObject.c revision ed0d4ccac4cbe85936511b94a907111e824fc588
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync Contains: A Copy/Delete Files/Folders engine which uses the HFS+ API's.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync This code is a combination of MoreFilesX and MPFileCopy
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync with some added features. This code will run on OS 9.1 and up
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync and 10.1.x (Classic and Carbon)
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync Disclaimer: IMPORTANT: This Apple software is supplied to you by Apple Computer, Inc.
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync ("Apple") in consideration of your agreement to the following terms, and your
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync use, installation, modification or redistribution of this Apple software
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync constitutes acceptance of these terms. If you do not agree with these terms,
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync please do not use, install, modify or redistribute this Apple software.
5c65eaa08f2ec993a19c9bef6e5463918e40e0ebvboxsync In consideration of your agreement to abide by the following terms, and subject
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync to these terms, Apple grants you a personal, non-exclusive license, under Apple�s
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync copyrights in this original Apple software (the "Apple Software"), to use,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync reproduce, modify and redistribute the Apple Software, with or without
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync modifications, in source and/or binary forms; provided that if you redistribute
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync the Apple Software in its entirety and without modifications, you must retain
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync this notice and the following text and disclaimers in all such redistributions of
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync the Apple Software. Neither the name, trademarks, service marks or logos of
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync Apple Computer, Inc. may be used to endorse or promote products derived from the
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync Apple Software without specific prior written permission from Apple. Except as
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync expressly stated in this notice, no other rights or licenses, express or implied,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync are granted by Apple herein, including but not limited to any patent rights that
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync may be infringed by your derivative works or by other works in which the Apple
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync Software may be incorporated.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync The Apple Software is provided by Apple on an "AS IS" basis. APPLE MAKES NO
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND OPERATION ALONE OR IN
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync COMBINATION WITH YOUR PRODUCTS.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync OF THE APPLE SOFTWARE, HOWEVER CAUSED AND WHETHER UNDER THEORY OF CONTRACT, TORT
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync (INCLUDING NEGLIGENCE), STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync Copyright � 2002 Apple Computer, Inc., All Rights Reserved
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync// Modified 2006-01-23 - added this comment.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync// The following constants control the behavior of the copy engine.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncenum { // BufferSizeForThisVolumeSpeed
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync// kDefaultCopyBufferSize = 2L * 1024 * 1024, // Fast be not very responsive.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync kDefaultCopyBufferSize = 256L * 1024, // Slower, but can still use machine.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncenum { // CalculateForksToCopy
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync kExpectedForkCount = 10 // Number of non-classic forks we expect.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync}; // (i.e. non resource/data forks)
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncenum { // CheckForDestInsideSource
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // for use with PBHGetDirAccess in IsDropBox
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync kPrivilegesMask = kioACAccessUserWriteMask | kioACAccessUserReadMask | kioACAccessUserSearchMask,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // for use with FSGetCatalogInfo and FSPermissionInfo->mode
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // from sys/stat.h... note -- sys/stat.h definitions are in octal
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // You can use these values to adjust the users/groups permissions
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // on a file/folder with FSSetCatalogInfo and extracting the
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // kFSCatInfoPermissions field. See code below for examples
2d97f8baccdd684bc0a8a15eb86bbe9ff2b85374vboxsync kDropFolderValue = kWriteAccessOther | kExecuteAccessOther
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync (((volParms)->vMAttrib & (1L << bHasCopyFile)) != 0)
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // The CopyParams data structure holds the copy buffer used
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // when copying the forks over, as well as special case
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // info on the destination
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // The FilterParams data structure holds the date and info
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // that the caller wants passed into the Filter Proc, as well
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // as the Filter Proc Pointer itself
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // The ForkTracker data structure holds information about a specific fork,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // specifically the name and the refnum. We use this to build a list of
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // all the forks before we start copying them. We need to do this because,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // if we're copying into a drop folder, we must open all the forks before
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // we start copying data into any of them.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Plus it's a convenient way to keep track of all the forks...
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // The FSCopyObjectGlobals data structure holds information needed to do
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // the recursive copy of a directory.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync FSRef destRef; /* FSRef to the destination directory */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync CopyParams *copyParams; /* pointer to info needed to do the copy */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync FilterParams *filterParams; /* pointer to info needed for the optional filter proc */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ItemCount maxLevels; /* maximum levels to iterate through */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ItemCount currentLevel; /* the current level FSCopyFolderLevel is on */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync Boolean quitFlag; /* set to true if filter wants to kill interation */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync Boolean containerChanged; /* temporary - set to true if the current container changed during iteration */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ItemCount actualObjects; /* number of objects returned */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsynctypedef struct FSCopyObjectGlobals FSCopyObjectGlobals;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // The FSDeleteObjectGlobals data structure holds information needed to
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // recursively delete a directory
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ItemCount actualObjects; /* number of objects returned */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsynctypedef struct FSDeleteObjectGlobals FSDeleteObjectGlobals;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync const HFSUniStr255 *destName, /* can be NULL (no rename during copy) */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr FSUsePBHCopyFile( const FSRef *srcFileRef,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync const UniChar *copyName, /* can be NULL (no rename during copy) */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync const HFSUniStr255 *destName, /* can be NULL (no rename during copy) */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr FSCopyFolderLevel( FSCopyObjectGlobals *theGlobals, const HFSUniStr255 *destName );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr CheckForDestInsideSource( const FSRef *source,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr CalculateForksToCopy( const FSRef *source,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr CalculateBufferSize( const FSRef *source,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic ByteCount BufferSizeForThisVolume(FSVolumeRefNum vRefNum);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic ByteCount BufferSizeForThisVolumeSpeed(UInt32 volumeBytesPerSecond);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr GetMagicBusyCreationDate( UTCDateTime *date );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic Boolean CompareHFSUniStr255(const HFSUniStr255 *lhs,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr FSGetVolParms( FSVolumeRefNum volRefNum,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr UnicodeNameGetHFSName( UniCharCount nameLength,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic OSErr FSDeleteFolder( const FSRef *container );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncstatic void FSDeleteFolderLevel( const FSRef *container,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // This routine acts as the top level of the copy engine. It exists
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // to a) present a nicer API than the various recursive routines, and
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // b) minimise the local variables in the recursive routines.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync const UniChar *copyName, // can be NULL (no rename during copy)
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync CopyObjectFilterProcPtr filterProcPtr, // can be NULL
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync OSErr osErr = ( source != NULL && destDir != NULL ) ? noErr : paramErr;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync BlockMoveData(copyName, destName.unicode, nameLength * sizeof(UniChar));
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // we want the settable info no matter what the user asked for
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync filterParams.whichInfo = whichInfo | kFSCatInfoSettableInfo;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync filterParams.fileSpecPtr = ( wantFSSpec ) ? &filterParams.fileSpec : NULL;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync filterParams.fileNamePtr = ( wantName ) ? &filterParams.fileName : NULL;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Calculate the optimal buffer size to copy the forks over
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // and create the buffer
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = CalculateBufferSize( source, destDir, ©Params.copyBufferSize);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync copyParams.copyBuffer = NewPtr( copyParams.copyBufferSize );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = GetMagicBusyCreationDate( ©Params.magicBusyCreateDate );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr == noErr ) // figure out if source is a file or folder
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync { // if it is on a local volume,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // if destination is a drop box
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // to figure out if the souce is a folder or directory
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSGetCatalogInfo(source, kFSCatInfoNodeFlags, &tmpCatInfo, NULL, NULL, NULL);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync isDirectory = ((tmpCatInfo.nodeFlags & kFSNodeIsDirectoryMask) != 0);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // are we copying to a drop folder?
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = IsDropBox( destDir, ©Params.copyingToDropFolder );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSGetVolParms( destVRefNum, sizeof(volParms), &volParms, NULL );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr == noErr ) // volParms.vMServerAdr is non-zero for remote volumes
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync copyParams.copyingToLocalVolume = (volParms.vMServerAdr == 0);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // now copy the file/folder...
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync { // is it a folder?
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSCopyFolder( source, destDir, destNamePtr, ©Params, &filterParams, maxLevels, newObject );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSCopyFile(source, destDir, destNamePtr, ©Params, &filterParams, newObject);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Clean up for space and safety... Who me?
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync mycheck_noerr( osErr ); // put up debug assert in debug builds
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync OSErr osErr = ( source != NULL && destDir != NULL &&
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync copyParams != NULL && filterParams != NULL ) ? noErr : paramErr;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // get needed info about the source file
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSGetCatalogInfo(source, filterParams->whichInfo, &sourceCatInfo, NULL, NULL, NULL);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSGetCatalogInfo(source, filterParams->whichInfo, &sourceCatInfo, &filterParams->fileName, NULL, NULL);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = CopyFile(source, &sourceCatInfo, destDir, &filterParams->fileName, copyParams, &tmpRef);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Call the IterateFilterProc _after_ the new file was created
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // even if an error occured
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync (void) CallCopyObjectFilterProc(filterParams->filterProcPtr, false, 0, osErr, &sourceCatInfo,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync filterParams->fileNamePtr, filterParams->yourDataPtr);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync mycheck_noerr(osErr); // put up debug assert in debug builds
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Clear the "inited" bit so that the Finder positions the icon for us.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ((FInfo *)(sourceCatInfo->finderInfo))->fdFlags &= ~kHasBeenInited;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // if the destination is on a remote volume, try to use PBHCopyFile
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSUsePBHCopyFile( source, destDir, 0, NULL, kTextEncodingUnknown, newFile );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // if PBHCopyFile didn't work or not supported,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr != noErr ) // then try old school file transfer
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = DoCopyFile( source, sourceCatInfo, destDir, destName, params, newFile );
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync mycheck_noerr(osErr); // put up debug assert in debug builds
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync const UniChar *copyName, /* can be NULL (no rename during copy) */
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // get source FSSpec from source FSRef
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSGetCatalogInfo(srcFileRef, kFSCatInfoNone, NULL, NULL, &srcFileSpec, NULL);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr == noErr ) // Make sure the volume supports CopyFile
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSGetVolParms( srcFileSpec.vRefNum, sizeof(GetVolParmsInfoBuffer), &volParmsInfo, NULL);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = VolHasCopyFile(&volParmsInfo) ? noErr : paramErr;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr == noErr ) // get the destination vRefNum and dirID
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSGetCatalogInfo(dstDirectoryRef, kFSCatInfoVolume | kFSCatInfoNodeID, &catalogInfo, NULL, NULL, NULL);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync pb.copyParam.ioNamePtr = (StringPtr)srcFileSpec.name;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync pb.copyParam.ioNewDirID = (long)catalogInfo.nodeID;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = UnicodeNameGetHFSName(nameLength, copyName, textEncodingHint, false, hfsName);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync pb.copyParam.ioCopyName = ( copyName != NULL && osErr == noErr ) ? hfsName : NULL;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr == noErr ) // tell the server to copy the object
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync myverify_noerr(FSMakeFSRef(pb.copyParam.ioDstVRefNum, pb.copyParam.ioNewDirID,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr != paramErr ) // returning paramErr is ok, it means PBHCopyFileSync was not supported
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync mycheck_noerr(osErr); // put up debug assert in debug builds
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Copies a file referenced by source to the directory referenced by
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // destDir. destName is the name the file should be given in the
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // destination directory. sourceCatInfo is the catalogue info of
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // the file, which is passed in as an optimization (we could get it
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // by doing a FSGetCatalogInfo but the caller has already done that
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // so we might as well take advantage of that).
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // If we're copying to a drop folder, we won't be able to reset this
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // information once the copy is done, so we don't mess it up in
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // the first place. We still clear the locked bit though; items dropped
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // into a drop folder always become unlocked.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Remember to clear the file's type, so the Finder doesn't
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // look at the file until we're done.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync originalFileType = ((FInfo *) &sourceCatInfo->finderInfo)->fdType;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ((FInfo *) &sourceCatInfo->finderInfo)->fdType = kFirstMagicBusyFiletype;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Remember and clear the file's locked status, so that we can
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // actually write the forks we're about to create.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Set the file's creation date to kMagicBusyCreationDate,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // remembering the old value for restoration later.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync sourceCatInfo->createDate = params->magicBusyCreateDate;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // we need to have user level read/write/execute access to the file we are going to create
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // otherwise FSCreateFileUnicode will return -5000 (afpAccessDenied),
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // and the FSRef returned will be invalid, yet the file is created (size 0k)... bug?
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync originalPermissions = *((FSPermissionInfo*)sourceCatInfo->permissions);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ((FSPermissionInfo*)sourceCatInfo->permissions)->mode |= kRWXUserAccessMask;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Classic only supports 9.1 and higher, so we don't have to worry about 2397324
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSCreateFileUnicode(destDir, destName->length, destName->unicode, kFSCatInfoSettableInfo, sourceCatInfo, &dest, NULL);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if( osErr == noErr ) // Copy the forks over to the new file
b8908d384db2324f04a2f68a13e67ea32ebf609avboxsync // Restore the original file type, creation and modification dates,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // locked status and permissions.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // This is one of the places where we need to handle drop
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // folders as a special case because this FSSetCatalogInfo will fail for
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // an item in a drop folder, so we don't even attempt it.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync if (osErr == noErr && !params->copyingToDropFolder)
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync ((FInfo *) &sourceCatInfo->finderInfo)->fdType = originalFileType;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync *((FSPermissionInfo*)sourceCatInfo->permissions) = originalPermissions;
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync osErr = FSSetCatalogInfo(&dest, kFSCatInfoSettableInfo, sourceCatInfo);
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // If we created the file and the copy failed, try to clean up by
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // deleting the file we created. We do this because, while it's
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // possible for the copy to fail halfway through and the File Manager
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // doesn't really clean up that well, we *really* don't wan't
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // any half-created files being left around.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // if the file already existed, we don't want to delete it
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // Note that there are cases where the assert can fire which are not
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // errors (for example, if the destination is in a drop folder) but
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // I'll leave it in anyway because I'm interested in discovering those
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // cases. Note that, if this fires and we're running MP, current versions
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // of MacsBug won't catch the exception and the MP task will terminate
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync // with a kMPTaskAbortedErr error.
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync else if( newRef != NULL ) // if everything was fine, then return the new file
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync mycheck_noerr(osErr); // put up debug assert in debug builds
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync/*****************************************************************************/
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsyncOSErr FSCopyFolder( const FSRef *source, const FSRef *destDir, const HFSUniStr255 *destName,
320484435d67c1fb90dca3e8836d81bf939c8ba2vboxsync CopyParams* copyParams, FilterParams *filterParams, ItemCount maxLevels, FSRef* newDir)
done = true;
done = true;
return osErr;
otherForksCount = 0;
OSErr osErr2 = CloseAllForks(dataFork.forkDestRefNum, rsrcFork.forkDestRefNum, otherForks, otherForksCount);
return osErr;
if (dataFork->forkSize != 0) // Data fork never needs to be created, so I don't have to FSCreateFork it here.
osErr = FSOpenFork(dest, dataFork->forkName.length, dataFork->forkName.unicode, fsWrPerm, &dataFork->forkDestRefNum);
if (osErr == noErr && rsrcFork->forkSize != 0) // Resource fork never needs to be created, so I don't have to FSCreateFork it here.
osErr = FSOpenFork(dest, rsrcFork->forkName.length, rsrcFork->forkName.unicode, fsWrPerm, &rsrcFork->forkDestRefNum);
osErr = FSCreateFork(dest, otherForks[thisForkIndex].forkName.length, otherForks[thisForkIndex].forkName.unicode);
osErr = FSOpenFork(dest, otherForks[thisForkIndex].forkName.length, otherForks[thisForkIndex].forkName.unicode, fsWrPerm, &otherForks[thisForkIndex].forkDestRefNum);
return osErr;
OSErr CopyFork( const FSRef *source, const FSRef *dest, const ForkTrackerPtr sourceFork, const CopyParams *params)
sourceRef = 0;
destRef = 0;
osErr = FSOpenFork(dest, sourceFork->forkName.length, sourceFork->forkName.unicode, fsWrPerm, &destRef);
osErr = FSOpenFork(source, sourceFork->forkName.length, sourceFork->forkName.unicode, fsRdPerm, &sourceRef);
// boring code except for the bytesToReadThisTime/bytesToWriteThisTime
osErr = FSReadFork(sourceRef, fsAtMark + noCacheMask, 0, bytesToReadThisTime, params->copyBuffer, NULL);
osErr = FSWriteFork(destRef, fsAtMark + noCacheMask, 0, bytesToWriteThisTime, params->copyBuffer, NULL);
if (sourceRef != 0)
return osErr;
OSErr CloseAllForks(SInt16 dataRefNum, SInt16 rsrcRefNum, ForkTrackerPtr otherForks, ItemCount otherForksCount)
if (dataRefNum != 0)
if (rsrcRefNum != 0)
return osErr;
otherForksCount = 0;
done = false;
done = true;
otherForksCount = 0;
return osErr;
return osErr;
if (volumeBytesPerSecond == 0)
return bufferSize;
if( osErr == noErr ) // delete the object (if it was a directory it is now empty, so we can delete it)
return ( osErr );
osErr = FSGetCatalogInfo(source, kFSCatInfoNodeFlags | kFSCatInfoPermissions, &tmpCatInfo, NULL, &sourceSpec, NULL);
// data passed in is correct. This is a workaround/hack for that problem,
return osErr;
return osErr;
return osErr;
return ( osErr );
hfsName[0] = 0;
if ( unicodeByteLength == 0 )
&textEncodingHint );
return ( osErr );
return ( osErr );