dummix dumbshell: fixed erratic 'cd' behaviours

Former-commit-id: 82698355d2566d579d2461bc1f1b7a4799f60163
Former-commit-id: 033a2a4f690b8e968e7626c46b998e3bba91031a
This commit is contained in:
Song Minjae
2016-10-04 22:43:58 +09:00
parent 862034b6c1
commit a757b12cab
5 changed files with 49 additions and 22 deletions

View File

@@ -9,6 +9,7 @@ import net.torvald.terrarum.virtualcomputer.computer.BaseTerrarumComputer
import net.torvald.terrarum.virtualcomputer.luaapi.Term.Companion.checkIBM437
import java.io.*
import java.nio.file.Files
import java.nio.file.NoSuchFileException
import java.nio.file.Path
import java.nio.file.Paths
import java.util.*
@@ -145,6 +146,7 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
}
}
/** Don't use this. Use isFile */
class FileExists(val computer: BaseTerrarumComputer) : OneArgFunction() {
override fun call(path: LuaValue) : LuaValue {
Filesystem.ensurePathSanity(path)
@@ -165,7 +167,23 @@ internal class Filesystem(globals: Globals, computer: BaseTerrarumComputer) {
override fun call(path: LuaValue) : LuaValue {
Filesystem.ensurePathSanity(path)
return LuaValue.valueOf(!Files.isDirectory(Paths.get(computer.getRealPath(path)).toAbsolutePath()))
// check if the path is file by checking:
// 1. isfile
// 2. canwrite
// 3. length
// Why? Our Java simply wants to fuck you.
val path = Paths.get(computer.getRealPath(path)).toAbsolutePath()
var result = false
result = Files.isRegularFile(path)
if (!result) result = Files.isWritable(path)
if (!result)
try { result = Files.size(path) > 0 }
catch (e: NoSuchFileException) { result = false }
return LuaValue.valueOf(result)
}
}