📄 pgpfilespecstd.c
字号:
return err;
}
PGPError
pgpPlatformNextFileInDirectory(
PFLDirectoryIterRef iter,
PFLFileSpecRef * outRef )
{
PGPError err = kPGPError_NoErr;
struct dirent * entry;
*outRef = NULL;
PGPValidatePtr( iter->dirRef );
entry = readdir( iter->dirRef );
if ( IsntNull( entry ) )
err = PFLComposeFileSpec( iter->parentDir, entry->d_name, outRef );
else
err = kPGPError_EndOfIteration;
return err;
}
PGPError
pgpPlatformFreeDirectoryIter(
PFLDirectoryIterRef iter )
{
PGPError err = kPGPError_NoErr;
PGPValidatePtr( iter );
if ( closedir( iter->dirRef ) != 0 )
err = kPGPError_FileOpFailed; /* XXX Better error code? */
PFLFreeFileSpec( iter->parentDir );
PGPFreeData( iter );
return err;
}
#endif /* ] USE_DIRENT */
/*____________________________________________________________________________
Export a C string which is the entire path.
____________________________________________________________________________*/
static PGPError
sExportProc(
PFLConstFileSpecRef ref,
PGPByte ** dataOut,
PGPSize * dataSizeOut )
{
PGPUInt32 dataSize;
PGPError err = kPGPError_NoErr;
char * exported = NULL;
char const * srcName;
srcName = GetMyData( ref )->path;
dataSize = strlen( srcName ) + 1; /* yes, include the NULL char */
exported = (char *)PGPNewData( ref->memoryMgr, dataSize, 0);
if ( IsntNull( exported ) )
{
strcpy( exported, srcName );
}
else
{
err = kPGPError_OutOfMemory;
}
pgpAssert( dataSize == strlen( srcName ) + 1 );
*dataOut = (PGPByte *)exported;
*dataSizeOut = dataSize;
return( err);
}
/*____________________________________________________________________________
The imported data should be a NULL-terminated C string
____________________________________________________________________________*/
static PGPError
sImportProc(
PFLFileSpecRef ref,
PGPByte const * importData,
PGPSize importDataSize )
{
PGPError err = kPGPError_NoErr;
char const * importedPath;
MyData * myData = NULL;
importedPath = (char const *)importData;
/* data should be a C string (NULL-terminated) */
PGPValidateParam( importedPath[ importDataSize - 1 ] == '\0' );
pgpAssert( strlen( importedPath ) == importDataSize - 1 );
err = PGPReallocData( ref->memoryMgr, &ref->data, importDataSize, 0 );
if ( IsntPGPError( err ) )
{
myData = GetMyData( ref );
ref->dataSize = importDataSize;
pgpAssert( ref->dataSize == strlen( importedPath ) + 1 );
strcpy( myData->path, importedPath );
}
return( err);
}
static PGPError
sGetNameProc(
PFLConstFileSpecRef ref,
char name[ 256 ] )
{
char const * tail = NULL;
PGPError err = kPGPError_NoErr;
PFLValidateFileSpec( ref );
PGPValidatePtr( ref->data );
tail = pgpFileNameTail( (const char *)ref->data );
if ( IsntNull( tail) && strlen( tail ) <= 256 )
{
strcpy( name, tail );
}
else
{
err = kPGPError_BadParams;
}
return( err );
}
static PGPError
sSetNameProc(
PFLFileSpecRef ref,
char const * newName )
{
PGPUInt32 nameLength;
char * path = NULL;
PGPUInt32 nameOffset;
PGPUInt32 newSize;
PGPError err = kPGPError_NoErr;
void * temp;
PFLValidateFileSpec( ref);
PGPValidatePtr( ref->data );
PGPValidatePtr( newName );
nameLength = strlen( newName );
PGPValidateParam( nameLength != 0 );
path = (char *)ref->data;
nameOffset = pgpFileNameTail( path ) - path;
newSize = nameOffset + nameLength + 1;
temp = path;
err = PGPReallocData(ref->memoryMgr, &temp, newSize, 0);
path = (char *)temp;
if ( IsntPGPError( err ) )
{
ref->data = (PGPByte *)path;
strcpy( path + nameOffset, newName);
ref->dataSize = newSize;
}
return( err );
}
static PGPError
sGetMaxNameLengthProc(
PFLConstFileSpecRef ref,
PGPSize * maxNameLength )
{
const MyData * myData = GetMyData( ref );
PGPValidatePtr( myData );
/* at some point, this will need to be done programmatically by
examining the file system in use which the file is on */
/* obviously, 8.3 file systems have a max of 8 + 1 + 3 */
*maxNameLength = 255;
return( kPGPError_NoErr );
}
static PGPError
sSetMetaInfoProc(
PFLFileSpecRef ref,
void const * infoIn )
{
PGPError err = kPGPError_NoErr;
(void)ref;
(void)infoIn;
return( err );
}
static PGPError
sParentDirProc(
PFLConstFileSpecRef fileFromDir,
PFLFileSpecRef * outParent )
{
PGPMemoryMgrRef memoryMgr = fileFromDir->memoryMgr;
PFLFileSpecRef newFileRef = NULL;
PGPError err = kPGPError_NoErr;
char * path = NULL;
PGPUInt32 nameOffset;
PGPValidatePtr( outParent );
*outParent = NULL;
PGPValidateParam( PGPMemoryMgrIsValid( memoryMgr ) );
path = (char *)fileFromDir->data;
nameOffset = pgpFileNameTail( path ) - path;
if (nameOffset <= 1) /* There is no parent directory */
return kPGPError_FileNotFound; /* XXX Reasonable error code? */
err = pgpNewFileSpec( memoryMgr, kPFLFileSpecFullPathType,
nameOffset, &newFileRef );
if ( IsntPGPError( err ) )
{
MyData * myData = GetMyData( newFileRef );
pgpCopyMemory( path, myData->path, nameOffset - 1 );
myData->path[nameOffset - 1] = '\0';
}
*outParent = newFileRef;
return err;
}
static PGPError
sComposeProc(
PFLConstFileSpecRef parent,
const char * fileName,
PFLFileSpecRef * outRef )
{
PGPMemoryMgrRef memoryMgr = parent->memoryMgr;
PFLFileSpecRef newFileRef = NULL;
PGPError err = kPGPError_NoErr;
char * path = NULL;
PGPSize pathLength;
PGPValidatePtr( outRef );
*outRef = NULL;
PGPValidateParam( PGPMemoryMgrIsValid( memoryMgr ) );
path = (char *)parent->data;
pathLength = strlen( path );
if ( pathLength > 0 && IsntNull( strchr( kDirectorySeparators,
path[ pathLength - 1 ] ) ) )
{
pathLength--;
}
err = pgpNewFileSpec( memoryMgr, kPFLFileSpecFullPathType,
pathLength + strlen( fileName ) + 2, &newFileRef );
if ( IsntPGPError( err ) )
{
MyData * myData = GetMyData( newFileRef );
pgpCopyMemory( path, myData->path, pathLength );
myData->path[ pathLength ] = kDirectorySeparators[0];
strcpy( myData->path + pathLength + 1, fileName );
}
*outRef = newFileRef;
return err;
}
static PGPError
sExistsProc(
PFLFileSpecRef ref,
PGPBoolean * exists)
{
PGPError err = kPGPError_NoErr;
MyData const * myData = GetMyData( ref );
FILE * stdIOFile;
/* Open the file read-only. If file doesn't exit, fopen fails */
stdIOFile = fopen( myData->path, "rb" );
if ( IsntNull( stdIOFile ) )
{
fclose(stdIOFile);
*exists = TRUE;
}
#ifdef EACCES
else if ( errno == EACCES )
{
#if PGP_WIN32
err = kPGPError_CantOpenFile;
#endif
*exists = TRUE;
}
#endif
#ifdef ENOENT
else if ( errno == ENOENT )
*exists = FALSE;
#endif
#ifdef EAGAIN
else if ( errno == EAGAIN )
*exists = FALSE;
#endif
#if 1 /* FOPEN_ERRNO_ZERO */
else if ( errno == 0 )
*exists = FALSE;
#endif
else
{
pgpDebugMsg( "pgpFileSpecStd.c:sExistsProc(): unknown errno" );
err = kPGPError_CantOpenFile;
}
return( err );
}
static PGPError
sCreateProc( PFLFileSpecRef ref )
{
PGPError err = kPGPError_NoErr;
char const * path = GetMyData( ref )->path;
#if PGP_UNIX /* [ */
int fileDes;
/*
* XXX Beware that calling this routine could be a security flaw,
* because there is a window between creating and opening
* the file, in between which the file might have been moved
* out of the way, thus possibly having you open a new file
* with the wrong permissions.
*/
fileDes = open( path, O_WRONLY | O_CREAT | O_EXCL, 0600 );
if (fileDes > 0)
close( fileDes );
else
err = kPGPError_FileOpFailed;
#else /* ] PGP_UNIX [ */
FILE * stdIOFile;
stdIOFile = fopen( path, "ab+");
if ( IsntNull( stdIOFile ) )
fclose(stdIOFile);
else
err = kPGPError_FileOpFailed;
#endif /* ] PGP_UNIX */
return( err );
}
static PGPError
sDeleteProc( PFLConstFileSpecRef ref )
{
PGPError err = kPGPError_NoErr;
const char * path = GetMyData( ref )->path;
if ( remove( path ) != 0 )
err = kPGPError_FileOpFailed;
return( err );
}
/*____________________________________________________________________________
Rename the file AND change the name in the file spec itself.
If the file cannot be renamed, the file spec is not changed.
____________________________________________________________________________*/
static PGPError
sRenameProc(
PFLFileSpecRef ref,
const char * newName)
{
PGPError err = kPGPError_NoErr;
MyData * oldDataCopy = NULL;
PGPMemoryMgrRef memoryMgr = ref->memoryMgr;
/* save the old data in case the rename fails */
/* also needed for rename (see below) */
oldDataCopy = (MyData *)PGPNewData( memoryMgr,
MyDataSize( ref ), 0);
if ( IsntNull( oldDataCopy ) )
{
pgpCopyMemory( GetMyData( ref ),
oldDataCopy, MyDataSize( ref ) );
/* this changes the current data */
err = sSetNameProc( ref, newName );
if ( rename( oldDataCopy->path, GetMyData( ref )->path ) != 0)
{
err = kPGPError_FileOpFailed; /* XXX Improve error */
/* put old path back in place of new one */
PGPFreeData( GetMyData( ref ) );
SetMyData( ref, oldDataCopy );
}
else
{
PGPFreeData( oldDataCopy );
}
}
else
{
err = kPGPError_OutOfMemory;
}
return( err );
}
/*
* Local Variables:
* tab-width: 4
* End:
* vi: ts=4 sw=4
* vim: si
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -