create backup only when receiver found
This commit is contained in:
+28
-21
@@ -14,6 +14,7 @@
|
||||
|
||||
#ifdef __SWITCH__
|
||||
#include <client.hpp>
|
||||
#include <io.hpp>
|
||||
#include <switch.h>
|
||||
#endif
|
||||
|
||||
@@ -32,6 +33,7 @@ bool isClientConnectionFailed() { return g_client_state.connection_failed.loa
|
||||
void cancelClientTransfer() { g_client_state.cancelled.store(true); }
|
||||
double getClientProgress() { return g_client_state.progress(); }
|
||||
std::string getClientStatusText() { return g_client_state.getStatus(); }
|
||||
std::string getClientFailReason() { return g_client_state.fail_reason; }
|
||||
|
||||
static bool send_all(int sock, const void* buf, size_t len) {
|
||||
size_t sent = 0;
|
||||
@@ -78,35 +80,52 @@ static bool sendFile(int sock, const fs::path& filepath) {
|
||||
return true;
|
||||
}
|
||||
|
||||
struct ThreadArgs { size_t index; AccountUid uid; };
|
||||
|
||||
static int find_server(char* server_ip);
|
||||
|
||||
static void fail_connect() {
|
||||
static void fail_connect(const std::string& reason) {
|
||||
g_client_state.fail_reason = reason;
|
||||
g_client_state.connection_failed.store(true);
|
||||
g_client_state.done.store(true);
|
||||
}
|
||||
|
||||
static void* discovery_and_send_thread(void* arg) {
|
||||
fs::path directory = *static_cast<fs::path*>(arg);
|
||||
delete static_cast<fs::path*>(arg);
|
||||
ThreadArgs* targs = static_cast<ThreadArgs*>(arg);
|
||||
size_t index = targs->index;
|
||||
AccountUid uid = targs->uid;
|
||||
delete targs;
|
||||
|
||||
char server_ip[INET_ADDRSTRLEN];
|
||||
if (find_server(server_ip) != 0) {
|
||||
if (!g_client_state.cancelled.load()) fail_connect();
|
||||
g_client_state.done.store(true);
|
||||
if (!g_client_state.cancelled.load())
|
||||
fail_connect("No receiver found.\nMake sure the other Switch is in Receive mode.");
|
||||
else
|
||||
g_client_state.done.store(true);
|
||||
return nullptr;
|
||||
}
|
||||
if (g_client_state.cancelled.load()) { g_client_state.done.store(true); return nullptr; }
|
||||
|
||||
g_client_state.setStatus("Creating backup...");
|
||||
auto backupResult = io::backup(index, uid);
|
||||
if (!std::get<0>(backupResult)) {
|
||||
fail_connect("Failed to create backup:\n" + std::get<2>(backupResult));
|
||||
return nullptr;
|
||||
}
|
||||
fs::path directory = std::get<2>(backupResult);
|
||||
|
||||
if (g_client_state.cancelled.load()) { g_client_state.done.store(true); return nullptr; }
|
||||
|
||||
g_client_state.setStatus("Connecting...");
|
||||
Socket tcp(socket(AF_INET, SOCK_STREAM, 0));
|
||||
if (!tcp.valid()) { fail_connect(); return nullptr; }
|
||||
if (!tcp.valid()) { fail_connect("Failed to open socket."); return nullptr; }
|
||||
|
||||
sockaddr_in serv{};
|
||||
serv.sin_family = AF_INET;
|
||||
serv.sin_port = htons(proto::TCP_PORT);
|
||||
if (inet_pton(AF_INET, server_ip, &serv.sin_addr) <= 0 ||
|
||||
connect(tcp, (sockaddr*)&serv, sizeof(serv)) < 0) {
|
||||
fail_connect();
|
||||
fail_connect("Failed to connect to receiver.");
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -179,11 +198,11 @@ static int find_server(char* server_ip) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int transfer_files(fs::path directory) {
|
||||
int transfer_files(size_t index, AccountUid uid) {
|
||||
g_client_state.reset();
|
||||
g_client_state.setStatus("Searching for receiver...");
|
||||
|
||||
fs::path* arg = new fs::path(directory);
|
||||
ThreadArgs* arg = new ThreadArgs{index, uid};
|
||||
pthread_t thread;
|
||||
if (pthread_create(&thread, nullptr, discovery_and_send_thread, arg) != 0) {
|
||||
delete arg;
|
||||
@@ -192,15 +211,3 @@ int transfer_files(fs::path directory) {
|
||||
pthread_detach(thread);
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef __SWITCH__
|
||||
int main(int argc, char* argv[]) {
|
||||
if (argc < 2) {
|
||||
std::cerr << "Usage: " << argv[0] << " <path>" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
if (transfer_files(fs::path(argv[1])) != 0) return 1;
|
||||
while (!isClientTransferDone()) usleep(16000);
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user