[vlc-commits] lua: io: Allow files to be explicitely closed

Hugo Beauzée-Luyssen git at videolan.org
Thu Apr 12 13:01:20 CEST 2018


vlc/vlc-3.0 | branch: master | Hugo Beauzée-Luyssen <hugo at beauzee.fr> | Mon Apr  9 18:16:20 2018 +0200| [8d3c75fbf0296c9f573b711a6f861741589d908f] | committer: Hugo Beauzée-Luyssen

lua: io: Allow files to be explicitely closed

And add a runtime check to match lua's io package behavior

(cherry picked from commit 086a2838eb63070605b184f0e62c08c33b5893d9)
Signed-off-by: Hugo Beauzée-Luyssen <hugo at beauzee.fr>

> http://git.videolan.org/gitweb.cgi/vlc/vlc-3.0.git/?a=commit;h=8d3c75fbf0296c9f573b711a6f861741589d908f
---

 modules/lua/libs/io.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/modules/lua/libs/io.c b/modules/lua/libs/io.c
index 73911ce074..52ac91c752 100644
--- a/modules/lua/libs/io.c
+++ b/modules/lua/libs/io.c
@@ -78,6 +78,8 @@ static int vlclua_io_file_read_chars( lua_State *L, size_t i_len, FILE* p_file )
 static int vlclua_io_file_read( lua_State *L )
 {
     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
+    if ( !*pp_file )
+        return luaL_error( L, "Attempt to use a closed file" );
     if( lua_type( L, 2 ) == LUA_TNUMBER )
     {
         return vlclua_io_file_read_chars( L, (size_t)lua_tointeger( L, 2 ),
@@ -103,6 +105,8 @@ static int vlclua_io_file_read( lua_State *L )
 static int vlclua_io_file_write( lua_State *L )
 {
     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
+    if ( !*pp_file )
+        return luaL_error( L, "Attempt to use a closed file" );
     int i_nb_args = lua_gettop( L );
     bool b_success = true;
     for ( int i = 2; i <= i_nb_args; ++i )
@@ -125,6 +129,8 @@ static int vlclua_io_file_write( lua_State *L )
 static int vlclua_io_file_seek( lua_State *L )
 {
     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
+    if ( !*pp_file )
+        return luaL_error( L, "Attempt to use a closed file" );
     const char* psz_mode = luaL_optstring( L, 2, NULL );
     if ( psz_mode != NULL )
     {
@@ -146,6 +152,8 @@ static int vlclua_io_file_seek( lua_State *L )
 static int vlclua_io_file_flush( lua_State *L )
 {
     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
+    if ( !*pp_file )
+        return luaL_error( L, "Attempt to use a closed file" );
     fflush( *pp_file );
     return 0;
 }
@@ -153,7 +161,11 @@ static int vlclua_io_file_flush( lua_State *L )
 static int vlclua_io_file_close( lua_State *L )
 {
     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
-    fclose( *pp_file );
+    if ( *pp_file )
+    {
+        fclose( *pp_file );
+        *pp_file = NULL;
+    }
     return 0;
 }
 
@@ -162,6 +174,7 @@ static const luaL_Reg vlclua_io_file_reg[] = {
     { "write", vlclua_io_file_write },
     { "seek", vlclua_io_file_seek },
     { "flush", vlclua_io_file_flush },
+    { "close", vlclua_io_file_close },
     { NULL, NULL }
 };
 



More information about the vlc-commits mailing list