void database_push(struct request *r) { struct lnode *n = calloc(sizeof(struct lnode), 1); struct file_entry *fe = calloc(sizeof(struct file_entry), 1); hash(r->data, r->len, fe->hash); unsigned long long id = 0; if (exists(fe->hash, &id)){ free(fe); free(n); errno = EEXIST; r->id = id; return; } fe->data = r->data; fe->len = r->len; fe->id = next_id++; strcpy(fe->ext, r->ext); n->data = fe; pthread_mutex_lock(&lock); file_list = lnode_push(file_list, n); pthread_mutex_unlock(&lock); database_write(fe); r->id = fe->id; return; }
int database_init() { if (!config->db_persist) return 0; FILE *fp = fopen(DATA_DIR "/database.txt", "r"); if (!fp) return 1; while(!feof(fp)){ struct lnode *n = calloc(sizeof(struct lnode), 1); struct file_entry *fe = calloc(sizeof(struct file_entry), 1); fscanf(fp, "%llx %zu\n", &fe->id, &fe->len); if (fe->len == 0 && fe->id == 0){ //Not a valid entry. free(n); free(fe); continue; } n->data = fe; file_list = lnode_push(file_list, n); if (fe->id >= next_id) next_id = fe->id + 1; } fclose(fp); return 0; }
void cache_bump(struct lnode *n) { if (n == cache_list) return; pthread_mutex_lock(&lock); lnode_pop(n); cache_list = lnode_push(cache_list, n); pthread_mutex_unlock(&lock); }
void socket_ban(char *str) { char *ip = calloc(strlen(str) + 1, 1); struct lnode *n = calloc(sizeof(*n), 1); strcpy(ip, str); n->data = ip; pthread_mutex_lock(&ban_lock); banned = lnode_push(banned, n); pthread_mutex_unlock(&ban_lock); }
int database_init() { if (!config->db_persist) return 0; // Backup DB file before we f**k anything up. cp(DATA_DIR "/database.txt", DATA_DIR "/database.txt.bak"); FILE *fp = fopen(DATA_DIR "/database.txt", "r"); if (!fp) return 0; while(!feof(fp)){ struct lnode *n = calloc(sizeof(struct lnode), 1); struct file_entry *fe = calloc(sizeof(struct file_entry), 1); fscanf(fp, "%llx %zu %s %s\n", &fe->id, &fe->len, fe->ext, fe->hash); if (fe->len == 0 || !isonfs(fe->id)){ //Not a valid entry. wkb_log(LOG_DB, "%llx: Invalid entry, ignoring", fe->id); free(n); free(fe); continue; } if (!strcmp(fe->ext, "NULL")){ // File has no extension. fe->ext[0] = 0; } if (!strcmp(fe->hash, "0")){ //File has no hash, generate one. database_read(fe); hash(fe->data, fe->len, fe->hash); free(fe->data); } if (exists(fe->hash, 0)){ //Check if file is already in DB. wkb_log(LOG_DB, "%llx: Duplicate detected, removing and continuing", fe->id); char buf[512]; snprintf(buf, 512, DATA_DIR "/database/%llx", fe->id); remove(buf); free(n); free(fe); continue; } n->data = fe; file_list = lnode_push(file_list, n); if (fe->id >= next_id) next_id = fe->id + 1; } fclose(fp); return 0; }
void cache_push(char *data, size_t len, unsigned long long id) { struct lnode *n = calloc(sizeof(*n), 1); struct cache_entry *ce = calloc(sizeof(*ce), 1); ce->data = data; ce->len = len; ce->id = id; pthread_mutex_lock(&lock); n->data = ce; cache_list = lnode_push(cache_list, n); pthread_mutex_unlock(&lock); }
void queue_push(struct lnode *n, struct client_ctx *cc) { if (queue_size >= SERVER_BACKLOG){ socket_puts(cc, "Server too overloaded\n"); free(cc); free(n); return; } pthread_mutex_lock(&avail_lock); n->data = cc; client_queue = lnode_push(client_queue, n); queue_size++; pthread_cond_signal(&avail_cond); pthread_mutex_unlock(&avail_lock); }
unsigned long long database_push(char *data, size_t len) { struct lnode *n = calloc(sizeof(struct lnode), 1); struct file_entry *fe = calloc(sizeof(struct file_entry), 1); pthread_mutex_lock(&lock); fe->len = len; fe->id = next_id++; n->data = fe; file_list = lnode_push(file_list, n); pthread_mutex_unlock(&lock); database_write(fe, data); cache_push(data, len, fe->id); return fe->id; }