1
0
mirror of https://github.com/falk-werner/webfuse-provider synced 2024-10-27 20:44:10 +00:00
falk-werner_webfuse-provider/lib/webfuse_provider/impl/dirbuffer.c

66 lines
1.4 KiB
C
Raw Normal View History

2020-06-16 21:39:45 +00:00
#include "webfuse_provider/impl/dirbuffer.h"
2019-03-03 12:34:43 +00:00
#include <stdlib.h>
#include <string.h>
#define WFP_IMPL_DIRBUFFER_DEFAULT_CAPACITY 8
2019-03-03 12:34:43 +00:00
2019-03-26 22:04:53 +00:00
struct wfp_dirbuffer * wfp_impl_dirbuffer_create(void)
2019-03-03 12:34:43 +00:00
{
2019-03-26 22:04:53 +00:00
struct wfp_dirbuffer * buffer = malloc(sizeof(struct wfp_dirbuffer));
buffer->size = 0;
buffer->capacity = WFP_IMPL_DIRBUFFER_DEFAULT_CAPACITY;
buffer->entries = malloc(sizeof(struct wfp_dirbuffer_entry) * buffer->capacity);
2019-03-03 12:34:43 +00:00
return buffer;
}
2019-03-26 22:04:53 +00:00
void wfp_impl_dirbuffer_dispose(
struct wfp_dirbuffer * buffer)
2019-03-03 12:34:43 +00:00
{
for (size_t i = 0; i < buffer->size; i++)
2019-03-03 12:34:43 +00:00
{
free(buffer->entries[i].name);
2019-03-03 12:34:43 +00:00
}
free(buffer->entries);
2019-03-03 12:34:43 +00:00
free(buffer);
}
2019-03-26 22:04:53 +00:00
void wfp_impl_dirbuffer_add(
struct wfp_dirbuffer * buffer,
2019-03-03 12:34:43 +00:00
char const * name,
ino_t inode)
{
if (buffer->size >= buffer->capacity)
{
buffer->capacity *= 2;
buffer->entries = realloc(buffer->entries, (sizeof(struct wfp_dirbuffer_entry) * buffer->capacity));
}
2019-03-03 12:34:43 +00:00
buffer->entries[buffer->size].name = strdup(name);
buffer->entries[buffer->size].inode = inode;
buffer->size++;
2019-03-03 12:34:43 +00:00
}
size_t
wfp_impl_dirbuffer_size(
2019-03-26 22:04:53 +00:00
struct wfp_dirbuffer * buffer)
2019-03-03 12:34:43 +00:00
{
return buffer->size;
}
struct wfp_dirbuffer_entry const *
wfp_impl_dirbuffer_entry_at(
struct wfp_dirbuffer * buffer,
size_t pos)
{
struct wfp_dirbuffer_entry const * entry = NULL;
if (pos < buffer->size)
{
entry = &(buffer->entries[pos]);
}
2019-03-03 12:34:43 +00:00
return entry;
2019-03-03 12:34:43 +00:00
}