Formatting and cleanup

This commit is contained in:
Maschell 2021-09-24 19:49:11 +02:00
parent f4dd784456
commit 9f784cbf1b
16 changed files with 548 additions and 528 deletions

View File

@ -1,44 +1,36 @@
#include "BackgroundThread.hpp"
#include <malloc.h>
#include <stdio.h>
#include <string.h>
#include <coreinit/cache.h>
#include <utils/logger.h>
#include <cstring>
#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);
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);
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);
mutex.unlock();
if(network_down) {
if (network_down) {
DEBUG_FUNCTION_LINE("Network is down %d", this->serverSocket);
mutex.lock();
cleanup_ftp();

View File

@ -1,50 +1,50 @@
#pragma once
#include "utils/BackgroundThreadWrapper.hpp"
#include <coreinit/cache.h>
#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;
};

View File

@ -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.");
@ -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,8 +664,8 @@ 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",
@ -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);
}

View File

@ -29,11 +29,15 @@ misrepresented as being the original software.
#ifdef __cplusplus
extern "C"{
#endif
#include <stdbool.h>
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

View File

@ -8,7 +8,6 @@
#include <coreinit/cache.h>
#include "utils/logger.h"
#include <whb/log_udp.h>
#include <whb/libmanager.h>
#include "virtualpath.h"
#include "BackgroundThread.hpp"
@ -27,7 +26,7 @@ uint32_t hostIpAddress = 0;
int iosuhaxMount = 0;
int fsaFd = -1;
BackgroundThread * thread = nullptr;
BackgroundThread *thread = nullptr;
/* Entry point */
ON_APPLICATION_START() {
@ -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();

View File

@ -5,6 +5,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "net.h"

View File

@ -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) {

View File

@ -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);

View File

@ -1,28 +1,22 @@
#include "BackgroundThreadWrapper.hpp"
#include <string.h>
#include <coreinit/cache.h>
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!");
}

View File

@ -4,10 +4,12 @@
#include <wut_types.h>
#include <mutex>
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;

View File

@ -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,64 +48,73 @@ 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,
@ -124,6 +130,7 @@ private:
((CThread *) argv)->executeThread();
return 0;
}
int iAttributes;
OSThread *pThread;
uint8_t *pThreadStack;

View File

@ -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); \

View File

@ -1,31 +1,31 @@
/****************************************************************************
* Copyright (C) 2008
* Joseph Jordan <joe.ftpii@psychlaw.com.au>
*
* 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 <joe.ftpii@psychlaw.com.au>
*
* 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 <malloc.h>
#include <string.h>
#include "virtualpath.h"
@ -40,9 +40,8 @@ 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)
void VirtualMountDevice(const char *path) {
if (!path)
return;
int i = 0;
@ -53,29 +52,25 @@ void VirtualMountDevice(const char * path)
alias[0] = '/';
do
{
if(path[i] == ':')
do {
if (path[i] == ':')
namestop = true;
if(!namestop)
{
if (!namestop) {
name[i] = path[i];
name[i+1] = '\0';
alias[i+1] = path[i];
alias[i+2] = '\0';
name[i + 1] = '\0';
alias[i + 1] = path[i];
alias[i + 2] = '\0';
}
prefix[i] = path[i];
prefix[i+1] = '\0';
prefix[i + 1] = '\0';
i++;
}
while(path[i-1] != '/');
} 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,7 +94,7 @@ void AddVirtualPath(const char *name, const char *alias, const char *prefix)
}
void AddVirtualFSPath(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));
}
@ -119,9 +114,9 @@ void AddVirtualPath(const char *name, const char *alias, const char *prefix)
VIRTUAL_FS[MAX_VIRTUAL_FS].name = strdup(name);
MAX_VIRTUAL_FS++;
}
}
void AddVirtualFSVOLPath(const char *name, const char *alias, const char *prefix) {
void AddVirtualFSVOLPath(const char *name, const char *alias, const char *prefix) {
if (!VIRTUAL_FS_VOL) {
VIRTUAL_FS_VOL = (VIRTUAL_PARTITION *) malloc(sizeof(VIRTUAL_PARTITION));
}
@ -138,10 +133,9 @@ void AddVirtualPath(const char *name, const char *alias, const char *prefix)
VIRTUAL_FS_VOL[MAX_VIRTUAL_FS_VOL].name = strdup(name);
MAX_VIRTUAL_FS_VOL++;
}
}
void MountVirtualDevices()
{
void MountVirtualDevices() {
VirtualMountDevice("fs:/");
VirtualMountDevice("slccmpt01:/");
VirtualMountDevice("storage_odd_tickets:/");
@ -158,7 +152,7 @@ void MountVirtualDevices()
AddVirtualFSVOLPath("content", NULL, NULL);
}
void UnmountVirtualPaths() {
void UnmountVirtualPaths() {
uint32_t i = 0;
for (i = 0; i < MAX_VIRTUAL_PARTITIONS; i++) {
if (VIRTUAL_PARTITIONS[i].name) {
@ -199,4 +193,4 @@ void MountVirtualDevices()
MAX_VIRTUAL_PARTITIONS = 0;
MAX_VIRTUAL_FS = 0;
MAX_VIRTUAL_FS_VOL = 0;
}
}

View File

@ -1,32 +1,32 @@
/****************************************************************************
* Copyright (C) 2010
* by Dimok
*
* Original VIRTUAL_PART Struct
* Copyright (C) 2008
* Joseph Jordan <joe.ftpii@psychlaw.com.au>
*
* 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 <joe.ftpii@psychlaw.com.au>
*
* 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_
@ -44,20 +44,25 @@ typedef struct {
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

View File

@ -170,13 +170,13 @@ 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;
if (!path || !*path) return (void *) failed;
path_func f = (path_func)void_f;
path_func f = (path_func) void_f;
va_list ap;
void *args[3];
unsigned int num_args = 0;
@ -190,11 +190,21 @@ static void *with_virtual_path(void *virtual_cwd, void *void_f, char *virtual_pa
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;
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);
@ -207,32 +217,26 @@ FILE *vrt_fopen(char *cwd, char *path, char *mode) {
int vrt_stat(char *cwd, char *path, struct stat *st) {
char *real_path = to_real_path(cwd, path);
if (!real_path)
{
if (!real_path) {
return -1;
}
else if (!*real_path || (strcmp(path, ".") == 0) || (strlen(cwd) == 1) || ((strlen(cwd) > 1) && (strcmp(path, "..") == 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);
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)
{
if (!real_path) {
return -1;
}
else if (!*real_path || (strcmp(path, ".") == 0) || (strlen(cwd) == 1) || ((strlen(cwd) > 1) && (strcmp(path, "..") == 0)))
{
} 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);
return (int) with_virtual_path(cwd, checkdir, path, -1, NULL);
}
int vrt_chdir(char *cwd, char *path) {
@ -252,17 +256,17 @@ int vrt_chdir(char *cwd, char *path) {
}
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);
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,10 +360,9 @@ 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->dir) {
if (iter->virt_root)
free(iter->dir);
else
@ -367,7 +370,7 @@ int vrt_closedir(DIR_P *iter) {
}
// root path is not allocated
if(iter->path && *iter->path != 0)
if (iter->path && *iter->path != 0)
free(iter->path);
free(iter);

View File

@ -32,8 +32,7 @@ extern "C"{
#include <stdio.h>
#include <sys/dirent.h>
typedef struct
{
typedef struct {
DIR *dir;
char *path;
uint8_t virt_root;
@ -44,13 +43,21 @@ typedef struct
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