Skip to content

Commit

Permalink
enhancement: Optionally keep a backup of existing file, when download…
Browse files Browse the repository at this point in the history
…ing updates (#1155)
  • Loading branch information
midwan committed Nov 11, 2023
1 parent ecb6aca commit 07f7c4f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
27 changes: 19 additions & 8 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1089,7 +1089,7 @@ static void parse_cmdline_and_init_file(int argc, TCHAR **argv)
* of start_program () and leave_program () if you need to do anything special.
* Add #ifdefs around these as appropriate.
*/
static void do_start_program (void)
static void do_start_program ()
{
if (quit_program == -UAE_QUIT)
return;
Expand All @@ -1111,12 +1111,12 @@ static void do_start_program (void)
}
}

static void start_program (void)
static void start_program ()
{
do_start_program ();
}

static void leave_program (void)
static void leave_program ()
{
do_leave_program ();
}
Expand All @@ -1128,7 +1128,7 @@ long get_file_size(const std::string& filename)
return rc == 0 ? static_cast<long>(stat_buf.st_size) : -1;
}

bool file_exists(std::string file)
bool file_exists(const std::string& file)
{
#ifdef USE_OLDGCC
namespace fs = std::experimental::filesystem;
Expand All @@ -1139,7 +1139,7 @@ bool file_exists(std::string file)
return (fs::exists(f));
}

bool download_file(const std::string& source, const std::string& destination)
bool download_file(const std::string& source, const std::string& destination, bool keep_backup)
{
// homebrew installs in different locations on OSX Intel vs OSX Apple Silicon
#if defined (__MACH__) && defined (__arm64__)
Expand Down Expand Up @@ -1204,7 +1204,18 @@ bool download_file(const std::string& source, const std::string& destination)

if (file_exists(tmp))
{
write_log("Tmp file will now be renamed: %s\n", tmp.c_str());
if (file_exists(destination) && keep_backup)
{
write_log("Backup requested, renaming destination file %s to .bak\n", destination.c_str());
std::string new_filename = destination.substr(0, destination.find_last_of('.')).append(".bak");
if (std::rename(destination.c_str(), new_filename.c_str()) < 0)
{
write_log(strerror(errno));
write_log("\n");
}
}

write_log("Renaming downloaded temporary file %s to final destination\n", tmp.c_str());
if (std::rename(tmp.c_str(), destination.c_str()) < 0)
{
write_log(strerror(errno));
Expand All @@ -1216,15 +1227,15 @@ bool download_file(const std::string& source, const std::string& destination)
return false;
}

void download_rtb(const std::string filename)
void download_rtb(const std::string& filename)
{
std::string destination_filename = "save-data/Kickstarts/" + filename;
const std::string destination = prefix_with_whdboot_path(destination_filename);
if (!file_exists(destination))
{
write_log("Downloading %s ...\n", destination.c_str());
const std::string url = "https://github.com/midwan/amiberry/blob/master/whdboot/save-data/Kickstarts/" + filename + "?raw=true";
download_file(url, destination);
download_file(url, destination, false);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/osdep/gui/PanelPaths.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ class DownloadXMLButtonActionListener : public gcn::ActionListener
// download WHDLoad executable
destination = prefix_with_whdboot_path("WHDLoad");
write_log("Downloading %s ...\n", destination.c_str());
download_file("https://github.com/midwan/amiberry/blob/master/whdboot/WHDLoad?raw=true", destination);
download_file("https://github.com/midwan/amiberry/blob/master/whdboot/WHDLoad?raw=true", destination, false);

// download boot-data.zip
destination = prefix_with_whdboot_path("boot-data.zip");
write_log("Downloading %s ...\n", destination.c_str());
download_file("https://github.com/midwan/amiberry/blob/master/whdboot/boot-data.zip?raw=true", destination);
download_file("https://github.com/midwan/amiberry/blob/master/whdboot/boot-data.zip?raw=true", destination, false);

// download kickstart RTB files for maximum compatibility
download_rtb("kick33180.A500.RTB");
Expand All @@ -231,7 +231,7 @@ class DownloadXMLButtonActionListener : public gcn::ActionListener
destination = prefix_with_whdboot_path("game-data/whdload_db.xml");
const auto old_timestamp = get_xml_timestamp(destination);
write_log("Downloading %s ...\n", destination.c_str());
const auto result = download_file("https://github.com/HoraceAndTheSpider/Amiberry-XML-Builder/blob/master/whdload_db.xml?raw=true", destination);
const auto result = download_file("https://github.com/HoraceAndTheSpider/Amiberry-XML-Builder/blob/master/whdload_db.xml?raw=true", destination, true);

if (result)
{
Expand All @@ -258,7 +258,7 @@ class DownloadControllerDbActionListener : public gcn::ActionListener
destination += "gamecontrollerdb.txt";
write_log("Downloading % ...\n", destination.c_str());
const auto* const url = "https://raw.githubusercontent.com/gabomdq/SDL_GameControllerDB/master/gamecontrollerdb.txt";
const auto result = download_file(url, destination);
const auto result = download_file(url, destination, true);

if (result)
{
Expand Down
4 changes: 2 additions & 2 deletions src/osdep/gui/gui_handling.h
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,8 @@ extern void init_dispmanx_gui();
extern void update_gui_screen();
extern void cap_fps(Uint64 start);
extern long get_file_size(const std::string& filename);
extern bool download_file(const std::string& source, const std::string& destination);
extern void download_rtb(std::string filename);
extern bool download_file(const std::string& source, const std::string& destination, bool keep_backup);
extern void download_rtb(const std::string& filename);

extern int fromdfxtype(int num, int dfx, int subtype);
extern int todfxtype(int num, int dfx, int* subtype);
Expand Down

0 comments on commit 07f7c4f

Please sign in to comment.