mirror of
https://github.com/MightyPirates/OpenComputers.git
synced 2025-09-17 19:25:20 -04:00
Little bit of cleanup and fixed case sensitivity detection being inverted.
This commit is contained in:
parent
d544f9748d
commit
794f3384ec
@ -233,8 +233,10 @@ opencomputers {
|
|||||||
# using the native library.
|
# using the native library.
|
||||||
disableMemoryLimit: false
|
disableMemoryLimit: false
|
||||||
|
|
||||||
# Force the Buffered FileSystem to be case insensitive. This makes it impossible
|
# Force the buffered file system to be case insensitive. This makes it
|
||||||
# To have 2 files whose names only differ by their capitalization
|
# impossible to have multiple files whose names only differ in their
|
||||||
|
# capitalization, which is commonly the case on Windows, for example.
|
||||||
|
# This only takes effect when bufferChanges is set to true.
|
||||||
forceCaseInsensitiveFS: false
|
forceCaseInsensitiveFS: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,6 +2,8 @@ package li.cil.oc.server.fs
|
|||||||
|
|
||||||
import java.io
|
import java.io
|
||||||
import java.net.URL
|
import java.net.URL
|
||||||
|
import java.util.UUID
|
||||||
|
import java.util.logging.Level
|
||||||
|
|
||||||
import li.cil.oc.api.driver.Container
|
import li.cil.oc.api.driver.Container
|
||||||
import li.cil.oc.api.fs.{Label, Mode}
|
import li.cil.oc.api.fs.{Label, Mode}
|
||||||
@ -10,11 +12,29 @@ import li.cil.oc.util.mods.{ComputerCraft15, ComputerCraft16, Mods}
|
|||||||
import li.cil.oc.{OpenComputers, Settings, api}
|
import li.cil.oc.{OpenComputers, Settings, api}
|
||||||
import net.minecraft.nbt.NBTTagCompound
|
import net.minecraft.nbt.NBTTagCompound
|
||||||
import net.minecraftforge.common.DimensionManager
|
import net.minecraftforge.common.DimensionManager
|
||||||
import java.util.UUID
|
|
||||||
|
|
||||||
object FileSystem extends api.detail.FileSystemAPI {
|
object FileSystem extends api.detail.FileSystemAPI {
|
||||||
|
lazy val isCaseInsensitive = Settings.get.forceCaseInsensitive || (try {
|
||||||
lazy val isCaseSensitive = !Settings.get.forceCaseInsensitive && calculateCaseSensitive
|
val uuid = UUID.randomUUID().toString
|
||||||
|
val lowerCase = new io.File(DimensionManager.getCurrentSaveRootDirectory, uuid + "oc_rox")
|
||||||
|
val upperCase = new io.File(DimensionManager.getCurrentSaveRootDirectory, uuid + "OC_ROX")
|
||||||
|
// This should NEVER happen but could also lead to VERY weird bugs, so we
|
||||||
|
// make sure the files don't exist.
|
||||||
|
lowerCase.exists() && lowerCase.delete()
|
||||||
|
upperCase.exists() && upperCase.delete()
|
||||||
|
lowerCase.createNewFile()
|
||||||
|
val insensitive = upperCase.exists()
|
||||||
|
lowerCase.delete()
|
||||||
|
insensitive
|
||||||
|
}
|
||||||
|
catch {
|
||||||
|
case t: Throwable =>
|
||||||
|
// Among the security errors, createNewFile can throw an IOException.
|
||||||
|
// We just fall back to assuming case insensitive, since that's always
|
||||||
|
// safe in those cases.
|
||||||
|
OpenComputers.log.log(Level.WARNING, "Couldn't determine if file system is case sensitive, falling back to insensitive.", t)
|
||||||
|
true
|
||||||
|
})
|
||||||
|
|
||||||
override def fromClass(clazz: Class[_], domain: String, root: String): api.fs.FileSystem = {
|
override def fromClass(clazz: Class[_], domain: String, root: String): api.fs.FileSystem = {
|
||||||
val innerPath = ("/assets/" + domain + "/" + (root.trim + "/")).replace("//", "/")
|
val innerPath = ("/assets/" + domain + "/" + (root.trim + "/")).replace("//", "/")
|
||||||
@ -111,17 +131,6 @@ object FileSystem extends api.detail.FileSystemAPI {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def calculateCaseSensitive = {
|
|
||||||
val uuid = UUID.randomUUID().toString
|
|
||||||
val checkFile1 = new io.File(DimensionManager.getCurrentSaveRootDirectory, uuid + "oc_rox")
|
|
||||||
val checkFile2 = new io.File(DimensionManager.getCurrentSaveRootDirectory, uuid + "OC_ROX")
|
|
||||||
checkFile2.exists() && checkFile2.delete() // this should NEVER happen but could also lead to VERY weird bugs
|
|
||||||
checkFile1.createNewFile()
|
|
||||||
val ret = checkFile2.exists()
|
|
||||||
checkFile1.delete()
|
|
||||||
ret
|
|
||||||
}
|
|
||||||
|
|
||||||
private class ReadOnlyFileSystem(protected val root: io.File)
|
private class ReadOnlyFileSystem(protected val root: io.File)
|
||||||
extends InputStreamFileSystem
|
extends InputStreamFileSystem
|
||||||
with FileInputStreamFileSystem
|
with FileInputStreamFileSystem
|
||||||
@ -150,57 +159,31 @@ object FileSystem extends api.detail.FileSystemAPI {
|
|||||||
override protected def openOutputHandle(id: Int, path: String, mode: Mode) = super.openOutputHandle(id, validatePath(path), mode)
|
override protected def openOutputHandle(id: Int, path: String, mode: Mode) = super.openOutputHandle(id, validatePath(path), mode)
|
||||||
|
|
||||||
protected override def segments(path: String) = {
|
protected override def segments(path: String) = {
|
||||||
super.segments(
|
val parts = super.segments(path)
|
||||||
if (isCaseSensitive) {
|
if (isCaseInsensitive) toCaseInsensitive(parts) else parts
|
||||||
path
|
|
||||||
} else {
|
|
||||||
"/" + toCaseInsensitive(withoutSourroundingSlashes(path), root)
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private def validatePath(path: String) = {
|
private def validatePath(path: String) = {
|
||||||
|
|
||||||
if (path.exists(invalidChars.contains)) {
|
if (path.exists(invalidChars.contains)) {
|
||||||
throw new java.io.IOException("path contains invalid characters")
|
throw new java.io.IOException("path contains invalid characters")
|
||||||
}
|
}
|
||||||
|
|
||||||
path
|
path
|
||||||
}
|
}
|
||||||
|
|
||||||
private def withoutSourroundingSlashes(path: String) = {
|
private def toCaseInsensitive(path: Array[String]): Array[String] = {
|
||||||
val path2 = if (path.startsWith("/"))
|
var node = root
|
||||||
path.substring(1)
|
path.map(segment => {
|
||||||
else
|
assert(node != null, "corrupted virtual file system")
|
||||||
path
|
node.children.find(entry => entry._1.toLowerCase == segment.toLowerCase) match {
|
||||||
if (path2.endsWith("/"))
|
case Some((name, child: VirtualDirectory)) =>
|
||||||
path2.substring(0, path2.length - 1)
|
node = child
|
||||||
else
|
name
|
||||||
path2
|
case Some((name, child: VirtualFile)) =>
|
||||||
}
|
node = null
|
||||||
|
name
|
||||||
private def toCaseInsensitive(path: String, node: VirtualDirectory): String = {
|
case _ => segment
|
||||||
val idx = path.indexOf('/')
|
}
|
||||||
val first = if (idx == -1) path else path.substring(0, idx)
|
})
|
||||||
val rest = if (idx == -1) "" else path.substring(idx + 1)
|
|
||||||
|
|
||||||
val lowerFirst = first.toLowerCase
|
|
||||||
var name = first
|
|
||||||
node.children.foreach {
|
|
||||||
case (childName, child) =>
|
|
||||||
if (childName.toLowerCase == lowerFirst) {
|
|
||||||
child match {
|
|
||||||
case file: VirtualFile =>
|
|
||||||
name = childName + "/" + rest
|
|
||||||
case dir: VirtualDirectory =>
|
|
||||||
name = childName + "/" + toCaseInsensitive(rest, dir)
|
|
||||||
case abc: Object =>
|
|
||||||
OpenComputers.log.warning(s"[WTF] when resolving case insensitive name, child was a ${abc.getClass.getName}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
name
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -133,7 +133,7 @@ trait VirtualFileSystem extends OutputStreamFileSystem {
|
|||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
|
||||||
protected def segments(path: String) = path.split("/").view.filter(_ != "")
|
protected def segments(path: String) = path.split("/").filter(_ != "")
|
||||||
|
|
||||||
// ----------------------------------------------------------------------- //
|
// ----------------------------------------------------------------------- //
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user