author | haftmann |
Tue, 06 Jul 2010 09:21:13 +0200 | |
changeset 37724 | 6607ccf77946 |
parent 37668 | 892f8d00426c |
child 37739 | 312fe7201f88 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/file.ML |
5009 | 2 |
Author: Markus Wenzel, TU Muenchen |
3 |
||
4 |
File system operations. |
|
5 |
*) |
|
6 |
||
7 |
signature FILE = |
|
8 |
sig |
|
16261 | 9 |
val platform_path: Path.T -> string |
32943 | 10 |
val shell_quote: string -> string |
16261 | 11 |
val shell_path: Path.T -> string |
6224 | 12 |
val cd: Path.T -> unit |
13 |
val pwd: unit -> Path.T |
|
14 |
val full_path: Path.T -> Path.T |
|
6182 | 15 |
val tmp_path: Path.T -> Path.T |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
16 |
val isabelle_tool: string -> string -> int |
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
17 |
eqtype ident |
23874 | 18 |
val rep_ident: ident -> string |
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
19 |
val ident: Path.T -> ident option |
17826 | 20 |
val exists: Path.T -> bool |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
21 |
val check: Path.T -> unit |
17826 | 22 |
val rm: Path.T -> unit |
23 |
val mkdir: Path.T -> unit |
|
37651
62fc16341922
mkdir_leaf -- avoiding surprises with typos in user-given paths
haftmann
parents:
35010
diff
changeset
|
24 |
val mkdir_leaf: Path.T -> unit |
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
25 |
val open_input: (TextIO.instream -> 'a) -> Path.T -> 'a |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
26 |
val open_output: (TextIO.outstream -> 'a) -> Path.T -> 'a |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
27 |
val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a |
28028 | 28 |
val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
6182 | 29 |
val read: Path.T -> string |
30 |
val write: Path.T -> string -> unit |
|
31 |
val append: Path.T -> string -> unit |
|
16713 | 32 |
val write_list: Path.T -> string list -> unit |
33 |
val append_list: Path.T -> string list -> unit |
|
28028 | 34 |
val write_buffer: Path.T -> Buffer.T -> unit |
16603 | 35 |
val eq: Path.T * Path.T -> bool |
6182 | 36 |
val copy: Path.T -> Path.T -> unit |
16261 | 37 |
val copy_dir: Path.T -> Path.T -> unit |
5009 | 38 |
end; |
39 |
||
40 |
structure File: FILE = |
|
41 |
struct |
|
42 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
43 |
(* system path representations *) |
6224 | 44 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
45 |
val platform_path = Path.implode o Path.expand; |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
46 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
47 |
val shell_quote = enclose "'" "'"; |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
48 |
val shell_path = shell_quote o platform_path; |
6224 | 49 |
|
50 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
51 |
(* current working directory *) |
6224 | 52 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22145
diff
changeset
|
53 |
val cd = cd o platform_path; |
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
54 |
val pwd = Path.explode o pwd; |
6224 | 55 |
|
16261 | 56 |
fun full_path path = |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
57 |
if Path.is_absolute path then path |
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
58 |
else Path.append (pwd ()) path; |
5009 | 59 |
|
6182 | 60 |
|
61 |
(* tmp_path *) |
|
62 |
||
63 |
fun tmp_path path = |
|
64 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 65 |
|
66 |
||
16261 | 67 |
(* system commands *) |
68 |
||
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
69 |
fun isabelle_tool name args = |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
70 |
(case space_explode ":" (getenv "ISABELLE_TOOLS") |> get_first (fn dir => |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
71 |
let val path = dir ^ "/" ^ name in |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
72 |
if can OS.FileSys.modTime path andalso |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
73 |
not (OS.FileSys.isDir path) andalso |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
74 |
OS.FileSys.access (path, [OS.FileSys.A_READ, OS.FileSys.A_EXEC]) |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
75 |
then SOME path |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
76 |
else NONE |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
77 |
end handle OS.SysErr _ => NONE) of |
35010
d6e492cea6e4
renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents:
33223
diff
changeset
|
78 |
SOME path => bash (shell_quote path ^ " " ^ args) |
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
79 |
| NONE => (writeln ("Unknown Isabelle tool: " ^ name); 2)); |
16261 | 80 |
|
81 |
fun system_command cmd = |
|
35010
d6e492cea6e4
renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents:
33223
diff
changeset
|
82 |
if bash cmd <> 0 then error ("System command failed: " ^ cmd) |
16261 | 83 |
else (); |
84 |
||
85 |
||
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
86 |
(* file identification *) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
87 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
88 |
local |
32738 | 89 |
val ident_cache = |
90 |
Unsynchronized.ref (Symtab.empty: {time_stamp: string, id: string} Symtab.table); |
|
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
91 |
in |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
92 |
|
33223 | 93 |
fun check_cache (path, time) = |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
94 |
(case Symtab.lookup (! ident_cache) path of |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
95 |
NONE => NONE |
33223 | 96 |
| SOME {time_stamp, id} => if time_stamp = time then SOME id else NONE); |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
97 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
98 |
fun update_cache (path, (time, id)) = CRITICAL (fn () => |
32738 | 99 |
Unsynchronized.change ident_cache |
100 |
(Symtab.update (path, {time_stamp = time, id = id}))); |
|
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
101 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
102 |
end; |
16261 | 103 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
104 |
datatype ident = Ident of string; |
23874 | 105 |
fun rep_ident (Ident s) = s; |
106 |
||
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
107 |
fun ident path = |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
108 |
let val physical_path = perhaps (try OS.FileSys.fullPath) (platform_path path) in |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
109 |
(case try (Time.toString o OS.FileSys.modTime) physical_path of |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
110 |
NONE => NONE |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
111 |
| SOME mod_time => SOME (Ident |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
112 |
(case getenv "ISABELLE_FILE_IDENT" of |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
113 |
"" => physical_path ^ ": " ^ mod_time |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
114 |
| cmd => |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
115 |
(case check_cache (physical_path, mod_time) of |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
116 |
SOME id => id |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
117 |
| NONE => |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
118 |
let val (id, rc) = (*potentially slow*) |
35010
d6e492cea6e4
renamed system/system_out to bash/bash_output -- to emphasized that this is really GNU bash, not some undefined POSIX sh;
wenzelm
parents:
33223
diff
changeset
|
119 |
bash_output ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ shell_quote physical_path) |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
120 |
in |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
121 |
if id <> "" andalso rc = 0 then (update_cache (physical_path, (mod_time, id)); id) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
122 |
else error ("Failed to identify file " ^ quote (Path.implode path) ^ " by " ^ cmd) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
123 |
end)))) |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
124 |
end; |
16261 | 125 |
|
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
126 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
127 |
(* directory entries *) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
128 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
129 |
val exists = can OS.FileSys.modTime o platform_path; |
16261 | 130 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
131 |
fun check path = |
17826 | 132 |
if exists path then () |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
133 |
else error ("No such file or directory: " ^ quote (Path.implode path)); |
17826 | 134 |
|
16261 | 135 |
val rm = OS.FileSys.remove o platform_path; |
136 |
||
137 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
138 |
||
37668
892f8d00426c
refined semantics of mkdir_leaf: do not fail if directory already exists
haftmann
parents:
37651
diff
changeset
|
139 |
fun mkdir_leaf path = (check (Path.dir path); mkdir path); |
37651
62fc16341922
mkdir_leaf -- avoiding surprises with typos in user-given paths
haftmann
parents:
35010
diff
changeset
|
140 |
|
16261 | 141 |
fun is_dir path = |
19473 | 142 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
16261 | 143 |
|
144 |
||
28028 | 145 |
(* open files *) |
6224 | 146 |
|
16261 | 147 |
local |
148 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
149 |
fun with_file open_file close_file f path = |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
150 |
let val file = open_file path |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
151 |
in Exn.release (Exn.capture f file before close_file file) end; |
6224 | 152 |
|
16261 | 153 |
in |
6218 | 154 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
155 |
fun open_input f = with_file TextIO.openIn TextIO.closeIn f o platform_path; |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
156 |
fun open_output f = with_file TextIO.openOut TextIO.closeOut f o platform_path; |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
157 |
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path; |
6224 | 158 |
|
28028 | 159 |
end; |
160 |
||
161 |
||
162 |
(* input *) |
|
163 |
||
164 |
(*scalable iterator -- avoid size limit of TextIO.inputAll, and overhead of many TextIO.inputLine*) |
|
165 |
fun fold_lines f path a = open_input (fn file => |
|
166 |
let |
|
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
167 |
fun read buf x = |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
168 |
(case TextIO.input file of |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
169 |
"" => (case Buffer.content buf of "" => x | line => f line x) |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
170 |
| input => |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
171 |
(case String.fields (fn c => c = #"\n") input of |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
172 |
[rest] => read (Buffer.add rest buf) x |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
173 |
| line :: more => read_lines more (f (Buffer.content (Buffer.add line buf)) x))) |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
174 |
and read_lines [rest] x = read (Buffer.add rest Buffer.empty) x |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
175 |
| read_lines (line :: more) x = read_lines more (f line x); |
28028 | 176 |
in read Buffer.empty a end) path; |
177 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
178 |
val read = open_input TextIO.inputAll; |
5009 | 179 |
|
28028 | 180 |
|
181 |
(* output *) |
|
182 |
||
183 |
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts; |
|
184 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
185 |
fun write_list path txts = open_output (output txts) path; |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
186 |
fun append_list path txts = open_append (output txts) path; |
16713 | 187 |
|
188 |
fun write path txt = write_list path [txt]; |
|
189 |
fun append path txt = append_list path [txt]; |
|
6182 | 190 |
|
28028 | 191 |
fun write_buffer path buf = open_output (Buffer.output buf) path; |
192 |
||
193 |
||
194 |
(* copy *) |
|
5009 | 195 |
|
16603 | 196 |
fun eq paths = |
197 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
26656 | 198 |
SOME ids => is_equal (OS.FileSys.compare ids) |
16603 | 199 |
| NONE => false); |
200 |
||
21962 | 201 |
fun copy src dst = |
202 |
if eq (src, dst) then () |
|
203 |
else |
|
204 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
205 |
in write target (read src) end; |
|
6318 | 206 |
|
21962 | 207 |
fun copy_dir src dst = |
208 |
if eq (src, dst) then () |
|
209 |
else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ()); |
|
6640 | 210 |
|
5009 | 211 |
end; |