1
/* NSC -- new Scala compiler
2
* Copyright 2005-2011 LAMP/EPFL
3
* @author Martin Odersky
7
package scala.tools.nsc
10
import java.io.{ FileInputStream, FileOutputStream, IOException }
11
import PartialFunction._
15
* If the specified File exists, returns an abstract file backed
16
* by it. Otherwise, returns null.
18
def fromPath(file: Path): PlainFile =
19
if (file.isDirectory) new PlainDirectory(file.toDirectory)
20
else if (file.isFile) new PlainFile(file)
24
class PlainDirectory(givenPath: Directory) extends PlainFile(givenPath) {
25
override def isDirectory = true
26
override def iterator = givenPath.list filter (_.exists) map (x => new PlainFile(x))
27
override def delete(): Unit = givenPath.deleteRecursively()
30
/** This class implements an abstract file backed by a File.
32
class PlainFile(val givenPath: Path) extends AbstractFile {
35
val file = givenPath.jfile
36
override def underlyingSource = Some(this)
38
private val fpath = givenPath.toAbsolute
40
/** Returns the name of this abstract file. */
41
def name = givenPath.name
43
/** Returns the path of this abstract file. */
44
def path = givenPath.path
46
/** The absolute file. */
47
def absolute = new PlainFile(givenPath.toAbsolute)
49
override def container: AbstractFile = new PlainFile(givenPath.parent)
50
override def input = givenPath.toFile.inputStream()
51
override def output = givenPath.toFile.outputStream()
52
override def sizeOption = Some(givenPath.length.toInt)
54
override def hashCode(): Int = fpath.hashCode
55
override def equals(that: Any): Boolean = that match {
56
case x: PlainFile => fpath == x.fpath
60
/** Is this abstract file a directory? */
61
def isDirectory: Boolean = givenPath.isDirectory
63
/** Returns the time that this abstract file was last modified. */
64
def lastModified: Long = givenPath.lastModified
66
/** Returns all abstract subfiles of this abstract directory. */
67
def iterator: Iterator[AbstractFile] = {
68
if (!isDirectory) Iterator.empty
69
else givenPath.toDirectory.list filter (_.exists) map (new PlainFile(_))
73
* Returns the abstract file in this abstract directory with the
74
* specified name. If there is no such file, returns null. The
75
* argument "directory" tells whether to look for a directory or
79
* @param directory ...
82
def lookupName(name: String, directory: Boolean): AbstractFile = {
83
val child = givenPath / name
84
if ((child.isDirectory && directory) || (child.isFile && !directory)) new PlainFile(child)
88
/** Does this abstract file denote an existing file? */
89
def create(): Unit = if (!exists) givenPath.createFile()
91
/** Delete the underlying file or directory (recursively). */
93
if (givenPath.isFile) givenPath.delete()
94
else if (givenPath.isDirectory) givenPath.toDirectory.deleteRecursively()
96
/** Returns a plain file with the given name. It does not
97
* check that it exists.
99
def lookupNameUnchecked(name: String, directory: Boolean): AbstractFile =
100
new PlainFile(givenPath / name)