Merge pull request #1050 from jfollas/file-exists

Added file.exists() method
This commit is contained in:
Terry Ellison 2016-02-17 22:04:09 +00:00
commit 4501f655f6
2 changed files with 52 additions and 12 deletions

View File

@ -20,8 +20,8 @@ static int file_open( lua_State* L )
}
const char *fname = luaL_checklstring( L, 1, &len );
if( len > FS_NAME_MAX_LENGTH )
return luaL_error(L, "filename too long");
luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long");
const char *mode = luaL_optstring(L, 2, "r");
file_fd = fs_open(fname, fs_mode2flag(mode));
@ -114,13 +114,27 @@ static int file_seek (lua_State *L)
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)
static int file_remove( lua_State* L )
{
size_t len;
const char *fname = luaL_checklstring( L, 1, &len );
if( len > FS_NAME_MAX_LENGTH )
return luaL_error(L, "filename too long");
const char *fname = luaL_checklstring( L, 1, &len );
luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long");
file_close(L);
SPIFFS_remove(&fs, (char *)fname);
return 0;
@ -157,12 +171,10 @@ static int file_rename( lua_State* L )
}
const char *oldname = luaL_checklstring( L, 1, &len );
if( len > FS_NAME_MAX_LENGTH )
return luaL_error(L, "filename too long");
const char *newname = luaL_checklstring( L, 2, &len );
if( len > FS_NAME_MAX_LENGTH )
return luaL_error(L, "filename too long");
luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 1, "filename too long");
const char *newname = luaL_checklstring( L, 2, &len );
luaL_argcheck(L, len <= FS_NAME_MAX_LENGTH, 2, "filename too long");
if(SPIFFS_OK==myspiffs_rename( oldname, newname )){
lua_pushboolean(L, 1);
@ -309,9 +321,9 @@ static const LUA_REG_TYPE file_map[] = {
{ LSTRKEY( "remove" ), LFUNCVAL( file_remove ) },
{ LSTRKEY( "seek" ), LFUNCVAL( file_seek ) },
{ LSTRKEY( "flush" ), LFUNCVAL( file_flush ) },
//{ LSTRKEY( "check" ), LFUNCVAL( file_check ) },
{ LSTRKEY( "rename" ), LFUNCVAL( file_rename ) },
{ LSTRKEY( "fsinfo" ), LFUNCVAL( file_fsinfo ) },
{ LSTRKEY( "exists" ), LFUNCVAL( file_exists ) },
#endif
{ LNILKEY, LNILVAL }
};

View File

@ -28,6 +28,34 @@ file.close()
#### See also
[`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()
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.