blob: 22464ca09fdbddbfd3d0fadfb12c5363c9069372 [file] [log] [blame] [raw]
package li.cil.oc.server.fs
import java.io
trait FileInputStreamFileSystem extends InputStreamFileSystem {
protected def root: io.File
// ----------------------------------------------------------------------- //
override def spaceTotal = spaceUsed
override def spaceUsed = spaceUsed_
private lazy val spaceUsed_ = {
def recurse(path: io.File): Long = {
if (path.isDirectory)
path.listFiles.foldLeft(0L)((acc, f) => acc + recurse(f))
else
path.length
}
recurse(root)
}
// ----------------------------------------------------------------------- //
override def exists(path: String) = new io.File(root, path).exists()
override def size(path: String) = new io.File(root, path) match {
case file if file.isFile => file.length()
case _ => 0L
}
override def isDirectory(path: String) = new io.File(root, path).isDirectory
override def isRealDirectory(path: String) = {
var file = new io.File(root, path)
file.isDirectory() && file.getAbsolutePath().equals(file.getCanonicalPath())
}
override def lastModified(path: String) = new io.File(root, path).lastModified
override def list(path: String) = new io.File(root, path) match {
case file if file.exists() && file.isFile => Array(file.getName)
case directory if directory.exists() && directory.isDirectory =>
//System.err.printf("list(%s)\n", path)
if(path.length > 1024) throw new Exception("path too long " + path)
if(directory.list() == null) new Array[String](0)
else directory.listFiles().map(file => if (file.isDirectory) file.getName + "/" else file.getName)
//case _ => throw new io.FileNotFoundException(path)
//case _ => null
case _ => new Array[String](0)
}
// ----------------------------------------------------------------------- //
override protected def openInputChannel(path: String) = Some(new FileChannel(new io.File(root, path)))
protected class FileChannel(file: io.File) extends InputChannel {
val channel = new io.RandomAccessFile(file, "r").getChannel
override def position(newPosition: Long) = {
channel.position(newPosition)
channel.position
}
override def position = channel.position
override def close() = channel.close()
override def isOpen = channel.isOpen
override def read(dst: Array[Byte]) = channel.read(java.nio.ByteBuffer.wrap(dst))
}
}