1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

Better handling of zero-length flow files

Closes #819

If a flow file is found to be zero-bytes:
  If there is a non-empty backup, restore the backup and resolve
  If there is no backup or it is also empty, resolve empty flow
If a flow file is found to be invalid json:
  Log and resolve empty flow
This commit is contained in:
Nick O'Leary 2016-03-11 22:42:04 +00:00
parent 349ebfe4db
commit f2191e94b3

View File

@ -1,5 +1,5 @@
/**
* Copyright 2013, 2014 IBM Corp.
* Copyright 2013, 2016 IBM Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -199,10 +199,42 @@ var localfilesystem = {
}
fs.readFile(flowsFullPath,'utf8',function(err,data) {
if (!err) {
return resolve(JSON.parse(data));
if (data.length === 0) {
log.warn(log._("storage.localfilesystem.empty"));
try {
var backupStat = fs.statSync(flowsFileBackup);
if (backupStat.size === 0) {
// Empty flows, empty backup - return empty flow
return resolve([]);
}
// Empty flows, restore backup
log.warn(log._("storage.localfilesystem.restore",{path:flowsFileBackup}));
fs.copy(flowsFileBackup,flowsFullPath,function(backupCopyErr) {
if (backupCopyErr) {
// Restore backup failed
log.warn(log._("storage.localfilesystem.restore-fail",{message:backupCopyErr.toString()}));
resolve([]);
} else {
// Loop back in to load the restored backup
resolve(localfilesystem.getFlows());
}
});
return;
} catch(backupStatErr) {
// Empty flow file, no back-up file
return resolve([]);
}
}
try {
return resolve(JSON.parse(data));
} catch(parseErr) {
log.warn(log._("storage.localfilesystem.invalid"));
return resolve([]);
}
} else {
log.info(log._("storage.localfilesystem.create"));
resolve([]);
}
});
});
},