author | wenzelm |
Sat, 24 Jul 2021 15:38:41 +0200 | |
changeset 74056 | fb8d5c0133c9 |
parent 73945 | e61add9d5b5e |
child 74811 | 1f40ded31b78 |
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} |
|
73317 | 13 |
import java.nio.file.{StandardOpenOption, Path => JPath, Files, SimpleFileVisitor, |
14 |
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} |
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
18 |
import java.util.EnumSet |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
19 |
|
64002 | 20 |
import org.tukaani.xz.{XZInputStream, XZOutputStream} |
21 |
||
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
22 |
import scala.collection.mutable |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
23 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
24 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
25 |
object File |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
26 |
{ |
60992 | 27 |
/* standard path (Cygwin or Posix) */ |
60988 | 28 |
|
29 |
def standard_path(path: Path): String = path.expand.implode |
|
30 |
||
60992 | 31 |
def standard_path(platform_path: String): String = |
73911 | 32 |
isabelle.setup.Environment.standard_path(platform_path) |
60992 | 33 |
|
34 |
def standard_path(file: JFile): String = standard_path(file.getPath) |
|
35 |
||
36 |
def standard_url(name: String): String = |
|
37 |
try { |
|
38 |
val url = new URL(name) |
|
64775 | 39 |
if (url.getProtocol == "file" && Url.is_wellformed_file(name)) |
40 |
standard_path(Url.parse_file(name)) |
|
60992 | 41 |
else name |
42 |
} |
|
43 |
catch { case _: MalformedURLException => standard_path(name) } |
|
44 |
||
45 |
||
46 |
/* platform path (Windows or Posix) */ |
|
47 |
||
48 |
def platform_path(standard_path: String): String = |
|
73911 | 49 |
isabelle.setup.Environment.platform_path(standard_path) |
60992 | 50 |
|
51 |
def platform_path(path: Path): String = platform_path(standard_path(path)) |
|
60988 | 52 |
def platform_file(path: Path): JFile = new JFile(platform_path(path)) |
53 |
||
60992 | 54 |
|
66232 | 55 |
/* platform files */ |
56 |
||
57 |
def absolute(file: JFile): JFile = file.toPath.toAbsolutePath.normalize.toFile |
|
66233 | 58 |
def absolute_name(file: JFile): String = absolute(file).getPath |
59 |
||
66232 | 60 |
def canonical(file: JFile): JFile = file.getCanonicalFile |
66233 | 61 |
def canonical_name(file: JFile): String = canonical(file).getPath |
66232 | 62 |
|
63 |
def path(file: JFile): Path = Path.explode(standard_path(file)) |
|
64 |
def pwd(): Path = path(Path.current.absolute_file) |
|
65 |
||
66 |
||
66693 | 67 |
/* relative paths */ |
68 |
||
69 |
def relative_path(base: Path, other: Path): Option[Path] = |
|
70 |
{ |
|
73945 | 71 |
val base_path = base.java_path |
72 |
val other_path = other.java_path |
|
66693 | 73 |
if (other_path.startsWith(base_path)) |
74 |
Some(path(base_path.relativize(other_path).toFile)) |
|
75 |
else None |
|
76 |
} |
|
77 |
||
78 |
||
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
|
79 |
/* bash path */ |
60992 | 80 |
|
64304 | 81 |
def bash_path(path: Path): String = Bash.string(standard_path(path)) |
82 |
def bash_path(file: JFile): String = Bash.string(standard_path(file)) |
|
60988 | 83 |
|
72036 | 84 |
def bash_platform_path(path: Path): String = Bash.string(platform_path(path)) |
85 |
||
60988 | 86 |
|
62544 | 87 |
/* directory entries */ |
88 |
||
89 |
def check_dir(path: Path): Path = |
|
90 |
if (path.is_dir) path else error("No such directory: " + path) |
|
91 |
||
92 |
def check_file(path: Path): Path = |
|
93 |
if (path.is_file) path else error("No such file: " + path) |
|
94 |
||
95 |
||
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
96 |
/* directory content */ |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
97 |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
98 |
def read_dir(dir: Path): List[String] = |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
99 |
{ |
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
|
100 |
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
|
101 |
val files = dir.file.listFiles |
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
102 |
if (files == null) Nil |
69427
ff2f39a221d4
clarified operations: uniform sorting of results;
wenzelm
parents:
69405
diff
changeset
|
103 |
else files.toList.map(_.getName).sorted |
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
104 |
} |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
105 |
|
72442 | 106 |
def get_dir(dir: Path): String = |
107 |
read_dir(dir).filter(name => (dir + Path.basic(name)).is_dir) match { |
|
108 |
case List(entry) => entry |
|
109 |
case dirs => |
|
110 |
error("Exactly one directory entry expected: " + commas_quote(dirs.sorted)) |
|
111 |
} |
|
112 |
||
64932 | 113 |
def find_files( |
114 |
start: JFile, |
|
115 |
pred: JFile => Boolean = _ => true, |
|
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
116 |
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
|
117 |
follow_links: Boolean = false): List[JFile] = |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
118 |
{ |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
119 |
val result = new mutable.ListBuffer[JFile] |
73340 | 120 |
def check(file: JFile): Unit = if (pred(file)) result += file |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
121 |
|
64932 | 122 |
if (start.isFile) check(start) |
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
123 |
else if (start.isDirectory) { |
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
124 |
val options = |
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
125 |
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
|
126 |
else EnumSet.noneOf(classOf[FileVisitOption]) |
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
127 |
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
|
128 |
new SimpleFileVisitor[JPath] { |
64932 | 129 |
override def preVisitDirectory(path: JPath, attrs: BasicFileAttributes): FileVisitResult = |
130 |
{ |
|
131 |
if (include_dirs) check(path.toFile) |
|
132 |
FileVisitResult.CONTINUE |
|
133 |
} |
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
134 |
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
|
135 |
{ |
69301 | 136 |
val file = path.toFile |
137 |
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
|
138 |
FileVisitResult.CONTINUE |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
139 |
} |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
140 |
} |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
141 |
) |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
142 |
} |
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
143 |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
144 |
result.toList |
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
145 |
} |
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
146 |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
147 |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
148 |
/* read */ |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
149 |
|
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
|
150 |
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
|
151 |
def read(path: Path): String = read(path.file) |
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
152 |
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
153 |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
154 |
def read_stream(reader: BufferedReader): String = |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
155 |
{ |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
156 |
val output = new StringBuilder(100) |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
157 |
var c = -1 |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
158 |
while ({ c = reader.read; c != -1 }) output += c.toChar |
73367 | 159 |
reader.close() |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
160 |
output.toString |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
161 |
} |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
162 |
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
163 |
def read_stream(stream: InputStream): String = |
64000 | 164 |
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
|
165 |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
166 |
def read_gzip(file: JFile): String = |
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
167 |
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) |
64000 | 168 |
def read_gzip(path: Path): String = read_gzip(path.file) |
51504 | 169 |
|
64002 | 170 |
def read_xz(file: JFile): String = |
171 |
read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file)))) |
|
64000 | 172 |
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
|
173 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
174 |
|
50845 | 175 |
/* read lines */ |
176 |
||
69487 | 177 |
def read_line(reader: BufferedReader): Option[String] = |
178 |
{ |
|
179 |
val line = |
|
180 |
try { reader.readLine} |
|
181 |
catch { case _: IOException => null } |
|
72698 | 182 |
Option(line).map(Library.trim_line) |
69487 | 183 |
} |
184 |
||
50845 | 185 |
def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = |
186 |
{ |
|
187 |
val result = new mutable.ListBuffer[String] |
|
69487 | 188 |
var line: Option[String] = None |
189 |
while ({ line = read_line(reader); line.isDefined }) { |
|
190 |
progress(line.get) |
|
191 |
result += line.get |
|
50845 | 192 |
} |
73367 | 193 |
reader.close() |
50845 | 194 |
result.toList |
195 |
} |
|
196 |
||
197 |
||
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
198 |
/* write */ |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
199 |
|
71534 | 200 |
def writer(file: JFile): BufferedWriter = |
201 |
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF8.charset)) |
|
202 |
||
73340 | 203 |
def write_file( |
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
204 |
file: JFile, text: String, make_stream: OutputStream => OutputStream): Unit = |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
205 |
{ |
51504 | 206 |
val stream = make_stream(new FileOutputStream(file)) |
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69301
diff
changeset
|
207 |
using(new BufferedWriter(new OutputStreamWriter(stream, UTF8.charset)))(_.append(text)) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
208 |
} |
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
209 |
|
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
210 |
def write(file: JFile, text: String): Unit = write_file(file, text, s => s) |
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
211 |
def write(path: Path, text: String): Unit = write(path.file, text) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
212 |
|
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
213 |
def write_gzip(file: JFile, text: String): Unit = |
51504 | 214 |
write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s))) |
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
215 |
def write_gzip(path: Path, text: String): Unit = write_gzip(path.file, text) |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
216 |
|
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
217 |
def write_xz(file: JFile, text: String, options: XZ.Options): Unit = |
64003 | 218 |
File.write_file(file, text, s => new XZOutputStream(new BufferedOutputStream(s), options)) |
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
219 |
def write_xz(file: JFile, text: String): Unit = write_xz(file, text, XZ.options()) |
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
220 |
def write_xz(path: Path, text: String, options: XZ.Options): Unit = |
64002 | 221 |
write_xz(path.file, text, options) |
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
222 |
def write_xz(path: Path, text: String): Unit = write_xz(path, text, XZ.options()) |
64000 | 223 |
|
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
224 |
def write_backup(path: Path, text: String): Unit = |
53336 | 225 |
{ |
73317 | 226 |
if (path.is_file) Isabelle_System.move_file(path, path.backup) |
62444 | 227 |
write(path, text) |
53336 | 228 |
} |
229 |
||
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
230 |
def write_backup2(path: Path, text: String): Unit = |
58610 | 231 |
{ |
73317 | 232 |
if (path.is_file) Isabelle_System.move_file(path, path.backup2) |
62444 | 233 |
write(path, text) |
58610 | 234 |
} |
235 |
||
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
236 |
|
72378 | 237 |
/* change */ |
238 |
||
73627 | 239 |
def change(file: JFile, f: String => String): Unit = |
240 |
{ |
|
241 |
val x = read(file) |
|
242 |
val y = f(x) |
|
243 |
if (x != y) write(file, y) |
|
244 |
} |
|
245 |
||
73660 | 246 |
def change_lines(file: JFile, f: List[String] => List[String]): Unit = |
247 |
change(file, text => cat_lines(f(split_lines(text)))) |
|
248 |
||
73627 | 249 |
def change(path: Path, f: String => String): Unit = change(path.file, f) |
73660 | 250 |
def change_lines(path: Path, f: List[String] => List[String]): Unit = change_lines(path.file, f) |
72378 | 251 |
|
252 |
||
62703 | 253 |
/* append */ |
254 |
||
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
255 |
def append(file: JFile, text: String): Unit = |
73627 | 256 |
Files.write(file.toPath, UTF8.bytes(text), |
62703 | 257 |
StandardOpenOption.APPEND, StandardOpenOption.CREATE) |
258 |
||
73574
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
wenzelm
parents:
73367
diff
changeset
|
259 |
def append(path: Path, text: String): Unit = append(path.file, text) |
62703 | 260 |
|
261 |
||
64213 | 262 |
/* eq */ |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
263 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
264 |
def eq(file1: JFile, file2: JFile): Boolean = |
73318 | 265 |
try { Files.isSameFile(file1.toPath, file2.toPath) } |
49673
2a088cff1e7b
more robust File.eq, and thus File.copy of "~~/lib/logo/isabelle.gif";
wenzelm
parents:
49610
diff
changeset
|
266 |
catch { case ERROR(_) => false } |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
267 |
|
64213 | 268 |
def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file) |
269 |
||
270 |
||
64934 | 271 |
/* eq_content */ |
272 |
||
273 |
def eq_content(file1: JFile, file2: JFile): Boolean = |
|
274 |
if (eq(file1, file2)) true |
|
275 |
else if (file1.length != file2.length) false |
|
276 |
else Bytes.read(file1) == Bytes.read(file2) |
|
277 |
||
278 |
def eq_content(path1: Path, path2: Path): Boolean = eq_content(path1.file, path2.file) |
|
279 |
||
280 |
||
69405
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
281 |
/* permissions */ |
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
282 |
|
69788 | 283 |
def is_executable(path: Path): Boolean = |
284 |
{ |
|
285 |
if (Platform.is_windows) Isabelle_System.bash("test -x " + bash_path(path)).check.ok |
|
286 |
else path.file.canExecute |
|
287 |
} |
|
288 |
||
73340 | 289 |
def set_executable(path: Path, flag: Boolean): Unit = |
69405
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
290 |
{ |
71114 | 291 |
if (Platform.is_windows && flag) Isabelle_System.chmod("a+x", path) |
292 |
else if (Platform.is_windows) Isabelle_System.chmod("a-x", path) |
|
69789
2c3e5e58d93f
more thorough File.set_executable, notably for Windows;
wenzelm
parents:
69788
diff
changeset
|
293 |
else path.file.setExecutable(flag, false) |
69405
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
294 |
} |
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
295 |
} |