Add in lfs_removeall(...) and lfs_rename_with_removeall(...).
This commit is contained in:
Tim Nordell
2021-06-13 21:39:55 -05:00
parent 2612ef130b
commit 40bebef368
2 changed files with 60 additions and 0 deletions

36
lfs.c
View File

@@ -5067,6 +5067,24 @@ int lfs_remove(lfs_t *lfs, const char *path) {
} }
#endif #endif
#ifndef LFS_READONLY
int lfs_removeall(lfs_t *lfs, const char *path) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
return err;
}
LFS_TRACE("lfs_removeall(%p, \"%s\")", (void*)lfs, path);
// Note: We pass in a helper pointer here so that this extra
// logic can be dropped if it is never referenced
err = lfs_rawremove(lfs, path, lfs_dir_prep_remove_nonempty_folders);
LFS_TRACE("lfs_removeall -> %d", err);
LFS_UNLOCK(lfs->cfg);
return err;
}
#endif
#ifndef LFS_READONLY #ifndef LFS_READONLY
int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) { int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
int err = LFS_LOCK(lfs->cfg); int err = LFS_LOCK(lfs->cfg);
@@ -5083,6 +5101,24 @@ int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath) {
} }
#endif #endif
#ifndef LFS_READONLY
int lfs_rename_with_removeall(lfs_t *lfs, const char *oldpath, const char *newpath) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
return err;
}
LFS_TRACE("lfs_rename(%p, \"%s\", \"%s\")", (void*)lfs, oldpath, newpath);
// Note: We pass in a helper pointer here so that this extra
// logic can be dropped if it is never referenced
err = lfs_rawrename(lfs, oldpath, newpath, lfs_dir_prep_remove_nonempty_folders);
LFS_TRACE("lfs_rename -> %d", err);
LFS_UNLOCK(lfs->cfg);
return err;
}
#endif
int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) { int lfs_stat(lfs_t *lfs, const char *path, struct lfs_info *info) {
int err = LFS_LOCK(lfs->cfg); int err = LFS_LOCK(lfs->cfg);
if (err) { if (err) {

24
lfs.h
View File

@@ -458,6 +458,17 @@ int lfs_unmount(lfs_t *lfs);
int lfs_remove(lfs_t *lfs, const char *path); int lfs_remove(lfs_t *lfs, const char *path);
#endif #endif
#ifndef LFS_READONLY
// Removes a file or directory
//
// If removing a directory, the directory must not have
// any directories but it may contain files. This is
// non-POSIX behavior, and thus is a different call
// than lfs_remove(...)
// Returns a negative error code on failure.
int lfs_removeall(lfs_t *lfs, const char *path);
#endif
#ifndef LFS_READONLY #ifndef LFS_READONLY
// Rename or move a file or directory // Rename or move a file or directory
// //
@@ -468,6 +479,19 @@ int lfs_remove(lfs_t *lfs, const char *path);
int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath); int lfs_rename(lfs_t *lfs, const char *oldpath, const char *newpath);
#endif #endif
#ifndef LFS_READONLY
// Rename or move a file or directory
//
// If the destination exists, it must match the source in type.
// If the destination is a directory, it may not contain
// any directories but it may contain files. This is
// non-POSIX behavior, and thus is a different call
// than lfs_rename(...)
//
// Returns a negative error code on failure.
int lfs_rename_with_removeall(lfs_t *lfs, const char *oldpath, const char *newpath);
#endif
// Find info about a file or directory // Find info about a file or directory
// //
// Fills out the info structure, based on the specified file or directory. // Fills out the info structure, based on the specified file or directory.