periodic backup

This commit is contained in:
Fufu Fang 2021-09-04 03:00:25 +01:00
parent 5d539c30b1
commit ebcfb0a79e
6 changed files with 78 additions and 11 deletions

View File

@ -69,9 +69,6 @@ struct Cache {
/** \brief the FUSE filesystem path to the remote file*/
char *fs_path;
/** Transfer struct for the streaming cache */
TransferStruct *ts;
};
/**

View File

@ -41,8 +41,7 @@ static Link *Link_new(const char *linkname, LinkType type)
/*
* remove the '/' from linkname if it exists
*/
char *c =
&(link->linkname[strnlen(link->linkname, MAX_FILENAME_LEN) - 1]);
char *c = &(link->linkname[strnlen(link->linkname, MAX_FILENAME_LEN) - 1]);
if (*c == '/') {
*c = '\0';
}

View File

@ -13,7 +13,8 @@ typedef enum {
link_lock_debug = 1 << 5,
network_lock_debug = 1 << 6,
cache_lock_debug = 1 << 7,
libcurl_debug = 1 << 8,
ramcache_debug = 1 << 8,
libcurl_debug = 1 << 9,
} LogType;
/**

View File

@ -1,6 +1,7 @@
#include "ramcache.h"
#include "log.h"
#include "util.h"
#include <stdlib.h>
#include <string.h>
@ -11,6 +12,12 @@ size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb,
size_t recv_size = size * nmemb;
TransferStruct *ts = (TransferStruct *) userp;
if (ts->bg_transfer) {
lprintf(ramcache_debug, "ramcache: thread %x: locking;\n",
pthread_self());
}
PTHREAD_MUTEX_LOCK(&ts->lock);
ts->data = realloc(ts->data, ts->curr_size + recv_size + 1);
if (!ts->data) {
/*
@ -22,5 +29,49 @@ size_t write_memory_callback(void *recv_data, size_t size, size_t nmemb,
memmove(&ts->data[ts->curr_size], recv_data, recv_size);
ts->curr_size += recv_size;
ts->data[ts->curr_size] = '\0';
if (ts->bg_transfer) {
lprintf(ramcache_debug, "ramcache: thread %x: unlocking;\n",
pthread_self());
}
PTHREAD_MUTEX_UNLOCK(&ts->lock);
return recv_size;
}
}
TransferStruct *TransferStruct_bg_transfer_new()
{
TransferStruct *ts = CALLOC(1, sizeof(TransferStruct));
if (pthread_mutexattr_init(&ts->lock_attr)) {
lprintf(fatal, "lock_attr initialisation failed!\n");
}
if (pthread_mutexattr_setpshared(&ts->lock_attr,
PTHREAD_PROCESS_SHARED)) {
lprintf(fatal, "could not set lock_attr!\n");
}
if (pthread_mutex_init(&ts->lock, &ts->lock_attr)) {
lprintf(fatal, "lock initialisation failed!\n");
}
ts->bg_transfer = 1;
return ts;
}
void TransferStruct_bg_transfer_free(TransferStruct *ts)
{
if (ts->bg_transfer) {
if (pthread_mutex_destroy(&ts->lock)) {
lprintf(fatal, "could not destroy lock!\n");
}
if (pthread_mutexattr_destroy(&ts->lock_attr)) {
lprintf(fatal, "could not destroy lock_attr!\n");
}
}
/* free(NULL) does nothing */
free(ts->data);
FREE(ts);
}

View File

@ -26,12 +26,32 @@ struct TransferStruct {
Link *link;
/** \brief The minium requested size */
size_t min_req_size;
/** \brief Whether we have sent off the minimally requested data*/
int min_data_sent;
/** \brief mutex for background transfer */
pthread_mutex_t lock;
/** \brief attribute associated with the mutex */
pthread_mutexattr_t lock_attr;
/** \brief Whether this TransferStruct was used for background transfer */
int bg_transfer;
/** \brief The cache file used for background transfer */
Cache *cf;
/** \brief The ID of the segment being downloaded */
off_t seg_id;
};
/** \brief callback function for file transfer */
/**
* \brief Callback function for file transfer
*/
size_t write_memory_callback(void *contents, size_t size, size_t nmemb,
void *userp);
/**
* \brief Create a TransferStruct for background transfer
*/
TransferStruct *TransferStruct_bg_transfer_new();
/**
* \brief Free a TransferStruct used for background transfer
*/
void TransferStruct_free(TransferStruct *ts);
#endif

View File

@ -142,7 +142,6 @@ void FREE(void *ptr)
{
if (ptr) {
free(ptr);
ptr = NULL;
} else {
lprintf(fatal, "attempted to double free a pointer!\n");
}