/*
* build a path from an entry upto the root using recursion
*
* it appends '/' after strlcat, therefore buf[0] needs to be prepared with '/'
* and it will leave a trailing '/'
*
* returns the position of that trailing '/' so it can be deleted afterwards
* (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)
{
int offset = 1;
/* has parent? */
if (entry->up)
offset += copy_path_helper(entry->up, buf, size);

size_t len = strlcpy(buf+offset, entry->d_name, size - offset) + offset;
if (len < size)
{
buf[len++] = '/';
buf[len] = '\0';
}
return len-1;
}
/**
* Function to copy the full absolute path from dircache to the given buffer
* using the given dircache_entry pointer.
*
* Returns the size of the resulting string, or 0 if an error occured
*/
size_t dircache_copy_path(int index, char *buf, size_t size)
{
if (!size || !buf || index < 0)
return 0;

buf[0] = '/';
size_t res = copy_path_helper(&dircache_root[index], buf, size - 1);
/* fixup trailing '/' */
buf[res] = '\0';
return res;
}