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