Skip to content

Filesystem Implementation Exercise #3

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

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
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
20 changes: 20 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ lazy val `scala-courses` =
library.scalaTestPlusMock % Test
)
)
.aggregate(`file-system-exercise`)

lazy val `file-system-exercise` =
project
.in(file("filesystem"))
.enablePlugins(AutomateHeaderPlugin, GitVersioning, JavaAppPackaging, AshScriptPlugin)
.settings(name := "Virtual Filesystem (Exercise)")
.settings(settings)
.settings(
libraryDependencies ++= Seq(
library.log4j2Api,
library.log4j2Core,
library.log4j2Scala,
library.scalaLogging,
library.slf4j,
library.scalaCheck % Test,
library.specs2 % Test
),
Docker / version := "0.1.0-SNAPSHOT"
)

// *****************************************************************************
// Library dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* Copyright (c) Geektimus <https://github.com/geektimus>
*/

package com.codingmaniacs.scala.exercises.fs

import java.util.Scanner

import com.codingmaniacs.scala.exercises.fs.commands.Command
import com.codingmaniacs.scala.exercises.fs.directories.Directory

object FileSystem {

def main(args: Array[String]): Unit = {

val root = Directory.ROOT
var state = State(root, root)

val scanner = new Scanner(System.in)

while (true) {
state.show()
state = Command.from(scanner.nextLine()).apply(state)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Copyright (c) Geektimus <https://github.com/geektimus>
*/

package com.codingmaniacs.scala.exercises.fs

import com.codingmaniacs.scala.exercises.fs.directories.Directory

class State(val root: Directory, val workingDir: Directory, val output: String) {

def show(): Unit = {
println(output)
print(State.SHELL_PROMPT)
}

def setMessage(message: String): State = State(root, workingDir, message)
}

object State {
val SHELL_PROMPT = "$ "

def apply(root: Directory, workingDir: Directory, output: String = ""): State = new State(root, workingDir, output)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright (c) Geektimus <https://github.com/geektimus>
*/

package com.codingmaniacs.scala.exercises.fs.commands

import com.codingmaniacs.scala.exercises.fs.State

class Cat(filename: String) extends Command {

override def apply(state: State): State = {
val wd = state.workingDir
val dirEntry = wd.findEntry(filename)
if (dirEntry == null || !dirEntry.isFile) {
state.setMessage(s"$filename: No such file")
} else {
state.setMessage(dirEntry.asFile.contents)
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (c) Geektimus <https://github.com/geektimus>
*/

package com.codingmaniacs.scala.exercises.fs.commands

import com.codingmaniacs.scala.exercises.fs.State
import com.codingmaniacs.scala.exercises.fs.directories.{ DirEntry, Directory }

import scala.annotation.tailrec

class Cd(dir: String) extends Command {

override def apply(state: State): State = {
val root = state.root

val wd = state.workingDir

val absPath = if (dir.startsWith(Directory.SEPARATOR)) {
dir
} else if (wd.isRoot) {
wd.path + dir
} else {
wd.path + Directory.SEPARATOR + dir
}

val dstDir = doFindEntry(root, absPath)

if (dstDir == null || !dstDir.isDirectory) {
state.setMessage(s"$dir: Not such directory")
} else {
State(root, dstDir.asDirectory)
}
}

def doFindEntry(root: Directory, absPath: String): DirEntry = {

@tailrec
def findEntryHelper(currDir: Directory, path: List[String]): DirEntry =
if (path.isEmpty || path.head.isEmpty) currDir
else if (path.tail.isEmpty) currDir.findEntry(path.head)
else {
val nextDir = currDir.findEntry(path.head)
if (nextDir == null || !nextDir.isDirectory) null
else findEntryHelper(nextDir.asDirectory, path.tail)
}

val tokens = absPath.substring(1).split(Directory.SEPARATOR).toList

val newTokens = collapseRelativeTokens(tokens)

if (newTokens == null) null else findEntryHelper(root, newTokens)
}

def collapseRelativeTokens(tokens: List[String]): List[String] = {
@tailrec
def collapseTokensRec(tokens: List[String], res: List[String]): List[String] =
tokens match {
case List() => res
case h :: tail if h.equals(".") => collapseTokensRec(tail, res)
case h :: tail if h.equals("..") =>
res match {
case List() => List()
case init :+ _ => collapseTokensRec(tail, init)
case _ => List()
}
case h :: tail => collapseTokensRec(tail, res :+ h)
}

collapseTokensRec(tokens, List())
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) Geektimus <https://github.com/geektimus>
*/

package com.codingmaniacs.scala.exercises.fs.commands

import com.codingmaniacs.scala.exercises.fs.State

trait Command {
def apply(state: State): State
}

object Command {

val MKDIR = "mkdir"
val LS = "ls"
val PWD = "pwd"
val TOUCH = "touch"
val CD = "cd"
val RM = "rm"
val ECHO = "echo"
val CAT = "cat"

def emptyCommand: Command =
new Command {
override def apply(state: State): State = state
}

def incompleteCommand(name: String): Command =
new Command {
override def apply(state: State): State = state.setMessage(s"$name is an incomplete command")
}

def from(input: String): Command = {
val tokens = input.split(" ")
if (input.isEmpty || tokens.isEmpty) {
emptyCommand
} else if (MKDIR.equals(tokens(0))) {
if (tokens.length < 2) {
incompleteCommand(MKDIR)
} else {
new MkDir(tokens(1))
}
} else if (LS.equals(tokens(0))) {
new Ls()
} else if (PWD.equals(tokens(0))) {
new Pwd()
} else if (TOUCH.equals(tokens(0))) {
if (tokens.length < 2) {
incompleteCommand(TOUCH)
} else {
new Touch(tokens(1))
}
} else if (CD.equals(tokens(0))) {
if (tokens.length < 2) {
incompleteCommand(CD)
} else {
new Cd(tokens(1))
}
} else if (RM.equals(tokens(0))) {
if (tokens.length < 2) {
incompleteCommand(RM)
} else {
new Rm(tokens(1))
}
} else if (ECHO.equals(tokens(0))) {
if (tokens.length < 2) {
incompleteCommand(ECHO)
} else {
new Echo(tokens.tail)
}
} else if (CAT.equals(tokens(0))) {
if (tokens.length < 2) {
incompleteCommand(CAT)
} else {
new Cat(tokens(1))
}
} else {
new UnknownCommand
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
* Copyright (c) Geektimus <https://github.com/geektimus>
*/

package com.codingmaniacs.scala.exercises.fs.commands

import com.codingmaniacs.scala.exercises.fs.State
import com.codingmaniacs.scala.exercises.fs.directories.{ DirEntry, Directory }

abstract class CreateEntry(name: String) extends Command {

def updateStructure(currentDir: Directory, paths: List[String], newEntry: DirEntry): Directory =
if (paths.isEmpty) {
currentDir.addEntry(newEntry)
} else {
val oldEntry = currentDir.findEntry(paths.head).asDirectory
currentDir.replaceEntry(oldEntry.name, updateStructure(oldEntry, paths.tail, newEntry))
}

override def apply(state: State): State = {
val wd = state.workingDir
if (wd.hasEntry(name)) {
state.setMessage(s"$name already exists")
} else if (name.contains(Directory.SEPARATOR)) {
state.setMessage(s"$name must not contain separators")
} else if (checkIllegal(name)) {
state.setMessage(s"$name: illegal entry name!")
} else {
doCreateEntry(state)
}
}

def checkIllegal(name: String): Boolean = name.contains(".")

def doCreateEntry(state: State): State = {
val wd = state.workingDir

val allDirsInPath = wd.getAllFoldersInPath

val newEntry = createSpecificEntry(state)

val newRoot = updateStructure(state.root, allDirsInPath, newEntry)

val newWD = newRoot.findDescendant(allDirsInPath)

State(newRoot, newWD)

}

def createSpecificEntry(state: State): DirEntry
}
Loading