| author | wenzelm | 
| Fri, 21 Oct 2022 11:08:01 +0200 | |
| changeset 76348 | a15f16e8ad18 | 
| parent 75906 | 2167b9e3157a | 
| child 76349 | b4daf7577ca0 | 
| 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  | 
|
| 64002 | 20  | 
import org.tukaani.xz.{XZInputStream, XZOutputStream}
 | 
21  | 
||
| 76348 | 22  | 
import com.github.luben.zstd.{ZstdInputStream, ZstdOutputStream}
 | 
23  | 
||
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
24  | 
import scala.collection.mutable  | 
| 
 
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  | 
|
| 75393 | 27  | 
object File {
 | 
| 60992 | 28  | 
/* standard path (Cygwin or Posix) */  | 
| 60988 | 29  | 
|
30  | 
def standard_path(path: Path): String = path.expand.implode  | 
|
31  | 
||
| 60992 | 32  | 
def standard_path(platform_path: String): String =  | 
| 73911 | 33  | 
isabelle.setup.Environment.standard_path(platform_path)  | 
| 60992 | 34  | 
|
35  | 
def standard_path(file: JFile): String = standard_path(file.getPath)  | 
|
36  | 
||
37  | 
def standard_url(name: String): String =  | 
|
38  | 
    try {
 | 
|
39  | 
val url = new URL(name)  | 
|
| 64775 | 40  | 
if (url.getProtocol == "file" && Url.is_wellformed_file(name))  | 
41  | 
standard_path(Url.parse_file(name))  | 
|
| 60992 | 42  | 
else name  | 
43  | 
}  | 
|
44  | 
    catch { case _: MalformedURLException => standard_path(name) }
 | 
|
45  | 
||
46  | 
||
47  | 
/* platform path (Windows or Posix) */  | 
|
48  | 
||
49  | 
def platform_path(standard_path: String): String =  | 
|
| 73911 | 50  | 
isabelle.setup.Environment.platform_path(standard_path)  | 
| 60992 | 51  | 
|
52  | 
def platform_path(path: Path): String = platform_path(standard_path(path))  | 
|
| 60988 | 53  | 
def platform_file(path: Path): JFile = new JFile(platform_path(path))  | 
54  | 
||
| 60992 | 55  | 
|
| 66232 | 56  | 
/* platform files */  | 
57  | 
||
58  | 
def absolute(file: JFile): JFile = file.toPath.toAbsolutePath.normalize.toFile  | 
|
| 66233 | 59  | 
def absolute_name(file: JFile): String = absolute(file).getPath  | 
60  | 
||
| 66232 | 61  | 
def canonical(file: JFile): JFile = file.getCanonicalFile  | 
| 66233 | 62  | 
def canonical_name(file: JFile): String = canonical(file).getPath  | 
| 66232 | 63  | 
|
64  | 
def path(file: JFile): Path = Path.explode(standard_path(file))  | 
|
65  | 
def pwd(): Path = path(Path.current.absolute_file)  | 
|
66  | 
||
| 75701 | 67  | 
def uri(file: JFile): URI = file.toURI  | 
68  | 
def uri(path: Path): URI = path.file.toURI  | 
|
69  | 
||
70  | 
def url(file: JFile): URL = uri(file).toURL  | 
|
71  | 
def url(path: Path): URL = url(path.file)  | 
|
72  | 
||
| 66232 | 73  | 
|
| 
75906
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
74  | 
/* adhoc file types */  | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
75  | 
|
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
76  | 
  def is_ML(s: String): Boolean = s.endsWith(".ML")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
77  | 
  def is_bib(s: String): Boolean = s.endsWith(".bib")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
78  | 
  def is_dll(s: String): Boolean = s.endsWith(".dll")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
79  | 
  def is_exe(s: String): Boolean = s.endsWith(".exe")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
80  | 
  def is_gz(s: String): Boolean = s.endsWith(".gz")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
81  | 
  def is_html(s: String): Boolean = s.endsWith(".html")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
82  | 
  def is_jar(s: String): Boolean = s.endsWith(".jar")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
83  | 
  def is_java(s: String): Boolean = s.endsWith(".java")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
84  | 
  def is_node(s: String): Boolean = s.endsWith(".node")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
85  | 
  def is_pdf(s: String): Boolean = s.endsWith(".pdf")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
86  | 
  def is_png(s: String): Boolean = s.endsWith(".png")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
87  | 
  def is_thy(s: String): Boolean = s.endsWith(".thy")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
88  | 
  def is_xz(s: String): Boolean = s.endsWith(".xz")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
89  | 
  def is_zip(s: String): Boolean = s.endsWith(".zip")
 | 
| 76348 | 90  | 
  def is_zst(s: String): Boolean = s.endsWith(".zst")
 | 
| 
75906
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
91  | 
|
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
92  | 
  def is_backup(s: String): Boolean = s.endsWith("~") || s.endsWith(".orig")
 | 
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
93  | 
|
| 
 
2167b9e3157a
clarified signature: support for adhoc file types;
 
wenzelm 
parents: 
75825 
diff
changeset
 | 
94  | 
|
| 66693 | 95  | 
/* relative paths */  | 
96  | 
||
| 75393 | 97  | 
  def relative_path(base: Path, other: Path): Option[Path] = {
 | 
| 73945 | 98  | 
val base_path = base.java_path  | 
99  | 
val other_path = other.java_path  | 
|
| 66693 | 100  | 
if (other_path.startsWith(base_path))  | 
101  | 
Some(path(base_path.relativize(other_path).toFile))  | 
|
102  | 
else None  | 
|
103  | 
}  | 
|
104  | 
||
105  | 
||
| 
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
 | 
106  | 
/* bash path */  | 
| 60992 | 107  | 
|
| 64304 | 108  | 
def bash_path(path: Path): String = Bash.string(standard_path(path))  | 
109  | 
def bash_path(file: JFile): String = Bash.string(standard_path(file))  | 
|
| 60988 | 110  | 
|
| 72036 | 111  | 
def bash_platform_path(path: Path): String = Bash.string(platform_path(path))  | 
112  | 
||
| 60988 | 113  | 
|
| 62544 | 114  | 
/* directory entries */  | 
115  | 
||
116  | 
def check_dir(path: Path): Path =  | 
|
117  | 
    if (path.is_dir) path else error("No such directory: " + path)
 | 
|
118  | 
||
119  | 
def check_file(path: Path): Path =  | 
|
120  | 
    if (path.is_file) path else error("No such file: " + path)
 | 
|
121  | 
||
122  | 
||
| 
62829
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
123  | 
/* directory content */  | 
| 
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
124  | 
|
| 75393 | 125  | 
  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
 | 
126  | 
    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
 | 
127  | 
val files = dir.file.listFiles  | 
| 
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
128  | 
if (files == null) Nil  | 
| 
69427
 
ff2f39a221d4
clarified operations: uniform sorting of results;
 
wenzelm 
parents: 
69405 
diff
changeset
 | 
129  | 
else files.toList.map(_.getName).sorted  | 
| 
62829
 
4141c2a8458b
clarified Isabelle tool wrapper: bash, Scala, no perl, no ML;
 
wenzelm 
parents: 
62704 
diff
changeset
 | 
130  | 
}  | 
| 
48613
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
131  | 
|
| 72442 | 132  | 
def get_dir(dir: Path): String =  | 
133  | 
    read_dir(dir).filter(name => (dir + Path.basic(name)).is_dir) match {
 | 
|
134  | 
case List(entry) => entry  | 
|
135  | 
case dirs =>  | 
|
136  | 
        error("Exactly one directory entry expected: " + commas_quote(dirs.sorted))
 | 
|
137  | 
}  | 
|
138  | 
||
| 64932 | 139  | 
def find_files(  | 
140  | 
start: JFile,  | 
|
141  | 
pred: JFile => Boolean = _ => true,  | 
|
| 
69293
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
142  | 
include_dirs: Boolean = false,  | 
| 75393 | 143  | 
follow_links: Boolean = false  | 
144  | 
  ): List[JFile] = {
 | 
|
| 
62443
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
145  | 
val result = new mutable.ListBuffer[JFile]  | 
| 73340 | 146  | 
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
 | 
147  | 
|
| 64932 | 148  | 
if (start.isFile) check(start)  | 
| 
62443
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
149  | 
    else if (start.isDirectory) {
 | 
| 
69293
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
150  | 
val options =  | 
| 
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
151  | 
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
 | 
152  | 
else EnumSet.noneOf(classOf[FileVisitOption])  | 
| 
 
72a9860f8602
clarified find_files: follow links by default, e.g. relevant for "~/cronjob/log";
 
wenzelm 
parents: 
66693 
diff
changeset
 | 
153  | 
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
 | 
154  | 
        new SimpleFileVisitor[JPath] {
 | 
| 75393 | 155  | 
override def preVisitDirectory(  | 
156  | 
path: JPath,  | 
|
157  | 
attrs: BasicFileAttributes  | 
|
158  | 
          ): FileVisitResult = {
 | 
|
| 64932 | 159  | 
if (include_dirs) check(path.toFile)  | 
160  | 
FileVisitResult.CONTINUE  | 
|
161  | 
}  | 
|
| 75393 | 162  | 
override def visitFile(  | 
163  | 
path: JPath,  | 
|
164  | 
attrs: BasicFileAttributes  | 
|
165  | 
          ): FileVisitResult = {
 | 
|
| 69301 | 166  | 
val file = path.toFile  | 
167  | 
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
 | 
168  | 
FileVisitResult.CONTINUE  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
169  | 
}  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
170  | 
}  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
171  | 
)  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
172  | 
}  | 
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
173  | 
|
| 
 
133f65ac17e5
just one File.find_files, based on Java 7 Files operations;
 
wenzelm 
parents: 
62294 
diff
changeset
 | 
174  | 
result.toList  | 
| 
48613
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
175  | 
}  | 
| 
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
176  | 
|
| 
 
232652ac346e
clarified directory content operations (similar to ML version);
 
wenzelm 
parents: 
48550 
diff
changeset
 | 
177  | 
|
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
178  | 
/* read */  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
179  | 
|
| 
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
 | 
180  | 
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
 | 
181  | 
def read(path: Path): String = read(path.file)  | 
| 
 
f686cb016c0c
more direct File.read_bytes -- avoid cumulative copying of StringBuilder;
 
wenzelm 
parents: 
48613 
diff
changeset
 | 
182  | 
|
| 
50684
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
183  | 
|
| 75393 | 184  | 
  def read_stream(reader: BufferedReader): String = {
 | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
185  | 
val output = new StringBuilder(100)  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
186  | 
var c = -1  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
187  | 
    while ({ c = reader.read; c != -1 }) output += c.toChar
 | 
| 73367 | 188  | 
reader.close()  | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
189  | 
output.toString  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
190  | 
}  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
191  | 
|
| 
50684
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
192  | 
def read_stream(stream: InputStream): String =  | 
| 64000 | 193  | 
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
 | 
194  | 
|
| 
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
195  | 
def read_gzip(file: JFile): String =  | 
| 
 
12b7e0b4a66e
support File.read_gzip as well, in accordance to File.write_gzip;
 
wenzelm 
parents: 
50203 
diff
changeset
 | 
196  | 
read_stream(new GZIPInputStream(new BufferedInputStream(new FileInputStream(file))))  | 
| 64000 | 197  | 
def read_gzip(path: Path): String = read_gzip(path.file)  | 
| 51504 | 198  | 
|
| 64002 | 199  | 
def read_xz(file: JFile): String =  | 
200  | 
read_stream(new XZInputStream(new BufferedInputStream(new FileInputStream(file))))  | 
|
| 64000 | 201  | 
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
 | 
202  | 
|
| 76348 | 203  | 
  def read_zstd(file: JFile): String = {
 | 
204  | 
Zstd.init_jni  | 
|
205  | 
read_stream(new ZstdInputStream(new BufferedInputStream(new FileInputStream(file))))  | 
|
206  | 
}  | 
|
207  | 
def read_zstd(path: Path): String = read_zstd(path.file)  | 
|
208  | 
||
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
209  | 
|
| 50845 | 210  | 
/* read lines */  | 
211  | 
||
| 75393 | 212  | 
  def read_line(reader: BufferedReader): Option[String] = {
 | 
| 69487 | 213  | 
val line =  | 
214  | 
      try { reader.readLine}
 | 
|
215  | 
      catch { case _: IOException => null }
 | 
|
| 72698 | 216  | 
Option(line).map(Library.trim_line)  | 
| 69487 | 217  | 
}  | 
218  | 
||
| 75393 | 219  | 
  def read_lines(reader: BufferedReader, progress: String => Unit): List[String] = {
 | 
| 50845 | 220  | 
val result = new mutable.ListBuffer[String]  | 
| 69487 | 221  | 
var line: Option[String] = None  | 
222  | 
    while ({ line = read_line(reader); line.isDefined }) {
 | 
|
223  | 
progress(line.get)  | 
|
224  | 
result += line.get  | 
|
| 50845 | 225  | 
}  | 
| 73367 | 226  | 
reader.close()  | 
| 50845 | 227  | 
result.toList  | 
228  | 
}  | 
|
229  | 
||
230  | 
||
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
231  | 
/* write */  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
232  | 
|
| 71534 | 233  | 
def writer(file: JFile): BufferedWriter =  | 
234  | 
new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), UTF8.charset))  | 
|
235  | 
||
| 73340 | 236  | 
def write_file(  | 
| 75393 | 237  | 
file: JFile,  | 
238  | 
text: String,  | 
|
239  | 
make_stream: OutputStream => OutputStream  | 
|
240  | 
  ): Unit = {
 | 
|
| 51504 | 241  | 
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
 | 
242  | 
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
 | 
243  | 
}  | 
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
244  | 
|
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
245  | 
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
 | 
246  | 
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
 | 
247  | 
|
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
248  | 
def write_gzip(file: JFile, text: String): Unit =  | 
| 51504 | 249  | 
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
 | 
250  | 
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
 | 
251  | 
|
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
252  | 
def write_xz(file: JFile, text: String, options: XZ.Options): Unit =  | 
| 64003 | 253  | 
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
 | 
254  | 
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
 | 
255  | 
def write_xz(path: Path, text: String, options: XZ.Options): Unit =  | 
| 64002 | 256  | 
write_xz(path.file, text, options)  | 
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
257  | 
def write_xz(path: Path, text: String): Unit = write_xz(path, text, XZ.options())  | 
| 64000 | 258  | 
|
| 76348 | 259  | 
  def write_zstd(file: JFile, text: String): Unit = {
 | 
260  | 
Zstd.init_jni  | 
|
261  | 
write_file(file, text, (s: OutputStream) => new ZstdOutputStream(new BufferedOutputStream(s)))  | 
|
262  | 
}  | 
|
263  | 
def write_zstd(path: Path, text: String): Unit = write_zstd(path.file, text)  | 
|
264  | 
||
| 75393 | 265  | 
  def write_backup(path: Path, text: String): Unit = {
 | 
| 73317 | 266  | 
if (path.is_file) Isabelle_System.move_file(path, path.backup)  | 
| 62444 | 267  | 
write(path, text)  | 
| 53336 | 268  | 
}  | 
269  | 
||
| 75393 | 270  | 
  def write_backup2(path: Path, text: String): Unit = {
 | 
| 73317 | 271  | 
if (path.is_file) Isabelle_System.move_file(path, path.backup2)  | 
| 62444 | 272  | 
write(path, text)  | 
| 58610 | 273  | 
}  | 
274  | 
||
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
275  | 
|
| 62703 | 276  | 
/* append */  | 
277  | 
||
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
278  | 
def append(file: JFile, text: String): Unit =  | 
| 73627 | 279  | 
Files.write(file.toPath, UTF8.bytes(text),  | 
| 62703 | 280  | 
StandardOpenOption.APPEND, StandardOpenOption.CREATE)  | 
281  | 
||
| 
73574
 
12b3f78dde61
clarified signature: avoid overlap of String vs. Bytes (both are CharSequence);
 
wenzelm 
parents: 
73367 
diff
changeset
 | 
282  | 
def append(path: Path, text: String): Unit = append(path.file, text)  | 
| 62703 | 283  | 
|
284  | 
||
| 75206 | 285  | 
/* change */  | 
286  | 
||
| 75393 | 287  | 
def change(  | 
288  | 
path: Path,  | 
|
289  | 
init: Boolean = false,  | 
|
290  | 
strict: Boolean = false  | 
|
291  | 
  )(f: String => String): Unit = {
 | 
|
| 75208 | 292  | 
if (!path.is_file && init) write(path, "")  | 
| 75206 | 293  | 
val x = read(path)  | 
294  | 
val y = f(x)  | 
|
295  | 
if (x != y) write(path, y)  | 
|
| 75213 | 296  | 
    else if (strict) error("Unchanged file: " + path)
 | 
| 75206 | 297  | 
}  | 
298  | 
||
| 75213 | 299  | 
def change_lines(path: Path, init: Boolean = false, strict: Boolean = false)(  | 
300  | 
f: List[String] => List[String]): Unit =  | 
|
301  | 
change(path, init = init, strict = strict)(text => cat_lines(f(split_lines(text))))  | 
|
| 75206 | 302  | 
|
303  | 
||
| 64213 | 304  | 
/* eq */  | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
305  | 
|
| 
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
306  | 
def eq(file1: JFile, file2: JFile): Boolean =  | 
| 73318 | 307  | 
    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
 | 
308  | 
    catch { case ERROR(_) => false }
 | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
309  | 
|
| 64213 | 310  | 
def eq(path1: Path, path2: Path): Boolean = eq(path1.file, path2.file)  | 
311  | 
||
312  | 
||
| 64934 | 313  | 
/* eq_content */  | 
314  | 
||
315  | 
def eq_content(file1: JFile, file2: JFile): Boolean =  | 
|
316  | 
if (eq(file1, file2)) true  | 
|
317  | 
else if (file1.length != file2.length) false  | 
|
318  | 
else Bytes.read(file1) == Bytes.read(file2)  | 
|
319  | 
||
320  | 
def eq_content(path1: Path, path2: Path): Boolean = eq_content(path1.file, path2.file)  | 
|
321  | 
||
322  | 
||
| 
69405
 
22428643351f
more direct File.executable operation: avoid external process (on Unix);
 
wenzelm 
parents: 
69402 
diff
changeset
 | 
323  | 
/* permissions */  | 
| 
 
22428643351f
more direct File.executable operation: avoid external process (on Unix);
 
wenzelm 
parents: 
69402 
diff
changeset
 | 
324  | 
|
| 75393 | 325  | 
  def is_executable(path: Path): Boolean = {
 | 
| 69788 | 326  | 
    if (Platform.is_windows) Isabelle_System.bash("test -x " + bash_path(path)).check.ok
 | 
327  | 
else path.file.canExecute  | 
|
328  | 
}  | 
|
329  | 
||
| 75393 | 330  | 
  def set_executable(path: Path, flag: Boolean): Unit = {
 | 
| 71114 | 331  | 
    if (Platform.is_windows && flag) Isabelle_System.chmod("a+x", path)
 | 
332  | 
    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
 | 
333  | 
else path.file.setExecutable(flag, false)  | 
| 
69405
 
22428643351f
more direct File.executable operation: avoid external process (on Unix);
 
wenzelm 
parents: 
69402 
diff
changeset
 | 
334  | 
}  | 
| 74811 | 335  | 
|
336  | 
||
337  | 
/* content */  | 
|
338  | 
||
| 75825 | 339  | 
def content(path: Path, content: Bytes): Content = new Content(path, content)  | 
340  | 
def content(path: Path, content: String): Content = new Content(path, Bytes(content))  | 
|
| 75824 | 341  | 
def content(path: Path, content: XML.Body): Content_XML = new Content_XML(path, content)  | 
| 74811 | 342  | 
|
| 75825 | 343  | 
  final class Content private[File](val path: Path, val content: Bytes) {
 | 
| 75677 | 344  | 
override def toString: String = path.toString  | 
| 74811 | 345  | 
|
| 75508 | 346  | 
    def write(dir: Path): Unit = {
 | 
347  | 
val full_path = dir + path  | 
|
348  | 
Isabelle_System.make_directory(full_path.expand.dir)  | 
|
349  | 
Bytes.write(full_path, content)  | 
|
350  | 
}  | 
|
| 74811 | 351  | 
}  | 
352  | 
||
| 75676 | 353  | 
  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
 | 
354  | 
override def toString: String = path.toString  | 
| 
 
6eb8d6cdb686
proper toString for Content_XML, which is not covered by trait Content;
 
wenzelm 
parents: 
75701 
diff
changeset
 | 
355  | 
|
| 75825 | 356  | 
def output(out: XML.Body => String): Content = new Content(path, Bytes(out(content)))  | 
| 74811 | 357  | 
}  | 
| 
48411
 
5b3440850d36
more abstract file system operations in Scala, corresponding to ML version;
 
wenzelm 
parents:  
diff
changeset
 | 
358  | 
}  |