httpdirfs/src/cache.c

100 lines
2.1 KiB
C
Raw Normal View History

#include "cache.h"
2019-04-12 14:52:11 +02:00
long Cache_read(const char *filepath, long offset, long len)
2019-04-12 14:52:11 +02:00
{
}
long Cache_write(const char *filepath, long offset, long len,
2019-04-12 14:52:11 +02:00
const uint8_t *content)
{
}
int Cache_create(const char *filepath, long len)
2019-04-12 14:52:11 +02:00
{
}
Cache *Cache_open(const char *filepath)
{
}
int Meta_write(Cache *cf)
{
}
int Meta_read(Cache *cf)
{
}
long Data_read(Cache *cf, long offset, long len,
const uint8_t *buf);
2019-04-12 14:52:11 +02:00
{
if (len == 0) {
fprintf(stderr, "Data_read(): fseek(): requested to read 0 byte!\n");
return -1;
}
FILE *fp;
fp = fopen(cf->filepath, "r");
if (!fp) {
/* The data file does not exist */
return -1;
}
if (fseek(fp, offset, SEEK_SET) == -1) {
/* fseek failed */
fprintf(stderr, "Data_read(): fseek(): %s\n", strerror(errno));
return -1;
}
long byte_read = fread((void*) buf, sizeof(uint8_t), len, fp);
if (byte_read != len) {
fprintf(stderr,
"Data_read(): fread(): requested %ld, returned %llu!\n",
len, byte_read);
if (feof(fp)) {
fprintf(stderr,
"Data_read(): fread(): reached the end of the file!\n");
}
if (ferror(fp)) {
fprintf(stderr,
"Data_read(): fread(): encountered error (from ferror)!\n");
}
}
if (fclose(fp)) {
fprintf(stderr, "Data_read(): fclose(): %s\n", strerror(errno));
return -1;
}
2019-04-12 14:52:11 +02:00
}
long Data_write(Cache *cf, long offset, long len,
const uint8_t *buf);
2019-04-12 14:52:11 +02:00
{
}
int Data_create(Cache *cf, long len)
2019-04-12 14:52:11 +02:00
{
2019-04-17 03:33:23 +02:00
int fd;
int mode;
mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
fd = open(cf->filepath, O_WRONLY | O_CREAT, mode);
if (fd == -1) {
fprintf(stderr, "Data_create(): %s\n", strerror(errno));
return 0;
}
if (ftruncate(fd, cf->len) == -1) {
2019-04-17 03:33:23 +02:00
fprintf(stderr, "Data_create(): %s\n", strerror(errno));
return 0;
}
if (close(fd) == -1) {
fprintf(stderr, "Data_create(): %s\n", strerror(errno));
return 0;
}
return 1;
2019-04-12 14:52:11 +02:00
}