Skip to content

Commit

Permalink
frogfs_get_name fix
Browse files Browse the repository at this point in the history
frogfs_get_name was orignally an internal function that got exposed as an api
function.  NULL terminating its string when the size is a multiple of 4 was
overlooked, so a small API change was required to ensure it always is NULL
terminated.
  • Loading branch information
jkent committed Dec 9, 2023
1 parent f7677a0 commit bf6173f
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ frogfs_vfs_register(&frogfs_vfs_conf);
#### Object functions:
* const frogfs_entry_t *[frogfs_get_entry](https://frogfs.readthedocs.io/en/latest/api-reference/bare.html#c.frogfs_get_entry)(const frogfs_fs_t *fs, const char *path)
* const char *[frogfs_get_name](https://frogfs.readthedocs.io/en/latest/api-reference/bare.html#c.frogfs_get_name)(const frogfs_entry_t *entry)
* char *[frogfs_get_name](https://frogfs.readthedocs.io/en/latest/api-reference/bare.html#c.frogfs_get_name)(const frogfs_entry_t *entry)
* char *[frogfs_get_path](https://frogfs.readthedocs.io/en/latest/api-reference/bare.html#c.frogfs_get_path)(const frogfs_fs_t *fs, const frogfs_entry_t *entry)
* int [frogfs_is_dir](https://frogfs.readthedocs.io/en/latest/api-reference/bare.html#c.frogfs_is_dir)(const frogfs_entry_t *entry)
* int [frogfs_is_file](https://frogfs.readthedocs.io/en/latest/api-reference/bare.html#c.frogfs_is_file)(const frogfs_entry_t *entry)
Expand Down
2 changes: 1 addition & 1 deletion idf_component.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version: "4.0.3"
version: "5.0.0"
description: A read-only file system for embedded use.
url: https://github.com/jkent/frogfs
dependencies:
Expand Down
7 changes: 4 additions & 3 deletions include/frogfs/frogfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,15 +132,16 @@ const frogfs_entry_t *frogfs_get_entry(const frogfs_fs_t *fs,
/**
* \brief Get name for frogfs entry
* \param[in] entry \a frogfs_entry_t pointer
* \return name string
* \return name string, caller is expected to free
*/
const char *frogfs_get_name(const frogfs_entry_t *entry);
char *frogfs_get_name(const frogfs_entry_t *entry);

/**
* \brief Get full path for frogfs entry
* \param[in] fs \a frogfs_fs_t pointer
* \param[in] entry \a frogfs_entry_t pointer
* \return full path string or \a NULL if entry is NULL
* \return full path string or \a NULL if entry is NULL, caller is
* expected to free
*/
char *frogfs_get_path(const frogfs_fs_t *fs, const frogfs_entry_t *entry);

Expand Down
28 changes: 18 additions & 10 deletions src/frogfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ static inline uint32_t djb2_hash(const char *s)
return hash;
}

static const char *get_name(const frogfs_entry_t *entry)
{
if (FROGFS_IS_DIR(entry)) {
return (const void *) entry + 8 + (entry->child_count * 4);
} else if (FROGFS_IS_FILE(entry) && !FROGFS_IS_COMP(entry)) {
return (const void *) entry + 16;
} else {
return (const void *) entry + 20;
}
}

frogfs_fs_t *frogfs_init(const frogfs_config_t *conf)
{
frogfs_fs_t *fs = calloc(1, sizeof(frogfs_fs_t));
Expand Down Expand Up @@ -192,15 +203,12 @@ const frogfs_entry_t *frogfs_get_entry(const frogfs_fs_t *fs, const char *path)
return NULL;
}

const char *frogfs_get_name(const frogfs_entry_t *entry)
char *frogfs_get_name(const frogfs_entry_t *entry)
{
if (FROGFS_IS_DIR(entry)) {
return (const void *) entry + 8 + (entry->child_count * 4);
} else if (FROGFS_IS_FILE(entry) && !FROGFS_IS_COMP(entry)) {
return (const void *) entry + 16;
} else {
return (const void *) entry + 20;
}
char *name = malloc(entry->seg_sz + 1);
memcpy(name, get_name(entry), entry->seg_sz);
name[entry->seg_sz] = '\0';
return name;
}

char *frogfs_get_path(const frogfs_fs_t *fs, const frogfs_entry_t *entry)
Expand All @@ -221,13 +229,13 @@ char *frogfs_get_path(const frogfs_fs_t *fs, const frogfs_entry_t *entry)
const frogfs_entry_t *parent = (const void *) fs->head + entry->parent;
if ((const void *) parent == (const void *) fs->root) {
memmove(path + entry->seg_sz, path, len);
memcpy(path, frogfs_get_name(entry), entry->seg_sz);
memcpy(path, get_name(entry), entry->seg_sz);
len += entry->seg_sz;
break;
} else {
memmove(path + entry->seg_sz + 1, path, len + 1);
path[0] = '/';
memcpy(path + 1, frogfs_get_name(entry), entry->seg_sz);
memcpy(path + 1, get_name(entry), entry->seg_sz);
len += entry->seg_sz + 1;
}
entry = parent;
Expand Down
2 changes: 1 addition & 1 deletion src/frogfs_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ typedef struct frogfs_fh_t {
typedef struct frogfs_dh_t {
const frogfs_fs_t *fs; /**< frogfs fs pointer */
const frogfs_dir_t *dir; /**< frogfs entry */
uint16_t index; /**< current index */
long index; /**< current index */
} frogfs_dh_t;

/**
Expand Down
1 change: 1 addition & 0 deletions src/vfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ static int frogfs_vfs_readdir_r(void *ctx, DIR *pdir, struct dirent *ent,
ent->d_ino = frogfs_telldir(dh->dh);
const char *name = frogfs_get_name(entry);
strlcpy(ent->d_name, name, sizeof(ent->d_name));
free(name);
ent->d_type = DT_UNKNOWN;
if (frogfs_is_dir(entry)) {
ent->d_type = DT_DIR;
Expand Down

0 comments on commit bf6173f

Please sign in to comment.