Compare commits
3 Commits
18b9dac09d
...
e7dd3fa073
Author | SHA1 | Date |
---|---|---|
Gleb Koval | e7dd3fa073 | |
Gleb Koval | 726980a669 | |
Gleb Koval | 8aa21dff54 |
|
@ -1,36 +1,49 @@
|
||||||
package filesystem
|
package filesystem
|
||||||
|
|
||||||
import java.nio.file.FileAlreadyExistsException
|
import java.nio.file.FileAlreadyExistsException
|
||||||
import java.nio.file.FileSystemException
|
|
||||||
import java.nio.file.Files
|
|
||||||
import java.nio.file.Path
|
import java.nio.file.Path
|
||||||
|
import kotlin.io.path.createDirectory
|
||||||
|
import kotlin.io.path.createFile
|
||||||
|
import kotlin.io.path.writeText
|
||||||
|
|
||||||
class FSCreator {
|
class FSCreator {
|
||||||
/**
|
/**
|
||||||
* Create entry, leaving existing folders' contents, but overwriting existing files.
|
* Create entry, leaving existing folders' contents, but overwriting existing files.
|
||||||
|
* @throws CyclicFolderException Cyclic folders cannot be created.
|
||||||
*/
|
*/
|
||||||
@Throws(FileSystemException::class)
|
@Throws(CyclicFolderException::class)
|
||||||
fun create(
|
fun create(
|
||||||
entryToCreate: FSEntry,
|
entryToCreate: FSEntry,
|
||||||
destination: String,
|
destination: String,
|
||||||
) {
|
) {
|
||||||
val queue = ArrayDeque<Pair<FSEntry, Path>>()
|
// No point in running anything if we know the input is invalid.
|
||||||
queue.add(entryToCreate to Path.of(destination))
|
if (entryToCreate is FSFolder && entryToCreate.isCyclic()) {
|
||||||
|
throw CyclicFolderException()
|
||||||
|
}
|
||||||
|
|
||||||
|
val queue =
|
||||||
|
ArrayDeque(
|
||||||
|
listOf(
|
||||||
|
entryToCreate to Path.of(destination),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
while (queue.isNotEmpty()) {
|
while (queue.isNotEmpty()) {
|
||||||
val (entry, dest) = queue.removeFirst()
|
val (entry, dest) = queue.removeFirst()
|
||||||
val path = dest.resolve(entry.name)
|
val path = dest.resolve(entry.name)
|
||||||
try {
|
try {
|
||||||
when (entry) {
|
when (entry) {
|
||||||
is FSFile -> Files.createFile(path)
|
is FSFile -> path.createFile()
|
||||||
is FSFolder -> Files.createDirectory(path)
|
is FSFolder -> path.createDirectory()
|
||||||
}
|
}
|
||||||
} catch (_: FileAlreadyExistsException) {
|
} catch (_: FileAlreadyExistsException) {
|
||||||
} // Allow files/folders to already exist.
|
} // Allow files/folders to already exist.
|
||||||
when (entry) {
|
when (entry) {
|
||||||
is FSFile -> Files.write(path, entry.content.toByteArray())
|
is FSFile -> path.writeText(entry.content)
|
||||||
is FSFolder -> queue.addAll(entry.entries.map { it to path })
|
is FSFolder -> queue.addAll(entry.entries.map { it to path })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class CyclicFolderException : Exception("Cyclic FSFolders are not supported")
|
|
@ -1,9 +1,39 @@
|
||||||
package filesystem
|
package filesystem
|
||||||
|
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
|
||||||
// Note sealed allows for simpler logic in FSCreator by guaranteeing FSFile and FSFolder are the only possible FSEntries
|
// Note sealed allows for simpler logic in FSCreator by guaranteeing FSFile and FSFolder are the only possible FSEntries
|
||||||
// (as we expect), and it also implicitly makes the class abstract as required.
|
// (as we expect), and it also implicitly makes the class abstract as required.
|
||||||
sealed class FSEntry(val name: String)
|
sealed class FSEntry(val name: String) {
|
||||||
|
init {
|
||||||
|
val p = Path(name)
|
||||||
|
// Only allow single filenames (no paths or relative references (e.g. ".."))
|
||||||
|
if (p.toList().size != 1 || p.fileName != p.toFile().canonicalFile.toPath().fileName) {
|
||||||
|
throw InvalidEntryNameException(name)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class FSFile(name: String, val content: String) : FSEntry(name)
|
class FSFile(name: String, val content: String) : FSEntry(name)
|
||||||
|
|
||||||
class FSFolder(name: String, val entries: List<FSEntry>) : FSEntry(name)
|
class FSFolder(name: String, val entries: List<FSEntry>) : FSEntry(name) {
|
||||||
|
/**
|
||||||
|
* Check whether a folder is cyclic.
|
||||||
|
*/
|
||||||
|
fun isCyclic(): Boolean {
|
||||||
|
val seen = listOf(this).toHashSet<FSEntry>()
|
||||||
|
val queue = ArrayDeque(entries)
|
||||||
|
while (queue.isNotEmpty()) {
|
||||||
|
val entry = queue.removeFirst()
|
||||||
|
if (!seen.add(entry)) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if (entry is FSFolder) {
|
||||||
|
queue.addAll(entry.entries)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class InvalidEntryNameException(name: String) : Exception("Invalid FSEntry name: '$name'")
|
|
@ -1,10 +1,10 @@
|
||||||
package filesystem
|
package filesystem
|
||||||
|
|
||||||
import org.junit.jupiter.api.*
|
import org.junit.jupiter.api.*
|
||||||
import java.nio.file.FileSystemException
|
import java.io.File
|
||||||
import java.nio.file.Files
|
|
||||||
import java.nio.file.Path
|
|
||||||
import java.util.concurrent.TimeUnit
|
import java.util.concurrent.TimeUnit
|
||||||
|
import kotlin.io.path.Path
|
||||||
|
import kotlin.io.path.createDirectory
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
|
||||||
|
@ -14,17 +14,26 @@ class FSCreatorTest {
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
fun `before each`() {
|
fun `before each`() {
|
||||||
assertDoesNotThrow("should create _tmp directory") {
|
assertDoesNotThrow("should create _tmp directory") {
|
||||||
Files.createDirectory(Path.of("_tmp"))
|
Path("_tmp").createDirectory()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
fun `after each`() {
|
fun `after each`() {
|
||||||
assertDoesNotThrow("should delete _tmp directory") {
|
assertDoesNotThrow("should delete _tmp directory") {
|
||||||
deleteRecursive(Path.of("_tmp"))
|
File("_tmp").deleteRecursively()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `create file`() {
|
||||||
|
val file = FSFile("test.txt", "This is a file")
|
||||||
|
assertDoesNotThrow("should create file") {
|
||||||
|
creator.create(file, "_tmp")
|
||||||
|
}
|
||||||
|
assertEquals(file.content, File("_tmp/", file.name).readText())
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun `create entries`() {
|
fun `create entries`() {
|
||||||
val readme = FSFile("README", "Hello World!")
|
val readme = FSFile("README", "Hello World!")
|
||||||
|
@ -62,10 +71,10 @@ class FSCreatorTest {
|
||||||
}
|
}
|
||||||
// If objects don't exist, these functions will throw anyway, so don't explicitly check for existence.
|
// If objects don't exist, these functions will throw anyway, so don't explicitly check for existence.
|
||||||
// Similarly, don't explicitly check if an object is a directory.
|
// Similarly, don't explicitly check if an object is a directory.
|
||||||
assertEquals(readme.content, Files.readString(Path.of("_tmp/folder", readme.name)))
|
assertEquals(readme.content, File("_tmp/folder", readme.name).readText())
|
||||||
assertEquals(gomod.content, Files.readString(Path.of("_tmp/folder", gomod.name)))
|
assertEquals(gomod.content, File("_tmp/folder", gomod.name).readText())
|
||||||
assertEquals(maingo.content, Files.readString(Path.of("_tmp/folder", maingo.name)))
|
assertEquals(maingo.content, File("_tmp/folder", maingo.name).readText())
|
||||||
assertEquals(helloworldgo.content, Files.readString(Path.of("_tmp/folder/utils", helloworldgo.name)))
|
assertEquals(helloworldgo.content, File("_tmp/folder/utils", helloworldgo.name).readText())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -81,7 +90,7 @@ class FSCreatorTest {
|
||||||
FSFile("hi", "hi"),
|
FSFile("hi", "hi"),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
FSFolder("another-folder", listOf()),
|
FSFolder("folder", listOf()),
|
||||||
FSFile("1.txt", "One!"),
|
FSFile("1.txt", "One!"),
|
||||||
FSFile("2.txt", "Two!"),
|
FSFile("2.txt", "Two!"),
|
||||||
),
|
),
|
||||||
|
@ -95,7 +104,7 @@ class FSCreatorTest {
|
||||||
"folder",
|
"folder",
|
||||||
listOf(
|
listOf(
|
||||||
FSFolder(
|
FSFolder(
|
||||||
"another-folder",
|
"folder",
|
||||||
listOf(
|
listOf(
|
||||||
FSFolder(
|
FSFolder(
|
||||||
"secrets",
|
"secrets",
|
||||||
|
@ -112,30 +121,35 @@ class FSCreatorTest {
|
||||||
"_tmp",
|
"_tmp",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
assertEquals("hi", Files.readString(Path.of("_tmp/folder/sub-folder/hi")))
|
assertEquals("hi", File("_tmp/folder/sub-folder/hi").readText())
|
||||||
assertEquals("P4ssW0rd", Files.readString(Path.of("_tmp/folder/another-folder/secrets/secret")))
|
assertEquals("P4ssW0rd", File("_tmp/folder/folder/secrets/secret").readText())
|
||||||
assertEquals("One is a good number", Files.readString(Path.of("_tmp/folder/1.txt")))
|
assertEquals("One is a good number", File("_tmp/folder/1.txt").readText())
|
||||||
assertEquals("Two!", Files.readString(Path.of("_tmp/folder/2.txt")))
|
assertEquals("Two!", File("_tmp/folder/2.txt").readText())
|
||||||
assertEquals("Three!", Files.readString(Path.of("_tmp/folder/3.txt")))
|
assertEquals("Three!", File("_tmp/folder/3.txt").readText())
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Timeout(500, unit = TimeUnit.MILLISECONDS) // in case implementation starts trying to handle recursion
|
@Timeout(500, unit = TimeUnit.MILLISECONDS) // in case implementation starts trying to handle cyclic folders
|
||||||
fun `create throws on recursive folder`() {
|
fun `create throws on cyclic folder`() {
|
||||||
val files = mutableListOf<FSEntry>()
|
val files = mutableListOf<FSEntry>()
|
||||||
val folder = FSFolder("folder", files)
|
val folder = FSFolder("folder", files)
|
||||||
files.add(folder)
|
files.add(folder)
|
||||||
assertThrows<FileSystemException> {
|
assertThrows<CyclicFolderException> {
|
||||||
creator.create(folder, "_tmp")
|
creator.create(folder, "_tmp")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
fun deleteRecursive(path: Path) {
|
@Test
|
||||||
if (Files.isDirectory(path)) {
|
@Timeout(500, unit = TimeUnit.MILLISECONDS)
|
||||||
for (child in Files.list(path)) {
|
fun `create throws on long cyclic folder`() {
|
||||||
deleteRecursive(child)
|
val files = mutableListOf<FSEntry>()
|
||||||
|
val folder1 = FSFolder("folder", files)
|
||||||
|
val folder2 = FSFolder("folder2", listOf(folder1))
|
||||||
|
val folder3 = FSFolder("folder3", listOf(folder2))
|
||||||
|
val folder4 = FSFolder("folder4", listOf(folder3))
|
||||||
|
files.add(folder4)
|
||||||
|
assertThrows<CyclicFolderException> {
|
||||||
|
creator.create(folder4, "_tmp")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Files.delete(path)
|
|
||||||
}
|
}
|
|
@ -0,0 +1,42 @@
|
||||||
|
package filesystem
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test
|
||||||
|
import kotlin.test.assertFalse
|
||||||
|
import kotlin.test.assertTrue
|
||||||
|
|
||||||
|
class FSEntryTest {
|
||||||
|
@Test
|
||||||
|
fun `non-cyclic folder`() {
|
||||||
|
val folder =
|
||||||
|
FSFolder(
|
||||||
|
"folder",
|
||||||
|
listOf(
|
||||||
|
FSFolder("folder", listOf()),
|
||||||
|
FSFile("text.txt", "Hello!"),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
assertFalse(folder.isCyclic())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `cyclic folder`() {
|
||||||
|
val files = mutableListOf<FSEntry>()
|
||||||
|
val folder = FSFolder("folder", files)
|
||||||
|
files.add(folder)
|
||||||
|
assertTrue(folder.isCyclic())
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun `long cyclic folder`() {
|
||||||
|
val files = mutableListOf<FSEntry>()
|
||||||
|
val folder1 = FSFolder("folder", files)
|
||||||
|
val folder2 = FSFolder("folder2", listOf(folder1))
|
||||||
|
val folder3 = FSFolder("folder3", listOf(folder2))
|
||||||
|
val folder4 = FSFolder("folder4", listOf(folder3))
|
||||||
|
files.add(folder4)
|
||||||
|
assertTrue(folder1.isCyclic())
|
||||||
|
assertTrue(folder2.isCyclic())
|
||||||
|
assertTrue(folder3.isCyclic())
|
||||||
|
assertTrue(folder4.isCyclic())
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue