changed the data type for file size. wrote Data_read()

This commit is contained in:
Fufu Fang 2019-04-17 17:38:27 +01:00
parent 4958d6cfea
commit 8d10316b95
2 changed files with 68 additions and 22 deletions

View File

@ -1,15 +1,15 @@
#include "cache.h"
off_t Cache_read(const char *filepath, off_t offset, size_t size)
long Cache_read(const char *filepath, long offset, long len)
{
}
size_t Cache_write(const char *filepath, off_t offset, size_t size,
long Cache_write(const char *filepath, long offset, long len,
const uint8_t *content)
{
}
int Cache_create(const char *filepath, size_t size)
int Cache_create(const char *filepath, long len)
{
}
@ -29,16 +29,54 @@ int Meta_read(Cache *cf)
}
size_t Data_read(Cache *cf, off_t offset, size_t size)
long Data_read(Cache *cf, long offset, long len,
const uint8_t *buf);
{
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;
}
}
long Data_write(Cache *cf, long offset, long len,
const uint8_t *buf);
{
}
size_t Data_write(Cache *cf, off_t offset, size_t size,
const uint8_t *content)
{
}
int Data_create(Cache *cf, size_t size)
int Data_create(Cache *cf, long len)
{
int fd;
int mode;
@ -49,7 +87,7 @@ int Data_create(Cache *cf, size_t size)
fprintf(stderr, "Data_create(): %s\n", strerror(errno));
return 0;
}
if (ftruncate(fd, cf->size) == -1) {
if (ftruncate(fd, cf->len) == -1) {
fprintf(stderr, "Data_create(): %s\n", strerror(errno));
return 0;
}

View File

@ -7,10 +7,16 @@
* \file cache.h
* \brief cache related structures and functions
* \details think of these as some sort of generic caching layer.
* \note apologies for whoever is going to be reading this. Sorry for being so
* \note
* - apologies for whoever is going to be reading this. Sorry for being so
* verbose in this header file, this is probably one of the most challenging
* thing I have ever written so far! Yes I am doing a PhD in computer science,
* but it doesn't imply that I am good at computer science or programming!
* - We are using 'long' to store file size, because the offset in fseek() is
* in long, because the way we use the cache system, you cannot seek past
* long. So the biggest file size has to be able to be stored in long. This
* makes this program architecturally dependent, but this is due to the
* dependency to fseek().
*/
@ -18,8 +24,8 @@
* \brief a cache segment
*/
typedef struct {
off_t *start;
off_t *end;
long *start;
long *end;
} Seg;
/**
@ -28,7 +34,7 @@ typedef struct {
*/
typedef struct {
const char* filepath;
size_t size;
long len;
FILE *data_fd; /**< the file descriptor for the data file */
FILE *meta_fd; /**< the file descriptor for the meta file */
int nseg; /**<the number of segments */
@ -40,12 +46,12 @@ typedef struct {
/**
* \brief read from a cache file
*/
off_t Cache_read(const char *filepath, off_t offset, size_t size);
long Cache_read(const char *filepath, long offset, long len);
/**
* \brief write to a cache file
*/
size_t Cache_write(const char *filepath, off_t offset, size_t size,
long Cache_write(const char *filepath, long offset, long len,
const uint8_t *content);
/**************************** Internal functions ******************************/
@ -53,7 +59,7 @@ size_t Cache_write(const char *filepath, off_t offset, size_t size,
/**
* \brief create a cache file
*/
int Cache_create(const char *filepath, size_t size);
int Cache_create(const char *filepath, long len);
/**
* \brief open a cache file
@ -72,18 +78,20 @@ int Meta_read(Cache *cf);
/**
* \brief read a data file
* \return -1 when the data file does not exist
*/
size_t Data_read(Cache *cf, off_t offset, size_t size);
long Data_read(Cache *cf, long offset, long len,
const uint8_t *buf);
/**
* \brief write to a data file
*/
size_t Data_write(Cache *cf, off_t offset, size_t size,
const uint8_t *content);
long Data_write(Cache *cf, long offset, long len,
const uint8_t *buf);
/**
* \brief create a data file
* \details We use sparse creation here
* \return 1 on success, 0 on failure
*/
int Data_create(Cache *cf, size_t size);
int Data_create(Cache *cf, long len);
#endif