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