restore.c
ds2.1.1/lib/secure/cmds/creators/restore.c · 2.4 KB
From the archive ds2.1.1.zip
Download the archive « Back to the archive contents
#include <lib.h>
#include <dirs.h>
#include <commands.h>
inherit LIB_DAEMON;
string globalstr1, globalstr2;
int globalint;
mixed cmd(string str) {
string *sorted_array, *sub_arr, *new_arr, *bkarr;
string line, s1, bkname, bkcontents;
int number;
if( !str ) return "You must specify a file to restore.";
if(str && sscanf(str,"%s %d",s1, number) > 1) str = s1;
else false();
new_arr = ({});
sub_arr = ({});
bkname = homedir(this_player())+"/bak/bk.db";
if( !str ) return "You must specify a file to restore.";
if( !file_exists(bkname)) {
write("The backup database file does not exist. Aborting.");
return 1;
}
bkcontents = read_file(bkname);
if(strsrch(bkcontents,str) == -1) {
write("You haven't backed up a file by that name.");
return 1;
}
if(str == "workroom.orig") {
unguarded( (: globalint = cp(homedir(this_player())+"/bak/workroom.orig",
homedir(this_player())+"/workroom.c") :) );
if(globalint) {
write("Workroom file restored.");
load_object(CMD_UPDATE)->cmd("-a "+homedir(this_player())+"/workroom.c");
}
else write("Workroom could not be restored.");
return 1;
}
bkarr = explode(bkcontents,"\n");
foreach(string zline in bkarr){
if(strsrch(zline,str) != -1) new_arr += ({ zline });
}
if(!number || number < 1) number = 1;
sorted_array = sort_array(new_arr, -1);
if(number > sizeof(sorted_array)) number = sizeof(sorted_array);
line = sorted_array[(number - 1)];
sub_arr = explode(line," : ");
if(!file_exists(REALMS_DIRS + "/" + this_player()->GetKeyName()+"/bak/"+sub_arr[0])){
write("That file has an entry in the backup database but can't be found in your backup directory.");
return 1;
}
globalstr1 = REALMS_DIRS + "/" + this_player()->GetKeyName()+"/bak/"+sub_arr[0];
globalstr2 = sub_arr[1];
unguarded( (: cp(globalstr1, globalstr2) :) );
write("File restored.");
return 1;
}
int help() {
message("help", "Syntax: <restore STRING> [NUMBER]\n\n"
"Restores the specified file from your bak/ directory. "
"If you specify a number, the command will try to find "
"the Nth newest file to restore. Examples:\n"
"To restore the most recent version of file.c:\n"
"restore file.c\n"
"To restore the version you backed up before the most "
"recent version:\n"
"restore file.c 2\n"
"And so on.\n"
"See also: bk",
this_player());
}