synchronized functions in zip filesystem and moved try-catch in factory method, hopefully prevents issues in mcpc+; allowing any type of value as os env var for more flexibility where needed

This commit is contained in:
Florian Nücke 2014-02-17 20:04:39 +01:00
parent 5a162c8058
commit 1845ba0e17
3 changed files with 69 additions and 56 deletions

View File

@ -17,7 +17,7 @@ class ZipFileInputStreamFileSystem(private val archive: ArchiveDirectory) extend
def spaceUsed = spaceUsed_
private lazy val spaceUsed_ = {
private lazy val spaceUsed_ = ZipFileInputStreamFileSystem.synchronized {
def recurse(d: ArchiveDirectory): Long = d.children.foldLeft(0L)((acc, c) => acc + (c match {
case directory: ArchiveDirectory => recurse(directory)
case file: ArchiveFile => file.size
@ -27,28 +27,40 @@ class ZipFileInputStreamFileSystem(private val archive: ArchiveDirectory) extend
// ----------------------------------------------------------------------- //
override def exists(path: String) = entry(path).isDefined
override def exists(path: String) = ZipFileInputStreamFileSystem.synchronized {
entry(path).isDefined
}
override def size(path: String) = entry(path) match {
override def size(path: String) = ZipFileInputStreamFileSystem.synchronized {
entry(path) match {
case Some(file) if !file.isDirectory => file.size
case _ => 0L
}
}
override def isDirectory(path: String) = entry(path).exists(_.isDirectory)
override def isDirectory(path: String) = ZipFileInputStreamFileSystem.synchronized {
entry(path).exists(_.isDirectory)
}
def lastModified(path: String) = entry(path) match {
def lastModified(path: String) = ZipFileInputStreamFileSystem.synchronized {
entry(path) match {
case Some(file) => file.lastModified
case _ => 0L
}
}
override def list(path: String) = entry(path) match {
override def list(path: String) = ZipFileInputStreamFileSystem.synchronized {
entry(path) match {
case Some(entry) if entry.isDirectory => entry.list()
case _ => null
}
}
// ----------------------------------------------------------------------- //
override protected def openInputStream(path: String) = entry(path).map(_.openStream())
override protected def openInputStream(path: String) = ZipFileInputStreamFileSystem.synchronized {
entry(path).map(_.openStream())
}
// ----------------------------------------------------------------------- //
@ -65,9 +77,10 @@ object ZipFileInputStreamFileSystem {
asInstanceOf[CacheBuilder[String, ArchiveDirectory]].
build[String, ArchiveDirectory]()
def fromFile(file: io.File, innerPath: String) = this.synchronized {
def fromFile(file: io.File, innerPath: String) = ZipFileInputStreamFileSystem.synchronized {
try {
Option(cache.get(file.getPath + ":" + innerPath, new Callable[ArchiveDirectory] {
def call = try {
def call = {
val zip = new ZipFile(file.getPath)
val cleanedPath = innerPath.stripPrefix("/").stripSuffix("/") + "/"
val rootEntry = zip.getEntry(cleanedPath)
@ -102,15 +115,16 @@ object ZipFileInputStreamFileSystem {
zip.close()
result
}
})) match {
case Some(archive) => new ZipFileInputStreamFileSystem(archive)
case _ => null
}
}
catch {
case e: Throwable =>
OpenComputers.log.log(Level.WARNING, "Failed creating ZIP file system.", e)
null
}
})) match {
case Some(archive) => new ZipFileInputStreamFileSystem(archive)
case _ => null
}
}
abstract class Archive(entry: ZipEntry, root: String) {

View File

@ -29,7 +29,6 @@ end
function os.setenv(varname, value)
checkArg(1, varname, "string")
checkArg(2, value, "string", "nil")
env[varname] = value
return env[varname]
end

View File

@ -511,7 +511,7 @@ function shell.load(path, env, init, name)
return thread
end
function shell.register(thread)
function shell.register(thread) -- called from coroutine.create
checkArg(1, thread, "thread")
if findProcess(thread) then
return false -- already attached somewhere