| author | wenzelm |
| Wed, 22 Jun 2022 16:25:22 +0200 | |
| changeset 75595 | ecbd0b38256b |
| parent 75593 | 3e09396db078 |
| child 75604 | 39df30349778 |
| permissions | -rw-r--r-- |
| 6118 | 1 |
(* Title: Pure/General/file.ML |
| 64698 | 2 |
Author: Makarius |
| 5009 | 3 |
|
| 64698 | 4 |
File-system operations. |
| 5009 | 5 |
*) |
6 |
||
7 |
signature FILE = |
|
8 |
sig |
|
|
60970
e08d868ceca9
clarified File.standard_path vs. File.platform_path (like Isabelle/Scala operations);
wenzelm
parents:
59058
diff
changeset
|
9 |
val standard_path: Path.T -> string |
| 16261 | 10 |
val platform_path: Path.T -> string |
|
62549
9498623b27f0
File.bash_string operations in ML as in Scala -- exclusively for GNU bash, not perl and not user output;
wenzelm
parents:
62468
diff
changeset
|
11 |
val bash_path: Path.T -> string |
| 69223 | 12 |
val bash_paths: Path.T list -> string |
| 72278 | 13 |
val bash_platform_path: Path.T -> string |
|
73314
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73264
diff
changeset
|
14 |
val absolute_path: Path.T -> Path.T |
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
15 |
val full_path: Path.T -> Path.T -> Path.T |
| 6182 | 16 |
val tmp_path: Path.T -> Path.T |
| 17826 | 17 |
val exists: Path.T -> bool |
18 |
val rm: Path.T -> unit |
|
| 40785 | 19 |
val is_dir: Path.T -> bool |
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
20 |
val is_file: Path.T -> bool |
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
21 |
val check_dir: Path.T -> Path.T |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
22 |
val check_file: Path.T -> Path.T |
| 43848 | 23 |
val open_dir: (OS.FileSys.dirstream -> 'a) -> Path.T -> 'a |
|
60982
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
24 |
val open_input: (BinIO.instream -> 'a) -> Path.T -> 'a |
|
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
25 |
val open_output: (BinIO.outstream -> 'a) -> Path.T -> 'a |
|
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
26 |
val open_append: (BinIO.outstream -> 'a) -> Path.T -> 'a |
| 43848 | 27 |
val fold_dir: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
28 |
val read_dir: Path.T -> string list |
|
| 75569 | 29 |
val input: BinIO.instream -> string |
30 |
val input_size: int -> BinIO.instream -> string |
|
31 |
val input_all: BinIO.instream -> string |
|
| 28028 | 32 |
val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
| 43848 | 33 |
val read_lines: Path.T -> string list |
| 6182 | 34 |
val read: Path.T -> string |
35 |
val write: Path.T -> string -> unit |
|
36 |
val append: Path.T -> string -> unit |
|
| 69223 | 37 |
val generate: Path.T -> string -> unit |
|
60982
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
38 |
val output: BinIO.outstream -> string -> unit |
| 75571 | 39 |
val outputs: BinIO.outstream -> string list -> unit |
| 16713 | 40 |
val write_list: Path.T -> string list -> unit |
41 |
val append_list: Path.T -> string list -> unit |
|
| 28028 | 42 |
val write_buffer: Path.T -> Buffer.T -> unit |
| 16603 | 43 |
val eq: Path.T * Path.T -> bool |
| 5009 | 44 |
end; |
45 |
||
46 |
structure File: FILE = |
|
47 |
struct |
|
48 |
||
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
49 |
(* system path representations *) |
| 6224 | 50 |
|
|
60970
e08d868ceca9
clarified File.standard_path vs. File.platform_path (like Isabelle/Scala operations);
wenzelm
parents:
59058
diff
changeset
|
51 |
val standard_path = Path.implode o Path.expand; |
| 62468 | 52 |
val platform_path = ML_System.platform_path o standard_path; |
|
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
53 |
|
| 73264 | 54 |
val bash_path = Bash.string o standard_path; |
55 |
val bash_paths = Bash.strings o map standard_path; |
|
| 6224 | 56 |
|
| 73264 | 57 |
val bash_platform_path = Bash.string o platform_path; |
| 72278 | 58 |
|
| 70292 | 59 |
|
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
60 |
(* full_path *) |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
61 |
|
|
73314
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73264
diff
changeset
|
62 |
val absolute_path = |
|
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73264
diff
changeset
|
63 |
Path.expand #> (fn path => |
|
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73264
diff
changeset
|
64 |
if Path.is_absolute path then path |
|
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73264
diff
changeset
|
65 |
else Path.explode (ML_System.standard_path (OS.FileSys.getDir ())) + path); |
|
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73264
diff
changeset
|
66 |
|
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
67 |
fun full_path dir path = |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
68 |
let |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
69 |
val path' = Path.expand path; |
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
70 |
val _ = Path.is_current path' andalso error "Bad file specification"; |
|
73314
87403fde8cc3
more direct make_directory in ML and Scala, but ssh still requires perl for Windows UNC paths (see a5dbad753552);
wenzelm
parents:
73264
diff
changeset
|
71 |
in absolute_path (dir + path') end; |
| 5009 | 72 |
|
| 6182 | 73 |
|
74 |
(* tmp_path *) |
|
75 |
||
|
72511
460d743010bc
clarified signature: overloaded "+" for Path.append;
wenzelm
parents:
72278
diff
changeset
|
76 |
fun tmp_path path = Path.variable "ISABELLE_TMP" + Path.base path; |
| 5009 | 77 |
|
78 |
||
|
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
79 |
(* directory entries *) |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
80 |
|
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
81 |
val exists = can OS.FileSys.modTime o platform_path; |
| 16261 | 82 |
|
83 |
val rm = OS.FileSys.remove o platform_path; |
|
84 |
||
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
85 |
fun test_dir path = the_default false (try OS.FileSys.isDir (platform_path path)); |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
86 |
fun is_dir path = exists path andalso test_dir path; |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
87 |
fun is_file path = exists path andalso not (test_dir path); |
| 40785 | 88 |
|
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
89 |
fun check_dir path = |
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
90 |
if is_dir path then path |
| 63668 | 91 |
else error ("No such directory: " ^ Path.print (Path.expand path));
|
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
92 |
|
|
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
93 |
fun check_file path = |
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
94 |
if is_file path then path |
| 63668 | 95 |
else error ("No such file: " ^ Path.print (Path.expand path));
|
|
42003
6e45dc518ebb
replaced File.check by specific File.check_file, File.check_dir;
wenzelm
parents:
41944
diff
changeset
|
96 |
|
| 16261 | 97 |
|
| 43848 | 98 |
(* open streams *) |
| 6224 | 99 |
|
| 16261 | 100 |
local |
101 |
||
|
69483
023d92df3d84
more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents:
69427
diff
changeset
|
102 |
fun with_file open_file close_file f = |
|
023d92df3d84
more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents:
69427
diff
changeset
|
103 |
Thread_Attributes.uninterruptible (fn restore_attributes => fn path => |
|
023d92df3d84
more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents:
69427
diff
changeset
|
104 |
let |
|
023d92df3d84
more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents:
69427
diff
changeset
|
105 |
val file = open_file path; |
|
023d92df3d84
more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents:
69427
diff
changeset
|
106 |
val result = Exn.capture (restore_attributes f) file; |
|
023d92df3d84
more robust open/close bracket, but with potential danger of blocking indefinitely in uninterruptible state;
wenzelm
parents:
69427
diff
changeset
|
107 |
in close_file file; Exn.release result end); |
| 6224 | 108 |
|
| 16261 | 109 |
in |
| 6218 | 110 |
|
| 43848 | 111 |
fun open_dir f = with_file OS.FileSys.openDir OS.FileSys.closeDir f o platform_path; |
|
60982
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
112 |
fun open_input f = with_file BinIO.openIn BinIO.closeIn f o platform_path; |
|
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
113 |
fun open_output f = with_file BinIO.openOut BinIO.closeOut f o platform_path; |
|
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
114 |
fun open_append f = with_file BinIO.openAppend BinIO.closeOut f o platform_path; |
| 6224 | 115 |
|
| 28028 | 116 |
end; |
117 |
||
118 |
||
| 43848 | 119 |
(* directory content *) |
120 |
||
|
69300
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
121 |
fun fold_dir f path a = |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
122 |
check_dir path |> open_dir (fn stream => |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
123 |
let |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
124 |
fun read x = |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
125 |
(case OS.FileSys.readDir stream of |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
126 |
NONE => x |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
127 |
| SOME entry => read (f entry x)); |
|
8b6ab9989bcd
is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents:
69223
diff
changeset
|
128 |
in read a end); |
| 43848 | 129 |
|
|
69427
ff2f39a221d4
clarified operations: uniform sorting of results;
wenzelm
parents:
69300
diff
changeset
|
130 |
fun read_dir path = sort_strings (fold_dir cons path []); |
| 43848 | 131 |
|
132 |
||
| 28028 | 133 |
(* input *) |
134 |
||
| 75569 | 135 |
val input = Byte.bytesToString o BinIO.input; |
136 |
fun input_size n stream = Byte.bytesToString (BinIO.inputN (stream, n)); |
|
137 |
val input_all = Byte.bytesToString o BinIO.inputAll; |
|
138 |
||
| 44879 | 139 |
(* |
140 |
scalable iterator: |
|
141 |
. avoid size limit of TextIO.inputAll and overhead of many TextIO.inputLine |
|
|
48911
5debc3e4fa81
tuned messages: end-of-input rarely means physical end-of-file from the past;
wenzelm
parents:
48446
diff
changeset
|
142 |
. optional terminator at end-of-input |
| 44879 | 143 |
*) |
| 75595 | 144 |
fun fold_lines f path a = open_input (fn file => |
| 28028 | 145 |
let |
|
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
146 |
fun read buf x = |
| 75569 | 147 |
(case input file of |
| 75595 | 148 |
"" => (case Buffer.content buf of "" => x | line => f line x) |
|
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
149 |
| input => |
| 75595 | 150 |
(case String.fields (fn c => c = #"\n") input of |
|
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
151 |
[rest] => read (Buffer.add rest buf) x |
| 75595 | 152 |
| line :: more => read_more more (f (Buffer.content (Buffer.add line buf)) x))) |
153 |
and read_more [rest] x = read (Buffer.add rest Buffer.empty) x |
|
154 |
| read_more (line :: more) x = read_more more (f line x); |
|
| 28028 | 155 |
in read Buffer.empty a end) path; |
156 |
||
| 43848 | 157 |
fun read_lines path = rev (fold_lines cons path []); |
158 |
||
| 75569 | 159 |
val read = open_input input_all; |
| 5009 | 160 |
|
| 28028 | 161 |
|
162 |
(* output *) |
|
163 |
||
|
60982
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
164 |
fun output file txt = BinIO.output (file, Byte.stringToBytes txt); |
| 75571 | 165 |
val outputs = List.app o output; |
| 28028 | 166 |
|
|
60982
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
167 |
fun output_list txts file = List.app (output file) txts; |
|
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
168 |
fun write_list path txts = open_output (output_list txts) path; |
|
67e389f67073
precise BinIO, without newline conversion on Windows;
wenzelm
parents:
60970
diff
changeset
|
169 |
fun append_list path txts = open_append (output_list txts) path; |
| 16713 | 170 |
|
171 |
fun write path txt = write_list path [txt]; |
|
172 |
fun append path txt = append_list path [txt]; |
|
| 69223 | 173 |
fun generate path txt = if try read path = SOME txt then () else write path txt; |
| 6182 | 174 |
|
| 75566 | 175 |
fun write_buffer path buf = |
176 |
open_output (fn file => List.app (output file) (Buffer.contents buf)) path; |
|
| 28028 | 177 |
|
178 |
||
|
56533
cd8b6d849b6a
explicit 'document_files' in session ROOT specifications;
wenzelm
parents:
48911
diff
changeset
|
179 |
(* eq *) |
| 5009 | 180 |
|
| 16603 | 181 |
fun eq paths = |
|
59058
a78612c67ec0
renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents:
56533
diff
changeset
|
182 |
(case try (apply2 (OS.FileSys.fileId o platform_path)) paths of |
| 26656 | 183 |
SOME ids => is_equal (OS.FileSys.compare ids) |
| 16603 | 184 |
| NONE => false); |
185 |
||
| 5009 | 186 |
end; |