| author | wenzelm |
| Thu, 12 Nov 2020 11:46:53 +0100 | |
| changeset 72595 | c806eeb9138c |
| parent 72572 | e7e93c0f6d96 |
| child 72698 | 6f83f7892317 |
| 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,
|
|
69402
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
14 |
Files, SimpleFileVisitor, FileVisitOption, FileVisitResult, FileSystemException} |
|
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 |
||
|
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
|
122 |
/* bash path */ |
| 60992 | 123 |
|
| 64304 | 124 |
def bash_path(path: Path): String = Bash.string(standard_path(path)) |
125 |
def bash_path(file: JFile): String = Bash.string(standard_path(file)) |
|
| 60988 | 126 |
|
| 72036 | 127 |
def bash_platform_path(path: Path): String = Bash.string(platform_path(path)) |
128 |
||
| 60988 | 129 |
|
| 62544 | 130 |
/* directory entries */ |
131 |
||
132 |
def check_dir(path: Path): Path = |
|
133 |
if (path.is_dir) path else error("No such directory: " + path)
|
|
134 |
||
135 |
def check_file(path: Path): Path = |
|
136 |
if (path.is_file) path else error("No such file: " + path)
|
|
137 |
||
138 |
||
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
139 |
/* directory content */ |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
140 |
|
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
141 |
def read_dir(dir: Path): List[String] = |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
142 |
{
|
|
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
|
143 |
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
|
144 |
val files = dir.file.listFiles |
|
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
145 |
if (files == null) Nil |
|
69427
ff2f39a221d4
clarified operations: uniform sorting of results;
wenzelm
parents:
69405
diff
changeset
|
146 |
else files.toList.map(_.getName).sorted |
|
62829
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
wenzelm
parents:
62704
diff
changeset
|
147 |
} |
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
148 |
|
| 72442 | 149 |
def get_dir(dir: Path): String = |
150 |
read_dir(dir).filter(name => (dir + Path.basic(name)).is_dir) match {
|
|
151 |
case List(entry) => entry |
|
152 |
case dirs => |
|
153 |
error("Exactly one directory entry expected: " + commas_quote(dirs.sorted))
|
|
154 |
} |
|
155 |
||
| 64932 | 156 |
def find_files( |
157 |
start: JFile, |
|
158 |
pred: JFile => Boolean = _ => true, |
|
|
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
159 |
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
|
160 |
follow_links: Boolean = false): List[JFile] = |
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
161 |
{
|
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
162 |
val result = new mutable.ListBuffer[JFile] |
| 64932 | 163 |
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
|
164 |
|
| 64932 | 165 |
if (start.isFile) check(start) |
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
166 |
else if (start.isDirectory) {
|
|
69293
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
167 |
val options = |
|
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
168 |
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
|
169 |
else EnumSet.noneOf(classOf[FileVisitOption]) |
|
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
wenzelm
parents:
66693
diff
changeset
|
170 |
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
|
171 |
new SimpleFileVisitor[JPath] {
|
| 64932 | 172 |
override def preVisitDirectory(path: JPath, attrs: BasicFileAttributes): FileVisitResult = |
173 |
{
|
|
174 |
if (include_dirs) check(path.toFile) |
|
175 |
FileVisitResult.CONTINUE |
|
176 |
} |
|
|
62443
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
177 |
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
|
178 |
{
|
| 69301 | 179 |
val file = path.toFile |
180 |
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
|
181 |
FileVisitResult.CONTINUE |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
182 |
} |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
183 |
} |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
184 |
) |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
185 |
} |
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
186 |
|
|
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
wenzelm
parents:
62294
diff
changeset
|
187 |
result.toList |
|
48613
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
188 |
} |
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
189 |
|
|
232652ac346e
clarified directory content operations (similar to ML version);
wenzelm
parents:
48550
diff
changeset
|
190 |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
191 |
/* read */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
192 |
|
|
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
|
193 |
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
|
194 |
def read(path: Path): String = read(path.file) |
|
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
wenzelm
parents:
48613
diff
changeset
|
195 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
196 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
197 |
def read_stream(reader: BufferedReader): String = |
|
48411
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 |
val output = new StringBuilder(100) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
200 |
var c = -1 |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
201 |
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
|
202 |
reader.close |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
203 |
output.toString |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
204 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
205 |
|
|
50684
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
206 |
def read_stream(stream: InputStream): String = |
| 64000 | 207 |
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
|
208 |
|
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
209 |
def read_gzip(file: JFile): String = |
|
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
wenzelm
parents:
50203
diff
changeset
|
210 |
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file)))) |
| 64000 | 211 |
def read_gzip(path: Path): String = read_gzip(path.file) |
| 51504 | 212 |
|
| 64002 | 213 |
def read_xz(file: JFile): String = |
214 |
read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file)))) |
|
| 64000 | 215 |
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
|
216 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
217 |
|
| 50845 | 218 |
/* read lines */ |
219 |
||
| 69487 | 220 |
def read_line(reader: BufferedReader): Option[String] = |
221 |
{
|
|
222 |
val line = |
|
223 |
try { reader.readLine}
|
|
224 |
catch { case _: IOException => null }
|
|
| 71601 | 225 |
Option(line) |
| 69487 | 226 |
} |
227 |
||
| 50845 | 228 |
def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = |
229 |
{
|
|
230 |
val result = new mutable.ListBuffer[String] |
|
| 69487 | 231 |
var line: Option[String] = None |
232 |
while ({ line = read_line(reader); line.isDefined }) {
|
|
233 |
progress(line.get) |
|
234 |
result += line.get |
|
| 50845 | 235 |
} |
236 |
reader.close |
|
237 |
result.toList |
|
238 |
} |
|
239 |
||
240 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
241 |
/* write */ |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
242 |
|
| 71534 | 243 |
def writer(file: JFile): BufferedWriter = |
244 |
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF8.charset)) |
|
245 |
||
| 64002 | 246 |
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
|
247 |
{
|
| 51504 | 248 |
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
|
249 |
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
|
250 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
251 |
|
| 64003 | 252 |
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
|
253 |
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
|
254 |
|
| 64002 | 255 |
def write_gzip(file: JFile, text: CharSequence): Unit = |
| 51504 | 256 |
write_file(file, text, (s: OutputStream) => new GZIPOutputStream(new BufferedOutputStream(s))) |
| 48494 | 257 |
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
|
258 |
|
| 64002 | 259 |
def write_xz(file: JFile, text: CharSequence, options: XZ.Options): Unit = |
| 64003 | 260 |
File.write_file(file, text, s => new XZOutputStream(new BufferedOutputStream(s), options)) |
261 |
def write_xz(file: JFile, text: CharSequence): Unit = write_xz(file, text, XZ.options()) |
|
| 64002 | 262 |
def write_xz(path: Path, text: CharSequence, options: XZ.Options): Unit = |
263 |
write_xz(path.file, text, options) |
|
| 64003 | 264 |
def write_xz(path: Path, text: CharSequence): Unit = write_xz(path, text, XZ.options()) |
| 64000 | 265 |
|
| 53336 | 266 |
def write_backup(path: Path, text: CharSequence) |
267 |
{
|
|
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
268 |
if (path.is_file) move(path, path.backup) |
| 62444 | 269 |
write(path, text) |
| 53336 | 270 |
} |
271 |
||
| 58610 | 272 |
def write_backup2(path: Path, text: CharSequence) |
273 |
{
|
|
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
274 |
if (path.is_file) move(path, path.backup2) |
| 62444 | 275 |
write(path, text) |
| 58610 | 276 |
} |
277 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
278 |
|
| 72378 | 279 |
/* change */ |
280 |
||
281 |
def change(file: JFile, f: String => String): Unit = write(file, f(read(file))) |
|
282 |
def change(path: Path, f: String => String): Unit = write(path, f(read(path))) |
|
283 |
||
284 |
||
| 62703 | 285 |
/* append */ |
286 |
||
287 |
def append(file: JFile, text: CharSequence): Unit = |
|
288 |
Files.write(file.toPath, UTF8.bytes(text.toString), |
|
289 |
StandardOpenOption.APPEND, StandardOpenOption.CREATE) |
|
290 |
||
291 |
def append(path: Path, text: CharSequence): Unit = append(path.file, text) |
|
292 |
||
293 |
||
| 64213 | 294 |
/* eq */ |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
295 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
296 |
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
|
297 |
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
|
298 |
catch { case ERROR(_) => false }
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
299 |
|
| 64213 | 300 |
def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file) |
301 |
||
302 |
||
| 64934 | 303 |
/* eq_content */ |
304 |
||
305 |
def eq_content(file1: JFile, file2: JFile): Boolean = |
|
306 |
if (eq(file1, file2)) true |
|
307 |
else if (file1.length != file2.length) false |
|
308 |
else Bytes.read(file1) == Bytes.read(file2) |
|
309 |
||
310 |
def eq_content(path1: Path, path2: Path): Boolean = eq_content(path1.file, path2.file) |
|
311 |
||
312 |
||
| 64213 | 313 |
/* copy */ |
314 |
||
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
315 |
def copy(src: JFile, dst: JFile) |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
316 |
{
|
|
61371
17944b500166
clarified, according to Isabelle_System.copy_file in ML;
wenzelm
parents:
61291
diff
changeset
|
317 |
val target = if (dst.isDirectory) new JFile(dst, src.getName) else dst |
| 61373 | 318 |
if (!eq(src, target)) |
319 |
Files.copy(src.toPath, target.toPath, |
|
320 |
StandardCopyOption.COPY_ATTRIBUTES, |
|
321 |
StandardCopyOption.REPLACE_EXISTING) |
|
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
322 |
} |
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
323 |
|
|
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
324 |
def copy(path1: Path, path2: Path): Unit = copy(path1.file, path2.file) |
| 64213 | 325 |
|
| 72572 | 326 |
def copy_base(base_dir: Path, src0: Path, target_dir: Path) |
327 |
{
|
|
328 |
val src = src0.expand |
|
329 |
val src_dir = src.dir |
|
330 |
if (!src.starts_basic) error("Illegal path specification " + src + " beyond base directory")
|
|
331 |
copy(base_dir + src, Isabelle_System.make_directory(target_dir + src_dir)) |
|
332 |
} |
|
333 |
||
| 64213 | 334 |
|
335 |
/* move */ |
|
336 |
||
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
337 |
def move(src: JFile, dst: JFile) |
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
338 |
{
|
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
339 |
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
|
340 |
if (!eq(src, target)) |
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
341 |
Files.move(src.toPath, target.toPath, StandardCopyOption.REPLACE_EXISTING) |
|
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
342 |
} |
| 64213 | 343 |
|
|
64482
43f6c28ff496
clarified File.move: target directory like File.copy;
wenzelm
parents:
64345
diff
changeset
|
344 |
def move(path1: Path, path2: Path): Unit = move(path1.file, path2.file) |
|
69402
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
345 |
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
346 |
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
347 |
/* symbolic link */ |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
348 |
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
349 |
def link(src: Path, dst: Path, force: Boolean = false) |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
350 |
{
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
351 |
val src_file = src.file |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
352 |
val dst_file = dst.file |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
353 |
val target = if (dst_file.isDirectory) new JFile(dst_file, src_file.getName) else dst_file |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
354 |
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
355 |
if (force) target.delete |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
356 |
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
357 |
try { Files.createSymbolicLink(target.toPath, src_file.toPath) }
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
358 |
catch {
|
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
359 |
case _: UnsupportedOperationException if Platform.is_windows => |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
360 |
Cygwin.link(standard_path(src), target) |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
361 |
case _: FileSystemException if Platform.is_windows => |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
362 |
Cygwin.link(standard_path(src), target) |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
363 |
} |
|
61f4c406d727
more direct File.link operation: avoid external process;
wenzelm
parents:
69393
diff
changeset
|
364 |
} |
|
69405
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
365 |
|
|
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
366 |
|
|
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
367 |
/* permissions */ |
|
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
368 |
|
| 69788 | 369 |
def is_executable(path: Path): Boolean = |
370 |
{
|
|
371 |
if (Platform.is_windows) Isabelle_System.bash("test -x " + bash_path(path)).check.ok
|
|
372 |
else path.file.canExecute |
|
373 |
} |
|
374 |
||
|
69789
2c3e5e58d93f
more thorough File.set_executable, notably for Windows;
wenzelm
parents:
69788
diff
changeset
|
375 |
def set_executable(path: Path, flag: Boolean) |
|
69405
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
376 |
{
|
| 71114 | 377 |
if (Platform.is_windows && flag) Isabelle_System.chmod("a+x", path)
|
378 |
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
|
379 |
else path.file.setExecutable(flag, false) |
|
69405
22428643351f
more direct File.executable operation: avoid external process (on Unix);
wenzelm
parents:
69402
diff
changeset
|
380 |
} |
|
48411
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
wenzelm
parents:
diff
changeset
|
381 |
} |