diff --git a/src/BackgroundThread.cpp b/src/BackgroundThread.cpp index 9f224f3..12ec5f2 100644 --- a/src/BackgroundThread.cpp +++ b/src/BackgroundThread.cpp @@ -1,50 +1,42 @@ #include "BackgroundThread.hpp" -#include -#include -#include -#include - -#include - +#include #include "ftp.h" #include "net.h" -BackgroundThread * BackgroundThread::instance = nullptr; +BackgroundThread *BackgroundThread::instance = nullptr; -BackgroundThread::BackgroundThread(): BackgroundThreadWrapper(BackgroundThread::getPriority()) { - DEBUG_FUNCTION_LINE("Create new Server"); +BackgroundThread::BackgroundThread() : BackgroundThreadWrapper(BackgroundThread::getPriority()) { + DEBUG_FUNCTION_LINE("Start FTP Server"); mutex.lock(); - this->serverSocket = create_server(PORT); - DCFlushRange(&(this->serverSocket), 4); + this->serverSocket = create_server(PORT); + DCFlushRange(&(this->serverSocket), 4); mutex.unlock(); - DEBUG_FUNCTION_LINE("handle %d", this->serverSocket); CThread::resumeThread(); } BackgroundThread::~BackgroundThread() { - DEBUG_FUNCTION_LINE("Clean up FTP"); - if(this->serverSocket != -1){ + DEBUG_FUNCTION_LINE("Shutting down FTP Server"); + if (this->serverSocket != -1) { mutex.lock(); - cleanup_ftp(); - network_close(this->serverSocket); + cleanup_ftp(); + network_close(this->serverSocket); mutex.unlock(); this->serverSocket = -1; } - DEBUG_FUNCTION_LINE("Cleaned up FTP"); } BOOL BackgroundThread::whileLoop() { - if(this->serverSocket != -1){ + if (this->serverSocket != -1) { mutex.lock(); - network_down = process_ftp_events(this->serverSocket); + network_down = process_ftp_events(this->serverSocket); mutex.unlock(); - if(network_down) { + if (network_down) { DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket); mutex.lock(); - cleanup_ftp(); - network_close(this->serverSocket); - this->serverSocket = -1; - DCFlushRange(&(this->serverSocket), 4); + cleanup_ftp(); + network_close(this->serverSocket); + this->serverSocket = -1; + DCFlushRange(&(this->serverSocket), 4); mutex.unlock(); } } diff --git a/src/BackgroundThread.hpp b/src/BackgroundThread.hpp index a557498..89dbe91 100644 --- a/src/BackgroundThread.hpp +++ b/src/BackgroundThread.hpp @@ -1,50 +1,50 @@ #pragma once + #include "utils/BackgroundThreadWrapper.hpp" #include #include "utils/logger.h" #define PORT 21 -class BackgroundThread: BackgroundThreadWrapper { +class BackgroundThread : BackgroundThreadWrapper { public: static BackgroundThread *getInstance() { - DCFlushRange(&instance, sizeof(instance)); - ICInvalidateRange(&instance, sizeof(instance)); - if(instance == NULL) { + DCFlushRange(&instance, sizeof(BackgroundThread)); + ICInvalidateRange(&instance, sizeof(BackgroundThread)); + if (instance == nullptr) { instance = new BackgroundThread(); - DCFlushRange(&instance, sizeof(instance)); - ICInvalidateRange(&instance, sizeof(instance)); + DCFlushRange(&instance, sizeof(BackgroundThread)); + ICInvalidateRange(&instance, sizeof(BackgroundThread)); } return instance; } static void destroyInstance() { - DCFlushRange(&instance, sizeof(instance)); - ICInvalidateRange(&instance, sizeof(instance)); + DCFlushRange(&instance, sizeof(BackgroundThread)); + ICInvalidateRange(&instance, sizeof(BackgroundThread)); DEBUG_FUNCTION_LINE("Instance is %08X\n", instance); OSSleepTicks(OSSecondsToTicks(1)); - if(instance != NULL) { + if (instance != nullptr) { delete instance; - instance = NULL; - DCFlushRange(&instance, sizeof(instance)); - ICInvalidateRange(&instance, sizeof(instance)); + instance = nullptr; + DCFlushRange(&instance, sizeof(BackgroundThread)); + ICInvalidateRange(&instance, sizeof(BackgroundThread)); } } BackgroundThread(); - virtual ~BackgroundThread(); + ~BackgroundThread() override; private: static int32_t getPriority() { return 16; } - virtual BOOL whileLoop(); + BOOL whileLoop() override; - static BackgroundThread * instance; + static BackgroundThread *instance; int serverSocket = -1; int network_down = 0; - }; diff --git a/src/ftp.c b/src/ftp.c index 099a39a..b98df05 100644 --- a/src/ftp.c +++ b/src/ftp.c @@ -42,7 +42,7 @@ misrepresented as being the original software. #include "net.h" #include "vrt.h" -#define UNUSED __attribute__((unused)) +#define UNUSED __attribute__((unused)) #define FTP_BUFFER_SIZE 1024 #define MAX_CLIENTS 5 @@ -56,7 +56,7 @@ static uint8_t num_clients = 0; static uint16_t passive_port = 1024; static char *password = NULL; -void console_printf(const char *format, ...){ +void console_printf(const char *format, ...) { } typedef int32_t (*data_connection_callback)(int32_t data_socket, void *arg); @@ -76,13 +76,15 @@ struct client_struct { bool data_connection_connected; data_connection_callback data_callback; void *data_connection_callback_arg; + void (*data_connection_cleanup)(void *arg); + uint64_t data_connection_timer; }; typedef struct client_struct client_t; -static client_t *clients[MAX_CLIENTS] = { NULL }; +static client_t *clients[MAX_CLIENTS] = {NULL}; void set_ftp_password(char *new_password) { if (password) @@ -92,14 +94,14 @@ void set_ftp_password(char *new_password) { if (!password) return; - strcpy((char *)password, new_password); + strcpy((char *) password, new_password); } else { password = NULL; } } static bool compare_ftp_password(char *password_attempt) { - return !password || !strcmp((char *)password, password_attempt); + return !password || !strcmp((char *) password, password_attempt); } /* @@ -107,7 +109,7 @@ static bool compare_ftp_password(char *password_attempt) { */ static int32_t write_reply(client_t *client, uint16_t code, char *msg) { uint32_t msglen = 4 + strlen(msg) + CRLF_LENGTH; - char * msgbuf = (char *) malloc(msglen + 1); + char *msgbuf = (char *) malloc(msglen + 1); if (msgbuf == NULL) return -ENOMEM; sprintf(msgbuf, "%u %s\r\n", code, msg); @@ -197,7 +199,7 @@ static int32_t ftp_SYST(client_t *client, char *rest UNUSED) { static int32_t ftp_TYPE(client_t *client, char *rest) { char representation_type[FTP_BUFFER_SIZE], param[FTP_BUFFER_SIZE]; - char *args[] = { representation_type, param }; + char *args[] = {representation_type, param}; uint32_t num_args = split(rest, ' ', 1, args); if (num_args == 0) { return write_reply(client, 501, "Syntax error in parameters."); @@ -231,7 +233,7 @@ static int32_t ftp_CWD(client_t *client, char *path) { int32_t result; if (!vrt_chdir(client->cwd, path)) { result = write_reply(client, 250, "CWD command successful."); - } else { + } else { result = write_reply(client, 550, strerror(errno)); } return result; @@ -241,7 +243,7 @@ static int32_t ftp_CDUP(client_t *client, char *rest UNUSED) { int32_t result; if (!vrt_chdir(client->cwd, "..")) { result = write_reply(client, 250, "CDUP command successful."); - } else { + } else { result = write_reply(client, 550, strerror(errno)); } return result; @@ -287,7 +289,7 @@ static int32_t ftp_RNTO(client_t *client, char *path) { } else { result = write_reply(client, 550, strerror( - errno)); + errno)); } *client->pending_rename = '\0'; return result; @@ -317,7 +319,7 @@ static int32_t ftp_PASV(client_t *client, char *rest UNUSED) { bindAddress.sin_port = htons(passive_port++); // XXX: BUG: This will overflow eventually, with interesting results... bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); int32_t result; - if ((result = network_bind(client->passive_socket, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { + if ((result = network_bind(client->passive_socket, (struct sockaddr *) &bindAddress, sizeof(bindAddress))) < 0) { close_passive_socket(client); return write_reply(client, 520, "Unable to bind listening socket."); } @@ -347,7 +349,7 @@ static int32_t ftp_PORT(client_t *client, char *portspec) { return write_reply(client, 501, "Syntax error in parameters."); } close_passive_socket(client); - uint16_t port = ((p1 &0xff) << 8) | (p2 & 0xff); + uint16_t port = ((p1 & 0xff) << 8) | (p2 & 0xff); client->address.sin_addr = sin_addr; client->address.sin_port = htons(port); console_printf("Set client address to %s:%u\n", addr_str, port); @@ -367,7 +369,7 @@ static int32_t prepare_data_connection_active(client_t *client, data_connection_ bindAddress.sin_port = htons(SRC_PORT); bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); int32_t result; - if ((result = network_bind(data_socket, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { + if ((result = network_bind(data_socket, (struct sockaddr *) &bindAddress, sizeof(bindAddress))) < 0) { network_close(data_socket); return result; } @@ -389,7 +391,7 @@ static int32_t prepare_data_connection(client_t *client, void *callback, void *a data_connection_handler handler = prepare_data_connection_active; if (client->passive_socket >= 0) handler = prepare_data_connection_passive; - result = handler(client, (data_connection_callback)callback, arg); + result = handler(client, (data_connection_callback) callback, arg); if (result < 0) { result = write_reply(client, 520, "Closing data connection, error occurred during transfer."); } else { @@ -409,7 +411,7 @@ static int32_t send_nlst(int32_t data_socket, DIR_P *iter) { struct dirent *dirent = NULL; while ((dirent = vrt_readdir(iter)) != 0) { size_t end_index = strlen(dirent->d_name); - if(end_index + 2 >= MAXPATHLEN) + if (end_index + 2 >= MAXPATHLEN) continue; strcpy(filename, dirent->d_name); filename[end_index] = CRLF[0]; @@ -433,7 +435,7 @@ static int32_t send_list(int32_t data_socket, DIR_P *iter) { while ((dirent = vrt_readdir(iter)) != 0) { snprintf(filename, sizeof(filename), "%s/%s", iter->path, dirent->d_name); - if(stat(filename, &st) == 0) { + if (stat(filename, &st) == 0) { mtime = st.st_mtime; size = st.st_size; } else { @@ -472,7 +474,7 @@ static int32_t ftp_LIST(client_t *client, char *path) { // handle buggy clients that use "LIST -aL" or similar, at the expense of breaking paths that begin with '-' char flags[FTP_BUFFER_SIZE]; char rest[FTP_BUFFER_SIZE]; - char *args[] = { flags, rest }; + char *args[] = {flags, rest}; split(path, ' ', 1, args); path = rest; } @@ -480,8 +482,8 @@ static int32_t ftp_LIST(client_t *client, char *path) { path = "."; } - if(path && client->cwd) { - if(strcmp(path, ".") == 0 && strcmp(client->cwd, "/") == 0) { + if (path && client->cwd) { + if (strcmp(path, ".") == 0 && strcmp(client->cwd, "/") == 0) { UnmountVirtualPaths(); MountVirtualDevices(); } @@ -628,7 +630,7 @@ typedef int32_t (*ftp_command_handler)(client_t *client, char *args); static int32_t dispatch_to_handler(client_t *client, char *cmd_line, const char **commands, const ftp_command_handler *handlers) { char cmd[FTP_BUFFER_SIZE], rest[FTP_BUFFER_SIZE]; - char *args[] = { cmd, rest }; + char *args[] = {cmd, rest}; split(cmd_line, ' ', 1, args); int32_t i; for (i = 0; commands[i]; i++) { @@ -638,8 +640,9 @@ static int32_t dispatch_to_handler(client_t *client, char *cmd_line, const char return handlers[i](client, rest); } -static const char *site_commands[] = { "LOADER", "CLEAR", "CHMOD", "PASSWD", "NOPASSWD", "EJECT", "MOUNT", "UNMOUNT", "LOAD", NULL }; -static const ftp_command_handler site_handlers[] = { ftp_SITE_LOADER, ftp_SITE_CLEAR, ftp_SITE_CHMOD, ftp_SITE_PASSWD, ftp_SITE_NOPASSWD, ftp_SITE_EJECT, ftp_SITE_MOUNT, ftp_SITE_UNMOUNT, ftp_SITE_LOAD, ftp_SITE_UNKNOWN }; +static const char *site_commands[] = {"LOADER", "CLEAR", "CHMOD", "PASSWD", "NOPASSWD", "EJECT", "MOUNT", "UNMOUNT", "LOAD", NULL}; +static const ftp_command_handler site_handlers[] = {ftp_SITE_LOADER, ftp_SITE_CLEAR, ftp_SITE_CHMOD, ftp_SITE_PASSWD, ftp_SITE_NOPASSWD, ftp_SITE_EJECT, ftp_SITE_MOUNT, ftp_SITE_UNMOUNT, + ftp_SITE_LOAD, ftp_SITE_UNKNOWN}; static int32_t ftp_SITE(client_t *client, char *cmd_line) { return dispatch_to_handler(client, cmd_line, site_commands, site_handlers); @@ -661,22 +664,22 @@ static int32_t ftp_UNKNOWN(client_t *client, char *rest UNUSED) { return write_reply(client, 502, "Command not implemented."); } -static const char *unauthenticated_commands[] = { "USER", "PASS", "QUIT", "REIN", "NOOP", NULL }; -static const ftp_command_handler unauthenticated_handlers[] = { ftp_USER, ftp_PASS, ftp_QUIT, ftp_REIN, ftp_NOOP, ftp_NEEDAUTH }; +static const char *unauthenticated_commands[] = {"USER", "PASS", "QUIT", "REIN", "NOOP", NULL}; +static const ftp_command_handler unauthenticated_handlers[] = {ftp_USER, ftp_PASS, ftp_QUIT, ftp_REIN, ftp_NOOP, ftp_NEEDAUTH}; static const char *authenticated_commands[] = { - "USER", "PASS", "LIST", "PWD", "CWD", "CDUP", - "SIZE", "PASV", "PORT", "TYPE", "SYST", "MODE", - "RETR", "STOR", "APPE", "REST", "DELE", "MKD", - "RMD", "RNFR", "RNTO", "NLST", "QUIT", "REIN", - "SITE", "NOOP", "ALLO", NULL + "USER", "PASS", "LIST", "PWD", "CWD", "CDUP", + "SIZE", "PASV", "PORT", "TYPE", "SYST", "MODE", + "RETR", "STOR", "APPE", "REST", "DELE", "MKD", + "RMD", "RNFR", "RNTO", "NLST", "QUIT", "REIN", + "SITE", "NOOP", "ALLO", NULL }; static const ftp_command_handler authenticated_handlers[] = { - ftp_USER, ftp_PASS, ftp_LIST, ftp_PWD, ftp_CWD, ftp_CDUP, - ftp_SIZE, ftp_PASV, ftp_PORT, ftp_TYPE, ftp_SYST, ftp_MODE, - ftp_RETR, ftp_STOR, ftp_APPE, ftp_REST, ftp_DELE, ftp_MKD, - ftp_DELE, ftp_RNFR, ftp_RNTO, ftp_NLST, ftp_QUIT, ftp_REIN, - ftp_SITE, ftp_NOOP, ftp_SUPERFLUOUS, ftp_UNKNOWN + ftp_USER, ftp_PASS, ftp_LIST, ftp_PWD, ftp_CWD, ftp_CDUP, + ftp_SIZE, ftp_PASV, ftp_PORT, ftp_TYPE, ftp_SYST, ftp_MODE, + ftp_RETR, ftp_STOR, ftp_APPE, ftp_REST, ftp_DELE, ftp_MKD, + ftp_DELE, ftp_RNFR, ftp_RNTO, ftp_NLST, ftp_QUIT, ftp_REIN, + ftp_SITE, ftp_NOOP, ftp_SUPERFLUOUS, ftp_UNKNOWN }; /* @@ -746,7 +749,7 @@ static bool process_accept_events(int32_t server) { int32_t peer; struct sockaddr_in client_address; int32_t addrlen = sizeof(client_address); - while ((peer = network_accept(server, (struct sockaddr *)&client_address, &addrlen)) != -EAGAIN) { + while ((peer = network_accept(server, (struct sockaddr *) &client_address, &addrlen)) != -EAGAIN) { if (peer < 0) { console_printf("Error accepting connection: [%i] %s\n", -peer, strerror(-peer)); return false; @@ -805,13 +808,13 @@ static void process_data_events(client_t *client) { if (client->passive_socket >= 0) { struct sockaddr_in data_peer_address; int32_t addrlen = sizeof(data_peer_address); - result = network_accept(client->passive_socket, (struct sockaddr *)&data_peer_address,&addrlen); + result = network_accept(client->passive_socket, (struct sockaddr *) &data_peer_address, &addrlen); if (result >= 0) { client->data_socket = result; client->data_connection_connected = true; } } else { - if ((result = network_connect(client->data_socket, (struct sockaddr *)&client->address, sizeof(client->address))) < 0) { + if ((result = network_connect(client->data_socket, (struct sockaddr *) &client->address, sizeof(client->address))) < 0) { if (result == -EINPROGRESS || result == -EALREADY) result = -EAGAIN; if ((result != -EAGAIN) && (result != -EISCONN)) { @@ -900,7 +903,7 @@ static void process_control_events(client_t *client) { } console_printf("Received line longer than %u bytes, closing client.\n", FTP_BUFFER_SIZE - 1); -recv_loop_end: + recv_loop_end: cleanup_client(client); } diff --git a/src/ftp.h b/src/ftp.h index e459c16..8a2887d 100644 --- a/src/ftp.h +++ b/src/ftp.h @@ -29,11 +29,15 @@ misrepresented as being the original software. #ifdef __cplusplus extern "C"{ #endif + #include void accept_ftp_client(int32_t server); + void set_ftp_password(char *new_password); + bool process_ftp_events(int32_t server); + void cleanup_ftp(); #ifdef __cplusplus diff --git a/src/main.cpp b/src/main.cpp index 20f66b2..ad4f1b7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -8,7 +8,6 @@ #include #include "utils/logger.h" #include -#include #include "virtualpath.h" #include "BackgroundThread.hpp" @@ -27,15 +26,15 @@ uint32_t hostIpAddress = 0; int iosuhaxMount = 0; int fsaFd = -1; -BackgroundThread * thread = nullptr; +BackgroundThread *thread = nullptr; /* Entry point */ -ON_APPLICATION_START() { +ON_APPLICATION_START() { nn::ac::Initialize(); - nn::ac::ConnectAsync(); + nn::ac::ConnectAsync(); nn::ac::GetAssignedAddress(&hostIpAddress); - - WHBLogUdpInit(); + + WHBLogUdpInit(); //!******************************************************************* //! Initialize FS * @@ -43,7 +42,7 @@ ON_APPLICATION_START() { DEBUG_FUNCTION_LINE("IOSUHAX_Open"); int res = IOSUHAX_Open(nullptr); - if(res < 0) { + if (res < 0) { DEBUG_FUNCTION_LINE("IOSUHAX_open failed"); VirtualMountDevice("fs:/"); } else { @@ -52,7 +51,7 @@ ON_APPLICATION_START() { DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open"); fsaFd = IOSUHAX_FSA_Open(); - if(fsaFd < 0) { + if (fsaFd < 0) { DEBUG_FUNCTION_LINE("IOSUHAX_FSA_Open failed"); } @@ -63,9 +62,9 @@ ON_APPLICATION_START() { mount_fs("storage_odd_updates", fsaFd, "/dev/odd02", "/vol/storage_odd_updates"); mount_fs("storage_odd_content", fsaFd, "/dev/odd03", "/vol/storage_odd_content"); mount_fs("storage_odd_content2", fsaFd, "/dev/odd04", "/vol/storage_odd_content2"); - mount_fs("storage_slc", fsaFd, NULL, "/vol/system"); - mount_fs("storage_mlc", fsaFd, NULL, "/vol/storage_mlc01"); - mount_fs("storage_usb", fsaFd, NULL, "/vol/storage_usb01"); + mount_fs("storage_slc", fsaFd, nullptr, "/vol/system"); + mount_fs("storage_mlc", fsaFd, nullptr, "/vol/storage_mlc01"); + mount_fs("storage_usb", fsaFd, nullptr, "/vol/storage_usb01"); VirtualMountDevice("fs:/"); VirtualMountDevice("slccmpt01:/"); @@ -87,15 +86,15 @@ ON_APPLICATION_START() { DCFlushRange(&thread, 4); } -void stopThread(){ +void stopThread() { BackgroundThread::destroyInstance(); } -ON_APPLICATION_REQUESTS_EXIT(){ +ON_APPLICATION_REQUESTS_EXIT() { DEBUG_FUNCTION_LINE("Ending ftp server"); stopThread(); - if(iosuhaxMount) { + if (iosuhaxMount) { IOSUHAX_sdio_disc_interface.shutdown(); IOSUHAX_usb_disc_interface.shutdown(); diff --git a/src/main.h b/src/main.h index 77d7698..a2fad53 100644 --- a/src/main.h +++ b/src/main.h @@ -5,6 +5,7 @@ #ifdef __cplusplus extern "C" { #endif + #include #include "net.h" diff --git a/src/net.c b/src/net.c index 9d57f58..13693f0 100644 --- a/src/net.c +++ b/src/net.c @@ -67,54 +67,54 @@ void initialise_network() { } #endif -int32_t network_socket(uint32_t domain,uint32_t type,uint32_t protocol) { +int32_t network_socket(uint32_t domain, uint32_t type, uint32_t protocol) { int sock = socket(domain, type, protocol); - if(sock < 0) { + if (sock < 0) { int err = -wiiu_geterrno(); return (err < 0) ? err : sock; } return sock; } -int32_t network_bind(int32_t s,struct sockaddr *name,int32_t namelen) { +int32_t network_bind(int32_t s, struct sockaddr *name, int32_t namelen) { int res = bind(s, name, namelen); - if(res < 0) { + if (res < 0) { int err = -wiiu_geterrno(); return (err < 0) ? err : res; } return res; } -int32_t network_listen(int32_t s,uint32_t backlog) { +int32_t network_listen(int32_t s, uint32_t backlog) { int res = listen(s, backlog); - if(res < 0) { + if (res < 0) { int err = -wiiu_geterrno(); return (err < 0) ? err : res; } return res; } -int32_t network_accept(int32_t s,struct sockaddr *addr,int32_t *addrlen) { +int32_t network_accept(int32_t s, struct sockaddr *addr, int32_t *addrlen) { int res = accept(s, addr, addrlen); - if(res < 0) { + if (res < 0) { int err = -wiiu_geterrno(); return (err < 0) ? err : res; } return res; } -int32_t network_connect(int32_t s,struct sockaddr *addr, int32_t addrlen) { +int32_t network_connect(int32_t s, struct sockaddr *addr, int32_t addrlen) { int res = connect(s, addr, addrlen); - if(res < 0) { + if (res < 0) { int err = -wiiu_geterrno(); return (err < 0) ? err : res; } return res; } -int32_t network_read(int32_t s,void *mem,int32_t len) { +int32_t network_read(int32_t s, void *mem, int32_t len) { int res = recv(s, mem, len, 0); - if(res < 0) { + if (res < 0) { int err = -wiiu_geterrno(); return (err < 0) ? err : res; } @@ -125,12 +125,12 @@ uint32_t network_gethostip() { return hostIpAddress; } -int32_t network_write(int32_t s, const void *mem,int32_t len) { +int32_t network_write(int32_t s, const void *mem, int32_t len) { int32_t transfered = 0; - while(len) { + while (len) { int ret = send(s, mem, len, 0); - if(ret < 0) { + if (ret < 0) { int err = -wiiu_geterrno(); transfered = (err < 0) ? err : ret; break; @@ -144,7 +144,7 @@ int32_t network_write(int32_t s, const void *mem,int32_t len) { } int32_t network_close(int32_t s) { - if(s < 0) + if (s < 0) return -1; return close(s); @@ -178,7 +178,7 @@ int32_t create_server(uint16_t port) { bindAddress.sin_addr.s_addr = htonl(INADDR_ANY); int32_t ret; - if ((ret = network_bind(server, (struct sockaddr *)&bindAddress, sizeof(bindAddress))) < 0) { + if ((ret = network_bind(server, (struct sockaddr *) &bindAddress, sizeof(bindAddress))) < 0) { network_close(server); //gxprintf("Error binding socket: [%i] %s\n", -ret, strerror(-ret)); return ret; @@ -193,13 +193,14 @@ int32_t create_server(uint16_t port) { } typedef int32_t (*transferrer_type)(int32_t s, void *mem, int32_t len); + static int32_t transfer_exact(int32_t s, char *buf, int32_t length, transferrer_type transferrer) { int32_t result = 0; int32_t remaining = length; int32_t bytes_transferred; set_blocking(s, true); while (remaining) { -try_again_with_smaller_buffer: + try_again_with_smaller_buffer: bytes_transferred = transferrer(s, buf, MIN(remaining, (int) NET_BUFFER_SIZE)); if (bytes_transferred > 0) { remaining -= bytes_transferred; @@ -222,12 +223,12 @@ try_again_with_smaller_buffer: } int32_t send_exact(int32_t s, char *buf, int32_t length) { - return transfer_exact(s, buf, length, (transferrer_type)network_write); + return transfer_exact(s, buf, length, (transferrer_type) network_write); } int32_t send_from_file(int32_t s, FILE *f) { - char * buf = (char *) memalign(0x40, FREAD_BUFFER_SIZE); - if(!buf) + char *buf = (char *) memalign(0x40, FREAD_BUFFER_SIZE); + if (!buf) return -1; int32_t bytes_read; @@ -245,19 +246,19 @@ int32_t send_from_file(int32_t s, FILE *f) { } free(buf); return -EAGAIN; -end: + end: free(buf); return result; } int32_t recv_to_file(int32_t s, FILE *f) { - char * buf = (char *) memalign(0x40, NET_BUFFER_SIZE); - if(!buf) + char *buf = (char *) memalign(0x40, NET_BUFFER_SIZE); + if (!buf) return -1; int32_t bytes_read; while (1) { -try_again_with_smaller_buffer: + try_again_with_smaller_buffer: bytes_read = network_read(s, buf, NET_BUFFER_SIZE); if (bytes_read < 0) { if (bytes_read == -EINVAL && NET_BUFFER_SIZE == MAX_NET_BUFFER_SIZE) { diff --git a/src/net.h b/src/net.h index 2406df9..20a76c5 100644 --- a/src/net.h +++ b/src/net.h @@ -43,13 +43,20 @@ extern "C"{ void initialise_network(); #endif -int32_t network_socket(uint32_t domain,uint32_t type,uint32_t protocol); -int32_t network_bind(int32_t s,struct sockaddr *name,int32_t namelen); -int32_t network_listen(int32_t s,uint32_t backlog); -int32_t network_accept(int32_t s,struct sockaddr *addr,int32_t *addrlen); -int32_t network_connect(int32_t s,struct sockaddr *,int32_t); -int32_t network_read(int32_t s,void *mem,int32_t len); +int32_t network_socket(uint32_t domain, uint32_t type, uint32_t protocol); + +int32_t network_bind(int32_t s, struct sockaddr *name, int32_t namelen); + +int32_t network_listen(int32_t s, uint32_t backlog); + +int32_t network_accept(int32_t s, struct sockaddr *addr, int32_t *addrlen); + +int32_t network_connect(int32_t s, struct sockaddr *, int32_t); + +int32_t network_read(int32_t s, void *mem, int32_t len); + int32_t network_close(int32_t s); + uint32_t network_gethostip(); int32_t set_blocking(int32_t s, bool blocking); diff --git a/src/utils/BackgroundThreadWrapper.cpp b/src/utils/BackgroundThreadWrapper.cpp index b15e7fb..7f25a5d 100644 --- a/src/utils/BackgroundThreadWrapper.cpp +++ b/src/utils/BackgroundThreadWrapper.cpp @@ -1,28 +1,22 @@ #include "BackgroundThreadWrapper.hpp" - -#include #include - -BackgroundThreadWrapper::BackgroundThreadWrapper(int32_t priority): CThread(CThread::eAttributeAffCore2, priority, 0x100000) { +BackgroundThreadWrapper::BackgroundThreadWrapper(int32_t priority) : CThread(CThread::eAttributeAffCore2, priority, 0x100000) { } BackgroundThreadWrapper::~BackgroundThreadWrapper() { exitThread = 1; - DCFlushRange((void*)&exitThread, 4); - DEBUG_FUNCTION_LINE("Exit thread"); + DCFlushRange((void *) &exitThread, 4); } void BackgroundThreadWrapper::executeThread() { while (true) { - if(exitThread) { - DEBUG_FUNCTION_LINE("We want to exit"); + if (exitThread) { break; } - if(!whileLoop()){ + if (!whileLoop()) { break; } } - DEBUG_FUNCTION_LINE("Exit!"); } diff --git a/src/utils/BackgroundThreadWrapper.hpp b/src/utils/BackgroundThreadWrapper.hpp index bbde285..0088465 100644 --- a/src/utils/BackgroundThreadWrapper.hpp +++ b/src/utils/BackgroundThreadWrapper.hpp @@ -4,10 +4,12 @@ #include #include -class BackgroundThreadWrapper: public CThread { +class BackgroundThreadWrapper : public CThread { public: explicit BackgroundThreadWrapper(int32_t priority); + ~BackgroundThreadWrapper() override; + protected: [[nodiscard]] BOOL shouldExit() const { return (exitThread == 1); @@ -16,6 +18,7 @@ protected: void setThreadPriority(int32_t priority) override { CThread::setThreadPriority(priority); } + std::recursive_mutex mutex; private: void executeThread() override; diff --git a/src/utils/CThread.h b/src/utils/CThread.h index be96e90..f0dbda0 100644 --- a/src/utils/CThread.h +++ b/src/utils/CThread.h @@ -25,23 +25,20 @@ class CThread { public: - typedef void (* Callback)(CThread *thread, void *arg); + typedef void (*Callback)(CThread *thread, void *arg); //! constructor - CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = NULL, void *callbackArg = NULL) - : pThread(NULL) - , pThreadStack(NULL) - , pCallback(callback) - , pCallbackArg(callbackArg) { + explicit CThread(int32_t iAttr, int32_t iPriority = 16, int32_t iStackSize = 0x8000, CThread::Callback callback = nullptr, void *callbackArg = nullptr) + : pThread(nullptr), pThreadStack(nullptr), pCallback(callback), pCallbackArg(callbackArg) { //! save attribute assignment iAttributes = iAttr; //! allocate the thread - pThread = (OSThread*)memalign(8, sizeof(OSThread)); + pThread = (OSThread *) memalign(8, sizeof(OSThread)); //! allocate the stack pThreadStack = (uint8_t *) memalign(0x20, iStackSize); //! create the thread - if(pThread && pThreadStack) - OSCreateThread(pThread, &CThread::threadCallback, 1, (char*)this, pThreadStack+iStackSize, iStackSize, iPriority, iAttributes); + if (pThread && pThreadStack) + OSCreateThread(pThread, &CThread::threadCallback, 1, (char *) this, pThreadStack + iStackSize, iStackSize, iPriority, iAttributes); } //! destructor @@ -51,72 +48,81 @@ public: } static CThread *create(CThread::Callback callback, void *callbackArg, int32_t iAttr = eAttributeNone, int32_t iPriority = 16, int32_t iStackSize = 0x8000) { - return ( new CThread(iAttr, iPriority, iStackSize, callback, callbackArg) ); + return (new CThread(iAttr, iPriority, iStackSize, callback, callbackArg)); } //! Get thread ID - virtual void* getThread() const { + [[nodiscard]] virtual void *getThread() const { return pThread; } + //! Thread entry function - virtual void executeThread(void) { - if(pCallback) + virtual void executeThread() { + if (pCallback) pCallback(this, pCallbackArg); } + //! Suspend thread - virtual void suspendThread(void) { - if(isThreadSuspended()) return; - if(pThread) OSSuspendThread(pThread); + virtual void suspendThread() { + if (isThreadSuspended()) return; + if (pThread) OSSuspendThread(pThread); } + //! Resume thread - virtual void resumeThread(void) { - if(!isThreadSuspended()) return; - if(pThread) OSResumeThread(pThread); + virtual void resumeThread() { + if (!isThreadSuspended()) return; + if (pThread) OSResumeThread(pThread); } + //! Set thread priority virtual void setThreadPriority(int prio) { - if(pThread) OSSetThreadPriority(pThread, prio); + if (pThread) OSSetThreadPriority(pThread, prio); } + //! Check if thread is suspended - virtual BOOL isThreadSuspended(void) const { - if(pThread) return OSIsThreadSuspended(pThread); + [[nodiscard]] virtual BOOL isThreadSuspended() const { + if (pThread) return OSIsThreadSuspended(pThread); return false; } + //! Check if thread is terminated - virtual BOOL isThreadTerminated(void) const { - if(pThread) return OSIsThreadTerminated(pThread); + [[nodiscard]] virtual BOOL isThreadTerminated() const { + if (pThread) return OSIsThreadTerminated(pThread); return false; } + //! Check if thread is running - virtual BOOL isThreadRunning(void) const { + [[nodiscard]] virtual BOOL isThreadRunning() const { return !isThreadSuspended() && !isThreadRunning(); } + //! Shutdown thread - virtual void shutdownThread(void) { + virtual void shutdownThread() { //! wait for thread to finish - if(pThread && !(iAttributes & eAttributeDetach)) { - if(isThreadSuspended()) + if (pThread && !(iAttributes & eAttributeDetach)) { + if (isThreadSuspended()) resumeThread(); - OSJoinThread(pThread, NULL); + OSJoinThread(pThread, nullptr); } //! free the thread stack buffer - if(pThreadStack) + if (pThreadStack) free(pThreadStack); - if(pThread) + if (pThread) free(pThread); - pThread = NULL; - pThreadStack = NULL; + pThread = nullptr; + pThreadStack = nullptr; } + //! Thread attributes enum eCThreadAttributes { - eAttributeNone = 0x07, - eAttributeAffCore0 = 0x01, - eAttributeAffCore1 = 0x02, - eAttributeAffCore2 = 0x04, - eAttributeDetach = 0x08, - eAttributePinnedAff = 0x10 + eAttributeNone = 0x07, + eAttributeAffCore0 = 0x01, + eAttributeAffCore1 = 0x02, + eAttributeAffCore2 = 0x04, + eAttributeDetach = 0x08, + eAttributePinnedAff = 0x10 }; private: static int threadCallback(int argc, const char **argv) { @@ -124,6 +130,7 @@ private: ((CThread *) argv)->executeThread(); return 0; } + int iAttributes; OSThread *pThread; uint8_t *pThreadStack; diff --git a/src/utils/logger.h b/src/utils/logger.h index 95f28f9..dc67c5f 100644 --- a/src/utils/logger.h +++ b/src/utils/logger.h @@ -16,7 +16,7 @@ extern "C" { #define DEBUG_FUNCTION_LINE(FMT, ARGS...)do { \ WHBLogPrintf("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ - } while (0); + } while (0) #define DEBUG_FUNCTION_LINE_WRITE(FMT, ARGS...)do { \ WHBLogWritef("[%23s]%30s@L%04d: " FMT "",__FILENAME__,__FUNCTION__, __LINE__, ## ARGS); \ diff --git a/src/virtualpath.c b/src/virtualpath.c index b50abd7..91dfaf2 100644 --- a/src/virtualpath.c +++ b/src/virtualpath.c @@ -1,31 +1,31 @@ - /**************************************************************************** - * Copyright (C) 2008 - * Joseph Jordan - * - * Copyright (C) 2010 - * by Dimok - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any - * damages arising from the use of this software. - * - * Permission is granted to anyone to use this software for any - * purpose, including commercial applications, and to alter it and - * redistribute it freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you - * must not claim that you wrote the original software. If you use - * this software in a product, an acknowledgment in the product - * documentation would be appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and - * must not be misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. - * - * for WiiXplorer 2010 - ***************************************************************************/ +/**************************************************************************** +* Copyright (C) 2008 +* Joseph Jordan +* +* Copyright (C) 2010 +* by Dimok +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any +* damages arising from the use of this software. +* +* Permission is granted to anyone to use this software for any +* purpose, including commercial applications, and to alter it and +* redistribute it freely, subject to the following restrictions: +* +* 1. The origin of this software must not be misrepresented; you +* must not claim that you wrote the original software. If you use +* this software in a product, an acknowledgment in the product +* documentation would be appreciated but is not required. +* +* 2. Altered source versions must be plainly marked as such, and +* must not be misrepresented as being the original software. +* +* 3. This notice may not be removed or altered from any source +* distribution. +* +* for WiiXplorer 2010 +***************************************************************************/ #include #include #include "virtualpath.h" @@ -40,42 +40,37 @@ VIRTUAL_PARTITION *VIRTUAL_FS = NULL; uint8_t MAX_VIRTUAL_FS_VOL = 0; VIRTUAL_PARTITION *VIRTUAL_FS_VOL = NULL; -void VirtualMountDevice(const char * path) -{ - if(!path) - return; +void VirtualMountDevice(const char *path) { + if (!path) + return; - int i = 0; - char name[255]; - char alias[255]; - char prefix[255]; - bool namestop = false; + int i = 0; + char name[255]; + char alias[255]; + char prefix[255]; + bool namestop = false; - alias[0] = '/'; + alias[0] = '/'; - do - { - if(path[i] == ':') - namestop = true; + do { + if (path[i] == ':') + namestop = true; - if(!namestop) - { - name[i] = path[i]; - name[i+1] = '\0'; - alias[i+1] = path[i]; - alias[i+2] = '\0'; - } + if (!namestop) { + name[i] = path[i]; + name[i + 1] = '\0'; + alias[i + 1] = path[i]; + alias[i + 2] = '\0'; + } - prefix[i] = path[i]; - prefix[i+1] = '\0'; - i++; - } - while(path[i-1] != '/'); - AddVirtualPath(name, alias, prefix); + prefix[i] = path[i]; + prefix[i + 1] = '\0'; + i++; + } while (path[i - 1] != '/'); + AddVirtualPath(name, alias, prefix); } -void AddVirtualPath(const char *name, const char *alias, const char *prefix) -{ +void AddVirtualPath(const char *name, const char *alias, const char *prefix) { if (!VIRTUAL_PARTITIONS) { VIRTUAL_PARTITIONS = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION)); } @@ -99,49 +94,48 @@ void AddVirtualPath(const char *name, const char *alias, const char *prefix) } - void AddVirtualFSPath(const char *name, const char *alias, const char *prefix) { - if (!VIRTUAL_FS) { - VIRTUAL_FS = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION)); - } +void AddVirtualFSPath(const char *name, const char *alias, const char *prefix) { + if (!VIRTUAL_FS) { + VIRTUAL_FS = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION)); + } - VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_FS, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_FS + 1)); - if (!tmp) { - if (VIRTUAL_FS) { - free(VIRTUAL_FS); - VIRTUAL_FS = 0; - } - MAX_VIRTUAL_FS = 0; - return; - } + VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_FS, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_FS + 1)); + if (!tmp) { + if (VIRTUAL_FS) { + free(VIRTUAL_FS); + VIRTUAL_FS = 0; + } + MAX_VIRTUAL_FS = 0; + return; + } - VIRTUAL_FS = tmp; + VIRTUAL_FS = tmp; - VIRTUAL_FS[MAX_VIRTUAL_FS].name = strdup(name); + VIRTUAL_FS[MAX_VIRTUAL_FS].name = strdup(name); - MAX_VIRTUAL_FS++; - } + MAX_VIRTUAL_FS++; +} - void AddVirtualFSVOLPath(const char *name, const char *alias, const char *prefix) { - if (!VIRTUAL_FS_VOL) { - VIRTUAL_FS_VOL = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION)); - } +void AddVirtualFSVOLPath(const char *name, const char *alias, const char *prefix) { + if (!VIRTUAL_FS_VOL) { + VIRTUAL_FS_VOL = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION)); + } - VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_FS_VOL, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_FS_VOL + 1)); - if (!tmp) { - free(VIRTUAL_FS_VOL); - MAX_VIRTUAL_FS_VOL = 0; - return; - } + VIRTUAL_PARTITION *tmp = realloc(VIRTUAL_FS_VOL, sizeof(VIRTUAL_PARTITION) * (MAX_VIRTUAL_FS_VOL + 1)); + if (!tmp) { + free(VIRTUAL_FS_VOL); + MAX_VIRTUAL_FS_VOL = 0; + return; + } - VIRTUAL_FS_VOL = tmp; + VIRTUAL_FS_VOL = tmp; - VIRTUAL_FS_VOL[MAX_VIRTUAL_FS_VOL].name = strdup(name); + VIRTUAL_FS_VOL[MAX_VIRTUAL_FS_VOL].name = strdup(name); - MAX_VIRTUAL_FS_VOL++; - } + MAX_VIRTUAL_FS_VOL++; +} -void MountVirtualDevices() -{ +void MountVirtualDevices() { VirtualMountDevice("fs:/"); VirtualMountDevice("slccmpt01:/"); VirtualMountDevice("storage_odd_tickets:/"); @@ -158,45 +152,45 @@ void MountVirtualDevices() AddVirtualFSVOLPath("content", NULL, NULL); } - void UnmountVirtualPaths() { - uint32_t i = 0; - for (i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) { - if (VIRTUAL_PARTITIONS[i].name) { - free(VIRTUAL_PARTITIONS[i].name); - } - if (VIRTUAL_PARTITIONS[i].alias) { - free(VIRTUAL_PARTITIONS[i].alias); - } - if (VIRTUAL_PARTITIONS[i].prefix) { - free(VIRTUAL_PARTITIONS[i].prefix); - } - } +void UnmountVirtualPaths() { + uint32_t i = 0; + for (i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) { + if (VIRTUAL_PARTITIONS[i].name) { + free(VIRTUAL_PARTITIONS[i].name); + } + if (VIRTUAL_PARTITIONS[i].alias) { + free(VIRTUAL_PARTITIONS[i].alias); + } + if (VIRTUAL_PARTITIONS[i].prefix) { + free(VIRTUAL_PARTITIONS[i].prefix); + } + } - for (i = 0; i < MAX_VIRTUAL_FS_VOL; i++) { - if (VIRTUAL_FS_VOL[i].name) { - free(VIRTUAL_FS_VOL[i].name); - } - } + for (i = 0; i < MAX_VIRTUAL_FS_VOL; i++) { + if (VIRTUAL_FS_VOL[i].name) { + free(VIRTUAL_FS_VOL[i].name); + } + } - for (i = 0; i < MAX_VIRTUAL_FS; i++) { - if (VIRTUAL_FS[i].name) { - free(VIRTUAL_FS[i].name); - } - } + for (i = 0; i < MAX_VIRTUAL_FS; i++) { + if (VIRTUAL_FS[i].name) { + free(VIRTUAL_FS[i].name); + } + } - if (VIRTUAL_PARTITIONS) { - free(VIRTUAL_PARTITIONS); - } - if (VIRTUAL_FS_VOL) { - free(VIRTUAL_FS_VOL); - } - if (VIRTUAL_FS) { - free(VIRTUAL_FS); - } - VIRTUAL_PARTITIONS = NULL; - VIRTUAL_FS_VOL = NULL; - VIRTUAL_FS = NULL; - MAX_VIRTUAL_PARTITIONS = 0; - MAX_VIRTUAL_FS = 0; - MAX_VIRTUAL_FS_VOL = 0; - } + if (VIRTUAL_PARTITIONS) { + free(VIRTUAL_PARTITIONS); + } + if (VIRTUAL_FS_VOL) { + free(VIRTUAL_FS_VOL); + } + if (VIRTUAL_FS) { + free(VIRTUAL_FS); + } + VIRTUAL_PARTITIONS = NULL; + VIRTUAL_FS_VOL = NULL; + VIRTUAL_FS = NULL; + MAX_VIRTUAL_PARTITIONS = 0; + MAX_VIRTUAL_FS = 0; + MAX_VIRTUAL_FS_VOL = 0; +} diff --git a/src/virtualpath.h b/src/virtualpath.h index 3cae99e..7061d76 100644 --- a/src/virtualpath.h +++ b/src/virtualpath.h @@ -1,32 +1,32 @@ - /**************************************************************************** - * Copyright (C) 2010 - * by Dimok - * - * Original VIRTUAL_PART Struct - * Copyright (C) 2008 - * Joseph Jordan - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any - * damages arising from the use of this software. - * - * Permission is granted to anyone to use this software for any - * purpose, including commercial applications, and to alter it and - * redistribute it freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you - * must not claim that you wrote the original software. If you use - * this software in a product, an acknowledgment in the product - * documentation would be appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and - * must not be misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. - * - * for WiiXplorer 2010 - ***************************************************************************/ +/**************************************************************************** +* Copyright (C) 2010 +* by Dimok +* +* Original VIRTUAL_PART Struct +* Copyright (C) 2008 +* Joseph Jordan +* +* This software is provided 'as-is', without any express or implied +* warranty. In no event will the authors be held liable for any +* damages arising from the use of this software. +* +* Permission is granted to anyone to use this software for any +* purpose, including commercial applications, and to alter it and +* redistribute it freely, subject to the following restrictions: +* +* 1. The origin of this software must not be misrepresented; you +* must not claim that you wrote the original software. If you use +* this software in a product, an acknowledgment in the product +* documentation would be appreciated but is not required. +* +* 2. Altered source versions must be plainly marked as such, and +* must not be misrepresented as being the original software. +* +* 3. This notice may not be removed or altered from any source +* distribution. +* +* for WiiXplorer 2010 +***************************************************************************/ #ifndef _VIRTUALPATH_H_ #define _VIRTUALPATH_H_ @@ -38,26 +38,31 @@ extern "C"{ #include typedef struct { - char *name; - char *alias; - char *prefix; - bool inserted; + char *name; + char *alias; + char *prefix; + bool inserted; } VIRTUAL_PARTITION; -extern VIRTUAL_PARTITION * VIRTUAL_PARTITIONS; +extern VIRTUAL_PARTITION *VIRTUAL_PARTITIONS; extern uint8_t MAX_VIRTUAL_PARTITIONS; -extern VIRTUAL_PARTITION * VIRTUAL_FS; +extern VIRTUAL_PARTITION *VIRTUAL_FS; extern uint8_t MAX_VIRTUAL_FS; -extern VIRTUAL_PARTITION * VIRTUAL_FS_VOL; +extern VIRTUAL_PARTITION *VIRTUAL_FS_VOL; extern uint8_t MAX_VIRTUAL_FS_VOL; -void VirtualMountDevice(const char * devicepath); +void VirtualMountDevice(const char *devicepath); + void AddVirtualPath(const char *name, const char *alias, const char *prefix); + void AddVirtualFSPath(const char *name, const char *alias, const char *prefix); + void AddVirtualFSVOLPath(const char *name, const char *alias, const char *prefix); + void MountVirtualDevices(); + void UnmountVirtualPaths(); #ifdef __cplusplus diff --git a/src/vrt.c b/src/vrt.c index 314e88f..505c65c 100644 --- a/src/vrt.c +++ b/src/vrt.c @@ -34,67 +34,67 @@ misrepresented as being the original software. #include "vrt.h" static char *virtual_abspath(char *virtual_cwd, char *virtual_path) { - char *path; - if (virtual_path[0] == '/') { - path = virtual_path; - } else { - size_t path_size = strlen(virtual_cwd) + strlen(virtual_path) + 1; - if (path_size > MAXPATHLEN || !(path = malloc(path_size))) return NULL; - strcpy(path, virtual_cwd); - strcat(path, virtual_path); - } + char *path; + if (virtual_path[0] == '/') { + path = virtual_path; + } else { + size_t path_size = strlen(virtual_cwd) + strlen(virtual_path) + 1; + if (path_size > MAXPATHLEN || !(path = malloc(path_size))) return NULL; + strcpy(path, virtual_cwd); + strcat(path, virtual_path); + } - char *normalised_path = malloc(strlen(path) + 1); - if (!normalised_path) goto end; - *normalised_path = '\0'; - char *curr_dir = normalised_path; + char *normalised_path = malloc(strlen(path) + 1); + if (!normalised_path) goto end; + *normalised_path = '\0'; + char *curr_dir = normalised_path; - uint32_t state = 0; // 0:start, 1:slash, 2:dot, 3:dotdot - char *token = path; - while (1) { - switch (state) { - case 0: - if (*token == '/') { - state = 1; - curr_dir = normalised_path + strlen(normalised_path); - strncat(normalised_path, token, 1); - } - break; - case 1: - if (*token == '.') state = 2; - else if (*token != '/') state = 0; - break; - case 2: - if (*token == '/' || !*token) { - state = 1; - *(curr_dir + 1) = '\0'; - } else if (*token == '.') state = 3; - else state = 0; - break; - case 3: - if (*token == '/' || !*token) { - state = 1; - *curr_dir = '\0'; - char *prev_dir = strrchr(normalised_path, '/'); - if (prev_dir) curr_dir = prev_dir; - else *curr_dir = '/'; - *(curr_dir + 1) = '\0'; - } else state = 0; - break; - } - if (!*token) break; - if (state == 0 || *token != '/') strncat(normalised_path, token, 1); - token++; - } + uint32_t state = 0; // 0:start, 1:slash, 2:dot, 3:dotdot + char *token = path; + while (1) { + switch (state) { + case 0: + if (*token == '/') { + state = 1; + curr_dir = normalised_path + strlen(normalised_path); + strncat(normalised_path, token, 1); + } + break; + case 1: + if (*token == '.') state = 2; + else if (*token != '/') state = 0; + break; + case 2: + if (*token == '/' || !*token) { + state = 1; + *(curr_dir + 1) = '\0'; + } else if (*token == '.') state = 3; + else state = 0; + break; + case 3: + if (*token == '/' || !*token) { + state = 1; + *curr_dir = '\0'; + char *prev_dir = strrchr(normalised_path, '/'); + if (prev_dir) curr_dir = prev_dir; + else *curr_dir = '/'; + *(curr_dir + 1) = '\0'; + } else state = 0; + break; + } + if (!*token) break; + if (state == 0 || *token != '/') strncat(normalised_path, token, 1); + token++; + } - uint32_t end = strlen(normalised_path); - while (end > 1 && normalised_path[end - 1] == '/') { - normalised_path[--end] = '\x00'; - } + uint32_t end = strlen(normalised_path); + while (end > 1 && normalised_path[end - 1] == '/') { + normalised_path[--end] = '\x00'; + } - end: - if (path != virtual_path) free(path); - return normalised_path; + end: + if (path != virtual_path) free(path); + return normalised_path; } /* @@ -106,52 +106,52 @@ static char *virtual_abspath(char *virtual_cwd, char *virtual_path) { Returns NULL to indicate that the client-visible path is invalid */ char *to_real_path(char *virtual_cwd, char *virtual_path) { - errno = ENOENT; - if (strchr(virtual_path, ':')) { - return NULL; // colon is not allowed in virtual path, i've decided =P - } + errno = ENOENT; + if (strchr(virtual_path, ':')) { + return NULL; // colon is not allowed in virtual path, i've decided =P + } - virtual_path = virtual_abspath(virtual_cwd, virtual_path); - if (!virtual_path) return NULL; + virtual_path = virtual_abspath(virtual_cwd, virtual_path); + if (!virtual_path) return NULL; - char *path = NULL; - char *rest = virtual_path; + char *path = NULL; + char *rest = virtual_path; - if (!strcmp("/", virtual_path)) { - // indicate vfs-root with "" - path = ""; - goto end; - } + if (!strcmp("/", virtual_path)) { + // indicate vfs-root with "" + path = ""; + goto end; + } - const char *prefix = NULL; - uint32_t i; - for (i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) { - VIRTUAL_PARTITION *partition = VIRTUAL_PARTITIONS + i; - const char *alias = partition->alias; - size_t alias_len = strlen(alias); - if (!strcasecmp(alias, virtual_path) || (!strncasecmp(alias, virtual_path, alias_len) && virtual_path[alias_len] == '/')) { - prefix = partition->prefix; - rest += alias_len; - if (*rest == '/') rest++; - break; - } - } - if (!prefix) { - errno = ENODEV; - goto end; - } + const char *prefix = NULL; + uint32_t i; + for (i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) { + VIRTUAL_PARTITION *partition = VIRTUAL_PARTITIONS + i; + const char *alias = partition->alias; + size_t alias_len = strlen(alias); + if (!strcasecmp(alias, virtual_path) || (!strncasecmp(alias, virtual_path, alias_len) && virtual_path[alias_len] == '/')) { + prefix = partition->prefix; + rest += alias_len; + if (*rest == '/') rest++; + break; + } + } + if (!prefix) { + errno = ENODEV; + goto end; + } - size_t real_path_size = strlen(prefix) + strlen(rest) + 1; - if (real_path_size > MAXPATHLEN) goto end; + size_t real_path_size = strlen(prefix) + strlen(rest) + 1; + if (real_path_size > MAXPATHLEN) goto end; - path = malloc(real_path_size); - if (!path) goto end; - strcpy(path, prefix); - strcat(path, rest); + path = malloc(real_path_size); + if (!path) goto end; + strcpy(path, prefix); + strcat(path, rest); - end: - free(virtual_path); - return path; + end: + free(virtual_path); + return path; } static int checkdir(char *path) { @@ -170,101 +170,105 @@ static int checkdir(char *path) { return -1; } -typedef void * (*path_func)(char *path, ...); +typedef void *(*path_func)(char *path, ...); static void *with_virtual_path(void *virtual_cwd, void *void_f, char *virtual_path, int32_t failed, ...) { - char *path = to_real_path(virtual_cwd, virtual_path); - if (!path || !*path) return (void *)failed; + char *path = to_real_path(virtual_cwd, virtual_path); + if (!path || !*path) return (void *) failed; - path_func f = (path_func)void_f; - va_list ap; - void *args[3]; - unsigned int num_args = 0; - va_start(ap, failed); - do { - void *arg = va_arg(ap, void *); - if (!arg) break; - args[num_args++] = arg; - } while (1); - va_end(ap); + path_func f = (path_func) void_f; + va_list ap; + void *args[3]; + unsigned int num_args = 0; + va_start(ap, failed); + do { + void *arg = va_arg(ap, void *); + if (!arg) break; + args[num_args++] = arg; + } while (1); + va_end(ap); - void *result; - switch (num_args) { - case 0: result = f(path); break; - case 1: result = f(path, args[0]); break; - case 2: result = f(path, args[0], args[1]); break; - case 3: result = f(path, args[0], args[1], args[2]); break; - default: result = (void *)failed; break; - } + void *result; + switch (num_args) { + case 0: + result = f(path); + break; + case 1: + result = f(path, args[0]); + break; + case 2: + result = f(path, args[0], args[1]); + break; + case 3: + result = f(path, args[0], args[1], args[2]); + break; + default: + result = (void *) failed; + break; + } - free(path); - return result; + free(path); + return result; } FILE *vrt_fopen(char *cwd, char *path, char *mode) { - return with_virtual_path(cwd, fopen, path, 0, mode, NULL); + return with_virtual_path(cwd, fopen, path, 0, mode, NULL); } int vrt_stat(char *cwd, char *path, struct stat *st) { - char *real_path = to_real_path(cwd, path); - if (!real_path) - { + char *real_path = to_real_path(cwd, path); + if (!real_path) { return -1; + } else if (!*real_path || (strcmp(path, ".") == 0) || (strlen(cwd) == 1) || ((strlen(cwd) > 1) && (strcmp(path, "..") == 0))) { + st->st_mode = S_IFDIR; + st->st_size = 31337; + return 0; } - else if (!*real_path || (strcmp(path, ".") == 0) || (strlen(cwd) == 1) || ((strlen(cwd) > 1) && (strcmp(path, "..") == 0))) - { - st->st_mode = S_IFDIR; - st->st_size = 31337; - return 0; - } - free(real_path); - return (int)with_virtual_path(cwd, stat, path, -1, st, NULL); + free(real_path); + return (int) with_virtual_path(cwd, stat, path, -1, st, NULL); } static int vrt_checkdir(char *cwd, char *path) { - char *real_path = to_real_path(cwd, path); - if (!real_path) - { - return -1; - } - else if (!*real_path || (strcmp(path, ".") == 0) || (strlen(cwd) == 1) || ((strlen(cwd) > 1) && (strcmp(path, "..") == 0))) - { - return 0; - } - free(real_path); - return (int)with_virtual_path(cwd, checkdir, path, -1, NULL); + char *real_path = to_real_path(cwd, path); + if (!real_path) { + return -1; + } else if (!*real_path || (strcmp(path, ".") == 0) || (strlen(cwd) == 1) || ((strlen(cwd) > 1) && (strcmp(path, "..") == 0))) { + return 0; + } + free(real_path); + return (int) with_virtual_path(cwd, checkdir, path, -1, NULL); } int vrt_chdir(char *cwd, char *path) { - if (vrt_checkdir(cwd, path)) { - return -1; - } - char *abspath = virtual_abspath(cwd, path); - if (!abspath) { - errno = ENOMEM; - return -1; - } - strcpy(cwd, abspath); - if (cwd[1]) strcat(cwd, "/"); - free(abspath); - return 0; + if (vrt_checkdir(cwd, path)) { + return -1; + } + char *abspath = virtual_abspath(cwd, path); + if (!abspath) { + errno = ENOMEM; + return -1; + } + strcpy(cwd, abspath); + if (cwd[1]) strcat(cwd, "/"); + free(abspath); + return 0; } int vrt_unlink(char *cwd, char *path) { - return (int)with_virtual_path(cwd, unlink, path, -1, NULL); + return (int) with_virtual_path(cwd, unlink, path, -1, NULL); } int vrt_mkdir(char *cwd, char *path, mode_t mode) { - return (int)with_virtual_path(cwd, mkdir, path, -1, mode, NULL); + return (int) with_virtual_path(cwd, mkdir, path, -1, mode, NULL); } int vrt_rename(char *cwd, char *from_path, char *to_path) { - char *real_to_path = to_real_path(cwd, to_path); - if (!real_to_path || !*real_to_path) return -1; - int result = (int)with_virtual_path(cwd, rename, from_path, -1, real_to_path, NULL); - free(real_to_path); - return result; + char *real_to_path = to_real_path(cwd, to_path); + if (!real_to_path || !*real_to_path) return -1; + int result = (int) with_virtual_path(cwd, rename, from_path, -1, real_to_path, NULL); + free(real_to_path); + return result; } /* @@ -328,11 +332,11 @@ struct dirent *vrt_readdir(DIR_P *pDir) { DIR *iter = pDir->dir; if (pDir->virt_root || pDir->virtual_fs || pDir->virtual_fs_vol) { int max = MAX_VIRTUAL_PARTITIONS; - VIRTUAL_PARTITION * PARTITION_PTR = VIRTUAL_PARTITIONS; - if(pDir->virtual_fs){ + VIRTUAL_PARTITION *PARTITION_PTR = VIRTUAL_PARTITIONS; + if (pDir->virtual_fs) { max = MAX_VIRTUAL_FS; PARTITION_PTR = VIRTUAL_FS; - } else if (pDir->virtual_fs_vol){ + } else if (pDir->virtual_fs_vol) { max = MAX_VIRTUAL_FS_VOL; PARTITION_PTR = VIRTUAL_FS_VOL; } @@ -340,9 +344,9 @@ struct dirent *vrt_readdir(DIR_P *pDir) { VIRTUAL_PARTITION *partition = PARTITION_PTR + (int) iter->position; if (partition->inserted) { iter->fileData.d_type = DT_DIR; - if(pDir->virtual_fs || pDir->virtual_fs_vol){ + if (pDir->virtual_fs || pDir->virtual_fs_vol) { strcpy(iter->fileData.d_name, partition->name); - }else{ + } else { strcpy(iter->fileData.d_name, partition->alias + 1); } iter->position++; @@ -356,21 +360,20 @@ struct dirent *vrt_readdir(DIR_P *pDir) { } int vrt_closedir(DIR_P *iter) { - if(!iter) return -1; + if (!iter) return -1; - if(iter->dir) - { - if (iter->virt_root) - free(iter->dir); - else - closedir(iter->dir); - } + if (iter->dir) { + if (iter->virt_root) + free(iter->dir); + else + closedir(iter->dir); + } - // root path is not allocated - if(iter->path && *iter->path != 0) - free(iter->path); + // root path is not allocated + if (iter->path && *iter->path != 0) + free(iter->path); - free(iter); + free(iter); - return 0; + return 0; } diff --git a/src/vrt.h b/src/vrt.h index 29f18a5..dd642a9 100644 --- a/src/vrt.h +++ b/src/vrt.h @@ -32,25 +32,32 @@ extern "C"{ #include #include -typedef struct -{ - DIR *dir; - char *path; - uint8_t virt_root; - uint8_t virtual_fs; - uint8_t virtual_fs_vol; +typedef struct { + DIR *dir; + char *path; + uint8_t virt_root; + uint8_t virtual_fs; + uint8_t virtual_fs_vol; } DIR_P; char *to_real_path(char *virtual_cwd, char *virtual_path); FILE *vrt_fopen(char *cwd, char *path, char *mode); + int vrt_stat(char *cwd, char *path, struct stat *st); + int vrt_chdir(char *cwd, char *path); + int vrt_unlink(char *cwd, char *path); + int vrt_mkdir(char *cwd, char *path, mode_t mode); + int vrt_rename(char *cwd, char *from_path, char *to_path); + DIR_P *vrt_opendir(char *cwd, char *path); + struct dirent *vrt_readdir(DIR_P *iter); + int vrt_closedir(DIR_P *iter); #ifdef __cplusplus