Skip to content

Commit

Permalink
Add saved file backups
Browse files Browse the repository at this point in the history
  • Loading branch information
highperformancecoder committed Feb 26, 2024
1 parent 53f4e13 commit 6efcc7d
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 2 deletions.
2 changes: 1 addition & 1 deletion ecolab
1 change: 1 addition & 0 deletions gui-js/libs/shared/src/lib/backend/minsky.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1397,6 +1397,7 @@ export class Minsky extends CppClass {
async multipleEquities(...args: any[]): Promise<boolean> {return this.$callMethod('multipleEquities',...args);}
async nSteps(...args: number[]): Promise<number> {return this.$callMethod('nSteps',...args);}
async nameCurrentItem(a1: string): Promise<void> {return this.$callMethod('nameCurrentItem',a1);}
async numBackups(...args: number[]): Promise<number> {return this.$callMethod('numBackups',...args);}
async numOpArgs(a1: string): Promise<number> {return this.$callMethod('numOpArgs',a1);}
async openGroupInCanvas(): Promise<void> {return this.$callMethod('openGroupInCanvas');}
async openLogFile(a1: string): Promise<void> {return this.$callMethod('openLogFile',a1);}
Expand Down
16 changes: 15 additions & 1 deletion model/minsky.cc
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
#include <sys/sysinfo.h>
#endif

#include <stdio.h>

using namespace classdesc;
using namespace boost::posix_time;

Expand Down Expand Up @@ -1007,6 +1009,9 @@ namespace minsky

void Minsky::save(const std::string& filename)
{
// back up to temporary file name
rename(filename.c_str(), (filename+"~").c_str());

const schema3::Minsky m(*this);
Saver saver(filename);
saver.packer.prettyPrint=true;
Expand All @@ -1015,15 +1020,24 @@ namespace minsky
saver.save(m);
}
catch (...) {
// rename backup in place
rename((filename+"~").c_str(), filename.c_str());
// if exception is due to file error, provide a more useful message
if (!saver.os)
throw runtime_error("cannot save to "+filename);
throw runtime_error("cannot save to "+filename);
throw;
}
flags &= ~is_edited;
fileVersion=minskyVersion;
if (autoSaver)
boost::filesystem::remove(autoSaver->fileName);
// rotate saved versions
for (int i=numBackups; i>1; --i)
rename((filename+";"+to_string(i-1)).c_str(), (filename+";"+to_string(i)).c_str());
if (numBackups>0)
rename((filename+"~").c_str(), (filename+";1").c_str());
else
::remove((filename+"~").c_str());
}

void Minsky::load(const std::string& filename)
Expand Down
1 change: 1 addition & 0 deletions model/minsky.h
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ namespace minsky
void reset(); ///<resets the variables back to their initial values
std::vector<double> step(); ///< step the equations (by n steps, default 1)

int numBackups=1; ///< number of previous versions of saved files to keep
/// save to a file
void save(const std::string& filename);
/// load from a file
Expand Down
50 changes: 50 additions & 0 deletions test/00/saveBackups.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
#! /bin/sh

here=`pwd`
if test $? -ne 0; then exit 2; fi
tmp=/tmp/$$
mkdir $tmp
if test $? -ne 0; then exit 2; fi
cd $tmp
if test $? -ne 0; then exit 2; fi

fail()
{
echo "FAILED" 1>&2
cd $here
chmod -R u+w $tmp
rm -rf $tmp
exit 1
}

pass()
{
echo "PASSED" 1>&2
cd $here
chmod -R u+w $tmp
rm -rf $tmp
exit 0
}

trap "fail" 1 2 3 15
cat >input.tcl <<EOF
minsky.load $here/examples/exponentialGrowth.mky
minsky.numBackups 3
minsky.findObject "IntOp"
for {set i 1} {\$i<10} {incr i} {
minsky.canvas.item.description "y\$i"
minsky.save foo.mky
}
tcl_exit
EOF

$here/gui-tk/minsky input.tcl
if [ $? -ne 0 ]; then fail; fi

if [ `grep "<name>y" foo.mky*|wc -l` -ne 4 ]; then fail; fi

for i in 3 2 1; do
if ! grep "<name>y$[9-i]" "foo.mky;$i" >/dev/null; then fail; fi
done

pass

0 comments on commit 6efcc7d

Please sign in to comment.