Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lfs.c:Support get file path #975

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions lfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,8 @@ static uint16_t lfs_fs_disk_version_minor(lfs_t *lfs) {


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

static lfs_ssize_t lfs_dir_path_(lfs_t *lfs,
lfs_mdir_t *dir, uint16_t id, char *path, lfs_size_t size) {
struct lfs_info info;
char *next = path;
lfs_mdir_t parent;
lfs_ssize_t len;
lfs_stag_t tag;
int err;

if (lfs_pair_cmp(lfs->root, dir->pair) != 0) {
tag = lfs_fs_parent(lfs, dir->pair, &parent);
if (tag < 0) {
return tag;
}

len = lfs_dir_path_(lfs, &parent, lfs_tag_id(tag), next, size);
if (len < 0) {
return len;
}

next += len;
size -= len;
}

err = lfs_dir_getinfo(lfs, dir, id, &info);
if (err < 0) {
return err;
}

len = strlen(info.name);
if (len >= (lfs_ssize_t)size) {
return LFS_ERR_INVAL;
}

memcpy(next, info.name, len + 1);
next += len;

if (info.type == LFS_TYPE_DIR) {
*next++ = '/';
if (++len >= (lfs_ssize_t)size) {
return LFS_ERR_INVAL;
}
*next = '\0';
}

return next - path;
}

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

int lfs_file_path(lfs_t *lfs, lfs_file_t *file, char *path, lfs_size_t size) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
return err;
}

LFS_TRACE("lfs_file_path(%p, %p)", (void*)lfs, (void*)file);
LFS_ASSERT(lfs_mlist_isopen(lfs->mlist, (struct lfs_mlist*)file));

err = lfs_dir_path_(lfs, &file->m, file->id, path, size);

LFS_TRACE("lfs_file_path -> %d", err);
LFS_UNLOCK(lfs->cfg);
return err < 0 ? err : 0;
}

#ifndef LFS_READONLY
int lfs_mkdir(lfs_t *lfs, const char *path) {
int err = LFS_LOCK(lfs->cfg);
Expand Down Expand Up @@ -6366,6 +6431,20 @@ lfs_ssize_t lfs_fs_size(lfs_t *lfs) {
return res;
}

int lfs_dir_path(lfs_t *lfs, lfs_dir_t *dir, char *path, lfs_size_t size) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
return err;
}

LFS_TRACE("lfs_dir_path(%p, %p)", (void*)lfs, (void*)dir);
err = lfs_dir_path_(lfs, &dir->m, dir->id, path, size);

LFS_TRACE("lfs_dir_path -> %d", err);
LFS_UNLOCK(lfs->cfg);
return err < 0 ? err : 0;
}

int lfs_fs_traverse(lfs_t *lfs, int (*cb)(void *, lfs_block_t), void *data) {
int err = LFS_LOCK(lfs->cfg);
if (err) {
Expand Down
8 changes: 8 additions & 0 deletions lfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,10 @@ int lfs_file_rewind(lfs_t *lfs, lfs_file_t *file);
// Returns the size of the file, or a negative error code on failure.
lfs_soff_t lfs_file_size(lfs_t *lfs, lfs_file_t *file);

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

/// Directory operations ///

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

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

/// Filesystem-level filesystem operations

Expand Down
Loading