| author | wenzelm | 
| Tue, 24 Jan 2023 17:16:00 +0100 | |
| changeset 77075 | 973de7855948 | 
| parent 76884 | a004c5322ea4 | 
| child 77109 | e3a2b3536030 | 
| 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  | 
| 75701 | 16  | 
import java.net.{URI, 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  | 
|
| 76353 | 20  | 
import org.tukaani.xz  | 
21  | 
import com.github.luben.zstd  | 
|
| 76348 | 22  | 
|
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
23  | 
import scala.collection.mutable  | 
| 
 
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  | 
|
| 75393 | 26  | 
object File {
 | 
| 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  | 
|
| 76884 | 55  | 
/* symbolic path representation, e.g. "~~/src/Pure/ROOT.ML" */  | 
56  | 
||
57  | 
  def symbolic_path(path: Path): String = {
 | 
|
58  | 
val directories =  | 
|
59  | 
      Library.space_explode(':', Isabelle_System.getenv("ISABELLE_DIRECTORIES")).reverse
 | 
|
60  | 
val full_name = standard_path(path)  | 
|
61  | 
directories.view.flatMap(a =>  | 
|
62  | 
      try {
 | 
|
63  | 
val b = standard_path(Path.explode(a))  | 
|
64  | 
if (full_name == b) Some(a)  | 
|
65  | 
        else {
 | 
|
66  | 
          Library.try_unprefix(b + "/", full_name) match {
 | 
|
67  | 
case Some(name) => Some(a + "/" + name)  | 
|
68  | 
case None => None  | 
|
69  | 
}  | 
|
70  | 
}  | 
|
71  | 
      } catch { case ERROR(_) => None }).headOption.getOrElse(path.implode)
 | 
|
72  | 
}  | 
|
73  | 
||
74  | 
||
| 66232 | 75  | 
/* platform files */  | 
76  | 
||
77  | 
def absolute(file: JFile): JFile = file.toPath.toAbsolutePath.normalize.toFile  | 
|
| 66233 | 78  | 
def absolute_name(file: JFile): String = absolute(file).getPath  | 
79  | 
||
| 66232 | 80  | 
def canonical(file: JFile): JFile = file.getCanonicalFile  | 
| 66233 | 81  | 
def canonical_name(file: JFile): String = canonical(file).getPath  | 
| 66232 | 82  | 
|
83  | 
def path(file: JFile): Path = Path.explode(standard_path(file))  | 
|
| 76546 | 84  | 
def path(java_path: JPath): Path = path(java_path.toFile)  | 
85  | 
||
| 66232 | 86  | 
def pwd(): Path = path(Path.current.absolute_file)  | 
87  | 
||
| 75701 | 88  | 
def uri(file: JFile): URI = file.toURI  | 
89  | 
def uri(path: Path): URI = path.file.toURI  | 
|
90  | 
||
91  | 
def url(file: JFile): URL = uri(file).toURL  | 
|
92  | 
def url(path: Path): URL = url(path.file)  | 
|
93  | 
||
| 66232 | 94  | 
|
| 
75906
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
95  | 
/* adhoc file types */  | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
96  | 
|
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
97  | 
  def is_ML(s: String): Boolean = s.endsWith(".ML")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
98  | 
  def is_bib(s: String): Boolean = s.endsWith(".bib")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
99  | 
  def is_dll(s: String): Boolean = s.endsWith(".dll")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
100  | 
  def is_exe(s: String): Boolean = s.endsWith(".exe")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
101  | 
  def is_gz(s: String): Boolean = s.endsWith(".gz")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
102  | 
  def is_html(s: String): Boolean = s.endsWith(".html")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
103  | 
  def is_jar(s: String): Boolean = s.endsWith(".jar")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
104  | 
  def is_java(s: String): Boolean = s.endsWith(".java")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
105  | 
  def is_node(s: String): Boolean = s.endsWith(".node")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
106  | 
  def is_pdf(s: String): Boolean = s.endsWith(".pdf")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
107  | 
  def is_png(s: String): Boolean = s.endsWith(".png")
 | 
| 
76540
 
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
 
wenzelm 
parents: 
76533 
diff
changeset
 | 
108  | 
  def is_tar_bz2(s: String): Boolean = s.endsWith(".tar.bz2")
 | 
| 76533 | 109  | 
  def is_tar_gz(s: String): Boolean = s.endsWith(".tar.gz")
 | 
| 
76540
 
83de6e9ae983
clarified signature: prefer Scala functions instead of shell scripts;
 
wenzelm 
parents: 
76533 
diff
changeset
 | 
110  | 
  def is_tgz(s: String): Boolean = s.endsWith(".tgz")
 | 
| 
75906
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
111  | 
  def is_thy(s: String): Boolean = s.endsWith(".thy")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
112  | 
  def is_xz(s: String): Boolean = s.endsWith(".xz")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
113  | 
  def is_zip(s: String): Boolean = s.endsWith(".zip")
 | 
| 76348 | 114  | 
  def is_zst(s: String): Boolean = s.endsWith(".zst")
 | 
| 
75906
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
115  | 
|
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
116  | 
  def is_backup(s: String): Boolean = s.endsWith("~") || s.endsWith(".orig")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
117  | 
|
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
118  | 
|
| 66693 | 119  | 
/* relative paths */  | 
120  | 
||
| 75393 | 121  | 
  def relative_path(base: Path, other: Path): Option[Path] = {
 | 
| 73945 | 122  | 
val base_path = base.java_path  | 
123  | 
val other_path = other.java_path  | 
|
| 66693 | 124  | 
if (other_path.startsWith(base_path))  | 
125  | 
Some(path(base_path.relativize(other_path).toFile))  | 
|
126  | 
else None  | 
|
127  | 
}  | 
|
128  | 
||
129  | 
||
| 
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
 | 
130  | 
/* bash path */  | 
| 60992 | 131  | 
|
| 64304 | 132  | 
def bash_path(path: Path): String = Bash.string(standard_path(path))  | 
133  | 
def bash_path(file: JFile): String = Bash.string(standard_path(file))  | 
|
| 60988 | 134  | 
|
| 72036 | 135  | 
def bash_platform_path(path: Path): String = Bash.string(platform_path(path))  | 
136  | 
||
| 60988 | 137  | 
|
| 62544 | 138  | 
/* directory entries */  | 
139  | 
||
140  | 
def check_dir(path: Path): Path =  | 
|
141  | 
    if (path.is_dir) path else error("No such directory: " + path)
 | 
|
142  | 
||
143  | 
def check_file(path: Path): Path =  | 
|
144  | 
    if (path.is_file) path else error("No such file: " + path)
 | 
|
145  | 
||
146  | 
||
| 
62829
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
147  | 
/* directory content */  | 
| 
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
148  | 
|
| 75393 | 149  | 
  def read_dir(dir: Path): List[String] = {
 | 
| 
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
 | 
150  | 
    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
 | 
151  | 
val files = dir.file.listFiles  | 
| 
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
152  | 
if (files == null) Nil  | 
| 
69427
 
ff2f39a221d4
clarified operations: uniform sorting of results;
 
wenzelm 
parents: 
69405 
diff
changeset
 | 
153  | 
else files.toList.map(_.getName).sorted  | 
| 
62829
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
154  | 
}  | 
| 
48613
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
155  | 
|
| 76529 | 156  | 
def get_entry(  | 
157  | 
dir: Path,  | 
|
158  | 
pred: Path => Boolean = _ => true,  | 
|
159  | 
title: String = ""  | 
|
160  | 
): Path =  | 
|
161  | 
    read_dir(dir).filter(name => pred(dir + Path.basic(name))) match {
 | 
|
162  | 
case List(entry) => dir + Path.basic(entry)  | 
|
163  | 
case bad =>  | 
|
164  | 
        error("Bad directory content in " + (if (title.nonEmpty) title else dir.toString) +
 | 
|
165  | 
"\nexpected a single entry, but found" +  | 
|
166  | 
(if (bad.isEmpty) " nothing"  | 
|
167  | 
           else bad.sorted.map(quote).mkString(":\n  ", "\n  ", "")))
 | 
|
| 72442 | 168  | 
}  | 
169  | 
||
| 76529 | 170  | 
def get_file(dir: Path, title: String = ""): Path =  | 
171  | 
get_entry(dir, pred = _.is_file, title = title)  | 
|
172  | 
||
173  | 
def get_dir(dir: Path, title: String = ""): Path =  | 
|
174  | 
get_entry(dir, pred = _.is_dir, title = title)  | 
|
175  | 
||
| 64932 | 176  | 
def find_files(  | 
177  | 
start: JFile,  | 
|
178  | 
pred: JFile => Boolean = _ => true,  | 
|
| 
69293
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
179  | 
include_dirs: Boolean = false,  | 
| 75393 | 180  | 
follow_links: Boolean = false  | 
181  | 
  ): List[JFile] = {
 | 
|
| 
62443
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
182  | 
val result = new mutable.ListBuffer[JFile]  | 
| 73340 | 183  | 
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
 | 
184  | 
|
| 64932 | 185  | 
if (start.isFile) check(start)  | 
| 
62443
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
186  | 
    else if (start.isDirectory) {
 | 
| 
69293
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
187  | 
val options =  | 
| 
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
188  | 
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
 | 
189  | 
else EnumSet.noneOf(classOf[FileVisitOption])  | 
| 
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
190  | 
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
 | 
191  | 
        new SimpleFileVisitor[JPath] {
 | 
| 75393 | 192  | 
override def preVisitDirectory(  | 
193  | 
path: JPath,  | 
|
194  | 
attrs: BasicFileAttributes  | 
|
195  | 
          ): FileVisitResult = {
 | 
|
| 64932 | 196  | 
if (include_dirs) check(path.toFile)  | 
197  | 
FileVisitResult.CONTINUE  | 
|
198  | 
}  | 
|
| 75393 | 199  | 
override def visitFile(  | 
200  | 
path: JPath,  | 
|
201  | 
attrs: BasicFileAttributes  | 
|
202  | 
          ): FileVisitResult = {
 | 
|
| 69301 | 203  | 
val file = path.toFile  | 
204  | 
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
 | 
205  | 
FileVisitResult.CONTINUE  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
206  | 
}  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
207  | 
}  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
208  | 
)  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
209  | 
}  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
210  | 
|
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
211  | 
result.toList  | 
| 
48613
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
212  | 
}  | 
| 
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
213  | 
|
| 
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
214  | 
|
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
215  | 
/* read */  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
216  | 
|
| 
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
 | 
217  | 
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
 | 
218  | 
def read(path: Path): String = read(path.file)  | 
| 
 
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
 
wenzelm 
parents: 
48613 
diff
changeset
 | 
219  | 
|
| 
50684
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
220  | 
|
| 75393 | 221  | 
  def read_stream(reader: BufferedReader): String = {
 | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
222  | 
val output = new StringBuilder(100)  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
223  | 
var c = -1  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
224  | 
    while ({ c = reader.read; c != -1 }) output += c.toChar
 | 
| 73367 | 225  | 
reader.close()  | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
226  | 
output.toString  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
227  | 
}  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
228  | 
|
| 
50684
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
229  | 
def read_stream(stream: InputStream): String =  | 
| 64000 | 230  | 
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
 | 
231  | 
|
| 
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
232  | 
def read_gzip(file: JFile): String =  | 
| 
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
233  | 
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))))  | 
| 64000 | 234  | 
def read_gzip(path: Path): String = read_gzip(path.file)  | 
| 51504 | 235  | 
|
| 64002 | 236  | 
def read_xz(file: JFile): String =  | 
| 76353 | 237  | 
read_stream(new xz.XZInputStream(new BufferedInputStream(new FileInputStream(file))))  | 
| 64000 | 238  | 
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
 | 
239  | 
|
| 76348 | 240  | 
  def read_zstd(file: JFile): String = {
 | 
| 
76349
 
b4daf7577ca0
clarified Zstd.init(): avoid accidential com.github.luben.zstd.util.Native.load() operation;
 
wenzelm 
parents: 
76348 
diff
changeset
 | 
241  | 
Zstd.init()  | 
| 76353 | 242  | 
read_stream(new zstd.ZstdInputStream(new BufferedInputStream(new FileInputStream(file))))  | 
| 76348 | 243  | 
}  | 
244  | 
def read_zstd(path: Path): String = read_zstd(path.file)  | 
|
245  | 
||
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
246  | 
|
| 50845 | 247  | 
/* read lines */  | 
248  | 
||
| 75393 | 249  | 
  def read_line(reader: BufferedReader): Option[String] = {
 | 
| 69487 | 250  | 
val line =  | 
251  | 
      try { reader.readLine}
 | 
|
252  | 
      catch { case _: IOException => null }
 | 
|
| 72698 | 253  | 
Option(line).map(Library.trim_line)  | 
| 69487 | 254  | 
}  | 
255  | 
||
| 75393 | 256  | 
  def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = {
 | 
| 50845 | 257  | 
val result = new mutable.ListBuffer[String]  | 
| 69487 | 258  | 
var line: Option[String] = None  | 
259  | 
    while ({ line = read_line(reader); line.isDefined }) {
 | 
|
260  | 
progress(line.get)  | 
|
261  | 
result += line.get  | 
|
| 50845 | 262  | 
}  | 
| 73367 | 263  | 
reader.close()  | 
| 50845 | 264  | 
result.toList  | 
265  | 
}  | 
|
266  | 
||
267  | 
||
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
268  | 
/* write */  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
269  | 
|
| 71534 | 270  | 
def writer(file: JFile): BufferedWriter =  | 
271  | 
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF8.charset))  | 
|
272  | 
||
| 73340 | 273  | 
def write_file(  | 
| 75393 | 274  | 
file: JFile,  | 
275  | 
text: String,  | 
|
276  | 
make_stream: OutputStream => OutputStream  | 
|
277  | 
  ): Unit = {
 | 
|
| 51504 | 278  | 
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
 | 
279  | 
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
 | 
280  | 
}  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
281  | 
|
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
282  | 
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
 | 
283  | 
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
 | 
284  | 
|
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
285  | 
def write_gzip(file: JFile, text: String): Unit =  | 
| 51504 | 286  | 
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
 | 
287  | 
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
 | 
288  | 
|
| 
76351
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
289  | 
def write_xz(file: JFile, text: String, options: Compress.Options_XZ): Unit =  | 
| 76353 | 290  | 
File.write_file(file, text,  | 
291  | 
s => new xz.XZOutputStream(new BufferedOutputStream(s), options.make))  | 
|
| 
76351
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
292  | 
def write_xz(file: JFile, text: String): Unit = write_xz(file, text, Compress.Options_XZ())  | 
| 
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
293  | 
def write_xz(path: Path, text: String, options: Compress.Options_XZ): Unit =  | 
| 64002 | 294  | 
write_xz(path.file, text, options)  | 
| 
76351
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
295  | 
def write_xz(path: Path, text: String): Unit = write_xz(path, text, Compress.Options_XZ())  | 
| 64000 | 296  | 
|
| 
76351
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
297  | 
  def write_zstd(file: JFile, text: String, options: Compress.Options_Zstd): Unit = {
 | 
| 
76349
 
b4daf7577ca0
clarified Zstd.init(): avoid accidential com.github.luben.zstd.util.Native.load() operation;
 
wenzelm 
parents: 
76348 
diff
changeset
 | 
298  | 
Zstd.init()  | 
| 76353 | 299  | 
File.write_file(file, text,  | 
300  | 
s => new zstd.ZstdOutputStream(new BufferedOutputStream(s), options.level))  | 
|
| 76348 | 301  | 
}  | 
| 
76351
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
302  | 
def write_zstd(file: JFile, text: String): Unit =  | 
| 
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
303  | 
write_zstd(file, text, Compress.Options_Zstd())  | 
| 
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
304  | 
def write_zstd(path: Path, text: String, options: Compress.Options_Zstd): Unit =  | 
| 
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
305  | 
write_zstd(path.file, text, options)  | 
| 
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
306  | 
def write_zstd(path: Path, text: String): Unit =  | 
| 
 
2cee31cd92f0
generic support for XZ and Zstd compression in Isabelle/Scala;
 
wenzelm 
parents: 
76349 
diff
changeset
 | 
307  | 
write_zstd(path, text, Compress.Options_Zstd())  | 
| 76348 | 308  | 
|
| 75393 | 309  | 
  def write_backup(path: Path, text: String): Unit = {
 | 
| 73317 | 310  | 
if (path.is_file) Isabelle_System.move_file(path, path.backup)  | 
| 62444 | 311  | 
write(path, text)  | 
| 53336 | 312  | 
}  | 
313  | 
||
| 75393 | 314  | 
  def write_backup2(path: Path, text: String): Unit = {
 | 
| 73317 | 315  | 
if (path.is_file) Isabelle_System.move_file(path, path.backup2)  | 
| 62444 | 316  | 
write(path, text)  | 
| 58610 | 317  | 
}  | 
318  | 
||
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
319  | 
|
| 62703 | 320  | 
/* append */  | 
321  | 
||
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
322  | 
def append(file: JFile, text: String): Unit =  | 
| 73627 | 323  | 
Files.write(file.toPath, UTF8.bytes(text),  | 
| 62703 | 324  | 
StandardOpenOption.APPEND, StandardOpenOption.CREATE)  | 
325  | 
||
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
326  | 
def append(path: Path, text: String): Unit = append(path.file, text)  | 
| 62703 | 327  | 
|
328  | 
||
| 75206 | 329  | 
/* change */  | 
330  | 
||
| 75393 | 331  | 
def change(  | 
332  | 
path: Path,  | 
|
333  | 
init: Boolean = false,  | 
|
334  | 
strict: Boolean = false  | 
|
335  | 
  )(f: String => String): Unit = {
 | 
|
| 75208 | 336  | 
if (!path.is_file && init) write(path, "")  | 
| 75206 | 337  | 
val x = read(path)  | 
338  | 
val y = f(x)  | 
|
339  | 
if (x != y) write(path, y)  | 
|
| 75213 | 340  | 
    else if (strict) error("Unchanged file: " + path)
 | 
| 75206 | 341  | 
}  | 
342  | 
||
| 75213 | 343  | 
def change_lines(path: Path, init: Boolean = false, strict: Boolean = false)(  | 
344  | 
f: List[String] => List[String]): Unit =  | 
|
345  | 
change(path, init = init, strict = strict)(text => cat_lines(f(split_lines(text))))  | 
|
| 75206 | 346  | 
|
347  | 
||
| 64213 | 348  | 
/* eq */  | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
349  | 
|
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
350  | 
def eq(file1: JFile, file2: JFile): Boolean =  | 
| 73318 | 351  | 
    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
 | 
352  | 
    catch { case ERROR(_) => false }
 | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
353  | 
|
| 64213 | 354  | 
def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file)  | 
355  | 
||
356  | 
||
| 64934 | 357  | 
/* eq_content */  | 
358  | 
||
359  | 
def eq_content(file1: JFile, file2: JFile): Boolean =  | 
|
360  | 
if (eq(file1, file2)) true  | 
|
361  | 
else if (file1.length != file2.length) false  | 
|
362  | 
else Bytes.read(file1) == Bytes.read(file2)  | 
|
363  | 
||
364  | 
def eq_content(path1: Path, path2: Path): Boolean = eq_content(path1.file, path2.file)  | 
|
365  | 
||
366  | 
||
| 
69405
 
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  | 
|
| 75393 | 369  | 
  def is_executable(path: Path): Boolean = {
 | 
| 69788 | 370  | 
    if (Platform.is_windows) Isabelle_System.bash("test -x " + bash_path(path)).check.ok
 | 
371  | 
else path.file.canExecute  | 
|
372  | 
}  | 
|
373  | 
||
| 75393 | 374  | 
  def set_executable(path: Path, flag: Boolean): Unit = {
 | 
| 71114 | 375  | 
    if (Platform.is_windows && flag) Isabelle_System.chmod("a+x", path)
 | 
376  | 
    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
 | 
377  | 
else path.file.setExecutable(flag, false)  | 
| 
69405
 
22428643351f
more direct File.executable operation: avoid external process (on Unix);
 
wenzelm 
parents: 
69402 
diff
changeset
 | 
378  | 
}  | 
| 74811 | 379  | 
|
380  | 
||
381  | 
/* content */  | 
|
382  | 
||
| 75825 | 383  | 
def content(path: Path, content: Bytes): Content = new Content(path, content)  | 
384  | 
def content(path: Path, content: String): Content = new Content(path, Bytes(content))  | 
|
| 75824 | 385  | 
def content(path: Path, content: XML.Body): Content_XML = new Content_XML(path, content)  | 
| 74811 | 386  | 
|
| 75825 | 387  | 
  final class Content private[File](val path: Path, val content: Bytes) {
 | 
| 75677 | 388  | 
override def toString: String = path.toString  | 
| 74811 | 389  | 
|
| 75508 | 390  | 
    def write(dir: Path): Unit = {
 | 
391  | 
val full_path = dir + path  | 
|
392  | 
Isabelle_System.make_directory(full_path.expand.dir)  | 
|
393  | 
Bytes.write(full_path, content)  | 
|
394  | 
}  | 
|
| 74811 | 395  | 
}  | 
396  | 
||
| 75676 | 397  | 
  final class Content_XML private[File](val path: Path, val content: XML.Body) {
 | 
| 
75823
 
6eb8d6cdb686
proper toString for Content_XML, which is not covered by trait Content;
 
wenzelm 
parents: 
75701 
diff
changeset
 | 
398  | 
override def toString: String = path.toString  | 
| 
 
6eb8d6cdb686
proper toString for Content_XML, which is not covered by trait Content;
 
wenzelm 
parents: 
75701 
diff
changeset
 | 
399  | 
|
| 75825 | 400  | 
def output(out: XML.Body => String): Content = new Content(path, Bytes(out(content)))  | 
| 74811 | 401  | 
}  | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
402  | 
}  |