Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
fs.copy(backupPath,path,function(backupCopyErr) {
if (backupCopyErr) {
// Restore backup failed
log.warn(log._("storage.localfilesystem.restore-fail",{message:backupCopyErr.toString(),type:type}));
resolve([]);
} else {
// Loop back in to load the restored backup
resolve(readFile(path,backupPath,emptyResponse,type));
}
});
return;
fs.readFile(path,'utf8',function(err,data) {
if (!err) {
if (data.length === 0) {
log.warn(log._("storage.localfilesystem.empty",{type:type}));
try {
var backupStat = fs.statSync(backupPath);
if (backupStat.size === 0) {
// Empty flows, empty backup - return empty flow
return resolve(emptyResponse);
}
// Empty flows, restore backup
log.warn(log._("storage.localfilesystem.restore",{path:backupPath,type:type}));
fs.copy(backupPath,path,function(backupCopyErr) {
if (backupCopyErr) {
// Restore backup failed
log.warn(log._("storage.localfilesystem.restore-fail",{message:backupCopyErr.toString(),type:type}));
resolve([]);
} else {
// Loop back in to load the restored backup
resolve(readFile(path,backupPath,emptyResponse,type));
resolve([]);
} else {
// Loop back in to load the restored backup
resolve(readFile(path,backupPath,emptyResponse,type));
}
});
return;
} catch(backupStatErr) {
// Empty flow file, no back-up file
return resolve(emptyResponse);
}
}
try {
return resolve(parseJSON(data));
} catch(parseErr) {
log.warn(log._("storage.localfilesystem.invalid",{type:type}));
return resolve(emptyResponse);
}
} else {
if (type === 'flow') {
log.info(log._("storage.localfilesystem.create",{type:type}));
}
resolve(emptyResponse);
}
});
});
});
return;
} catch(backupStatErr) {
// Empty flow file, no back-up file
return resolve(emptyResponse);
}
}
try {
return resolve(parseJSON(data));
} catch(parseErr) {
log.warn(log._("storage.localfilesystem.invalid",{type:type}));
return resolve(emptyResponse);
}
} else {
if (type === 'flow') {
log.info(log._("storage.localfilesystem.create",{type:type}));
}
resolve(emptyResponse);
}
});
});
fs.readFile(path,'utf8',function(err,data) {
if (!err) {
if (data.length === 0) {
log.warn(log._("storage.localfilesystem.empty",{type:type}));
try {
var backupStat = fs.statSync(backupPath);
if (backupStat.size === 0) {
// Empty flows, empty backup - return empty flow
return resolve(emptyResponse);
}
// Empty flows, restore backup
log.warn(log._("storage.localfilesystem.restore",{path:backupPath,type:type}));
fs.copy(backupPath,path,function(backupCopyErr) {
if (backupCopyErr) {
// Restore backup failed
log.warn(log._("storage.localfilesystem.restore-fail",{message:backupCopyErr.toString(),type:type}));
resolve([]);
} else {
// Loop back in to load the restored backup
resolve(readFile(path,backupPath,emptyResponse,type));
}
});
return;
} catch(backupStatErr) {
// Empty flow file, no back-up file
return resolve(emptyResponse);
}
}
fs.fsync(fd,function(err) {
if (err) {
log.warn(log._("storage.localfilesystem.fsync-fail",{path: path, message: err.toString()}));
}
stream.end(resolve);
});
});