diff --git a/cfgwriter.c b/cfgwriter.c index 43a9539..30df430 100644 --- a/cfgwriter.c +++ b/cfgwriter.c @@ -1,13 +1,8 @@ #include #include +#include "config.h" #include "simple-protobuf/simple_protobuf.h" -struct CONFIG { - char pwd[64]; //password - char sps[64]; //set password -}; -typedef struct CONFIG CONFIG; - CONFIG cfg; int main() { @@ -19,13 +14,14 @@ int main() { FILE* fp = fopen("cfg.sp", "wb"); if(fp) { set_pb(fp, types_len, sizeof(CONFIG), &cfg); - memset(&cfg, 0, sizeof(CONFIG)); fclose(fp); - puts("Write file succeed."); + puts("Config is saved to cfg.sp."); fp = NULL; + puts("Check config..."); fp = fopen("cfg.sp", "rb"); if(fp) { SIMPLE_PB* spb = get_pb(fp); + memset(&cfg, 0, sizeof(CONFIG)); memcpy(&cfg, spb->target, sizeof(CONFIG)); printf("set pwd: %s, sps: %s\n", cfg.pwd, cfg.sps); } else perror("[SPB]"); diff --git a/config.h b/config.h new file mode 100644 index 0000000..56dd421 --- /dev/null +++ b/config.h @@ -0,0 +1,10 @@ +#ifndef _CONFIG_H_ +#define _CONFIG_H_ + +struct CONFIG { + char pwd[64]; //password + char sps[64]; //set password +}; +typedef struct CONFIG CONFIG; + +#endif \ No newline at end of file diff --git a/server.c b/server.c index 4002fff..f224456 100644 --- a/server.c +++ b/server.c @@ -13,18 +13,13 @@ #include #include #include +#include "config.h" #include "simple-protobuf/simple_protobuf.h" #if !__APPLE__ #include #endif -struct CONFIG { - char pwd[64]; //password - char sps[64]; //set password -}; -typedef struct CONFIG CONFIG; - CONFIG* cfg; int fd; @@ -250,16 +245,17 @@ int s3_set_data(THREADTIMER *timer) { printf("Get data size: %tu\n", timer->numbytes); } } - if(file_size <= BUFSIZ - (is_first_data?0:sizeof(uint32_t))) { - while(timer->numbytes != file_size - (is_first_data?0:sizeof(uint32_t))) { - timer->numbytes += recv(timer->accept_fd, timer->data + timer->numbytes + (is_first_data?0:sizeof(uint32_t)), BUFSIZ - timer->numbytes - (is_first_data?0:sizeof(uint32_t)), 0); + size_t offset = (is_first_data?0:sizeof(uint32_t)); + if(file_size <= BUFSIZ - offset) { + while(timer->numbytes != file_size - offset) { + timer->numbytes += recv(timer->accept_fd, timer->data + timer->numbytes + offset, BUFSIZ - timer->numbytes - offset, 0); } - if(fwrite(timer->data + (is_first_data?0:sizeof(uint32_t)), file_size, 1, timer->fp) != 1) { + if(fwrite(timer->data + offset, file_size, 1, timer->fp) != 1) { puts("Set data error."); return close_file_and_send(timer, "erro", 4); } else return close_file_and_send(timer, "succ", 4); } else { - if(fwrite(timer->data + (is_first_data?0:sizeof(uint32_t)), timer->numbytes - (is_first_data?0:sizeof(uint32_t)), 1, timer->fp) != 1) { + if(fwrite(timer->data + offset, timer->numbytes - offset, 1, timer->fp) != 1) { puts("Set data error."); return close_file_and_send(timer, "erro", 4); }