Skip to content

Commit 6efcc7d

Browse files
Add saved file backups
1 parent 53f4e13 commit 6efcc7d

File tree

5 files changed

+68
-2
lines changed

5 files changed

+68
-2
lines changed

gui-js/libs/shared/src/lib/backend/minsky.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,6 +1397,7 @@ export class Minsky extends CppClass {
13971397
async multipleEquities(...args: any[]): Promise<boolean> {return this.$callMethod('multipleEquities',...args);}
13981398
async nSteps(...args: number[]): Promise<number> {return this.$callMethod('nSteps',...args);}
13991399
async nameCurrentItem(a1: string): Promise<void> {return this.$callMethod('nameCurrentItem',a1);}
1400+
async numBackups(...args: number[]): Promise<number> {return this.$callMethod('numBackups',...args);}
14001401
async numOpArgs(a1: string): Promise<number> {return this.$callMethod('numOpArgs',a1);}
14011402
async openGroupInCanvas(): Promise<void> {return this.$callMethod('openGroupInCanvas');}
14021403
async openLogFile(a1: string): Promise<void> {return this.$callMethod('openLogFile',a1);}

model/minsky.cc

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@
6666
#include <sys/sysinfo.h>
6767
#endif
6868

69+
#include <stdio.h>
70+
6971
using namespace classdesc;
7072
using namespace boost::posix_time;
7173

@@ -1007,6 +1009,9 @@ namespace minsky
10071009

10081010
void Minsky::save(const std::string& filename)
10091011
{
1012+
// back up to temporary file name
1013+
rename(filename.c_str(), (filename+"~").c_str());
1014+
10101015
const schema3::Minsky m(*this);
10111016
Saver saver(filename);
10121017
saver.packer.prettyPrint=true;
@@ -1015,15 +1020,24 @@ namespace minsky
10151020
saver.save(m);
10161021
}
10171022
catch (...) {
1023+
// rename backup in place
1024+
rename((filename+"~").c_str(), filename.c_str());
10181025
// if exception is due to file error, provide a more useful message
10191026
if (!saver.os)
1020-
throw runtime_error("cannot save to "+filename);
1027+
throw runtime_error("cannot save to "+filename);
10211028
throw;
10221029
}
10231030
flags &= ~is_edited;
10241031
fileVersion=minskyVersion;
10251032
if (autoSaver)
10261033
boost::filesystem::remove(autoSaver->fileName);
1034+
// rotate saved versions
1035+
for (int i=numBackups; i>1; --i)
1036+
rename((filename+";"+to_string(i-1)).c_str(), (filename+";"+to_string(i)).c_str());
1037+
if (numBackups>0)
1038+
rename((filename+"~").c_str(), (filename+";1").c_str());
1039+
else
1040+
::remove((filename+"~").c_str());
10271041
}
10281042

10291043
void Minsky::load(const std::string& filename)

model/minsky.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,7 @@ namespace minsky
311311
void reset(); ///<resets the variables back to their initial values
312312
std::vector<double> step(); ///< step the equations (by n steps, default 1)
313313

314+
int numBackups=1; ///< number of previous versions of saved files to keep
314315
/// save to a file
315316
void save(const std::string& filename);
316317
/// load from a file

test/00/saveBackups.sh

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#! /bin/sh
2+
3+
here=`pwd`
4+
if test $? -ne 0; then exit 2; fi
5+
tmp=/tmp/$$
6+
mkdir $tmp
7+
if test $? -ne 0; then exit 2; fi
8+
cd $tmp
9+
if test $? -ne 0; then exit 2; fi
10+
11+
fail()
12+
{
13+
echo "FAILED" 1>&2
14+
cd $here
15+
chmod -R u+w $tmp
16+
rm -rf $tmp
17+
exit 1
18+
}
19+
20+
pass()
21+
{
22+
echo "PASSED" 1>&2
23+
cd $here
24+
chmod -R u+w $tmp
25+
rm -rf $tmp
26+
exit 0
27+
}
28+
29+
trap "fail" 1 2 3 15
30+
cat >input.tcl <<EOF
31+
minsky.load $here/examples/exponentialGrowth.mky
32+
minsky.numBackups 3
33+
minsky.findObject "IntOp"
34+
for {set i 1} {\$i<10} {incr i} {
35+
minsky.canvas.item.description "y\$i"
36+
minsky.save foo.mky
37+
}
38+
tcl_exit
39+
EOF
40+
41+
$here/gui-tk/minsky input.tcl
42+
if [ $? -ne 0 ]; then fail; fi
43+
44+
if [ `grep "<name>y" foo.mky*|wc -l` -ne 4 ]; then fail; fi
45+
46+
for i in 3 2 1; do
47+
if ! grep "<name>y$[9-i]" "foo.mky;$i" >/dev/null; then fail; fi
48+
done
49+
50+
pass

0 commit comments

Comments
 (0)