mirror of
https://github.com/wesnoth/wesnoth
synced 2025-05-04 21:47:53 +00:00
It deletes existing copies of a downloaded campaign before writing the new one.
This commit is contained in:
parent
cab2014701
commit
ed7fce026b
@ -49,7 +49,8 @@ BPath be_path;
|
||||
|
||||
//for getenv
|
||||
#include <cstdlib>
|
||||
|
||||
#include <cerrno>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
@ -316,6 +317,41 @@ void make_directory(const std::string& path)
|
||||
#endif
|
||||
}
|
||||
|
||||
//this deletes a directory with no hidden files and subdirectories
|
||||
//also deletes a single file
|
||||
bool delete_directory(const std::string& path)
|
||||
{
|
||||
bool ret = true;
|
||||
std::vector<std::string> files;
|
||||
std::vector<std::string> dirs;
|
||||
|
||||
get_files_in_dir(path, &files, &dirs, ENTIRE_FILE_PATH);
|
||||
|
||||
if(!files.empty()) {
|
||||
for(std::vector<std::string>::const_iterator i = files.begin(); i != files.end(); ++i) {
|
||||
errno = 0;
|
||||
if(remove((*i).c_str()) != 0) {
|
||||
LOG_FS << "remove(" << (*i) << "): " << strerror(errno) << "\n";
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(!dirs.empty()) {
|
||||
for(std::vector<std::string>::const_iterator j = dirs.begin(); j != dirs.end(); ++j) {
|
||||
if(!delete_directory(*j))
|
||||
ret = false;
|
||||
}
|
||||
}
|
||||
|
||||
errno = 0;
|
||||
if(remove(path.c_str()) != 0) {
|
||||
LOG_FS << "remove(" << path << "): " << strerror(errno) << "\n";
|
||||
ret = false;
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
std::string get_cwd()
|
||||
{
|
||||
char buf[1024];
|
||||
|
@ -56,6 +56,7 @@ std::string get_user_data_dir();
|
||||
std::string get_cwd();
|
||||
|
||||
void make_directory(const std::string& dirname);
|
||||
bool delete_directory(const std::string& dirname);
|
||||
|
||||
//basic disk I/O
|
||||
bool filesystem_init();
|
||||
|
13
src/game.cpp
13
src/game.cpp
@ -120,6 +120,7 @@ private:
|
||||
void download_campaigns();
|
||||
void upload_campaign(const std::string& campaign, network::connection sock);
|
||||
void delete_campaign(const std::string& campaign, network::connection sock);
|
||||
void remove_campaign(const std::string& campaign);
|
||||
|
||||
const int argc_;
|
||||
int arg_;
|
||||
@ -987,6 +988,11 @@ void game_controller::download_campaigns()
|
||||
return;
|
||||
}
|
||||
|
||||
//remove any existing versions of the just downloaded campaign
|
||||
//assuming it consists of a dir and a cfg file
|
||||
remove_campaign(campaigns[index]);
|
||||
|
||||
//put a break at line below to see that it really works.
|
||||
unarchive_campaign(cfg);
|
||||
|
||||
// when using zipios, force a reread zip and directory indices
|
||||
@ -1093,6 +1099,13 @@ void game_controller::delete_campaign(const std::string& campaign, network::conn
|
||||
}
|
||||
}
|
||||
|
||||
void game_controller::remove_campaign(const std::string& campaign)
|
||||
{
|
||||
const std::string campaign_dir = get_user_data_dir() + "/data/campaigns/" + campaign;
|
||||
delete_directory(campaign_dir);
|
||||
delete_directory(campaign_dir + ".cfg");
|
||||
}
|
||||
|
||||
bool game_controller::play_multiplayer()
|
||||
{
|
||||
state_ = game_state();
|
||||
|
Loading…
x
Reference in New Issue
Block a user