📄 filespecutils.cpp
字号:
/* ***** BEGIN LICENSE BLOCK ***** * Source last modified: $Id: filespecutils.cpp,v 1.1.1.1.50.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"#ifndef _CARBON#include "morefilesextras.h"#else#include "MoreFilesX.h"#endif// ------------------------------------------------------------------------------------//// GetFreeSpaceOnDisk//// ------------------------------------------------------------------------------------HX_RESULT CHXFileSpecUtils::GetFreeSpaceOnDisk(const CHXDirSpecifier& volSpec, INT64& freeSpace){ HX_RESULT err; FSSpec targetSpec;#ifdef _CARBON UInt64 freeBytes; UInt64 totalBytes;#else short foundVRefNum; UnsignedWide freeBytes; UnsignedWide totalBytes;#endif require_return(volSpec.IsSet(), HXR_INVALID_PARAMETER); targetSpec = volSpec; const StringPtr kDontCareVolName = NULL; #ifndef _CARBON err = XGetVInfo(targetSpec.vRefNum, kDontCareVolName, &foundVRefNum, &freeBytes, &totalBytes);#else err = FSGetVInfo(targetSpec.vRefNum, (HFSUniStr255*) kDontCareVolName, &freeBytes, &totalBytes);#endif if (err == noErr) { freeSpace = *(long long *) &freeBytes; } return err;}// ------------------------------------------------------------------------------------//// GetTotalSpaceOnDisk//// ------------------------------------------------------------------------------------HX_RESULT CHXFileSpecUtils::GetTotalSpaceOnDisk(const CHXDirSpecifier& volSpec, INT64& totalSpace){ HX_RESULT err; FSSpec targetSpec;#ifdef _CARBON UInt64 freeBytes; UInt64 totalBytes;#else UnsignedWide freeBytes; UnsignedWide totalBytes; short foundVRefNum;#endif require_return(volSpec.IsSet(), HXR_INVALID_PARAMETER); targetSpec = volSpec; const StringPtr kDontCareVolName = NULL; #ifndef _CARBON err = XGetVInfo(targetSpec.vRefNum, kDontCareVolName, &foundVRefNum, &freeBytes, &totalBytes);#else err = FSGetVInfo(targetSpec.vRefNum, (HFSUniStr255*) kDontCareVolName, &freeBytes, &totalBytes);#endif if (err == noErr) { totalSpace = *(long long *) &totalBytes; } return err;}// ------------------------------------------------------------------------------------//// IsFileLocal// IsDirectoryLocal//// ------------------------------------------------------------------------------------BOOL CHXFileSpecUtils::IsDiskLocal(const CHXDirSpecifier& volSpec){ HParamBlockRec pb; HX_RESULT err; FSSpec targetSpec; GetVolParmsInfoBuffer buff; BOOL bLocalVol; require_return(volSpec.IsSet(), FALSE); targetSpec = volSpec; ZeroInit(&pb); pb.ioParam.ioVRefNum = targetSpec.vRefNum; pb.ioParam.ioBuffer = (Ptr) &buff; pb.ioParam.ioReqCount = sizeof(buff); err = PBHGetVolParmsSync(&pb); check_noerr(err); if (err == noErr) { bLocalVol = (buff.vMServerAdr == 0); } else { // error occurred, default to true bLocalVol = TRUE; } return bLocalVol;}// ------------------------------------------------------------------------------------//// IsDiskEjectable//// ------------------------------------------------------------------------------------BOOL CHXFileSpecUtils::IsDiskEjectable(const CHXDirSpecifier& volSpec){#ifdef _CARBON OSErr err; FSSpec targetSpec; GetVolParmsInfoBuffer volParmsInfo; HParamBlockRec pb; require_return(volSpec.IsSet(), FALSE); targetSpec = volSpec; ZeroInit(&volParmsInfo); ZeroInit(&pb); pb.ioParam.ioVRefNum = targetSpec.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;#else HX_RESULT err; FSSpec targetSpec; Boolean driverWantsEject, driveEjectable, volumeEjected, volumeOnline; require_return(volSpec.IsSet(), FALSE); targetSpec = volSpec; const StringPtr kDontCareVolName = NULL; err = GetVolState(kDontCareVolName, targetSpec.vRefNum, &volumeOnline, &volumeEjected, &driveEjectable, &driverWantsEject); check_noerr(err); if (err == noErr) { return driveEjectable; } else { return FALSE; }#endif}// ------------------------------------------------------------------------------------//// IsDiskWritable//// ------------------------------------------------------------------------------------BOOL CHXFileSpecUtils::IsDiskWritable(const CHXDirSpecifier& volSpec){ OSErr err; FSSpec targetSpec; require_return(volSpec.IsSet(), FALSE); targetSpec = volSpec; HParamBlockRec hpb; ZeroInit(&hpb); hpb.volumeParam.ioVolIndex = 0; hpb.volumeParam.ioVRefNum = targetSpec.vRefNum; err = PBHGetVInfoSync(&hpb); check_noerr(err); if (err == noErr) { if ((hpb.volumeParam.ioVAtrb & 0x8080) != 0) // locked, software or hardware, per tech note FL 530 { return FALSE; // not writable } } return TRUE;}// ------------------------------------------------------------------------------------//// GetFileSize//// ------------------------------------------------------------------------------------HX_RESULT CHXFileSpecUtils::GetFileSize(const CHXFileSpecifier& fileSpec, INT64& fSize){ require_return(fileSpec.IsSet(), HXR_INVALID_PARAMETER);#ifdef USE_FSREFS 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;#else HX_RESULT err; FSSpec targetSpec; targetSpec = fileSpec; HParamBlockRec pb; ZeroInit(&pb); pb.fileParam.ioVRefNum = targetSpec.vRefNum; pb.fileParam.ioDirID = targetSpec.parID; pb.fileParam.ioNamePtr = targetSpec.name; pb.fileParam.ioFDirIndex = 0; err = PBHGetFInfoSync(&pb); check_noerr(err); if ( err == noErr ) { fSize = (INT64) pb.fileParam.ioFlLgLen; fSize += (INT64) pb.fileParam.ioFlRLgLen; } return err;#endif}// ------------------------------------------------------------------------------------//// GetDirectorySize//// ------------------------------------------------------------------------------------HX_RESULT CHXFileSpecUtils::GetDirectorySize(const CHXDirSpecifier& dirSpec, BOOL shouldDescend, INT64& fSize){ // 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);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -