diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 00000000..755c365b --- /dev/null +++ b/package-lock.json @@ -0,0 +1,17 @@ +{ + "name": "node-nodejs-fundamentals", + "version": "1.0.0", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "node-nodejs-fundamentals", + "version": "1.0.0", + "license": "ISC", + "engines": { + "node": ">=24.10.0", + "npm": ">=10.9.2" + } + } + } +} diff --git a/src/fs/restore.js b/src/fs/restore.js index 96ae1ffb..a6740e67 100644 --- a/src/fs/restore.js +++ b/src/fs/restore.js @@ -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(); diff --git a/src/fs/snapshot.js b/src/fs/snapshot.js index 050103d3..6ed3bae0 100644 --- a/src/fs/snapshot.js +++ b/src/fs/snapshot.js @@ -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(); \ No newline at end of file