1
0
Fork 0
forked from len0rd/rockbox

Optimize new dircache_copy_path so that the helper (strlcat) doesn't need to walk through the entire string repeatedly.

Also fix a off-by-one.

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@30039 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Thomas Martitz 2011-06-20 20:12:47 +00:00
parent af7aaae478
commit 3b29aa49d3

View file

@ -1077,11 +1077,12 @@ struct dirinfo* _dircache_get_entry_dirinfo(int id)
* (or, in case of truncation, the position of the nul byte */ * (or, in case of truncation, the position of the nul byte */
static size_t copy_path_helper(const struct dircache_entry *entry, char *buf, size_t size) static size_t copy_path_helper(const struct dircache_entry *entry, char *buf, size_t size)
{ {
int offset = 1;
/* has parent? */ /* has parent? */
if (entry->up) if (entry->up)
copy_path_helper(entry->up, buf, size); offset += copy_path_helper(entry->up, buf, size);
size_t len = strlcat(buf, entry->d_name, size); size_t len = strlcpy(buf+offset, entry->d_name, size - offset) + offset;
if (len < size) if (len < size)
{ {
buf[len++] = '/'; buf[len++] = '/';
@ -1101,7 +1102,7 @@ size_t dircache_copy_path(int index, char *buf, size_t size)
return 0; return 0;
buf[0] = '/'; buf[0] = '/';
size_t res = copy_path_helper(&dircache_root[index], buf, size); size_t res = copy_path_helper(&dircache_root[index], buf, size - 1);
/* fixup trailing '/' */ /* fixup trailing '/' */
buf[res] = '\0'; buf[res] = '\0';
return res; return res;