author | haftmann |
Tue, 23 Sep 2008 18:11:44 +0200 | |
changeset 28337 | 93964076e7b8 |
parent 28051 | 56e3a695434f |
child 28496 | 4cff10648928 |
permissions | -rw-r--r-- |
6118 | 1 |
(* Title: Pure/General/file.ML |
5009 | 2 |
ID: $Id$ |
3 |
Author: Markus Wenzel, TU Muenchen |
|
4 |
||
5 |
File system operations. |
|
6 |
*) |
|
7 |
||
8 |
signature FILE = |
|
9 |
sig |
|
16261 | 10 |
val platform_path: Path.T -> string |
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 |
16261 | 16 |
val isatool: string -> int |
17 |
val system_command: string -> unit |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
18 |
eqtype ident |
23874 | 19 |
val rep_ident: ident -> string |
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
20 |
val ident: Path.T -> ident option |
17826 | 21 |
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
|
22 |
val check: Path.T -> unit |
17826 | 23 |
val rm: Path.T -> unit |
24 |
val mkdir: 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 |
||
69 |
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd); |
|
70 |
||
71 |
fun system_command cmd = |
|
72 |
if system cmd <> 0 then error ("System command failed: " ^ cmd) |
|
73 |
else (); |
|
74 |
||
75 |
||
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
76 |
(* file identification *) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
77 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
78 |
local |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
79 |
val ident_cache = ref (Symtab.empty: {time_stamp: string, id: string} Symtab.table); |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
80 |
in |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
81 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
82 |
fun check_cache (path, time) = CRITICAL (fn () => |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
83 |
(case Symtab.lookup (! ident_cache) path of |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
84 |
NONE => NONE |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
85 |
| SOME {time_stamp, id} => if time_stamp = time then SOME id else NONE)); |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
86 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
87 |
fun update_cache (path, (time, id)) = CRITICAL (fn () => |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
88 |
change ident_cache (Symtab.update (path, {time_stamp = time, id = id}))); |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
89 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
90 |
end; |
16261 | 91 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
92 |
datatype ident = Ident of string; |
23874 | 93 |
fun rep_ident (Ident s) = s; |
94 |
||
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
95 |
fun ident path = |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
96 |
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
|
97 |
(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
|
98 |
NONE => NONE |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
99 |
| SOME mod_time => SOME (Ident |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
100 |
(case getenv "ISABELLE_FILE_IDENT" of |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
101 |
"" => physical_path ^ ": " ^ mod_time |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
102 |
| cmd => |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
103 |
(case check_cache (physical_path, mod_time) of |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
104 |
SOME id => id |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
105 |
| NONE => |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
106 |
let val (id, rc) = (*potentially slow*) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
107 |
system_out ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ shell_quote physical_path) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
108 |
in |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
109 |
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
|
110 |
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
|
111 |
end)))) |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
112 |
end; |
16261 | 113 |
|
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
114 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
115 |
(* directory entries *) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
116 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
117 |
val exists = can OS.FileSys.modTime o platform_path; |
16261 | 118 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
119 |
fun check path = |
17826 | 120 |
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
|
121 |
else error ("No such file or directory: " ^ quote (Path.implode path)); |
17826 | 122 |
|
16261 | 123 |
val rm = OS.FileSys.remove o platform_path; |
124 |
||
125 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
126 |
||
127 |
fun is_dir path = |
|
19473 | 128 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
16261 | 129 |
|
130 |
||
28028 | 131 |
(* open files *) |
6224 | 132 |
|
16261 | 133 |
local |
134 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
135 |
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
|
136 |
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
|
137 |
in Exn.release (Exn.capture f file before close_file file) end; |
6224 | 138 |
|
16261 | 139 |
in |
6218 | 140 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
141 |
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
|
142 |
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
|
143 |
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path; |
6224 | 144 |
|
28028 | 145 |
end; |
146 |
||
147 |
||
148 |
(* input *) |
|
149 |
||
150 |
(*scalable iterator -- avoid size limit of TextIO.inputAll, and overhead of many TextIO.inputLine*) |
|
151 |
fun fold_lines f path a = open_input (fn file => |
|
152 |
let |
|
28050
7cef47b53feb
fold_lines: simplified, more efficient due to String.fields;
wenzelm
parents:
28028
diff
changeset
|
153 |
val split = split_last o String.fields (fn c => str c = "\n"); |
28051 | 154 |
fun read buf = |
28050
7cef47b53feb
fold_lines: simplified, more efficient due to String.fields;
wenzelm
parents:
28028
diff
changeset
|
155 |
(case split (TextIO.input file) of |
28051 | 156 |
([], "") => (case Buffer.content buf of "" => I | line => f line) |
157 |
| ([], rest) => read (Buffer.add rest buf) |
|
28050
7cef47b53feb
fold_lines: simplified, more efficient due to String.fields;
wenzelm
parents:
28028
diff
changeset
|
158 |
| (line :: lines, rest) => |
28051 | 159 |
f (Buffer.content (Buffer.add line buf)) |
160 |
#> fold f lines |
|
161 |
#> read (Buffer.add rest Buffer.empty)); |
|
28028 | 162 |
in read Buffer.empty a end) path; |
163 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
164 |
val read = open_input TextIO.inputAll; |
5009 | 165 |
|
28028 | 166 |
|
167 |
(* output *) |
|
168 |
||
169 |
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts; |
|
170 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
171 |
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
|
172 |
fun append_list path txts = open_append (output txts) path; |
16713 | 173 |
|
174 |
fun write path txt = write_list path [txt]; |
|
175 |
fun append path txt = append_list path [txt]; |
|
6182 | 176 |
|
28028 | 177 |
fun write_buffer path buf = open_output (Buffer.output buf) path; |
178 |
||
179 |
||
180 |
(* copy *) |
|
5009 | 181 |
|
16603 | 182 |
fun eq paths = |
183 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
26656 | 184 |
SOME ids => is_equal (OS.FileSys.compare ids) |
16603 | 185 |
| NONE => false); |
186 |
||
21962 | 187 |
fun copy src dst = |
188 |
if eq (src, dst) then () |
|
189 |
else |
|
190 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
191 |
in write target (read src) end; |
|
6318 | 192 |
|
21962 | 193 |
fun copy_dir src dst = |
194 |
if eq (src, dst) then () |
|
195 |
else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ()); |
|
6640 | 196 |
|
5009 | 197 |
end; |