Skip to content
Open
Show file tree
Hide file tree
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
17 changes: 17 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 29 additions & 4 deletions src/fs/restore.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,33 @@
import { readFile, stat, mkdir, writeFile } from 'node:fs/promises';
import path from 'node:path';

const restore = async () => {
// Write your code here
// Read snapshot.json
// Treat snapshot.rootPath as metadata only
// Recreate directory/file structure in workspace_restored
const rootPath = '/home/user/workspace_restored';

try {
const status = await stat(rootPath);
if (!status.isDirectory()) {
throw new Error('FS operation failed');
}
} catch {
throw new Error('FS operation failed');
}
const snapshot = path.join(rootPath, '..', 'snapshot.json');
const snapshotContent = await readFile(snapshot, 'utf-8');

const { rootPath: snapshotRootPath, entries } = JSON.parse(snapshotContent);

for (const entry of entries) {
const fullPath = path.join(rootPath, entry.path);
if (entry.type === 'directory') {
await mkdir(fullPath, { recursive: true });
}

if (entry.type === 'file') {
await mkdir(path.dirname(fullPath), { recursive: true });
await writeFile(fullPath, entry.content, 'base64');
}
}
};

await restore();
50 changes: 44 additions & 6 deletions src/fs/snapshot.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,47 @@
import { readdir, readFile, stat, writeFile } from 'node:fs/promises';
import path from 'node:path';

const snapshot = async () => {
// Write your code here
// Recursively scan workspace directory
// Write snapshot.json with:
// - rootPath: absolute path to workspace
// - entries: flat array of relative paths and metadata
const rootPath = '/home/user/workspace';

try {
const status = await stat(rootPath);
if (!status.isDirectory()) {
throw new Error('FS operation failed');
}
} catch {
throw new Error('FS operation failed');
}

const childPaths = await readdir(rootPath, { recursive: true });
const entries = [];

for (const childPath of childPaths) {
const normalizedPath = path.join(childPath).split(path.sep).join('/');
const fullPath = path.join(rootPath, childPath);
const info = await stat(fullPath);

if (info.isDirectory()) {
entries.push({ path: normalizedPath, type: 'directory' });
} else if (info.isFile()) {
const content = await readFile(fullPath);
entries.push({
path: normalizedPath,
type: 'file',
size: info.size,
content: content.toString('base64'),
});
}
}

const snapshot = path.join(rootPath, '..', 'snapshot.json');
await writeFile(
snapshot,
JSON.stringify({ rootPath, entries }),
'utf-8'
);

await snapshot();
};

await snapshot();
await snapshot();