Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: integrity checks for file recovery after crash #1889

Merged
merged 1 commit into from
Oct 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/extensionsIntegrated/NavigationAndHistory/FileRecovery.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,32 @@ define(function (require, exports, module) {
await createDir(folder);
}

function integrityCheck(input) {
// The backup is of the form "length,string_backed_up" so that we can do integrity checks. ideally we should use
// crypto hash functions but that may be expensive. since this is reversible with undo, not doing it for now.
if(!input){
return null;
}
const parts = input.split(',', 2);

if (parts.length !== 2) {
return null;
}

// Parse the length part (should be the first part before the comma)
const expectedLength = parseInt(parts[0], 10);
if (isNaN(expectedLength)) {
return null;
}

// The second part is the actual string after the comma
const actualString = parts[1];
if (actualString.length === expectedLength) {
return actualString;
}
return null;
}

async function loadLastBackedUpFileContents(projectRootPath) {
const project = trackedProjects[projectRootPath];
if(!project){
Expand All @@ -171,7 +197,10 @@ define(function (require, exports, module) {
if(entry.isDirectory){
continue;
}
let text = await jsPromise(FileUtils.readAsText(entry));
let text = integrityCheck(await jsPromise(FileUtils.readAsText(entry)));
if(!text){
continue;
}
let projectFilePath = getProjectFilePath(entry.fullPath, projectRootPath);
if(currentProjectLoadCount !== project.projectLoadCount){
// this means that while we were tying to load a project backup, the user switched to another project
Expand Down Expand Up @@ -257,7 +286,8 @@ define(function (require, exports, module) {
let parentDir = FileSystem.getDirectoryForPath(path.dirname(filePath));
await createDir(parentDir);
let file = FileSystem.getFileForPath(filePath);
await jsPromise(FileUtils.writeText(file, contents, true));
const restoreContentsWithIntegrity = contents.length + "," + contents;
await jsPromise(FileUtils.writeText(file, restoreContentsWithIntegrity, true));
} catch (e) {
console.error(e);
}
Expand Down
Loading