Upload files to "/"

This commit is contained in:
8nl 2024-06-29 20:40:42 +00:00
parent 8f78d5aa1d
commit fb64e2bbdd

View File

@ -106,17 +106,14 @@ char *rand_name(void) {
} }
} }
int send_str(int fd, const char *str, ssize_t size, int flag) { int send_str(int fd, char *str, ssize_t size, int flag) {
ssize_t n = 0; while (size) {
ssize_t rbytes = size;
while (rbytes) {
ssize_t ret = 0; ssize_t ret = 0;
if (flag) if (flag)
ret = send(fd, str + n, rbytes, 0); ret = send(fd, str, size, 0);
else else
ret = write(fd, str + n, rbytes); ret = write(fd, str, size);
if (ret < 0 && (errno != EAGAIN || errno != EINTR)) if (ret < 0 && (errno != EAGAIN || errno != EINTR))
return 1; return 1;
@ -124,8 +121,8 @@ int send_str(int fd, const char *str, ssize_t size, int flag) {
else if (ret == 0) else if (ret == 0)
return 0; return 0;
rbytes -= ret; size -= ret;
n += size - rbytes; str += ret;
} }
return 0; return 0;
@ -133,7 +130,7 @@ int send_str(int fd, const char *str, ssize_t size, int flag) {
void upload(int cfd, pid_t par) { void upload(int cfd, pid_t par) {
int ret = -1; int ret = -1;
int ffd = 0; int ffd = -1;
char *filename = rand_name(); char *filename = rand_name();
if (send(cfd, filename, strlen(filename), 0) < 0) if (send(cfd, filename, strlen(filename), 0) < 0)
@ -148,7 +145,7 @@ void upload(int cfd, pid_t par) {
char buf[BUFLEN + 1]; char buf[BUFLEN + 1];
ssize_t tbytes = 0; size_t tbytes = 0;
while (1) { while (1) {
ssize_t rbytes = recv(cfd, buf, sizeof(buf), 0); ssize_t rbytes = recv(cfd, buf, sizeof(buf), 0);
if (rbytes < 0 && (errno != EAGAIN || errno != EINTR)) if (rbytes < 0 && (errno != EAGAIN || errno != EINTR))
@ -157,8 +154,8 @@ void upload(int cfd, pid_t par) {
else if (rbytes == 0) else if (rbytes == 0)
break; break;
tbytes += rbytes; tbytes += (size_t)rbytes;
if (tbytes / 1024 / 1024 >= (ssize_t)max_size) { if (tbytes / 1024 / 1024 >= max_size) {
send_str(cfd, MSG2, sizeof(MSG2), WRITE); send_str(cfd, MSG2, sizeof(MSG2), WRITE);
break; break;
} }
@ -170,7 +167,7 @@ void upload(int cfd, pid_t par) {
ret = 0; ret = 0;
UPL_ERROR: UPL_ERROR:
if (ffd > 0) if (ffd != -1)
close(ffd); close(ffd);
send_str(cfd, MSG3, sizeof(MSG3), WRITE); send_str(cfd, MSG3, sizeof(MSG3), WRITE);
@ -185,7 +182,7 @@ UPL_ERROR:
void load(int cfd, pid_t par) { void load(int cfd, pid_t par) {
int ret = -1; int ret = -1;
int ffd = 0; int ffd = -1;
/* Get filename */ /* Get filename */
char *filename = malloc(name_len + 1); char *filename = malloc(name_len + 1);
@ -243,9 +240,10 @@ void load(int cfd, pid_t par) {
ret = 0; ret = 0;
LD_ERROR: LD_ERROR:
if (ffd > 0) if (ffd != -1)
close(ffd); close(ffd);
sleep(1);
close(cfd); close(cfd);
if (ret == -1) if (ret == -1)
fprintf(stderr, "%s: output thread: %s: %s\n", prog_name, filename, strerror(errno)); fprintf(stderr, "%s: output thread: %s: %s\n", prog_name, filename, strerror(errno));