Skip to content

Commit 1d68133

Browse files
committed
lfs.c:Support get file path
Signed-off-by: crafcat7 <[email protected]>
1 parent d01280e commit 1d68133

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

lfs.c

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,8 @@ static uint16_t lfs_fs_disk_version_minor(lfs_t *lfs) {
542542

543543

544544
/// Internal operations predeclared here ///
545+
static lfs_ssize_t lfs_dir_path_(lfs_t *lfs,
546+
lfs_mdir_t *dir, uint16_t id, char *path, lfs_size_t size);
545547
#ifndef LFS_READONLY
546548
static int lfs_dir_commit(lfs_t *lfs, lfs_mdir_t *dir,
547549
const struct lfs_mattr *attrs, int attrcount);
@@ -3833,6 +3835,53 @@ static lfs_soff_t lfs_file_size_(lfs_t *lfs, lfs_file_t *file) {
38333835
return file->ctz.size;
38343836
}
38353837

3838+
static lfs_ssize_t lfs_dir_path_(lfs_t *lfs,
3839+
lfs_mdir_t *dir, uint16_t id, char *path, lfs_size_t size) {
3840+
struct lfs_info info;
3841+
char *next = path;
3842+
lfs_mdir_t parent;
3843+
lfs_ssize_t len;
3844+
lfs_stag_t tag;
3845+
int err;
3846+
3847+
if (lfs_pair_cmp(lfs->root, dir->pair) != 0) {
3848+
tag = lfs_fs_parent(lfs, dir->pair, &parent);
3849+
if (tag < 0) {
3850+
return tag;
3851+
}
3852+
3853+
len = lfs_dir_path_(lfs, &parent, lfs_tag_id(tag), next, size);
3854+
if (len < 0) {
3855+
return len;
3856+
}
3857+
3858+
next += len;
3859+
size -= len;
3860+
}
3861+
3862+
err = lfs_dir_getinfo(lfs, dir, id, &info);
3863+
if (err < 0) {
3864+
return err;
3865+
}
3866+
3867+
len = strlen(info.name);
3868+
if (len >= (lfs_ssize_t)size) {
3869+
return LFS_ERR_INVAL;
3870+
}
3871+
3872+
memcpy(next, info.name, len + 1);
3873+
next += len;
3874+
3875+
if (info.type == LFS_TYPE_DIR) {
3876+
*next++ = '/';
3877+
if (++len >= (lfs_ssize_t)size) {
3878+
return LFS_ERR_INVAL;
3879+
}
3880+
*next = '\0';
3881+
}
3882+
3883+
return next - path;
3884+
}
38363885

38373886
/// General fs operations ///
38383887
static int lfs_stat_(lfs_t *lfs, const char *path, struct lfs_info *info) {
@@ -6235,6 +6284,22 @@ lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file) {
62356284
return res;
62366285
}
62376286

6287+
int lfs_file_path(lfs_t *lfs, lfs_file_t *file, char *path, lfs_size_t size) {
6288+
int err = LFS_LOCK(lfs->cfg);
6289+
if (err) {
6290+
return err;
6291+
}
6292+
6293+
LFS_TRACE("lfs_file_path(%p, %p)", (void*)lfs, (void*)file);
6294+
LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));
6295+
6296+
err = lfs_dir_path_(lfs, &file->m, file->id, path, size);
6297+
6298+
LFS_TRACE("lfs_file_path -> %d", err);
6299+
LFS_UNLOCK(lfs->cfg);
6300+
return err < 0 ? err : 0;
6301+
}
6302+
62386303
#ifndef LFS_READONLY
62396304
int lfs_mkdir(lfs_t *lfs, const char *path) {
62406305
int err = LFS_LOCK(lfs->cfg);
@@ -6366,6 +6431,20 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
63666431
return res;
63676432
}
63686433

6434+
int lfs_dir_path(lfs_t *lfs, lfs_dir_t *dir, char *path, lfs_size_t size) {
6435+
int err = LFS_LOCK(lfs->cfg);
6436+
if (err) {
6437+
return err;
6438+
}
6439+
6440+
LFS_TRACE("lfs_dir_path(%p, %p)", (void*)lfs, (void*)dir);
6441+
err = lfs_dir_path_(lfs, &dir->m, dir->id, path, size);
6442+
6443+
LFS_TRACE("lfs_dir_path -> %d", err);
6444+
LFS_UNLOCK(lfs->cfg);
6445+
return err < 0 ? err : 0;
6446+
}
6447+
63696448
int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) {
63706449
int err = LFS_LOCK(lfs->cfg);
63716450
if (err) {

lfs.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,10 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
656656
// Returns the size of the file, or a negative error code on failure.
657657
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);
658658

659+
// Get the absolute path of the open file.
660+
//
661+
// Returns a negative error code on failure.
662+
int lfs_file_path(lfs_t *lfs, lfs_file_t *file, char *path, lfs_size_t size);
659663

660664
/// Directory operations ///
661665

@@ -706,6 +710,10 @@ lfs_soff_t lfs_dir_tell(lfs_t *lfs, lfs_dir_t *dir);
706710
// Returns a negative error code on failure.
707711
int lfs_dir_rewind(lfs_t *lfs, lfs_dir_t *dir);
708712

713+
// Get the absolute path of the directory
714+
//
715+
// Returns a negative error code on failure.
716+
int lfs_dir_path(lfs_t *lfs, lfs_dir_t *dir, char *path, lfs_size_t size);
709717

710718
/// Filesystem-level filesystem operations
711719

0 commit comments

Comments
 (0)