Commit 4501f655 authored by Terry Ellison's avatar Terry Ellison
Browse files

Merge pull request #1050 from jfollas/file-exists

Added file.exists() method 
parents 19471f54 d4ae67d0
...@@ -20,8 +20,8 @@ static int file_open( lua_State* L ) ...@@ -20,8 +20,8 @@ static int file_open( lua_State* L )
} }
const char *fname = luaL_checklstring( L, 1, &len ); const char *fname = luaL_checklstring( L, 1, &len );
if( len > FS_NAME_MAX_LENGTH ) luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long");
return luaL_error(L, "filename too long");
const char *mode = luaL_optstring(L, 2, "r"); const char *mode = luaL_optstring(L, 2, "r");
file_fd = fs_open(fname, fs_mode2flag(mode)); file_fd = fs_open(fname, fs_mode2flag(mode));
...@@ -114,13 +114,27 @@ static int file_seek (lua_State *L) ...@@ -114,13 +114,27 @@ static int file_seek (lua_State *L)
return 1; return 1;
} }
// Lua: exists(filename)
static int file_exists( lua_State* L )
{
size_t len;
const char *fname = luaL_checklstring( L, 1, &len );
luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long");
spiffs_stat stat;
int rc = SPIFFS_stat(&fs, (char *)fname, &stat);
lua_pushboolean(L, (rc == SPIFFS_OK ? 1 : 0));
return 1;
}
// Lua: remove(filename) // Lua: remove(filename)
static int file_remove( lua_State* L ) static int file_remove( lua_State* L )
{ {
size_t len; size_t len;
const char *fname = luaL_checklstring( L, 1, &len ); const char *fname = luaL_checklstring( L, 1, &len );
if( len > FS_NAME_MAX_LENGTH ) luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long");
return luaL_error(L, "filename too long");
file_close(L); file_close(L);
SPIFFS_remove(&fs, (char *)fname); SPIFFS_remove(&fs, (char *)fname);
return 0; return 0;
...@@ -157,12 +171,10 @@ static int file_rename( lua_State* L ) ...@@ -157,12 +171,10 @@ static int file_rename( lua_State* L )
} }
const char *oldname = luaL_checklstring( L, 1, &len ); const char *oldname = luaL_checklstring( L, 1, &len );
if( len > FS_NAME_MAX_LENGTH ) luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long");
return luaL_error(L, "filename too long");
const char *newname = luaL_checklstring( L, 2, &len );
const char *newname = luaL_checklstring( L, 2, &len ); luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 2, "filename too long");
if( len > FS_NAME_MAX_LENGTH )
return luaL_error(L, "filename too long");
if(SPIFFS_OK==myspiffs_rename( oldname, newname )){ if(SPIFFS_OK==myspiffs_rename( oldname, newname )){
lua_pushboolean(L, 1); lua_pushboolean(L, 1);
...@@ -309,9 +321,9 @@ static const LUA_REG_TYPE file_map[] = { ...@@ -309,9 +321,9 @@ static const LUA_REG_TYPE file_map[] = {
{ LSTRKEY( "remove" ), LFUNCVAL( file_remove ) }, { LSTRKEY( "remove" ), LFUNCVAL( file_remove ) },
{ LSTRKEY( "seek" ), LFUNCVAL( file_seek ) }, { LSTRKEY( "seek" ), LFUNCVAL( file_seek ) },
{ LSTRKEY( "flush" ), LFUNCVAL( file_flush ) }, { LSTRKEY( "flush" ), LFUNCVAL( file_flush ) },
//{ LSTRKEY( "check" ), LFUNCVAL( file_check ) },
{ LSTRKEY( "rename" ), LFUNCVAL( file_rename ) }, { LSTRKEY( "rename" ), LFUNCVAL( file_rename ) },
{ LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) }, { LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) },
{ LSTRKEY( "exists" ), LFUNCVAL( file_exists ) },
#endif #endif
{ LNILKEY, LNILVAL } { LNILKEY, LNILVAL }
}; };
......
...@@ -28,6 +28,34 @@ file.close() ...@@ -28,6 +28,34 @@ file.close()
#### See also #### See also
[`file.open()`](#fileopen) [`file.open()`](#fileopen)
## file.exists()
Determines whether the specified file exists.
#### Syntax
`file.exists(filename)`
#### Parameters
- `filename` file to check
#### Returns
true of the file exists (even if 0 bytes in size), and false if it does not exist
#### Example
```lua
files = file.list()
if files["device.config"] then
print("Config file exists")
end
if file.exists("device.config") then
print("Config file exists")
end
```
#### See also
[`file.list()`](#filelist)
## file.flush() ## file.flush()
Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using [`file.close()`](#fileclose) performs an implicit flush as well. Flushes any pending writes to the file system, ensuring no data is lost on a restart. Closing the open file using [`file.close()`](#fileclose) performs an implicit flush as well.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment