os.stat() return negative size for big files - CHDK Releases - CHDK Forum  

os.stat() return negative size for big files

  • 3 Replies
  • 4333 Views
os.stat() return negative size for big files
« on: 24 / April / 2017, 10:09:41 »
Advertisements
Hi!
os.stat() return negative size for big files (e.g.: >9 min video on SX50). Int isn't a good for size.

loslib.c:
Code: [Select]
setfield(L,"size",st.st_size);    /* size of file, in bytes */

Code: [Select]
static void setfield (lua_State *L, const char *key, int value) {
  lua_pushinteger(L, value);
  lua_setfield(L, -2, key);
}

*

Offline reyalp

  • ******
  • 14080
Re: os.stat() return negative size for big files
« Reply #1 on: 24 / April / 2017, 13:38:59 »
Hi!
os.stat() return negative size for big files (e.g.: >9 min video on SX50). Int isn't a good for size.

loslib.c:
Code: [Select]
setfield(L,"size",st.st_size);    /* size of file, in bytes */

Code: [Select]
static void setfield (lua_State *L, const char *key, int value) {
  lua_pushinteger(L, value);
  lua_setfield(L, -2, key);
}
Unfortunately, this is difficult to solve since CHDK Lua is configured with Lua numbers as 32 bit integers, and Lua has no concept of signed/unsigned.

It's inconvenient, but you could detect this and get the real size using the bit functions.

Some cameras stat provided a block count, but AFAIK this is not true of sx50, and in any case, it was removed from Lua stat when Lua was converted to a module.

I guess we could provide the size in two fields.
Don't forget what the H stands for.

Re: os.stat() return negative size for big files
« Reply #2 on: 25 / April / 2017, 08:11:19 »
Unfortunately, this is difficult to solve since CHDK Lua is configured with Lua numbers as 32 bit integers, and Lua has no concept of signed/unsigned.

Can I build CHDK with int64_t as LUA_NUMBER?

*

Offline reyalp

  • ******
  • 14080
Re: os.stat() return negative size for big files
« Reply #3 on: 25 / April / 2017, 13:11:01 »
Can I build CHDK with int64_t as LUA_NUMBER?
Lua itself should be fine with any of the the supported LUA_NUMBER configurations. Personally, I'd probably try floats before 64 bit ints, since the hardware doesn't support either and floats are closer to standard Lua.

However, the code in modules/luascript.c assumes numbers are 32 bit ints, and a significant number of things would probably break in either case. It would also be a lot slower and probably larger, and you might need to import some additional functions gcc builtin functions in the module_exportlist.* files.

Making a workaround for stat using the bitwise operator functions, or updating stat to store the size in a different way in the C code would be much simpler.

you could do something like
Code: [Select]
local _orig_stat=os.stat
os.stat=function(...)
local st,err,errno=_orig_stat(...)
if not st then
return st,err,errno
end
st.size_kb = bitshru(st.size,10)
return st
end
st.size_kb would be the size rounded down to the nearest kb. You could make it round or preserve the remainder in another field, of course.
Don't forget what the H stands for.


 

Related Topics