From 97336895dea500cf724fad181b1b8d707652795a Mon Sep 17 00:00:00 2001 From: y0014984 Date: Thu, 26 Dec 2024 16:21:32 +0100 Subject: [PATCH] continue storage implementation --- src/logic/Storage.ts | 86 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/logic/Storage.ts b/src/logic/Storage.ts index 274efa2..8c052ab 100644 --- a/src/logic/Storage.ts +++ b/src/logic/Storage.ts @@ -85,6 +85,10 @@ export class Storage { param = this.getCommandStringParam(command, commandLength); this.createDirectory(param); break; + case 'CRF': + param = this.getCommandStringParam(command, commandLength); + this.createFile(param); + break; case 'RMD': param = this.getCommandStringParam(command, commandLength); this.removeDirectory(param); @@ -302,6 +306,88 @@ export class Storage { // ======================================== + createFile(fileName: string) { + fileName.trim(); + + if (fileName === '') { + this.mem.setInt(commandReturnValue, returnValError); + this.mem.setInt(commandLastError, errorMissingParameter); + return; + } + + if (fileName === '/') { + this.mem.setInt(commandReturnValue, returnValError); + this.mem.setInt(commandLastError, errorFileExists); + return; + } + + if (fileName[0] === '/' && fileName.length > 1) { + const path = fileName.split('/'); + path.shift(); + + const newFileName = (path.pop() as string).substring(0, fsObjMaxNameLength); + + let targetDir: Storage | FilesystemObject | undefined = this; + + targetDir = this.getDirFromPathArray(targetDir, path); + + if (!targetDir) { + this.mem.setInt(commandReturnValue, returnValError); + this.mem.setInt(commandLastError, errorNoFileOrDir); + } else { + const newFile = new File(targetDir as Directory, newFileName); + (targetDir as Directory).fsObjects.push(newFile); + + this.mem.setInt(commandReturnValue, returnValSuccess); + } + return; + } + + if (fileName[0] !== '/' && fileName.length > 1 && fileName.includes('/')) { + const path = fileName.split('/'); + + const newFileName = (path.pop() as string).substring(0, fsObjMaxNameLength); + + let targetDir: Storage | FilesystemObject | undefined; + targetDir = this.currentParentDir as Directory; + if (this.currentParentDir === null) targetDir = this; + + targetDir = this.getDirFromPathArray(targetDir, path); + + if (!targetDir) { + this.mem.setInt(commandReturnValue, returnValError); + this.mem.setInt(commandLastError, errorNoFileOrDir); + } else { + const newFile = new File(targetDir as Directory, newFileName); + (targetDir as Directory).fsObjects.push(newFile); + + this.mem.setInt(commandReturnValue, returnValSuccess); + } + return; + } + + if (fileName === '..') { + this.mem.setInt(commandReturnValue, returnValError); + this.mem.setInt(commandLastError, errorDirExists); + return; + } + + let subFile = this.currentDirFsObjects.find(fsObj => fsObj.name === fileName); + + if (subFile) { + this.mem.setInt(commandReturnValue, returnValError); + this.mem.setInt(commandLastError, errorFileExists); + return; + } + + const newFile = new File(this.currentParentDir, fileName); + this.currentDirFsObjects.push(newFile); + + this.mem.setInt(commandReturnValue, returnValSuccess); + } + + // ======================================== + removeDirectory(dirName: string) { dirName.trim();