1 /*****************************************************************************
2  * misc.c
3  *****************************************************************************
4  * Copyright (C) 2007-2018 the VideoLAN team
5  *
6  * Authors: Hugo Beauzée-Luyssen <hugo@beauzee.fr>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22 
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #endif
26 
27 #include <stdio.h>
28 #include <errno.h>
29 #include <sys/stat.h>
30 
31 #include <vlc_common.h>
32 #include <vlc_fs.h>
33 
34 #include "../vlc.h"
35 #include "../libs.h"
36 
37 #ifndef LUA_NUMBER_SCAN
38   /* Lua 5.3 only provides str<>long macros */
39   #define LUA_NUMBER_SCAN "%lf"
40 #endif
41 
vlclua_io_file_read_line(lua_State * L,FILE * p_file)42 static int vlclua_io_file_read_line( lua_State *L, FILE* p_file )
43 {
44     char* psz_line = NULL;
45     size_t i_buffer;
46     ssize_t i_len = getline( &psz_line, &i_buffer, p_file );
47     if ( i_len == -1 )
48         return 0;
49     if( psz_line[i_len - 1] == '\n' )
50         psz_line[--i_len] = 0;
51     lua_pushstring( L, psz_line );
52     free( psz_line );
53     return 1;
54 }
55 
vlclua_io_file_read_number(lua_State * L,FILE * p_file)56 static int vlclua_io_file_read_number( lua_State *L, FILE* p_file )
57 {
58     lua_Number num;
59     if ( fscanf( p_file, LUA_NUMBER_SCAN, &num ) != 1 )
60         return 0;
61     lua_pushnumber( L, num );
62     return 1;
63 }
64 
vlclua_io_file_read_chars(lua_State * L,size_t i_len,FILE * p_file)65 static int vlclua_io_file_read_chars( lua_State *L, size_t i_len, FILE* p_file )
66 {
67     size_t i_toread = LUAL_BUFFERSIZE;
68     size_t i_read;
69     luaL_Buffer b;
70     luaL_buffinit( L, &b );
71     do {
72         char *p = luaL_prepbuffer(&b);
73         if (i_toread > i_len)
74             i_toread = i_len;
75         i_read = fread(p, sizeof(char), i_toread, p_file);
76         luaL_addsize(&b, i_read);
77         i_len -= i_read;
78     } while (i_len > 0 && i_read == i_toread);
79     luaL_pushresult(&b);
80     return (i_len == 0 || lua_objlen(L, -1) > 0);
81 }
82 
vlclua_io_file_read(lua_State * L)83 static int vlclua_io_file_read( lua_State *L )
84 {
85     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
86     if ( !*pp_file )
87         return luaL_error( L, "Attempt to use a closed file" );
88     if( lua_type( L, 2 ) == LUA_TNUMBER )
89     {
90         return vlclua_io_file_read_chars( L, (size_t)lua_tointeger( L, 2 ),
91                                           *pp_file );
92     }
93     const char* psz_mode = luaL_optstring( L, 2, "*l" );
94     if ( *psz_mode != '*' )
95         return luaL_error( L, "Invalid file:read() format: %s", psz_mode );
96     switch ( psz_mode[1] )
97     {
98         case 'l':
99             return vlclua_io_file_read_line( L, *pp_file );
100         case 'n':
101             return vlclua_io_file_read_number( L, *pp_file );
102         case 'a':
103             return vlclua_io_file_read_chars( L, SIZE_MAX, *pp_file );
104         default:
105             break;
106     }
107     return luaL_error( L, "Invalid file:read() format: %s", psz_mode );
108 }
109 
vlclua_io_file_write(lua_State * L)110 static int vlclua_io_file_write( lua_State *L )
111 {
112     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
113     if ( !*pp_file )
114         return luaL_error( L, "Attempt to use a closed file" );
115     int i_nb_args = lua_gettop( L );
116     bool b_success = true;
117     for ( int i = 2; i <= i_nb_args; ++i )
118     {
119         bool i_res;
120         if ( lua_type( L, i ) == LUA_TNUMBER )
121             i_res = fprintf(*pp_file, LUA_NUMBER_FMT, lua_tonumber( L, i ) ) > 0;
122         else
123         {
124             size_t i_len;
125             const char* psz_value = luaL_checklstring( L, i, &i_len );
126             i_res = fwrite(psz_value, sizeof(*psz_value), i_len, *pp_file ) > 0;
127         }
128         b_success = b_success && i_res;
129     }
130     lua_pushboolean( L, b_success );
131     return 1;
132 }
133 
vlclua_io_file_seek(lua_State * L)134 static int vlclua_io_file_seek( lua_State *L )
135 {
136     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
137     if ( !*pp_file )
138         return luaL_error( L, "Attempt to use a closed file" );
139     const char* psz_mode = luaL_optstring( L, 2, NULL );
140     if ( psz_mode != NULL )
141     {
142         long i_offset = luaL_optlong( L, 3, 0 );
143         int i_mode;
144         if ( !strcmp( psz_mode, "set" ) )
145             i_mode = SEEK_SET;
146         else if ( !strcmp( psz_mode, "end" ) )
147             i_mode = SEEK_END;
148         else
149             i_mode = SEEK_CUR;
150         if( fseek( *pp_file, i_offset, i_mode ) != 0 )
151             return luaL_error( L, "Failed to seek" );
152     }
153     lua_pushinteger( L, ftell( *pp_file ) );
154     return 1;
155 }
156 
vlclua_io_file_flush(lua_State * L)157 static int vlclua_io_file_flush( lua_State *L )
158 {
159     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
160     if ( !*pp_file )
161         return luaL_error( L, "Attempt to use a closed file" );
162     fflush( *pp_file );
163     return 0;
164 }
165 
vlclua_io_file_close(lua_State * L)166 static int vlclua_io_file_close( lua_State *L )
167 {
168     FILE **pp_file = (FILE**)luaL_checkudata( L, 1, "io_file" );
169     if ( *pp_file )
170     {
171         fclose( *pp_file );
172         *pp_file = NULL;
173     }
174     return 0;
175 }
176 
177 static const luaL_Reg vlclua_io_file_reg[] = {
178     { "read", vlclua_io_file_read },
179     { "write", vlclua_io_file_write },
180     { "seek", vlclua_io_file_seek },
181     { "flush", vlclua_io_file_flush },
182     { "close", vlclua_io_file_close },
183     { NULL, NULL }
184 };
185 
vlclua_io_open(lua_State * L)186 static int vlclua_io_open( lua_State *L )
187 {
188     if( lua_gettop( L ) < 1 )
189         return luaL_error( L, "Usage: vlc.io.open(file_path [, mode])" );
190     const char* psz_path = luaL_checkstring( L, 1 );
191     const char* psz_mode = luaL_optstring( L, 2, "r" );
192     FILE *p_f =  vlc_fopen( psz_path, psz_mode );
193     if ( p_f == NULL )
194         return 0;
195 
196     FILE** pp_f = lua_newuserdata( L, sizeof( p_f ) );
197     *pp_f = p_f;
198 
199     if( luaL_newmetatable( L, "io_file" ) )
200     {
201         lua_newtable( L );
202         luaL_register( L, NULL, vlclua_io_file_reg );
203         lua_setfield( L, -2, "__index" );
204         lua_pushcfunction( L, vlclua_io_file_close );
205         lua_setfield( L, -2, "__gc" );
206     }
207     lua_setmetatable( L, -2 );
208     return 1;
209 }
210 
vlclua_io_readdir(lua_State * L)211 static int vlclua_io_readdir( lua_State *L )
212 {
213     if( lua_gettop( L ) < 1 )
214         return luaL_error( L, "Usage: vlc.io.readdir(name)" );
215     const char* psz_path = luaL_checkstring( L, 1 );
216     DIR* p_dir =  vlc_opendir( psz_path );
217     if ( p_dir == NULL )
218         return 0;
219 
220     lua_newtable( L );
221     const char* psz_entry;
222     int idx = 1;
223     while ( ( psz_entry = vlc_readdir( p_dir ) ) != NULL )
224     {
225         lua_pushstring( L, psz_entry );
226         lua_rawseti( L, -2, idx );
227         idx++;
228     }
229     closedir( p_dir );
230     return 1;
231 }
232 
vlclua_io_unlink(lua_State * L)233 static int vlclua_io_unlink( lua_State *L )
234 {
235     if( lua_gettop( L ) < 1 )
236         return luaL_error( L, "Usage: vlc.io.unlink(path)" );
237     const char* psz_path = luaL_checkstring( L, 1 );
238     int i_res = vlc_unlink( psz_path );
239     int i_err = i_res != 0 ? errno : 0;
240     lua_pushinteger( L, i_res );
241     lua_pushinteger( L, i_err );
242     return 2;
243 }
244 
vlclua_mkdir(lua_State * L)245 static int vlclua_mkdir( lua_State *L )
246 {
247     if( lua_gettop( L ) < 2 ) return vlclua_error( L );
248 
249     const char* psz_dir = luaL_checkstring( L, 1 );
250     const char* psz_mode = luaL_checkstring( L, 2 );
251     if ( !psz_dir || !psz_mode )
252         return vlclua_error( L );
253     int i_res = vlc_mkdir( psz_dir, strtoul( psz_mode, NULL, 0 ) );
254     int i_err = i_res != 0 ? errno : 0;
255     lua_pushinteger( L, i_res );
256     lua_pushinteger( L, i_err );
257     return 2;
258 }
259 
260 static const luaL_Reg vlclua_io_reg[] = {
261     { "mkdir", vlclua_mkdir },
262     { "open", vlclua_io_open },
263     { "readdir", vlclua_io_readdir },
264     { "unlink", vlclua_io_unlink },
265 
266     { NULL, NULL }
267 };
268 
luaopen_vlcio(lua_State * L)269 void luaopen_vlcio( lua_State *L )
270 {
271     lua_newtable( L );
272     luaL_register( L, NULL, vlclua_io_reg );
273     lua_setfield( L, -2, "io" );
274 }
275