| author | wenzelm |
| Fri, 23 Nov 2018 21:50:50 +0100 | |
| changeset 69336 | 14444ea196a0 |
| parent 69301 | f6c17b9e1104 |
| child 69393 | ed0824ef337e |
| 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 |
{
|
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69299
diff
changeset
|
144 |
if (!dir.is_dir) error("No such directory: " + dir.toString)
|
|
62829
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, |
|
69299
2fd070377c99
clarified default (amending 72a9860f8602): avoid implicit change of File.find_files (it can have bad effects e.g. on "isabelle update_cartouches");
wenzelm
parents:
69293
diff
changeset
|
154 |
follow_links: Boolean = false): 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 |
{
|
| 69301 | 173 |
val file = path.toFile |
174 |
if (include_dirs || !file.isDirectory) check(file) |
|
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
175 |
FileVisitResult.CONTINUE |
|
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 |
|
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
181 |
result.toList |
|
48613
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 |
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
184 |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
185 |
/* read */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
186 |
|
|
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
|
187 |
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
|
188 |
def read(path: Path): String = read(path.file) |
|
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
189 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
190 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
191 |
def read_stream(reader: BufferedReader): String = |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
192 |
{
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
193 |
val output = new StringBuilder(100) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
194 |
var c = -1 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
195 |
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
|
196 |
reader.close |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
197 |
output.toString |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
198 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
199 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
200 |
def read_stream(stream: InputStream): String = |
| 64000 | 201 |
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
|
202 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
203 |
def read_gzip(file: JFile): String = |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
204 |
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) |
| 64000 | 205 |
def read_gzip(path: Path): String = read_gzip(path.file) |
| 51504 | 206 |
|
| 64002 | 207 |
def read_xz(file: JFile): String = |
208 |
read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file)))) |
|
| 64000 | 209 |
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
|
210 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
211 |
|
| 50845 | 212 |
/* read lines */ |
213 |
||
214 |
def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = |
|
215 |
{
|
|
216 |
val result = new mutable.ListBuffer[String] |
|
217 |
var line: String = null |
|
|
51251
d55cce4d72dd
more permissive File.read_lines, which is relevant for Managed_Process join/kill;
wenzelm
parents:
50845
diff
changeset
|
218 |
while ({ line = try { reader.readLine} catch { case _: IOException => null }; line != null }) {
|
| 50845 | 219 |
progress(line) |
220 |
result += line |
|
221 |
} |
|
222 |
reader.close |
|
223 |
result.toList |
|
224 |
} |
|
225 |
||
226 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
227 |
/* write */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
228 |
|
| 64002 | 229 |
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
|
230 |
{
|
| 51504 | 231 |
val stream = make_stream(new FileOutputStream(file)) |
232 |
val writer = new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset)) |
|
| 64002 | 233 |
try { writer.append(text) } finally { writer.close }
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
234 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
235 |
|
| 64003 | 236 |
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
|
237 |
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
|
238 |
|
| 64002 | 239 |
def write_gzip(file: JFile, text: CharSequence): Unit = |
| 51504 | 240 |
write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s))) |
| 48494 | 241 |
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
|
242 |
|
| 64002 | 243 |
def write_xz(file: JFile, text: CharSequence, options: XZ.Options): Unit = |
| 64003 | 244 |
File.write_file(file, text, s => new XZOutputStream(new BufferedOutputStream(s), options)) |
245 |
def write_xz(file: JFile, text: CharSequence): Unit = write_xz(file, text, XZ.options()) |
|
| 64002 | 246 |
def write_xz(path: Path, text: CharSequence, options: XZ.Options): Unit = |
247 |
write_xz(path.file, text, options) |
|
| 64003 | 248 |
def write_xz(path: Path, text: CharSequence): Unit = write_xz(path, text, XZ.options()) |
| 64000 | 249 |
|
| 53336 | 250 |
def write_backup(path: Path, text: CharSequence) |
251 |
{
|
|
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
252 |
if (path.is_file) move(path, path.backup) |
| 62444 | 253 |
write(path, text) |
| 53336 | 254 |
} |
255 |
||
| 58610 | 256 |
def write_backup2(path: Path, text: CharSequence) |
257 |
{
|
|
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
258 |
if (path.is_file) move(path, path.backup2) |
| 62444 | 259 |
write(path, text) |
| 58610 | 260 |
} |
261 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
262 |
|
| 62703 | 263 |
/* append */ |
264 |
||
265 |
def append(file: JFile, text: CharSequence): Unit = |
|
266 |
Files.write(file.toPath, UTF8.bytes(text.toString), |
|
267 |
StandardOpenOption.APPEND, StandardOpenOption.CREATE) |
|
268 |
||
269 |
def append(path: Path, text: CharSequence): Unit = append(path.file, text) |
|
270 |
||
271 |
||
| 64213 | 272 |
/* eq */ |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
273 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
274 |
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
|
275 |
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
|
276 |
catch { case ERROR(_) => false }
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
277 |
|
| 64213 | 278 |
def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file) |
279 |
||
280 |
||
| 64934 | 281 |
/* eq_content */ |
282 |
||
283 |
def eq_content(file1: JFile, file2: JFile): Boolean = |
|
284 |
if (eq(file1, file2)) true |
|
285 |
else if (file1.length != file2.length) false |
|
286 |
else Bytes.read(file1) == Bytes.read(file2) |
|
287 |
||
288 |
def eq_content(path1: Path, path2: Path): Boolean = eq_content(path1.file, path2.file) |
|
289 |
||
290 |
||
| 64213 | 291 |
/* copy */ |
292 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
293 |
def copy(src: JFile, dst: JFile) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
294 |
{
|
|
61371
17944b500166
clarified, according to Isabelle_System.copy_file in ML;
wenzelm
parents:
61291
diff
changeset
|
295 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
| 61373 | 296 |
if (!eq(src, target)) |
297 |
Files.copy(src.toPath, target.toPath, |
|
298 |
StandardCopyOption.COPY_ATTRIBUTES, |
|
299 |
StandardCopyOption.REPLACE_EXISTING) |
|
|
48411
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 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
302 |
def copy(path1: Path, path2: Path): Unit = copy(path1.file, path2.file) |
| 64213 | 303 |
|
304 |
||
305 |
/* move */ |
|
306 |
||
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
307 |
def move(src: JFile, dst: JFile) |
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
308 |
{
|
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
309 |
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
|
310 |
if (!eq(src, target)) |
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
311 |
Files.move(src.toPath, target.toPath, StandardCopyOption.REPLACE_EXISTING) |
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
312 |
} |
| 64213 | 313 |
|
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
314 |
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
|
315 |
} |