fix up fs

This commit is contained in:
Jeremy Baxter 2023-12-26 17:47:07 +13:00
parent 058aa579ba
commit 7b8c7632e9

14
lfs.c
View file

@ -384,9 +384,18 @@ recursiveremove(lua_State *L, const char *path)
char *fullname; char *fullname;
size_t len, nlen, plen; size_t len, nlen, plen;
if ((d = opendir(path)) == NULL) if ((d = opendir(path)) == NULL) {
if (errno != ENOTDIR)
return lfail(L); return lfail(L);
/* if it's a file... */
if (remove(path) == -1)
return lfail(L);
lua_pushboolean(L, 1);
return 1;
}
while ((ent = readdir(d)) != NULL) { while ((ent = readdir(d)) != NULL) {
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue; continue;
@ -402,7 +411,6 @@ recursiveremove(lua_State *L, const char *path)
fullname[plen + 1] = 0; fullname[plen + 1] = 0;
strlcat(fullname, ent->d_name, len); strlcat(fullname, ent->d_name, len);
printf("fullname: %s\n", fullname);
if (ent->d_type == DT_DIR) { if (ent->d_type == DT_DIR) {
/* if rmdir succeeded, free fullname and /* if rmdir succeeded, free fullname and
* proceed with next entry */ * proceed with next entry */
@ -456,7 +464,7 @@ fs_remove(lua_State *L)
* On success, returns true. Otherwise returns nil, an error * On success, returns true. Otherwise returns nil, an error
* message and a platform-dependent error code. * message and a platform-dependent error code.
* *
* @function mkdir * @function rmdir
* @usage fs.rmdir("yourdirectory") * @usage fs.rmdir("yourdirectory")
* @tparam string dir The path of the directory to remove. * @tparam string dir The path of the directory to remove.
*/ */