r=dveditz, sr=brendan
Change registry buffered I/O APIs to use NSPR rather than C runtime for file I/O
These APIs are used most commonly to read and write to component.reg


git-svn-id: svn://10.0.0.236/trunk@86166 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
mkaply%us.ibm.com
2001-02-04 03:17:10 +00:00
parent 1c8409b156
commit ad3aca9a62
3 changed files with 23 additions and 56 deletions

View File

@@ -36,9 +36,6 @@
#include <stdio.h>
#include <string.h>
#include <errno.h>
#if defined(XP_MAC)
#include <Errors.h>
#endif
#if defined(SUNOS4)
#include <unistd.h> /* for SEEK_SET */
@@ -66,7 +63,7 @@ static num_reads = 0;
struct BufioFileStruct
{
FILE *fd; /* real file descriptor */
PRFileDesc *fd; /* real file descriptor */
PRInt32 fsize; /* total size of file */
PRInt32 fpos; /* our logical position in the file */
PRInt32 datastart; /* the file position at which the buffer starts */
@@ -92,12 +89,12 @@ static int _bufio_flushBuf( BufioFile* file );
/**
* like fopen() this routine takes *native* filenames, not NSPR names.
*/
BufioFile* bufio_Open(const char* name, const char* mode)
BufioFile* bufio_Open(const char* name, PRIntn flags, PRIntn mode)
{
FILE *fd;
PRFileDesc *fd;
BufioFile *file = NULL;
fd = fopen( name, mode );
fd = PR_Open( name, flags, mode );
if ( fd )
{
@@ -113,12 +110,10 @@ BufioFile* bufio_Open(const char* name, const char* mode)
if ( file->data )
{
/* get file size to finish initialization of bufio */
if ( !fseek( fd, 0, SEEK_END ) )
if ( PR_Seek( fd, 0, PR_SEEK_END ) >= 0)
{
file->fsize = ftell( fd );
file->readOnly = strcmp(mode,XP_FILE_READ) == 0 ||
strcmp(mode,XP_FILE_READ_BIN) == 0;
file->fsize = PR_Seek(fd, 0, PR_SEEK_CUR);
file->readOnly = (flags & PR_RDONLY);
}
else
{
@@ -133,39 +128,11 @@ BufioFile* bufio_Open(const char* name, const char* mode)
/* close file if we couldn't create BufioFile */
if (!file)
{
PR_Close( fd );
fclose( fd );
PR_SetError( PR_OUT_OF_MEMORY_ERROR, 0 );
}
}
else
{
/* couldn't open file. Figure out why and set NSPR errors */
switch (errno)
{
/* file not found */
#ifdef XP_MAC
case fnfErr:
#else
case ENOENT:
#endif
PR_SetError(PR_FILE_NOT_FOUND_ERROR,0);
break;
/* file in use */
#ifdef XP_MAC
case opWrErr:
#else
case EACCES:
#endif
PR_SetError(PR_NO_ACCESS_RIGHTS_ERROR,0);
break;
default:
PR_SetError(PR_UNKNOWN_ERROR,0);
break;
}
}
return file;
}
@@ -184,7 +151,7 @@ int bufio_Close(BufioFile* file)
if ( file->bufdirty )
_bufio_flushBuf( file );
retval = fclose( file->fd );
retval = PR_Close( file->fd );
if ( file->data )
PR_Free( file->data );
@@ -324,12 +291,12 @@ PRUint32 bufio_Read(BufioFile* file, char* dest, PRUint32 count)
/* we need more than we could load into a buffer, so */
/* skip buffering and just read the data directly */
if ( fseek( file->fd, file->fpos, SEEK_SET ) == 0 )
if ( PR_Seek( file->fd, file->fpos, PR_SEEK_SET ) >= 0 )
{
#if DEBUG_dougt
++num_reads;
#endif
bytesRead = fread(dest+bytesCopied, 1, leftover, file->fd);
bytesRead = PR_Read(file->fd, dest+bytesCopied, leftover);
file->fpos += bytesRead;
retcount += bytesRead;
}
@@ -387,9 +354,9 @@ PRUint32 bufio_Read(BufioFile* file, char* dest, PRUint32 count)
else
{
/* leftover data doesn't fit so skip buffering */
if ( fseek( file->fd, file->fpos, SEEK_SET ) == 0 )
if ( PR_Seek( file->fd, file->fpos, PR_SEEK_SET ) >= 0 )
{
bytesRead = fread(dest, 1, leftover, file->fd);
bytesRead = PR_Read(file->fd, dest, leftover);
#if DEBUG_dougt
++num_reads;
#endif
@@ -514,8 +481,8 @@ PRUint32 bufio_Write(BufioFile* file, const char* src, PRUint32 count)
else
{
/* request didn't fit in a buffer, write directly */
if ( fseek( file->fd, file->fpos, SEEK_SET ) == 0 )
bytesWritten = fwrite( newsrc, 1, leftover, file->fd );
if ( PR_Seek( file->fd, file->fpos, PR_SEEK_SET ) >= 0 )
bytesWritten = PR_Write(file->fd, newsrc, leftover);
else
bytesWritten = 0; /* seek failed! */
}
@@ -546,7 +513,7 @@ int bufio_Flush(BufioFile* file)
if ( file->bufdirty )
_bufio_flushBuf( file );
return fflush(file->fd);
return PR_Sync(file->fd);
}
@@ -590,14 +557,14 @@ static PRBool _bufio_loadBuf( BufioFile* file, PRUint32 count )
if ( endPos > endBuf )
startBuf += (endPos - endBuf);
if ( fseek( file->fd, startBuf, SEEK_SET ) != 0 )
if ( PR_Seek( file->fd, startBuf, PR_SEEK_SET ) < 0 )
return PR_FALSE;
else
{
#if DEBUG_dougt
++num_reads;
#endif
bytesRead = fread( file->data, 1, file->bufsize, file->fd );
bytesRead = PR_Read(file->fd, file->data, file->bufsize);
file->datastart = startBuf;
file->datasize = bytesRead;
file->bufdirty = PR_FALSE;
@@ -625,10 +592,10 @@ static int _bufio_flushBuf( BufioFile* file )
return 0;
startpos = file->datastart + file->dirtystart;
if ( !fseek( file->fd, startpos, SEEK_SET ) )
if ( PR_Seek( file->fd, startpos, PR_SEEK_SET ) >= 0)
{
dirtyamt = file->dirtyend - file->dirtystart;
written = fwrite( file->data+file->dirtystart, 1, dirtyamt, file->fd );
written = PR_Write( file->fd, file->data+file->dirtystart, dirtyamt );
if ( written == dirtyamt )
{
#ifdef DEBUG_dveditzbuf

View File

@@ -35,7 +35,7 @@
typedef struct BufioFileStruct BufioFile;
BufioFile* bufio_Open(const char* name, const char* mode);
BufioFile* bufio_Open(const char* name, PRIntn flags, PRIntn mode);
int bufio_Close(BufioFile* file);
int bufio_Seek(BufioFile* file, PRInt32 offset, int whence);
PRUint32 bufio_Read(BufioFile* file, char* dest, PRUint32 count);

View File

@@ -155,7 +155,7 @@ typedef MmioFile* XP_File;
/* home-grown XP buffering */
/* writes are buffered too so use flush! */
/*-----------------------------------------------*/
#define USE_STDIO_MODES
#define USE_NSPR_MODES
#include "nr_bufio.h"
#define XP_FileSeek(file,offset,whence) bufio_Seek((file),(offset),(whence))
@@ -163,7 +163,7 @@ typedef MmioFile* XP_File;
#define XP_FileWrite(src,count,file) bufio_Write((file), (src), (count))
#define XP_FileTell(file) bufio_Tell(file)
#define XP_FileClose(file) bufio_Close(file)
#define XP_FileOpen(path, mode) bufio_Open((path), (mode))
#define XP_FileOpen(path, mode) bufio_Open((path), mode)
#define XP_FileFlush(file) bufio_Flush(file)
#define XP_FileSetBufferSize(file,bufsize) bufio_SetBufferSize(file,bufsize)