can read TEVD but not modify

This commit is contained in:
minjaesong
2022-12-18 02:31:17 +09:00
parent e1e1a2f5db
commit 5fa195800c
4 changed files with 42 additions and 12 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -1,6 +1,8 @@
package net.torvald.tsvm.peripheral
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.EntryDirectory
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.VDUtil
import net.torvald.terrarum.modulecomputers.virtualcomputer.tvd.toCanonicalString
import net.torvald.tsvm.VM
import java.io.*
import java.util.*
@@ -141,6 +143,8 @@ class TevdDiskDrive(private val vm: VM, private val driveNum: Int, private val t
else {
val inputString = inputData.trimNull().toString(VM.CHARSET)
println("[TevDiskDrive] inputString=$inputString")
if (inputString.startsWith("DEVRST\u0017")) {
printdbg("Device Reset")
//readModeLength = -1
@@ -322,16 +326,25 @@ class TevdDiskDrive(private val vm: VM, private val driveNum: Int, private val t
val bootFile = TevdFileDescriptor(DOM, "!BOOTSEC")
println("bootFile = $bootFile, ID: ${bootFile.entryID}, exists = ${bootFile.exists()}")
if (!bootFile.exists()) {
println("bootfile not exists!")
statusCode.set(TestDiskDrive.STATE_CODE_NO_SUCH_FILE_EXISTS)
return
}
try {
val retMsg = bootFile.getHeadBytes(BLOCK_SIZE)
println("retMsg = ${retMsg.toString(VM.CHARSET)}")
recipient?.writeout(retMsg)
statusCode.set(TestDiskDrive.STATE_CODE_STANDBY)
}
catch (e: IOException) {
println("exception:")
e.printStackTrace()
statusCode.set(TestDiskDrive.STATE_CODE_SYSTEM_IO_ERROR)
return
}

View File

@@ -9,18 +9,23 @@ import java.io.IOException
*/
class TevdFileDescriptor(val DOM: VirtualDisk, _pathstr: String) {
val path = _pathstr.replace('/', '\\')
val path = _pathstr.replace('\\', '/')
val vdPath = VDUtil.VDPath(path, VM.CHARSET)
val entryID: EntryID?
get() = VDUtil.getFile(DOM, vdPath)?.entryID
val canonicalPath: String
get() = path
val name: String
get() = path.substring(path.lastIndexOf('\\') + 1)
val nameBytes: ByteArray
get() = name.toByteArray(VM.CHARSET)
val canonicalPath = path.replace('/', '\\')
/*init {
println("$path's vdPath:")
vdPath.hierarchy.forEach {
println(" ${it.toCanonicalString(VM.CHARSET)} [${it.joinToString(" ") { it.toString(16).padStart(2, '0') } }]")
}
}*/
val nameBytes = if (vdPath.hierarchy.isEmpty()) ByteArray(DiskEntry.NAME_LENGTH) else vdPath.last()
val name = nameBytes.toCanonicalString(VM.CHARSET)
val isFile: Boolean
@@ -45,16 +50,28 @@ class TevdFileDescriptor(val DOM: VirtualDisk, _pathstr: String) {
* @return actual bytes read, the size may be less than `length` if the actual file size is smaller
*/
fun getHeadBytes64(length: Long): ByteArray64 {
if (fileContent == null) throw IOException("No such file or not a file")
return fileContent!!.getContent().sliceArray64(0L until length)
if (isDirectory) throw RuntimeException("Not a file")
if (!exists()) throw IOException("File not found")
if (fileContent == null) fileContent = VDUtil.getAsNormalFile(DOM, vdPath)
return fileContent!!.getContent().let {
it.sliceArray64(0L until minOf(length, it.size))
}
}
/**
* @param length how many bytes to read
* @return actual bytes read, the size may be less than `length` if the actual file size is smaller
*/
fun getHeadBytes(length: Int): ByteArray {
if (fileContent == null) throw IOException("No such file or not a file")
return fileContent!!.getContent().sliceArray(0 until length)
if (isDirectory) throw RuntimeException("Not a file")
if (!exists()) throw IOException("File not found")
if (fileContent == null) fileContent = VDUtil.getAsNormalFile(DOM, vdPath)
return fileContent!!.getContent().let {
it.sliceArray(0 until minOf(length.toLong(), it.size).toInt())
}
}
fun exists(): Boolean {
@@ -78,7 +95,7 @@ class TevdFileDescriptor(val DOM: VirtualDisk, _pathstr: String) {
if (it == null) return null
return VDUtil.getDirectoryEntries(DOM, it).map {
TevdFileDescriptor(DOM, path + '\\' + it.getFilenameString(VM.CHARSET))
TevdFileDescriptor(DOM, path + '/' + it.getFilenameString(VM.CHARSET))
}.toTypedArray()
}
}