author | wenzelm |
Tue, 13 Nov 2018 12:37:46 +0100 | |
changeset 69293 | 72a9860f8602 |
parent 66693 | 02588021b581 |
child 69299 | 2fd070377c99 |
permissions | -rw-r--r-- |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
1 |
/* Title: Pure/General/file.scala |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
2 |
Author: Makarius |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
3 |
|
64698 | 4 |
File-system operations. |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
5 |
*/ |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
6 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
7 |
package isabelle |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
8 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
9 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
10 |
import java.io.{BufferedWriter, OutputStreamWriter, FileOutputStream, BufferedOutputStream, |
51504 | 11 |
OutputStream, InputStream, FileInputStream, BufferedInputStream, BufferedReader, |
12 |
InputStreamReader, File => JFile, IOException} |
|
62703 | 13 |
import java.nio.file.{StandardOpenOption, StandardCopyOption, Path => JPath, |
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
14 |
Files, SimpleFileVisitor, FileVisitOption, FileVisitResult} |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
15 |
import java.nio.file.attribute.BasicFileAttributes |
64775 | 16 |
import java.net.{URL, MalformedURLException} |
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
17 |
import java.util.zip.{GZIPInputStream, GZIPOutputStream} |
60992 | 18 |
import java.util.regex.Pattern |
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
19 |
import java.util.EnumSet |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
20 |
|
64002 | 21 |
import org.tukaani.xz.{XZInputStream, XZOutputStream} |
22 |
||
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
23 |
import scala.collection.mutable |
60992 | 24 |
import scala.util.matching.Regex |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
25 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
26 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
27 |
object File |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
28 |
{ |
60992 | 29 |
/* standard path (Cygwin or Posix) */ |
60988 | 30 |
|
31 |
def standard_path(path: Path): String = path.expand.implode |
|
32 |
||
60992 | 33 |
def standard_path(platform_path: String): String = |
34 |
if (Platform.is_windows) { |
|
35 |
val Platform_Root = new Regex("(?i)" + |
|
61291 | 36 |
Pattern.quote(Isabelle_System.cygwin_root()) + """(?:\\+|\z)(.*)""") |
64775 | 37 |
val Drive = new Regex("""([a-zA-Z]):\\*(.*)""") |
60992 | 38 |
|
39 |
platform_path.replace('/', '\\') match { |
|
40 |
case Platform_Root(rest) => "/" + rest.replace('\\', '/') |
|
41 |
case Drive(letter, rest) => |
|
42 |
"/cygdrive/" + Word.lowercase(letter) + |
|
43 |
(if (rest == "") "" else "/" + rest.replace('\\', '/')) |
|
44 |
case path => path.replace('\\', '/') |
|
45 |
} |
|
46 |
} |
|
47 |
else platform_path |
|
48 |
||
49 |
def standard_path(file: JFile): String = standard_path(file.getPath) |
|
50 |
||
51 |
def standard_url(name: String): String = |
|
52 |
try { |
|
53 |
val url = new URL(name) |
|
64775 | 54 |
if (url.getProtocol == "file" && Url.is_wellformed_file(name)) |
55 |
standard_path(Url.parse_file(name)) |
|
60992 | 56 |
else name |
57 |
} |
|
58 |
catch { case _: MalformedURLException => standard_path(name) } |
|
59 |
||
60 |
||
61 |
/* platform path (Windows or Posix) */ |
|
62 |
||
63 |
private val Cygdrive = new Regex("/cygdrive/([a-zA-Z])($|/.*)") |
|
64 |
private val Named_Root = new Regex("//+([^/]*)(.*)") |
|
65 |
||
66 |
def platform_path(standard_path: String): String = |
|
67 |
if (Platform.is_windows) { |
|
68 |
val result_path = new StringBuilder |
|
69 |
val rest = |
|
70 |
standard_path match { |
|
71 |
case Cygdrive(drive, rest) => |
|
72 |
result_path ++= (Word.uppercase(drive) + ":" + JFile.separator) |
|
73 |
rest |
|
74 |
case Named_Root(root, rest) => |
|
75 |
result_path ++= JFile.separator |
|
76 |
result_path ++= JFile.separator |
|
77 |
result_path ++= root |
|
78 |
rest |
|
79 |
case path if path.startsWith("/") => |
|
61291 | 80 |
result_path ++= Isabelle_System.cygwin_root() |
60992 | 81 |
path |
82 |
case path => path |
|
83 |
} |
|
84 |
for (p <- space_explode('/', rest) if p != "") { |
|
85 |
val len = result_path.length |
|
86 |
if (len > 0 && result_path(len - 1) != JFile.separatorChar) |
|
87 |
result_path += JFile.separatorChar |
|
88 |
result_path ++= p |
|
89 |
} |
|
90 |
result_path.toString |
|
91 |
} |
|
92 |
else standard_path |
|
93 |
||
94 |
def platform_path(path: Path): String = platform_path(standard_path(path)) |
|
60988 | 95 |
def platform_file(path: Path): JFile = new JFile(platform_path(path)) |
96 |
||
60992 | 97 |
|
66232 | 98 |
/* platform files */ |
99 |
||
100 |
def absolute(file: JFile): JFile = file.toPath.toAbsolutePath.normalize.toFile |
|
66233 | 101 |
def absolute_name(file: JFile): String = absolute(file).getPath |
102 |
||
66232 | 103 |
def canonical(file: JFile): JFile = file.getCanonicalFile |
66233 | 104 |
def canonical_name(file: JFile): String = canonical(file).getPath |
66232 | 105 |
|
106 |
def path(file: JFile): Path = Path.explode(standard_path(file)) |
|
107 |
def pwd(): Path = path(Path.current.absolute_file) |
|
108 |
||
109 |
||
66693 | 110 |
/* relative paths */ |
111 |
||
112 |
def relative_path(base: Path, other: Path): Option[Path] = |
|
113 |
{ |
|
114 |
val base_path = base.file.toPath |
|
115 |
val other_path = other.file.toPath |
|
116 |
if (other_path.startsWith(base_path)) |
|
117 |
Some(path(base_path.relativize(other_path).toFile)) |
|
118 |
else None |
|
119 |
} |
|
120 |
||
121 |
def rebase_path(base: Path, other: Path): Option[Path] = |
|
122 |
relative_path(base, other).map(base + _) |
|
123 |
||
124 |
||
62545
8ebffdaf2ce2
Bash.process always uses a closed script instead of an open argument list, for extra robustness on Windows, where quoting is not well-defined;
wenzelm
parents:
62544
diff
changeset
|
125 |
/* bash path */ |
60992 | 126 |
|
64304 | 127 |
def bash_path(path: Path): String = Bash.string(standard_path(path)) |
128 |
def bash_path(file: JFile): String = Bash.string(standard_path(file)) |
|
60988 | 129 |
|
130 |
||
62544 | 131 |
/* directory entries */ |
132 |
||
133 |
def check_dir(path: Path): Path = |
|
134 |
if (path.is_dir) path else error("No such directory: " + path) |
|
135 |
||
136 |
def check_file(path: Path): Path = |
|
137 |
if (path.is_file) path else error("No such file: " + path) |
|
138 |
||
139 |
||
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
140 |
/* directory content */ |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
141 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
142 |
def read_dir(dir: Path): List[String] = |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
143 |
{ |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
144 |
if (!dir.is_dir) error("Bad directory: " + dir.toString) |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
145 |
val files = dir.file.listFiles |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
146 |
if (files == null) Nil |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
147 |
else files.toList.map(_.getName) |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
148 |
} |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
149 |
|
64932 | 150 |
def find_files( |
151 |
start: JFile, |
|
152 |
pred: JFile => Boolean = _ => true, |
|
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
153 |
include_dirs: Boolean = false, |
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
154 |
follow_links: Boolean = true): List[JFile] = |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
155 |
{ |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
156 |
val result = new mutable.ListBuffer[JFile] |
64932 | 157 |
def check(file: JFile) { if (pred(file)) result += file } |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
158 |
|
64932 | 159 |
if (start.isFile) check(start) |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
160 |
else if (start.isDirectory) { |
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
161 |
val options = |
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
162 |
if (follow_links) EnumSet.of(FileVisitOption.FOLLOW_LINKS) |
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
163 |
else EnumSet.noneOf(classOf[FileVisitOption]) |
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
164 |
Files.walkFileTree(start.toPath, options, Integer.MAX_VALUE, |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
165 |
new SimpleFileVisitor[JPath] { |
64932 | 166 |
override def preVisitDirectory(path: JPath, attrs: BasicFileAttributes): FileVisitResult = |
167 |
{ |
|
168 |
if (include_dirs) check(path.toFile) |
|
169 |
FileVisitResult.CONTINUE |
|
170 |
} |
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
171 |
override def visitFile(path: JPath, attrs: BasicFileAttributes): FileVisitResult = |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
172 |
{ |
64932 | 173 |
check(path.toFile) |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
174 |
FileVisitResult.CONTINUE |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
175 |
} |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
176 |
} |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
177 |
) |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
178 |
} |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
179 |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
180 |
result.toList |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
181 |
} |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
182 |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
183 |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
184 |
/* read */ |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
185 |
|
65589
f70c617e9c26
more robust treatment of non-UTF8 text files (cf. 3ed43cfc8b14), notably old log files in ISO-8859-15;
wenzelm
parents:
64934
diff
changeset
|
186 |
def read(file: JFile): String = Bytes.read(file).text |
48913
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
187 |
def read(path: Path): String = read(path.file) |
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
188 |
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
189 |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
190 |
def read_stream(reader: BufferedReader): String = |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
191 |
{ |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
192 |
val output = new StringBuilder(100) |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
193 |
var c = -1 |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
194 |
while ({ c = reader.read; c != -1 }) output += c.toChar |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
195 |
reader.close |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
196 |
output.toString |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
197 |
} |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
198 |
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
199 |
def read_stream(stream: InputStream): String = |
64000 | 200 |
read_stream(new BufferedReader(new InputStreamReader(stream, UTF8.charset))) |
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
201 |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
202 |
def read_gzip(file: JFile): String = |
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
203 |
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) |
64000 | 204 |
def read_gzip(path: Path): String = read_gzip(path.file) |
51504 | 205 |
|
64002 | 206 |
def read_xz(file: JFile): String = |
207 |
read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file)))) |
|
64000 | 208 |
def read_xz(path: Path): String = read_xz(path.file) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
209 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
210 |
|
50845 | 211 |
/* read lines */ |
212 |
||
213 |
def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = |
|
214 |
{ |
|
215 |
val result = new mutable.ListBuffer[String] |
|
216 |
var line: String = null |
|
51251
d55cce4d72dd
more permissive File.read_lines, which is relevant for Managed_Process join/kill;
wenzelm
parents:
50845
diff
changeset
|
217 |
while ({ line = try { reader.readLine} catch { case _: IOException => null }; line != null }) { |
50845 | 218 |
progress(line) |
219 |
result += line |
|
220 |
} |
|
221 |
reader.close |
|
222 |
result.toList |
|
223 |
} |
|
224 |
||
225 |
||
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
226 |
/* write */ |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
227 |
|
64002 | 228 |
def write_file(file: JFile, text: CharSequence, make_stream: OutputStream => OutputStream) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
229 |
{ |
51504 | 230 |
val stream = make_stream(new FileOutputStream(file)) |
231 |
val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset)) |
|
64002 | 232 |
try { writer.append(text) } finally { writer.close } |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
233 |
} |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
234 |
|
64003 | 235 |
def write(file: JFile, text: CharSequence): Unit = write_file(file, text, s => s) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
236 |
def write(path: Path, text: CharSequence): Unit = write(path.file, text) |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
237 |
|
64002 | 238 |
def write_gzip(file: JFile, text: CharSequence): Unit = |
51504 | 239 |
write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s))) |
48494 | 240 |
def write_gzip(path: Path, text: CharSequence): Unit = write_gzip(path.file, text) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
241 |
|
64002 | 242 |
def write_xz(file: JFile, text: CharSequence, options: XZ.Options): Unit = |
64003 | 243 |
File.write_file(file, text, s => new XZOutputStream(new BufferedOutputStream(s), options)) |
244 |
def write_xz(file: JFile, text: CharSequence): Unit = write_xz(file, text, XZ.options()) |
|
64002 | 245 |
def write_xz(path: Path, text: CharSequence, options: XZ.Options): Unit = |
246 |
write_xz(path.file, text, options) |
|
64003 | 247 |
def write_xz(path: Path, text: CharSequence): Unit = write_xz(path, text, XZ.options()) |
64000 | 248 |
|
53336 | 249 |
def write_backup(path: Path, text: CharSequence) |
250 |
{ |
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
251 |
if (path.is_file) move(path, path.backup) |
62444 | 252 |
write(path, text) |
53336 | 253 |
} |
254 |
||
58610 | 255 |
def write_backup2(path: Path, text: CharSequence) |
256 |
{ |
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
257 |
if (path.is_file) move(path, path.backup2) |
62444 | 258 |
write(path, text) |
58610 | 259 |
} |
260 |
||
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
261 |
|
62703 | 262 |
/* append */ |
263 |
||
264 |
def append(file: JFile, text: CharSequence): Unit = |
|
265 |
Files.write(file.toPath, UTF8.bytes(text.toString), |
|
266 |
StandardOpenOption.APPEND, StandardOpenOption.CREATE) |
|
267 |
||
268 |
def append(path: Path, text: CharSequence): Unit = append(path.file, text) |
|
269 |
||
270 |
||
64213 | 271 |
/* eq */ |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
272 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
273 |
def eq(file1: JFile, file2: JFile): Boolean = |
49673
2a088cff1e7b
more robust File.eq, and thus File.copy of "~~/lib/logo/isabelle.gif";
wenzelm
parents:
49610
diff
changeset
|
274 |
try { java.nio.file.Files.isSameFile(file1.toPath, file2.toPath) } |
2a088cff1e7b
more robust File.eq, and thus File.copy of "~~/lib/logo/isabelle.gif";
wenzelm
parents:
49610
diff
changeset
|
275 |
catch { case ERROR(_) => false } |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
276 |
|
64213 | 277 |
def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file) |
278 |
||
279 |
||
64934 | 280 |
/* eq_content */ |
281 |
||
282 |
def eq_content(file1: JFile, file2: JFile): Boolean = |
|
283 |
if (eq(file1, file2)) true |
|
284 |
else if (file1.length != file2.length) false |
|
285 |
else Bytes.read(file1) == Bytes.read(file2) |
|
286 |
||
287 |
def eq_content(path1: Path, path2: Path): Boolean = eq_content(path1.file, path2.file) |
|
288 |
||
289 |
||
64213 | 290 |
/* copy */ |
291 |
||
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
292 |
def copy(src: JFile, dst: JFile) |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
293 |
{ |
61371
17944b500166
clarified, according to Isabelle_System.copy_file in ML;
wenzelm
parents:
61291
diff
changeset
|
294 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
61373 | 295 |
if (!eq(src, target)) |
296 |
Files.copy(src.toPath, target.toPath, |
|
297 |
StandardCopyOption.COPY_ATTRIBUTES, |
|
298 |
StandardCopyOption.REPLACE_EXISTING) |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
299 |
} |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
300 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
301 |
def copy(path1: Path, path2: Path): Unit = copy(path1.file, path2.file) |
64213 | 302 |
|
303 |
||
304 |
/* move */ |
|
305 |
||
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
306 |
def move(src: JFile, dst: JFile) |
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
307 |
{ |
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
308 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
309 |
if (!eq(src, target)) |
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
310 |
Files.move(src.toPath, target.toPath, StandardCopyOption.REPLACE_EXISTING) |
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
311 |
} |
64213 | 312 |
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
313 |
def move(path1: Path, path2: Path): Unit = move(path1.file, path2.file) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
314 |
} |