1
0
mirror of https://github.com/fumiama/simple-dict.git synced 2026-06-25 14:10:24 +08:00

修正md5

This commit is contained in:
fumiama
2021-06-01 20:00:28 +08:00
parent c86dce46c0
commit 7472da6610
4 changed files with 21 additions and 19 deletions

8
dict.c
View File

@@ -9,8 +9,8 @@ static uint8_t lock = 0;
static char* filepath; static char* filepath;
static uint8_t* dict_md5; static uint8_t* dict_md5;
static FILE *fp = NULL; //fp for EX static FILE* fp = NULL; //fp for EX
static FILE *fp5 = NULL; //fp for md5 static FILE* fp5 = NULL; //fp for md5
static FILE* thread_fp[THREADCNT]; static FILE* thread_fp[THREADCNT];
#ifdef CPUBIT64 #ifdef CPUBIT64
@@ -88,9 +88,11 @@ FILE* get_dict_fp(uint32_t index) {
else return NULL; else return NULL;
} }
void close_dict(uint8_t lock_type) { void close_dict(uint8_t lock_type, uint32_t index) {
puts("Close dict"); puts("Close dict");
lock &= ~lock_type; lock &= ~lock_type;
if(lock_type & LOCK_EX) fflush(fp);
else fflush(thread_fp[index]);
} }
off_t get_dict_size() { off_t get_dict_size() {

2
dict.h
View File

@@ -19,7 +19,7 @@ int fill_md5();
uint32_t last_nonnull(char* p, uint32_t max_size); uint32_t last_nonnull(char* p, uint32_t max_size);
int init_dict(char* file_path); int init_dict(char* file_path);
FILE *open_dict(uint8_t lock_type, uint32_t index); FILE *open_dict(uint8_t lock_type, uint32_t index);
void close_dict(uint8_t lock_type); void close_dict(uint8_t lock_type, uint32_t index);
FILE* get_dict_fp(uint32_t index); FILE* get_dict_fp(uint32_t index);
off_t get_dict_size(); off_t get_dict_size();
int is_md5_equal(uint8_t* digest); int is_md5_equal(uint8_t* digest);

View File

@@ -116,7 +116,7 @@ int send_all(THREADTIMER *timer) {
re = sendfile(timer->accept_fd, fileno(fp), &len, file_size) >= 0; re = sendfile(timer->accept_fd, fileno(fp), &len, file_size) >= 0;
#endif #endif
printf("Send %u bytes.\n", len); printf("Send %u bytes.\n", len);
close_dict(LOCK_SH); close_dict(LOCK_SH, timer->index);
} }
return re; return re;
} }
@@ -148,13 +148,13 @@ int s1_get(THREADTIMER *timer) {
SIMPLE_PB* spb = get_pb(fp); SIMPLE_PB* spb = get_pb(fp);
DICT* d = (DICT*)spb->target; DICT* d = (DICT*)spb->target;
if(!strcmp(timer->data, d->key)) { if(!strcmp(timer->data, d->key)) {
int r = close_and_send(timer->accept_fd, d->data, last_nonnull(d->data, ITEMSZ), LOCK_SH); int r = close_and_send(timer, d->data, last_nonnull(d->data, ITEMSZ));
free(spb); free(spb);
return r; return r;
} else free(spb); } else free(spb);
} }
} }
return close_and_send(timer->accept_fd, "null", 4, LOCK_SH); return close_and_send(timer, "null", 4);
} }
int s2_set(THREADTIMER *timer) { int s2_set(THREADTIMER *timer) {
@@ -180,10 +180,10 @@ int s3_set_data(THREADTIMER *timer) {
puts("Data copy to dict succ"); puts("Data copy to dict succ");
if(!set_pb(get_dict_fp(timer->index), items_len, sizeof(DICT), &d)) { if(!set_pb(get_dict_fp(timer->index), items_len, sizeof(DICT), &d)) {
printf("Error set data: dict[%s]=%s\n", d.key, timer->data); printf("Error set data: dict[%s]=%s\n", d.key, timer->data);
return close_and_send(timer->accept_fd, "erro", 4, LOCK_EX); return close_and_send(timer, "erro", 4);
} else { } else {
printf("Set data: dict[%s]=%s\n", d.key, timer->data); printf("Set data: dict[%s]=%s\n", d.key, timer->data);
return close_and_send(timer->accept_fd, "succ", 4, LOCK_EX); return close_and_send(timer, "succ", 4);
} }
} }
@@ -204,10 +204,10 @@ int s4_del(THREADTIMER *timer) {
if(next == end) { if(next == end) {
if(!ftruncate(fileno(fp), end - spb->real_len)) { if(!ftruncate(fileno(fp), end - spb->real_len)) {
free(spb); free(spb);
return close_and_send(timer->accept_fd, "succ", 4, LOCK_EX); return close_and_send(timer, "succ", 4);
} else { } else {
free(spb); free(spb);
return close_and_send(timer->accept_fd, "erro", 4, LOCK_EX); return close_and_send(timer, "erro", 4);
} }
} else { } else {
uint32_t cap = end - next; uint32_t cap = end - next;
@@ -221,19 +221,19 @@ int s4_del(THREADTIMER *timer) {
if(fwrite(data, cap, 1, fp) == 1) { if(fwrite(data, cap, 1, fp) == 1) {
free(data); free(data);
free(spb); free(spb);
return close_and_send(timer->accept_fd, "succ", 4, LOCK_EX); return close_and_send(timer, "succ", 4);
} }
} }
} }
free(data); free(data);
} }
free(spb); free(spb);
return close_and_send(timer->accept_fd, "erro", 4, LOCK_EX); return close_and_send(timer, "erro", 4);
} }
} else free(spb); } else free(spb);
} }
} }
return close_and_send(timer->accept_fd, "null", 4, LOCK_EX); return close_and_send(timer, "null", 4);
} }
int s5_md5(THREADTIMER *timer) { int s5_md5(THREADTIMER *timer) {
@@ -300,7 +300,7 @@ void kill_thread(THREADTIMER* timer) {
timer->data = NULL; timer->data = NULL;
puts("Free data."); puts("Free data.");
} }
if(timer->lock_type) close_dict(timer->lock_type); if(timer->lock_type) close_dict(timer->lock_type, timer->index);
puts("Finish killing."); puts("Finish killing.");
} }
@@ -391,9 +391,9 @@ void accept_client() {
} }
} }
int close_and_send(int accept_fd, char *data, size_t numbytes, uint32_t lock_type) { int close_and_send(THREADTIMER* timer, char *data, size_t numbytes) {
close_dict(lock_type); close_dict(timer->lock_type, timer->index);
return send_data(accept_fd, data, numbytes); return send_data(timer->accept_fd, data, numbytes);
} }
#define set_pass(pass, sps, slen, cmd) (pass=malloc(strlen(cmd)+slen+1),((pass)?(strcpy(pass,cmd),strcpy(pass+strlen(cmd),sps),1):0)) #define set_pass(pass, sps, slen, cmd) (pass=malloc(strlen(cmd)+slen+1),((pass)?(strcpy(pass,cmd),strcpy(pass+strlen(cmd),sps),1):0))

View File

@@ -22,7 +22,7 @@ void accept_client();
void accept_timer(void *p); void accept_timer(void *p);
int bind_server(uint16_t port, int try_times); int bind_server(uint16_t port, int try_times);
int check_buffer(THREADTIMER *timer); int check_buffer(THREADTIMER *timer);
int close_and_send(int accept_fd, char *data, size_t numbytes, uint32_t lock_type); int close_and_send(THREADTIMER* timer, char *data, size_t numbytes);
int free_after_send(int accept_fd, char *data, size_t length); int free_after_send(int accept_fd, char *data, size_t length);
void handle_accept(void *accept_fd_p); void handle_accept(void *accept_fd_p);
void handle_pipe(int signo); void handle_pipe(int signo);