Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am using a Lua script to determine the 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 only seem to work for files up to 2GB. For larger files filesize is always nil. Is there any limitation on io.open? And if so, how could I work around this?

Running Lua 5.1.4 on Windows Server 2008 R2 64bit

like image 320
silent Avatar asked Nov 05 '14 10:11

silent


People also ask

What is IO/O library used for in Lua?

I/O library is used for reading and manipulating files in Lua. There are two kinds of file operations in Lua namely implicit file descriptors and explicit file descriptors.

What is Lua file in Lua?

Lua - File I/O. I/O library is used for reading and manipulating files in Lua. There are two kinds of file operations in Lua namely implicit file descriptors and explicit file descriptors. For the following examples, we will use a sample file test.lua as shown below.

How to use simple mode in Lua?

The simple mode uses standard I/O Or use a current input file and a current output file . The following is file.lua File code , The operation file is test.lua ( If not, you need to create the file ), The code is as follows : -- Add... On the last line of the file Lua notes

What is the default path to open luastudio?

If used LuaStudio, Among them io.open () The default lookup path for is LuaStudio The root directory of the software , If necessary open Files in other paths , Use absolute path ; 2.


2 Answers

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

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

The error message is probably Invalid argument. That's because for files larger than 2GB, its size is over what 32-bit long can hold, which causes the C function fseek fail to work.

In POSIX systems, Lua uses fseeko which takes the size of off_t instead of long in fseek. In Windows, there's a _fseeki64 which I guess does similar job. If these are not available, fseek is used, and it would cause the problem.


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

like image 101
Yu Hao Avatar answered Nov 15 '22 08:11

Yu Hao


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

like image 37
siffiejoe Avatar answered Nov 15 '22 07:11

siffiejoe