Selaa lähdekoodia

Prevent file corruption by writing to a backup file before writing to the real board file

dev_h
Ophir LOJKINE 6 vuotta sitten
vanhempi
commit
078db23d47

+ 0
- 1
server-data/board-anonymous.json.2019-06-24T09:04:10.556Z.bak
File diff suppressed because it is too large
Näytä tiedosto


+ 0
- 1
server-data/board-anonymous.json.2019-06-24T09:06:10.734Z.bak
File diff suppressed because it is too large
Näytä tiedosto


+ 0
- 1
server-data/board-test.json.2019-06-25T11:52:32.336Z.bak Näytä tiedosto

1
-!{"tjp1lbrm74":{"type":"new","id":"tjp1lbrm74","color":"#1913b0","size":27,"x":552,"y":251,"tool":"Text","txt":""},"tjp1lbszvk":{"type":"new","id":"tjp1lbszvk","color":"#1913b0","size":27,"x":957,"y":385,"tool":"Text","txt":""},"tjp1lc73bo":{"type":"new","id":"tjp1lc73bo","color":"#1913b0","size":27,"x":962,"y":370,"tool":"Text","txt":""},"sjp1ly7ztg":{"type":"straight","id":"sjp1ly7ztg","color":"#1913b0","size":10,"x":498,"y":547,"tool":"Straight line","x2":652,"y2":698},"sjp1lybvdw":{"type":"straight","id":"sjp1lybvdw","color":"#1913b0","size":39,"x":407,"y":177,"tool":"Straight line","x2":407,"y2":177},"ljp2gfjkp8":{"type":"line","id":"ljp2gfjkp8","color":"#1913b0","size":10,"tool":"Pencil","_children":[{"type":"child","parent":"ljp2gfjkp8","x":502,"y":430,"tool":"Pencil"},{"type":"child","parent":"ljp2gfjkp8","x":513,"y":433,"tool":"Pencil"},{"type":"child","parent":"ljp2gfjkp8","x":585,"y":434,"tool":"Pencil"},{"type":"child","parent":"ljp2gfjkp8","x":738,"y":405,"tool":"Pencil"},{"type":"child","parent":"ljp2gfjkp8","x":845,"y":398,"tool":"Pencil"},{"type":"child","parent":"ljp2gfjkp8","x":868,"y":399,"tool":"Pencil"}]},"ljp2gfknc8":{"type":"line","id":"ljp2gfknc8","color":"#1913b0","size":10,"tool":"Pencil","_children":[{"type":"child","parent":"ljp2gfknc8","x":655,"y":281,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":675,"y":283,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":825,"y":248,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":999,"y":251,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1037,"y":272,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1089,"y":292,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1113,"y":311,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1130,"y":325,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1143,"y":333,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1160,"y":350,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1174,"y":388,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1169,"y":429,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1151,"y":460,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1136,"y":480,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1131,"y":485,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1127,"y":485,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1120,"y":445,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1108,"y":398,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1107,"y":401,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1098,"y":462,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1097,"y":478,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1094,"y":448,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1092,"y":445,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1084,"y":472,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1084,"y":470,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1082,"y":446,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1077,"y":460,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1075,"y":467,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1068,"y":448,"tool":"Pencil"},{"type":"child","parent":"ljp2gfknc8","x":1060,"y":460,"tool":"Pencil"}]},"sjp2gg3jeu":{"type":"straight","id":"sjp2gg3jeu","color":"#1913b0","size":10,"x":777,"y":743,"tool":"Straight line","x2":1343,"y2":638},"sjp2gg4rts":{"type":"straight","id":"sjp2gg4rts","color":"#1913b0","size":10,"x":1340,"y":649,"tool":"Straight line","x2":1055,"y2":916},"sjp2gg69zj":{"type":"straight","id":"sjp2gg69zj","color":"#1913b0","size":10,"x":929,"y":630,"tool":"Straight line","x2":1422,"y2":1139},"rjp2gg8dyo":{"type":"rect","id":"rjp2gg8dyo","color":"#1913b0","size":10,"x":1060,"y":61,"x2":1394,"y2":245,"tool":"Rectangle"}}

+ 22
- 7
server/boardData.js Näytä tiedosto

163
 	this.lastSaveDate = Date.now();
163
 	this.lastSaveDate = Date.now();
164
 	this.clean();
164
 	this.clean();
165
 	if (!file) file = this.file;
165
 	if (!file) file = this.file;
166
+	var tmp_file = backupFileName(file);
166
 	var board_txt = JSON.stringify(this.board);
167
 	var board_txt = JSON.stringify(this.board);
167
 	var that = this;
168
 	var that = this;
168
-	fs.writeFile(file, board_txt, function onBoardSaved(err) {
169
+	function afterSave(err) {
169
 		if (err) {
170
 		if (err) {
170
-			console.trace(new Error("Unable to save the board: " + err));
171
+			log("board saving error", {
172
+				'err': err,
173
+				'tmp_file': tmp_file,
174
+			});
171
 		} else {
175
 		} else {
172
-			log("saved board", { 'name': that.name });
176
+			log("saved board", {
177
+				'name': that.name,
178
+				'delay_ms': (Date.now() - that.lastSaveDate)
179
+			});
173
 		}
180
 		}
181
+	}
182
+	fs.writeFile(tmp_file, board_txt, function onBoardSaved(err) {
183
+		if (err) afterSave(err);
184
+		else fs.rename(tmp_file, file, afterSave);
174
 	});
185
 	});
175
 };
186
 };
176
 
187
 
235
 				boardData.board = {}
246
 				boardData.board = {}
236
 				if (data) {
247
 				if (data) {
237
 					// There was an error loading the board, but some data was still read
248
 					// There was an error loading the board, but some data was still read
238
-					var backupFileName = boardData.file + '.' + new Date().toISOString() + '.bak';
239
-					log("Writing the corrupted file to " + backupFileName);
240
-					fs.writeFile(backupFileName, data, function (err) {
241
-						if (err) log("Error writing " + backupFileName + ": " + err);
249
+					var backup = backupFileName(boardData.file);
250
+					log("Writing the corrupted file to " + backup);
251
+					fs.writeFile(backup, data, function (err) {
252
+						if (err) log("Error writing " + backup + ": " + err);
242
 					});
253
 					});
243
 				}
254
 				}
244
 			}
255
 			}
247
 	});
258
 	});
248
 };
259
 };
249
 
260
 
261
+function backupFileName(baseName) {
262
+	return baseName + '.' + new Date().toISOString() + '.bak';
263
+}
264
+
250
 module.exports.BoardData = BoardData;
265
 module.exports.BoardData = BoardData;

Loading…
Peruuta
Tallenna