Lua - io.open () only up to 2 GB?

I use Lua script to determine file size:

local filesize=0 local filePath = "somepath.bin" local file,msg = io.open(filePath, "r") if file then filesize=file:seek("end") file:close() filePresent = true end 

However, this seems to work only for files up to 2 GB in size. For large files, filesize always nil . Is there a restriction on io.open ? And if so, how could I get around this?

Running Lua 5.1.4 on Windows Server 2008 R2 64bit

+6
source share
3 answers

The problem is not in io.open , but in file:seek . You can check this error as follows:

 filesize, err = file:seek("end") if not filesize then print(err) end 

The error message is probably an Invalid argument . This is due to the fact that for files larger than 2 GB its size exceeds what a 32-bit long can contain, which makes the C fseek function fseek .

On POSIX systems, Lua uses fseeko , which takes the size off_t instead of long in fseek . There is _fseeki64 Windows, which I think does a similar job. If they are not available, fseek used and this will cause a problem.


The corresponding source is liolib.c (Lua 5.2). As @lhf points out, fseek ( source ) is always used in Lua 5.1. Upgrading to Lua 5.2 could solve the problem.

+5
source

Internally, Lua uses the ISO function C long int ftell(FILE *stream); to determine the return value for file:seek() . A long int always 32 bits on Windows, so you're out of luck here. If you can, you should use some external library to determine the file size - I recommend the luafilesystem .

+3
source

In older versions of Lua (where file:seek() limited to 2Gb) you can ask cmd.exe to get the file size:

 function filesize(filename) -- returns file size (or nil if the file doesn't exist or unable to open) local command = 'cmd /d/c for %f in ("'..filename..'") do @echo(%~zf' return tonumber(io.popen(command):read'*a') end print(filesize[[C:\Program Files\Windows Media Player\wmplayer.exe]]) --> 73728 print(filesize[[E:\City.of.the.Living.Dead.1980.720p.BluRay.x264.Skazhutin.mkv]]) --> 8505168882 
0
source

Source: https://habr.com/ru/post/977757/


All Articles