Code archives/File Utilities/Long Filesize

This code has been declared by its author to be Public Domain code.

Download source code

Long Filesize by N2006
Basically, this is the same as FileSize, but uses a Long instead of an Int. Win32 and Linux are the only platforms supported by this function currently.

If you find a way to do this on OS X, I encourage you to post it to save others time. In the mean time, I'll be looking for a solution.
?Win32
Const FILE_READ_ATTRIBUTES% = $00000080
Const FILE_SHARE_READ% = $00000001
Const OPEN_EXISTING% = 3

Extern "OS"
    Function GetFileSizeEx:Int( handle%, size:Long Ptr )
    Function CreateFileW:Int( filename$W, daccess%, sharem%, seca@ Ptr, cDisp%, flags%, template% )
    Function CloseHandle:Int( handle% )
End Extern

?Linux
Private
Global statbank:TBank = TBank.Create(256) ' Maximum possible size
Public

Extern "OS"
    Function stat64:Int( path$z, buf@ Ptr )
End Extern

?MacOS

?

Function GetFileSize:Long( file$ )
    Local out:Long=-1
    
    ?Win32
    Local handle% = CreateFileW( file, FILE_READ_ATTRIBUTES, FILE_SHARE_READ, Null, OPEN_EXISTING, 0, 0 )
    ASsert handle<>-1, "File does not exist"
    GetFileSizeEx( handle, Varptr out )
    CloseHandle( handle )
    
    ?Linux
    If stat64( file, statbank.Buf( ) ) <> 0 Then Assert "Unable to get file stats"
    out = statBank.PeekLong( 44 )
    
    ?MacOS
    Assert "GetFileSize not implemented for this platform"
    
    ?
    
    Return out
End Function

Comments

N2006
Added an untested implementation for Linux. Anyone got the balls to test it?


N2006
New version for Linux. Tested and works from what I can see.


Code Archives Forum