summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPaul Phillips <paulp@improving.org>2009-09-01 16:59:45 +0000
committerPaul Phillips <paulp@improving.org>2009-09-01 16:59:45 +0000
commit96e5cca150c4b1877b7d5899fb2edc5c7e62d220 (patch)
tree9611c5c26c441e4913948fdc7e15446e8a2127d4 /src
parent5f89d82719324d66bdd27307c10827bbf7af2335 (diff)
downloadscala-96e5cca150c4b1877b7d5899fb2edc5c7e62d220.tar.gz
scala-96e5cca150c4b1877b7d5899fb2edc5c7e62d220.tar.bz2
scala-96e5cca150c4b1877b7d5899fb2edc5c7e62d220.zip
Further organization of File/Path/Directory.
copyFile method.
Diffstat (limited to 'src')
-rw-r--r--src/library/scala/io/Directory.scala1
-rw-r--r--src/library/scala/io/File.scala43
-rw-r--r--src/library/scala/io/Path.scala6
3 files changed, 46 insertions, 4 deletions
diff --git a/src/library/scala/io/Directory.scala b/src/library/scala/io/Directory.scala
index 13e0d15e45..54ee784b4a 100644
--- a/src/library/scala/io/Directory.scala
+++ b/src/library/scala/io/Directory.scala
@@ -33,7 +33,6 @@ class Directory(jfile: JFile) extends Path(jfile)
{
override def toDirectory: Directory = this
override def toFile: File = new File(jfile)
- override def create(): Boolean = jfile.mkdirs()
override def isValid = jfile.isDirectory() || !jfile.exists()
/** An iterator over the contents of this directory.
diff --git a/src/library/scala/io/File.scala b/src/library/scala/io/File.scala
index 0b070868c1..b4131d7e57 100644
--- a/src/library/scala/io/File.scala
+++ b/src/library/scala/io/File.scala
@@ -12,7 +12,7 @@ package scala.io
import java.io.{
FileInputStream, FileOutputStream, BufferedReader, BufferedWriter, InputStreamReader, OutputStreamWriter,
- BufferedInputStream, BufferedOutputStream, File => JFile }
+ BufferedInputStream, BufferedOutputStream, IOException, File => JFile }
import java.nio.channels.FileChannel
import collection.Traversable
@@ -25,7 +25,14 @@ object File
// Create a temporary file
def makeTemp(prefix: String = Path.randomPrefix, suffix: String = null, dir: JFile = null) =
apply(JFile.createTempFile(prefix, suffix, dir))
+
+ import java.nio.channels.Channel
+ type Closeable = { def close(): Unit }
+ def closeQuietly(target: Closeable) {
+ try target.close() catch { case e: IOException => }
+ }
}
+import File._
import Path._
/** An abstraction for files. For character data, a Codec
@@ -45,7 +52,6 @@ with Streamable.Chars
override def toDirectory: Directory = new Directory(jfile)
override def toFile: File = this
- override def create(): Boolean = jfile.createNewFile()
override def isValid = jfile.isFile() || !jfile.exists()
override def length = super[Path].length
@@ -75,5 +81,38 @@ with Streamable.Chars
finally out close
}
+ def copyFile(destPath: Path, preserveFileDate: Boolean = false) = {
+ val FIFTY_MB = 1024 * 1024 * 50
+ val dest = destPath.toFile
+ if (!isValid) fail("Source %s is not a valid file." format name)
+ if (this.normalize == dest.normalize) fail("Source and destination are the same.")
+ if (!dest.parent.map(_.exists).getOrElse(false)) fail("Destination cannot be created.")
+ if (dest.exists && !dest.canWrite) fail("Destination exists but is not writable.")
+ if (dest.isDirectory) fail("Destination exists but is a directory.")
+
+ lazy val in_s = inputStream()
+ lazy val out_s = dest.outputStream()
+ lazy val in = in_s.getChannel()
+ lazy val out = out_s.getChannel()
+
+ try {
+ val size = in.size()
+ var pos, count = 0L
+ while (pos < size) {
+ count = (size - pos) min FIFTY_MB
+ pos += out.transferFrom(in, pos, count)
+ }
+ }
+ finally List[Closeable](out, out_s, in, in_s) foreach closeQuietly
+
+ if (this.length != dest.length)
+ fail("Failed to completely copy %s to %s".format(name, dest.name))
+
+ if (preserveFileDate)
+ dest.lastModified = this.lastModified
+
+ ()
+ }
+
override def toString() = "File(%s)".format(path)
}
diff --git a/src/library/scala/io/Path.scala b/src/library/scala/io/Path.scala
index 258076d0b5..b4b317081b 100644
--- a/src/library/scala/io/Path.scala
+++ b/src/library/scala/io/Path.scala
@@ -125,9 +125,14 @@ class Path private[io] (val jfile: JFile)
def isDirectory = jfile.isDirectory()
def isAbsolute = jfile.isAbsolute()
def isHidden = jfile.isHidden()
+ def isSymlink = parent.isDefined && {
+ val x = parent.get / name
+ x.normalize != x.toAbsolute
+ }
// Information
def lastModified = jfile.lastModified()
+ def lastModified_=(time: Long) = jfile setLastModified time // should use setXXX function?
def length = jfile.length()
// Boolean path comparisons
@@ -137,7 +142,6 @@ class Path private[io] (val jfile: JFile)
def isFresher(other: Path) = lastModified > other.lastModified
// creations
- def create(): Boolean = true
def createDirectory(force: Boolean = true, failIfExists: Boolean = false): Directory = {
val res = if (force) jfile.mkdirs() else jfile.mkdir()
if (!res && exists && failIfExists) fail("Directory '%s' already exists." format name)