diff options
author | Travis Geiselbrecht <geist@foobox.com> | 2015-10-28 19:58:02 -0700 |
---|---|---|
committer | Travis Geiselbrecht <geist@foobox.com> | 2015-10-28 20:06:09 -0700 |
commit | 15b6344b850c30cb4c30127e0a732820c790c5ee (patch) | |
tree | 51b454b30b5af618bc76ece5504b76a495d89cd1 /lib | |
parent | e3c6b0d63df7d070374c2c0998ec10f71fbc3dae (diff) | |
download | common-15b6344b850c30cb4c30127e0a732820c790c5ee.tar.gz |
[lib][fs] add file remove
Diffstat (limited to 'lib')
-rw-r--r-- | lib/fs/fs.c | 27 | ||||
-rw-r--r-- | lib/fs/include/lib/fs.h | 2 | ||||
-rw-r--r-- | lib/fs/memfs/memfs.c | 36 |
3 files changed, 60 insertions, 5 deletions
diff --git a/lib/fs/fs.c b/lib/fs/fs.c index ec407f60..be318612 100644 --- a/lib/fs/fs.c +++ b/lib/fs/fs.c @@ -147,8 +147,7 @@ static status_t mount(const char *path, const char *device, const struct fs_api fscookie *cookie; status_t err = api->mount(dev, &cookie); if (err < 0) { - bio_close(dev); - put_mount(mount); + if (dev) bio_close(dev); return err; } @@ -257,6 +256,30 @@ status_t fs_create_file(const char *path, filehandle **handle, uint64_t len) return 0; } +status_t fs_remove_file(const char *path) +{ + char temppath[FS_MAX_PATH_LEN]; + + strlcpy(temppath, path, sizeof(temppath)); + fs_normalize_path(temppath); + + const char *newpath; + struct fs_mount *mount = find_mount(temppath, &newpath); + if (!mount) + return ERR_NOT_FOUND; + + if (!mount->api->remove) { + put_mount(mount); + return ERR_NOT_SUPPORTED; + } + + status_t err = mount->api->remove(mount->cookie, newpath); + + put_mount(mount); + + return err; +} + ssize_t fs_read_file(filehandle *handle, void *buf, off_t offset, size_t len) { return handle->mount->api->read(handle->cookie, buf, offset, len); diff --git a/lib/fs/include/lib/fs.h b/lib/fs/include/lib/fs.h index a86a8f6f..1bc1d531 100644 --- a/lib/fs/include/lib/fs.h +++ b/lib/fs/include/lib/fs.h @@ -47,6 +47,7 @@ status_t fs_unmount(const char *path) __NONNULL(); /* file api */ status_t fs_create_file(const char *path, filehandle **handle, uint64_t len) __NONNULL(); status_t fs_open_file(const char *path, filehandle **handle) __NONNULL(); +status_t fs_remove_file(const char *path) __NONNULL(); ssize_t fs_read_file(filehandle *handle, void *buf, off_t offset, size_t len) __NONNULL(); ssize_t fs_write_file(filehandle *handle, const void *buf, off_t offset, size_t len) __NONNULL(); status_t fs_close_file(filehandle *handle) __NONNULL(); @@ -75,6 +76,7 @@ struct fs_api { status_t (*unmount)(fscookie *); status_t (*open)(fscookie *, const char *, filecookie **); status_t (*create)(fscookie *, const char *, filecookie **, uint64_t); + status_t (*remove)(fscookie *, const char *); status_t (*stat)(filecookie *, struct file_stat *); ssize_t (*read)(filecookie *, void *, off_t, size_t); ssize_t (*write)(filecookie *, const void *, off_t, size_t); diff --git a/lib/fs/memfs/memfs.c b/lib/fs/memfs/memfs.c index aa9bb8ed..0c9035c1 100644 --- a/lib/fs/memfs/memfs.c +++ b/lib/fs/memfs/memfs.c @@ -98,6 +98,13 @@ static status_t memfs_mount(struct bdev *dev, fscookie **cookie) return NO_ERROR; } +static void free_file(memfs_file_t *file) +{ + free(file->ptr); + free(file->name); + free(file); +} + static status_t memfs_unmount(fscookie *cookie) { LTRACEF("cookie %p\n", cookie); @@ -109,9 +116,7 @@ static status_t memfs_unmount(fscookie *cookie) // free all the files memfs_file_t *file; while ((file = list_remove_head_type(&mem->files, memfs_file_t, node))) { - free(file->ptr); - free(file->name); - free(file); + free_file(file); } mutex_release(&mem->lock); @@ -200,6 +205,30 @@ static status_t memfs_open(fscookie *cookie, const char *name, filecookie **fcoo return NO_ERROR; } +static status_t memfs_remove(fscookie *cookie, const char *name) +{ + LTRACEF("cookie %p name '%s'\n", cookie, name); + + memfs_t *mem = (memfs_t *)cookie; + + // make sure we strip out any leading / + name = trim_name(name); + + mutex_acquire(&mem->lock); + memfs_file_t *file = find_file(mem, name); + if (file) + list_delete(&file->node); + mutex_release(&mem->lock); + + if (!file) + return ERR_NOT_FOUND; + + // XXX make sure there are no open file handles + free_file(file); + + return NO_ERROR; +} + static status_t memfs_close(filecookie *fcookie) { memfs_file_t *file = (memfs_file_t *)fcookie; @@ -357,6 +386,7 @@ static const struct fs_api memfs_api = { .create = memfs_create, .open = memfs_open, + .remove = memfs_remove, .close = memfs_close, .read = memfs_read, |