Skip to content

Commit

Permalink
Added mkdir/rmdir support to file module.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmattsson committed Jan 1, 2022
1 parent 4b4ce47 commit a81c908
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
27 changes: 27 additions & 0 deletions components/modules/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>

static const char *default_fs_label =
((CONFIG_NODEMCU_DEFAULT_SPIFFS_LABEL &&
Expand Down Expand Up @@ -116,6 +117,30 @@ static int file_fsinfo( lua_State* L )
}


static int file_mkdir( lua_State *L )
{
const char *name = luaL_checkstring(L, 1);
unsigned mode = luaL_optint(L, 2, 0777);
int ret = mkdir(name, mode);
if (ret != 0)
return
luaL_error(L, "failed to create directory '%s'; code %d", name, errno);
else
return 0;
}


static int file_rmdir( lua_State *L )
{
const char *name = luaL_checkstring(L, 1);
int ret = rmdir(name);
if (ret != 0)
return
luaL_error(L, "failed to remove directory '%s'; code %d", name, errno);
return 0;
}


// Module function map
LROT_BEGIN(file, NULL, 0)
LROT_FUNCENTRY( list, file_list )
Expand All @@ -124,6 +149,8 @@ LROT_BEGIN(file, NULL, 0)
LROT_FUNCENTRY( rename, file_rename )
LROT_FUNCENTRY( exists, file_exists )
LROT_FUNCENTRY( fsinfo, file_fsinfo )
LROT_FUNCENTRY( mkdir, file_mkdir )
LROT_FUNCENTRY( rmdir, file_rmdir )
LROT_END(file, NULL, 0)


Expand Down
33 changes: 33 additions & 0 deletions docs/modules/file.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,39 @@ for k,v in pairs(l) do
end
```

## file.mkdir()

Creates a directory, provided the underlying file system supports directories. SPIFFS does not, but FAT (which you may have on an attached SD card) does.

#### Syntax
`file.mkdir(path [, mode])
```
#### Parameters
- `path` the full path name of the directory to create. E.g. "/SD0/MYDIR".
- `mode` optional, only used for file systems which use mode permissions. Defaults to 0777 (octal).
#### Returns
`nil`
Throws an error if the directory could not be created. Error code 134 (at the
time of writing) indicates that the filesystem at the given path does not
support directories.
## file.rmdir()
Removes an empty directory, provided the underlying file system supports directories. SPIFFS does not, but FAT (which you may have on an attached SD card) does.
#### Syntax
`file.rmdir(path)`
#### Parameters
- `path` the path to the directory to remove. The directory must be empty.
#### Returns
`nil`
Throws an error if the directory could not be removed.
## file.remove()
Expand Down

0 comments on commit a81c908

Please sign in to comment.