From aed2106e3b94ebe8a397ffc696f320e3dd3eae2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20T=C3=B6rnblom?= Date: Thu, 30 Oct 2025 20:52:42 +0100 Subject: [PATCH] cosmetics --- Makefile.posix | 2 +- Makefile.ps4 | 2 +- Makefile.ps5 | 2 +- cmd_common.c | 4 +-- cmd_ps4.c | 95 +++++++------------------------------------------- cmd_ps5.c | 2 +- io.c | 83 +++++++++++++++++++++++++++++++++++++++++++ io.h | 38 ++++++++++++++++++++ srv.c | 3 +- 9 files changed, 142 insertions(+), 89 deletions(-) create mode 100644 io.c create mode 100644 io.h diff --git a/Makefile.posix b/Makefile.posix index 92fe059..5bf8931 100644 --- a/Makefile.posix +++ b/Makefile.posix @@ -21,7 +21,7 @@ CFLAGS := -Wall -Werror -DVERSION_TAG=\"$(VERSION_TAG)\" all: $(ELF) -$(ELF): main_posix.c srv.c cmd_common.c cmd_posix.c notify.c +$(ELF): main_posix.c io.c srv.c cmd_common.c cmd_posix.c notify.c $(CC) $(CFLAGS) -o $@ $^ clean: diff --git a/Makefile.ps4 b/Makefile.ps4 index f6b7537..1e75093 100644 --- a/Makefile.ps4 +++ b/Makefile.ps4 @@ -30,7 +30,7 @@ CFLAGS := -Wall -Werror -DVERSION_TAG=\"$(VERSION_TAG)\" all: $(ELF) -$(ELF): main_ps4.c srv.c cmd_common.c cmd_ps4.c notify.c +$(ELF): main_ps4.c io.c srv.c cmd_common.c cmd_ps4.c notify.c $(CC) $(CFLAGS) -o $@ $^ clean: diff --git a/Makefile.ps5 b/Makefile.ps5 index 5f230ff..8bde6d3 100644 --- a/Makefile.ps5 +++ b/Makefile.ps5 @@ -30,7 +30,7 @@ CFLAGS := -Wall -Werror -DVERSION_TAG=\"$(VERSION_TAG)\" all: $(ELF) -$(ELF): main_ps5.c srv.c cmd_common.c cmd_ps5.c notify.c +$(ELF): main_ps5.c io.c srv.c cmd_common.c cmd_ps5.c notify.c $(CC) $(CFLAGS) -o $@ $^ clean: diff --git a/cmd_common.c b/cmd_common.c index de255af..b7b77c9 100644 --- a/cmd_common.c +++ b/cmd_common.c @@ -31,6 +31,7 @@ along with this program; see the file COPYING. If not, see #include #include "cmd.h" +#include "io.h" #include "log.h" @@ -138,7 +139,7 @@ ftp_active_printf(ftp_env_t *env, const char *fmt, ...) { len = strlen(buf); - if(write(env->active_fd, buf, len) != len) { + if(io_nwrite(env->active_fd, buf, len)) { return -1; } @@ -529,7 +530,6 @@ ftp_cmd_RETR(ftp_env_t *env, const char* arg) { } close(fd); - if(ftp_data_close(env)) { return ftp_perror(env); } diff --git a/cmd_ps4.c b/cmd_ps4.c index 28f7d7a..7f35540 100644 --- a/cmd_ps4.c +++ b/cmd_ps4.c @@ -23,11 +23,11 @@ along with this program; see the file COPYING. If not, see #include #include -#include #include #include #include "cmd.h" +#include "io.h" /** @@ -87,75 +87,6 @@ typedef struct self_entry { } self_entry_t; -/** - * Read exactly N bytes from the given file descriptor. - **/ -static int -nread(int fd, void* buf, size_t n) { - int r; - - if((r=read(fd, buf, n)) < 0) { - return -1; - } - - if(r != n) { - errno = EIO; - return -1; - } - - return 0; -} - - -/** - * Write exactly N bytes to the given file descriptor. - **/ -static int -nwrite(int fd, const void* buf, size_t n) { - int r; - - if((r=write(fd, buf, n)) < 0) { - return -1; - } - - if(r != n) { - errno = EIO; - return -1; - } - - return 0; -} - - -/** - * Copy exactly N bytes from one file descriptor to another. - **/ -static int -ncopy(int fd_in, int fd_out, size_t size) { - size_t copied = 0; - char buf[0x1000]; - ssize_t n; - - while(copied < size) { - n = size - copied; - if(n > sizeof(buf)) { - n = sizeof(buf); - } - - if(nread(fd_in, buf, n)) { - return -1; - } - if(nwrite(fd_out, buf, n)) { - return -1; - } - - copied += n; - } - - return 0; -} - - /** * Decrypt and copy an ELF segment. **/ @@ -181,7 +112,7 @@ decrypt_segment(int self_fd, int elf_fd, const Elf64_Phdr* phdr, size_t ind) { // ensure kernel is mapping the segments memcpy(&tmp, data, 1); - if(nwrite(elf_fd, data, phdr->p_filesz)) { + if(io_nwrite(elf_fd, data, phdr->p_filesz)) { munmap(data, phdr->p_filesz); return -1; } @@ -222,7 +153,7 @@ copy_segment(int from_fd, off_t from_start, int to_fd, off_t to_start, return -1; } - if(ncopy(from_fd, to_fd, size)) { + if(io_ncopy(from_fd, to_fd, size)) { return -1; } @@ -257,7 +188,7 @@ self2elf(const char* self_path, const char* elf_path) { } // Read the SELF header - if(nread(self_fd, &head, sizeof(head))) { + if(io_nread(self_fd, &head, sizeof(head))) { close(self_fd); return -1; } @@ -275,7 +206,7 @@ self2elf(const char* self_path, const char* elf_path) { close(self_fd); return -1; } - if(nread(self_fd, entries, head.num_entries * sizeof(self_entry_t))) { + if(io_nread(self_fd, entries, head.num_entries * sizeof(self_entry_t))) { close(self_fd); free(entries); return -1; @@ -283,7 +214,7 @@ self2elf(const char* self_path, const char* elf_path) { // Read the ELF header bundled within the SELF file elf_off = lseek(self_fd, 0, SEEK_CUR); - if(nread(self_fd, &ehdr, sizeof(ehdr))) { + if(io_nread(self_fd, &ehdr, sizeof(ehdr))) { close(self_fd); free(entries); return -1; @@ -320,7 +251,7 @@ self2elf(const char* self_path, const char* elf_path) { } // Write the ELF header - if(nwrite(elf_fd, &ehdr, sizeof(ehdr))) { + if(io_nwrite(elf_fd, &ehdr, sizeof(ehdr))) { close(self_fd); close(elf_fd); free(entries); @@ -329,13 +260,13 @@ self2elf(const char* self_path, const char* elf_path) { // Enumerate ELF program headers for(int i=0; i. */ + +#include +#include + +#include + +#include "io.h" + + +int +io_nread(int fd, void* buf, size_t n) { + int r; + + if((r=read(fd, buf, n)) < 0) { + return -1; + } + + if(r != n) { + errno = EIO; + return -1; + } + + return 0; +} + + +int +io_nwrite(int fd, const void* buf, size_t n) { + int r; + + if((r=write(fd, buf, n)) < 0) { + return -1; + } + + if(r != n) { + errno = EIO; + return -1; + } + + return 0; +} + + +int +io_ncopy(int fd_in, int fd_out, size_t size) { + size_t copied = 0; + char buf[0x4000]; + ssize_t n; + + while(copied < size) { + n = size - copied; + if(n > sizeof(buf)) { + n = sizeof(buf); + } + + if(io_nread(fd_in, buf, n)) { + return -1; + } + if(io_nwrite(fd_out, buf, n)) { + return -1; + } + + copied += n; + } + + return 0; +} + diff --git a/io.h b/io.h new file mode 100644 index 0000000..f17a65f --- /dev/null +++ b/io.h @@ -0,0 +1,38 @@ +/* Copyright (C) 2025 John Törnblom + +This program is free software; you can redistribute it and/or modify it +under the terms of the GNU General Public License as published by the +Free Software Foundation; either version 3, or (at your option) any +later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; see the file COPYING. If not, see +. */ + +#pragma once + +#include + + +/** + * Read exactly N bytes from the given file descriptor. + **/ +int io_nread(int fd, void* buf, size_t n); + + +/** + * Write exactly N bytes to the given file descriptor. + **/ +int io_nwrite(int fd, const void* buf, size_t n); + + +/** + * Copy exactly N bytes from one file descriptor to another. + **/ +int io_ncopy(int fd_in, int fd_out, size_t size); + diff --git a/srv.c b/srv.c index aa65e8e..50a81be 100644 --- a/srv.c +++ b/srv.c @@ -27,6 +27,7 @@ along with this program; see the file COPYING. If not, see #include #include "cmd.h" +#include "io.h" #include "log.h" #include "notify.h" @@ -187,7 +188,7 @@ ftp_greet(ftp_env_t *env) { strncat(msg, "220 Service is ready\r\n", sizeof(msg)-1); len = strlen(msg); - if(write(env->active_fd, msg, len) != len) { + if(io_nwrite(env->active_fd, msg, len)) { return -1; }