summary |
tags |
clone url |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
e0d21c1)
The intention is to protect the user from a presumably common mistake:
passing the size as single cell number instead of a double cell
number. This would create a very big file that is written in many
small chunks.
I also changed sdResizeFile to use uint64_t instead of Lo/Hi pairs.
* csrc/pfcompil.c (pfBuildDictionary): Rename RESIZE-FILE
to (RESIZE-FILE).
* fth/file.fth (RESIZE-FILE-LIMIT): New variable.
(RESIZE-FILE): Use it.
* fth/system.fth (D<, D>): Implemented. Needed for compare the limit.
* csrc/pf_io.h, csrc/pf_io.c (sdResizeFile): Use uint64_t.
* csrc/stdio/pf_fileio_stdio.c (sdResizeFile): Use uint64_t.
(IsGreaterThanLongMax): Deleted.
* csrc/pf_inner.c (ID_FILE_RESIZE): Convert the Lo/Hi pair to uint64_t.
(UdToUint64, UdIsUint64): New helpers.
/* Use local copy of CODE_BASE for speed. */
#define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase))
/* Use local copy of CODE_BASE for speed. */
#define LOCAL_CODEREL_TO_ABS( a ) ((cell_t *) (((cell_t) a) + CodeBase))
+/* Truncate the unsigned double cell integer LO/HI to an uint64_t. */
+static uint64_t UdToUint64( ucell_t Lo, ucell_t Hi )
+{
+ return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
+ ? (((uint64_t)Lo) | (((uint64_t)Hi) >> (sizeof(ucell_t) * 8)))
+ : Lo );
+}
+
+/* Return TRUE if the unsigned double cell integer LO/HI is not greater
+ * then the greatest uint64_t.
+ */
+static int UdIsUint64( ucell_t Lo, ucell_t Hi )
+{
+ return (( 2 * sizeof(ucell_t) == sizeof(uint64_t) )
+ ? TRUE
+ : Hi == 0 );
+}
+
static const char *pfSelectFileModeCreate( int fam );
static const char *pfSelectFileModeOpen( int fam );
static const char *pfSelectFileModeCreate( int fam );
static const char *pfSelectFileModeOpen( int fam );
FileStream *File = (FileStream *) TOS;
ucell_t SizeHi = (ucell_t) M_POP;
ucell_t SizeLo = (ucell_t) M_POP;
FileStream *File = (FileStream *) TOS;
ucell_t SizeHi = (ucell_t) M_POP;
ucell_t SizeLo = (ucell_t) M_POP;
- TOS = sdResizeFile( File, SizeLo, SizeHi );
+ TOS = ( UdIsUint64( SizeLo, SizeHi )
+ ? sdResizeFile( File, UdToUint64( SizeLo, SizeHi ))
+ : THROW_RESIZE_FILE );
-ThrowCode sdResizeFile( FileStream * File, ucell_t SizeLo, ucell_t SizeHi )
+ThrowCode sdResizeFile( FileStream * File, uint64_t NewSize )
{
UNIMPLEMENTED("sdResizeFile");
{
UNIMPLEMENTED("sdResizeFile");
- TOUCH(File);
- TOUCH(SizeLo);
- TOUCH(SizeHi);
return THROW_RESIZE_FILE;
}
return THROW_RESIZE_FILE;
}
cell_t sdSeekFile( FileStream * Stream, off_t Position, int32_t Mode );
cell_t sdRenameFile( const char *OldName, const char *NewName );
cell_t sdDeleteFile( const char *FileName );
cell_t sdSeekFile( FileStream * Stream, off_t Position, int32_t Mode );
cell_t sdRenameFile( const char *OldName, const char *NewName );
cell_t sdDeleteFile( const char *FileName );
- ThrowCode sdResizeFile( FileStream *, ucell_t SizeLo, ucell_t SizeHi );
+ ThrowCode sdResizeFile( FileStream *, uint64_t Size);
off_t sdTellFile( FileStream * Stream );
cell_t sdCloseFile( FileStream * Stream );
cell_t sdInputChar( FileStream *stream );
off_t sdTellFile( FileStream * Stream );
cell_t sdCloseFile( FileStream * Stream );
cell_t sdInputChar( FileStream *stream );
#define PF_SEEK_CUR (SEEK_CUR)
#define PF_SEEK_END (SEEK_END)
#define PF_SEEK_CUR (SEEK_CUR)
#define PF_SEEK_END (SEEK_END)
- ThrowCode sdResizeFile( FileStream *, ucell_t SizeLo, ucell_t SizeHi );
+ ThrowCode sdResizeFile( FileStream *, uint64_t Size);
/*
** printf() is only used for debugging purposes.
/*
** printf() is only used for debugging purposes.
CreateDicEntryC( ID_FILE_REPOSITION, "REPOSITION-FILE", 0 );
CreateDicEntryC( ID_FILE_FLUSH, "FLUSH-FILE", 0 );
CreateDicEntryC( ID_FILE_RENAME, "(RENAME-FILE)", 0 );
CreateDicEntryC( ID_FILE_REPOSITION, "REPOSITION-FILE", 0 );
CreateDicEntryC( ID_FILE_FLUSH, "FLUSH-FILE", 0 );
CreateDicEntryC( ID_FILE_RENAME, "(RENAME-FILE)", 0 );
- CreateDicEntryC( ID_FILE_RESIZE, "RESIZE-FILE", 0 );
+ CreateDicEntryC( ID_FILE_RESIZE, "(RESIZE-FILE)", 0 );
CreateDicEntryC( ID_FILE_RO, "R/O", 0 );
CreateDicEntryC( ID_FILE_RW, "R/W", 0 );
CreateDicEntryC( ID_FILE_WO, "W/O", 0 );
CreateDicEntryC( ID_FILE_RO, "R/O", 0 );
CreateDicEntryC( ID_FILE_RW, "R/W", 0 );
CreateDicEntryC( ID_FILE_WO, "W/O", 0 );
-/* Return non-FALSE if the double-cell unsigned number LO/HI
- * is greater then LONG_MAX.
- */
-static bool_t IsGreaterThanLongMax( ucell_t Lo, ucell_t Hi )
-{
- return (Hi != 0) || (Lo > LONG_MAX);
-}
-
-ThrowCode sdResizeFile( FileStream *File, ucell_t SizeLo, ucell_t SizeHi )
+ThrowCode sdResizeFile( FileStream *File, uint64_t Size )
- if( !IsGreaterThanLongMax( SizeLo, SizeHi ) )
- long Newsize = (long) SizeLo;
+ long Newsize = (long) Size;
if( fseek( File, 0, SEEK_END ) == 0 )
{
long Oldsize = ftell( File );
if( fseek( File, 0, SEEK_END ) == 0 )
{
long Oldsize = ftell( File );
+\ A limit used to perform a sanity check on the size argument for
+\ RESIZE-FILE.
+2variable RESIZE-FILE-LIMIT
+10000000 0 resize-file-limit 2! \ 10MB is somewhat arbitrarily chosen
+
+: RESIZE-FILE ( ud fileid -- ior )
+ -rot 2dup resize-file-limit 2@ d> ( fileid ud big? )
+ IF
+ ." Argument (" 0 d.r ." ) is larger then RESIZE-FILE-LIMIT." cr
+ ." (You can increase RESIZE-FILE-LIMIT with 2!)" cr
+ abort
+ ELSE
+ rot (resize-file)
+ THEN
+;
+
: ( ( "comment<rparen>" -- )
source-id
CASE
: ( ( "comment<rparen>" -- )
source-id
CASE
+: D< ( d1 d2 -- flag )
+ d- nip 0<
+;
+
+: D> ( d1 d2 -- flag )
+ 2swap d<
+;
+
\ define some useful constants ------------------------------
1 0= constant FALSE
0 0= constant TRUE
\ define some useful constants ------------------------------
1 0= constant FALSE
0 0= constant TRUE