filespecutils_carbon.cpp

来自「symbian 下的helix player源代码」· C++ 代码 · 共 1,621 行 · 第 1/3 页

CPP
1,621
字号
/* ***** BEGIN LICENSE BLOCK *****
 * Source last modified: $Id: filespecutils_carbon.cpp,v 1.4.32.3 2004/07/09 01:44:13 hubbe Exp $
 * 
 * Portions Copyright (c) 1995-2004 RealNetworks, Inc. All Rights Reserved.
 * 
 * The contents of this file, and the files included with this file,
 * are subject to the current version of the RealNetworks Public
 * Source License (the "RPSL") available at
 * http://www.helixcommunity.org/content/rpsl unless you have licensed
 * the file under the current version of the RealNetworks Community
 * Source License (the "RCSL") available at
 * http://www.helixcommunity.org/content/rcsl, in which case the RCSL
 * will apply. You may also obtain the license terms directly from
 * RealNetworks.  You may not use this file except in compliance with
 * the RPSL or, if you have a valid RCSL with RealNetworks applicable
 * to this file, the RCSL.  Please see the applicable RPSL or RCSL for
 * the rights, obligations and limitations governing use of the
 * contents of the file.
 * 
 * Alternatively, the contents of this file may be used under the
 * terms of the GNU General Public License Version 2 or later (the
 * "GPL") in which case the provisions of the GPL are applicable
 * instead of those above. If you wish to allow use of your version of
 * this file only under the terms of the GPL, and not to allow others
 * to use your version of this file under the terms of either the RPSL
 * or RCSL, indicate your decision by deleting the provisions above
 * and replace them with the notice and other provisions required by
 * the GPL. If you do not delete the provisions above, a recipient may
 * use your version of this file under the terms of any one of the
 * RPSL, the RCSL or the GPL.
 * 
 * This file is part of the Helix DNA Technology. RealNetworks is the
 * developer of the Original Code and owns the copyrights in the
 * portions it created.
 * 
 * This file, and the files included with this file, is distributed
 * and made available on an 'AS IS' basis, WITHOUT WARRANTY OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, AND REALNETWORKS HEREBY DISCLAIMS
 * ALL SUCH WARRANTIES, INCLUDING WITHOUT LIMITATION, ANY WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, QUIET
 * ENJOYMENT OR NON-INFRINGEMENT.
 * 
 * Technology Compatibility Kit Test Suite(s) Location:
 *    http://www.helixcommunity.org/content/tck
 * 
 * Contributor(s):
 * 
 * ***** END LICENSE BLOCK ***** */

#include "filespec.h"
#include "filespecutils.h"

#include "hxstring.h"
#include "hxtick.h"
#include "hxrand.h"
#include "carray.h"

#include "platform/mac/MoreFilesX.h"

// ------------------------------------------------------------------------------------
//
// GetFreeSpaceOnDisk
//
// ------------------------------------------------------------------------------------

HX_RESULT CHXFileSpecUtils::GetFreeSpaceOnDisk(const CHXDirSpecifier& volSpec, INT64& freeSpace)
{

	HX_RESULT		err;
	FSRef			volRef;
	FSVolumeRefNum		vRefNum;
	UInt64			freeBytes;
	UInt64			totalBytes;
	
	require_return(volSpec.IsSet(), HXR_INVALID_PARAMETER);
	
	volRef = (FSRef) volSpec;
	
	err = FSGetVRefNum(&volRef, &vRefNum);
	if (err == noErr)
	{
		HFSUniStr255* kDontCareVolName = NULL;
		
		err = FSGetVInfo(vRefNum, kDontCareVolName, &freeBytes, &totalBytes);
		if (err == noErr)
		{
			freeSpace = *(long long *) &freeBytes;
		}
	}
	return err;
}


// ------------------------------------------------------------------------------------
//
// GetTotalSpaceOnDisk
//
// ------------------------------------------------------------------------------------

HX_RESULT CHXFileSpecUtils::GetTotalSpaceOnDisk(const CHXDirSpecifier& volSpec, INT64& totalSpace)
{
	HX_RESULT		err;
	FSRef			volRef;
	FSVolumeRefNum		vRefNum;
	UInt64			freeBytes;
	UInt64			totalBytes;
	
	require_return(volSpec.IsSet(), HXR_INVALID_PARAMETER);
	
	volRef = (FSRef) volSpec;
	
	err = FSGetVRefNum(&volRef, &vRefNum);
	if (err == noErr)
	{
		HFSUniStr255* kDontCareVolName = NULL;
		
		err = FSGetVInfo(vRefNum, kDontCareVolName, &freeBytes, &totalBytes);
		if (err == noErr)
		{
			totalSpace = *(long long *) &totalBytes;
		}
	}
	return err;

}


// ------------------------------------------------------------------------------------
//
// IsFileLocal
// IsDirectoryLocal
//
// ------------------------------------------------------------------------------------

BOOL CHXFileSpecUtils::IsDiskLocal(const CHXDirSpecifier& volSpec)
{
	HParamBlockRec		pb;
	HX_RESULT		err;
	GetVolParmsInfoBuffer	buff;
	BOOL			bLocalVol;
	short			vRefNum;
	
	require_return(volSpec.IsSet(), FALSE);

	FSRef volRef;
	
	volRef = (FSRef) volSpec;
	
	err = FSGetVRefNum(&volRef, &vRefNum);
	if (err != noErr)
	{
		return TRUE;
	}
	
	ZeroInit(&pb);
	pb.ioParam.ioVRefNum = vRefNum;
	pb.ioParam.ioBuffer = (Ptr) &buff;
	pb.ioParam.ioReqCount = sizeof(buff);
	
	err = PBHGetVolParmsSync(&pb);
	
	if (err == noErr)
	{
		bLocalVol = (buff.vMServerAdr == 0);
	}
	else
	{
		// error occurred, default to true
		bLocalVol = TRUE;
	}
	
	return bLocalVol;
}



// ------------------------------------------------------------------------------------
//
// IsDiskEjectable
//
// ------------------------------------------------------------------------------------

BOOL CHXFileSpecUtils::IsDiskEjectable(const CHXDirSpecifier& volSpec)
{
	OSErr			err;
	short			vRefNum;
	GetVolParmsInfoBuffer 	volParmsInfo;
	HParamBlockRec		pb;
	FSRef 			volRef;

	require_return(volSpec.IsSet(), FALSE);
	
	volRef = (FSRef) volSpec;
	
	err = FSGetVRefNum(&volRef, &vRefNum);
	require_noerr_return(err, FALSE);
	
	ZeroInit(&volParmsInfo);
	ZeroInit(&pb);
	
	pb.ioParam.ioVRefNum = vRefNum;
	pb.ioParam.ioBuffer = (Ptr) &volParmsInfo;
	pb.ioParam.ioReqCount = sizeof(volParmsInfo);
	err = PBHGetVolParmsSync(&pb);
	if (err == noErr)
	{
		// we require version 3 of the info buffer to rely on the vMExtendedAttributes
		if (volParmsInfo.vMVersion >= 3)
		{
			if (volParmsInfo.vMExtendedAttributes & (1L << bIsEjectable))
			{
				return TRUE;
			}
		}
	}
	
	return FALSE;

}

// ------------------------------------------------------------------------------------
//
// IsDiskWritable
//
// ------------------------------------------------------------------------------------

BOOL CHXFileSpecUtils::IsDiskWritable(const CHXDirSpecifier& volSpec)
{
	FSVolumeInfo	volumeInfo;
	BOOL		bWritable;
	OSErr err;
	FSRef volRef;
	FSVolumeRefNum vRefNum;
	
	require_return(volSpec.IsSet(), FALSE);
	
	bWritable = TRUE;

	volRef = (FSRef) volSpec;
	
	err = FSGetVRefNum(&volRef, &vRefNum);
	check_noerr(err);
	
	if (err == noErr)
	{
		err = FSGetVolumeInfo(vRefNum, 0, NULL, kFSVolInfoFlags, &volumeInfo, NULL, NULL);
		if (err == noErr)
		{
			if ( 0 != (volumeInfo.flags & kFSVolFlagHardwareLockedMask) )
			{
				bWritable = FALSE;
			}
			else if ( 0 != (volumeInfo.flags & kFSVolFlagSoftwareLockedMask) )
			{
				bWritable = FALSE;
			}
		}
	}
	
	return bWritable;
	
}


// ------------------------------------------------------------------------------------
//
// GetFileSize
//
// ------------------------------------------------------------------------------------

HX_RESULT CHXFileSpecUtils::GetFileSize(const CHXFileSpecifier& fileSpec, INT64& fSize)
{
	require_return(fileSpec.IsSet(), HXR_INVALID_PARAMETER);

	HX_RESULT	err;
	FSRef		targetRef;
	UInt64		logicalSize, physicalSize;
	ItemCount	numForks;
	
	targetRef = fileSpec;

	err = FSGetTotalForkSizes(&targetRef, &logicalSize, &physicalSize, &numForks);
	if (err == noErr)
	{
		fSize = (INT64) logicalSize;
	}
	
	return err;
}

// ------------------------------------------------------------------------------------
//
// GetDirectorySize
//
// ------------------------------------------------------------------------------------

HX_RESULT CHXFileSpecUtils::GetDirectorySize(const CHXDirSpecifier& dirSpec, BOOL shouldDescend, INT64& fSize)
{
	// TODO: revise for Carbon
	
	// rather than literally recurse through the directory tree, this routine
	// will keep an array of directories yet to be added to the total size
	
	require_return(dirSpec.IsSet(), HXR_INVALID_PARAMETER);
	
	CHXPtrArray specArray;
	CHXDirSpecifier *pCurrDirSpec;
	INT64 totalSize;
	
	// push a copy of the initial directory spec into the array; 
	// it will be deleted when it is popped off
	
	pCurrDirSpec = new CHXDirSpecifier(dirSpec);
	require_nonnull_return(pCurrDirSpec, HXR_OUTOFMEMORY);
	
	specArray.Add(pCurrDirSpec);
	
	totalSize = 0;
	
	while (!specArray.IsEmpty())
	{
		FSSpec targetFSSpec;
		OSErr err;
		CInfoPBRec pb;
		short vRefNum;
		long currentDirID, index;
		Str63 fileName;
		Boolean bDoneWithThisDirectory;
		
		// grab a dirSpec from the array, delete the object, 
		// and step through all items in the directory
		
		pCurrDirSpec = (CHXDirSpecifier *) specArray.ElementAt(0);
		check_nonnull(pCurrDirSpec);
		
		if (pCurrDirSpec)
		{
			targetFSSpec = *pCurrDirSpec;
			currentDirID = pCurrDirSpec->GetDirID();
			vRefNum = targetFSSpec.vRefNum;
			
			// remove this dirSpec from our array and delete the object
			specArray.RemoveAt(0);
			HX_DELETE(pCurrDirSpec);
			check(vRefNum != 0 && currentDirID != 0);
			
			if (vRefNum != 0 && currentDirID != 0)
			{
				// step through all items in this directory
				index = 0;
				
				bDoneWithThisDirectory = false;
				while (!bDoneWithThisDirectory)
				{
					index++;
					
					ZeroInit(&pb);
					fileName[0] = 0;
					pb.hFileInfo.ioVRefNum = vRefNum;
					pb.hFileInfo.ioDirID = currentDirID;
					pb.hFileInfo.ioNamePtr = fileName;
					pb.hFileInfo.ioFDirIndex = index;

					err = PBGetCatInfoSync(&pb);					
					if (err != noErr)
					{
						// no more items in this directory
						bDoneWithThisDirectory = true;
					}
					else
					{
						if ((pb.hFileInfo.ioFlAttrib & ioDirMask) == 0)
						{
							// it's a file; add its size
							totalSize += (INT64) pb.hFileInfo.ioFlLgLen;	// data fork
							totalSize += (INT64) pb.hFileInfo.ioFlRLgLen;	// resource fork
						}
						else
						{
							// it's a directory; add a dirSpec for it to the array
							if (shouldDescend)
							{
								CHXDirSpecifier *pNewDirSpec;

								err = FSMakeFSSpec(vRefNum, currentDirID, fileName, &targetFSSpec);
								check_noerr(err);
								
								pNewDirSpec = new CHXDirSpecifier(targetFSSpec);
								check_nonnull(pNewDirSpec);
								
								if (pNewDirSpec)
								{
									specArray.Add(pNewDirSpec);
								}
								
							}
						}
					}
				} // while (!bDoneWithThisDirectory)
			}
		}
	} // while(!specArray.IsEmpty())
	
	fSize = totalSize;
	return HXR_OK;
}

// ------------------------------------------------------------------------------------
//
// GetCurrentApplication
// GetCurrentApplicationDir
//
// ------------------------------------------------------------------------------------

CHXFileSpecifier CHXFileSpecUtils::GetCurrentApplication()
{
	// TODO revise for Carbon, perhaps with GetProcessBundleLocation
	// if we're a bundle
	
	OSErr					err;
	FSSpec 					appFSSpec;
	ProcessSerialNumber 	appPSN = { 0, kCurrentProcess };
	ProcessInfoRec 			appPIR;
	CHXFileSpecifier 		appSpec;
	
	appPIR.processInfoLength = sizeof(ProcessInfoRec);
	appPIR.processAppSpec = &appFSSpec;
	appPIR.processName = NULL;
	
	err = GetProcessInformation(&appPSN, &appPIR);
	check_noerr(err);
	
	if (err == noErr)
	{
		appSpec = appFSSpec;
	}
	return appSpec;
}

CHXDirSpecifier CHXFileSpecUtils::GetCurrentApplicationDir()
{
	CHXFileSpecifier 	appSpec;
	CHXDirSpecifier 	dirSpec;
	
	appSpec = GetCurrentApplication();
	if (appSpec.IsSet())
	{
		dirSpec = appSpec.GetParentDirectory();
	}
	return dirSpec;
}

// ------------------------------------------------------------------------------------
//
// FileExists
// DirectoryExists
//
// ------------------------------------------------------------------------------------

BOOL CHXFileSpecUtils::FileExists(const CHXFileSpecifier& fileSpec)
{
	FSRef 	fileRef;
	Boolean	isDirectory;
	long *	kDontWantNodeID = NULL;
	
	if (!fileSpec.IsSet())
	{
		return FALSE;
	}
	fileRef = (FSRef) fileSpec;
	
 	return (FSGetNodeID(&fileRef, kDontWantNodeID, &isDirectory) == noErr) && !isDirectory;
}

BOOL CHXFileSpecUtils::DirectoryExists(const CHXDirSpecifier& dirSpec)
{
	FSRef 	dirRef;
	Boolean	isDirectory;
	long *	kDontWantNodeID = NULL;
	
	if (!dirSpec.IsSet())
	{
		return FALSE;
	}
	
	dirRef = (FSRef) dirSpec;
	
	return (FSGetNodeID(&dirRef, kDontWantNodeID, &isDirectory) == noErr) && isDirectory;
}

// ------------------------------------------------------------------------------------
//
// CreateDirectory
//
// ------------------------------------------------------------------------------------

HX_RESULT CHXFileSpecUtils::CreateDir(const CHXDirSpecifier& dirSpec)
{
	require_return(dirSpec.IsSet(), HXR_INVALID_PARAMETER);

	OSErr 		err;
	FSRef 		parentRef;
	CHXDirSpecifier parentSpec;
	HFSUniStr255 	hfsUni;
	UInt32		dirID;
	
	FSCatalogInfo*	kNotSettingCatInfo = NULL;
	FSRef*		kDontWantNewRef = NULL;
	FSSpec*		kDontWantFSSpec = NULL;
	
	err = HXR_INVALID_PARAMETER;
	
	// we need an FSRef for the parent where we will be creating the directory,
	// and an HFSUniStr255 of the name of the directory to be created
	
	parentSpec = dirSpec.GetParentDirectory();
	require(parentSpec.IsSet(), CantGetParent);
	
	parentRef = (FSRef) parentSpec;
	
	hfsUni = dirSpec.GetNameHFSUniStr255();
	require(hfsUni.length != 0, CantGetUnicodeName);
	
	err = FSCreateDirectoryUnicode(&parentRef, hfsUni.length, hfsUni.unicode, 
		kFSCatInfoNone, kNotSettingCatInfo, kDontWantNewRef, kDontWantFSSpec, &dirID);
	require_noerr(err, CantMakeDir);
	
	return HXR_OK;

CantMakeDir:
CantGetUnicodeName:
CantGetParent:
	return err;	  

}

// ------------------------------------------------------------------------------------
//
// RemoveDir - deletes an empty directory
//
// ------------------------------------------------------------------------------------

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?