Applied modified and cleaned up patch #4275 from ihsan for bug #13874.

It deletes existing copies of a downloaded campaign before writing the new one.
This commit is contained in:
Jon Daniel 2005-08-06 20:26:54 +00:00
parent cab2014701
commit ed7fce026b
3 changed files with 51 additions and 1 deletions

View File

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

View File

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

View File

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