use open instead of fopen for fire readwarite

This commit is contained in:
DanyLE 2022-08-27 16:57:44 +02:00
parent 923c6b2625
commit 088baa1ef5
2 changed files with 2226 additions and 2223 deletions

View File

@ -9,6 +9,7 @@
#include <unistd.h> #include <unistd.h>
#include <string.h> #include <string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h>
#ifdef USE_OPENSSL #ifdef USE_OPENSSL
#include <openssl/ssl.h> #include <openssl/ssl.h>
@ -1355,7 +1356,7 @@ void *decode_multi_part_request_data(void *data)
char *field; char *field;
int len; int len;
//dictionary dic = NULL; //dictionary dic = NULL;
FILE *fp = NULL; int fd = -1;
char *token, *keytoken, *valtoken; char *token, *keytoken, *valtoken;
antd_request_t *rq = (antd_request_t *)data; antd_request_t *rq = (antd_request_t *)data;
antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io); antd_task_t *task = antd_create_task(NULL, (void *)rq, NULL, rq->client->last_io);
@ -1399,10 +1400,11 @@ void *decode_multi_part_request_data(void *data)
} }
line = NULL; line = NULL;
// get the binary data // get the binary data
LOG("Part file: %s part name: %s", part_file, part_name);
if (part_name != NULL) if (part_name != NULL)
{ {
// go to the beginning of data bock // go to the beginning of data bock
while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && strcmp(buf, "\r\n") != 0) while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && strncmp(buf, "\r\n",2) != 0)
; ;
; ;
@ -1432,8 +1434,8 @@ void *decode_multi_part_request_data(void *data)
else else
{ {
file_path = __s("%s%s.%u", server_config.tmpdir, part_file, (unsigned)time(NULL)); file_path = __s("%s%s.%u", server_config.tmpdir, part_file, (unsigned)time(NULL));
fp = fopen(file_path, "wb"); fd = open(file_path, O_WRONLY | O_CREAT, 0600);
if (fp) if (fd > 0)
{ {
int totalsize = 0, len = 0; int totalsize = 0, len = 0;
//read until the next boundary //read until the next boundary
@ -1441,16 +1443,18 @@ void *decode_multi_part_request_data(void *data)
// need a solution // need a solution
while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && !strstr(buf, boundary)) while ((len = read_buf(rq->client, buf, sizeof(buf))) > 0 && !strstr(buf, boundary))
{ {
fwrite(buf, len, 1, fp); len = guard_write(fd, buf, len);
totalsize += len; totalsize += len;
} }
//remove \r\n at the end //remove \r\n at the end
fseek(fp, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
//fseek(fp,-2, SEEK_CUR); //fseek(fp,-2, SEEK_CUR);
totalsize -= 2; totalsize -= 2;
int stat = ftruncate(fileno(fp), totalsize); int stat = ftruncate(fd, totalsize);
LOG("Write %d bytes to %s", totalsize, file_path);
UNUSED(stat); UNUSED(stat);
fclose(fp); close(fd);
line = buf; line = buf;
field = __s("%s.file", part_name); field = __s("%s.file", part_name);

View File

@ -611,7 +611,7 @@ int antd_recv_upto(void *src, void *data, int len)
{ {
return -1; return -1;
}*/ }*/
if (received <= 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) if (received < 0 && (errno == EAGAIN || errno == EWOULDBLOCK))
{ {
return 0; return 0;
} }
@ -900,21 +900,20 @@ int __b(void *client, const unsigned char *data, int size)
int __f(void *client, const char *file) int __f(void *client, const char *file)
{ {
unsigned char buffer[BUFFLEN]; unsigned char buffer[BUFFLEN];
FILE *ptr; int fd;
ptr = fopen(file, "rb"); fd = open(file, O_RDONLY);
if (!ptr) if (fd < 0)
{ {
LOG("Cannot read : %s", file); LOG("Cannot read : %s", file);
return 0; return 0;
} }
size_t size; int size;
while (!feof(ptr)) while ((size = read(fd, buffer, BUFFLEN)) > 0)
{ {
size = fread(buffer, 1, BUFFLEN, ptr);
if (antd_send(client, buffer, size) == -1) if (antd_send(client, buffer, size) == -1)
return 0; return 0;
} }
fclose(ptr); close(fd);
return 1; return 1;
} }