author | wenzelm |
Sat, 27 Nov 2010 14:19:04 +0100 | |
changeset 40741 | 17d6293a1e26 |
parent 37739 | 312fe7201f88 |
child 40743 | b07a0dbc8a38 |
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 |
17826 | 17 |
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
|
18 |
val check: Path.T -> unit |
17826 | 19 |
val rm: Path.T -> unit |
37739 | 20 |
val rm_tree: Path.T -> unit |
17826 | 21 |
val mkdir: Path.T -> unit |
37651
62fc16341922
mkdir_leaf -- avoiding surprises with typos in user-given paths
haftmann
parents:
35010
diff
changeset
|
22 |
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
|
23 |
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
|
24 |
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
|
25 |
val open_append: (TextIO.outstream -> 'a) -> Path.T -> 'a |
28028 | 26 |
val fold_lines: (string -> 'a -> 'a) -> Path.T -> 'a -> 'a |
6182 | 27 |
val read: Path.T -> string |
28 |
val write: Path.T -> string -> unit |
|
29 |
val append: Path.T -> string -> unit |
|
16713 | 30 |
val write_list: Path.T -> string list -> unit |
31 |
val append_list: Path.T -> string list -> unit |
|
28028 | 32 |
val write_buffer: Path.T -> Buffer.T -> unit |
16603 | 33 |
val eq: Path.T * Path.T -> bool |
6182 | 34 |
val copy: Path.T -> Path.T -> unit |
16261 | 35 |
val copy_dir: Path.T -> Path.T -> unit |
5009 | 36 |
end; |
37 |
||
38 |
structure File: FILE = |
|
39 |
struct |
|
40 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
41 |
(* system path representations *) |
6224 | 42 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
43 |
val platform_path = Path.implode o Path.expand; |
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
44 |
|
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
45 |
val shell_quote = enclose "'" "'"; |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
46 |
val shell_path = shell_quote o platform_path; |
6224 | 47 |
|
48 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
49 |
(* current working directory *) |
6224 | 50 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22145
diff
changeset
|
51 |
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
|
52 |
val pwd = Path.explode o pwd; |
6224 | 53 |
|
16261 | 54 |
fun full_path path = |
26946
0b6eff8c088d
removed norm_absolute (not thread safe; chdir does not guarantee normalization anyway);
wenzelm
parents:
26656
diff
changeset
|
55 |
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
|
56 |
else Path.append (pwd ()) path; |
5009 | 57 |
|
6182 | 58 |
|
59 |
(* tmp_path *) |
|
60 |
||
61 |
fun tmp_path path = |
|
62 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 63 |
|
64 |
||
16261 | 65 |
(* system commands *) |
66 |
||
31818
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
67 |
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
|
68 |
(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
|
69 |
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
|
70 |
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
|
71 |
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
|
72 |
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
|
73 |
then SOME path |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
74 |
else NONE |
f698f76a3713
builtin isabelle_tool for ML and Scala -- avoids excessive shell script (especially important for Cygwin);
wenzelm
parents:
29606
diff
changeset
|
75 |
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
|
76 |
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
|
77 |
| NONE => (writeln ("Unknown Isabelle tool: " ^ name); 2)); |
16261 | 78 |
|
79 |
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
|
80 |
if bash cmd <> 0 then error ("System command failed: " ^ cmd) |
16261 | 81 |
else (); |
82 |
||
83 |
||
26980
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
84 |
(* directory entries *) |
f7f48bb9a025
ident: naive caching prevents potentially slow external invocations;
wenzelm
parents:
26946
diff
changeset
|
85 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
86 |
val exists = can OS.FileSys.modTime o platform_path; |
16261 | 87 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
88 |
fun check path = |
17826 | 89 |
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
|
90 |
else error ("No such file or directory: " ^ quote (Path.implode path)); |
17826 | 91 |
|
16261 | 92 |
val rm = OS.FileSys.remove o platform_path; |
93 |
||
37739 | 94 |
fun rm_tree path = system_command ("rm -r " ^ shell_path path); |
95 |
||
16261 | 96 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
97 |
||
37668
892f8d00426c
refined semantics of mkdir_leaf: do not fail if directory already exists
haftmann
parents:
37651
diff
changeset
|
98 |
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
|
99 |
|
16261 | 100 |
fun is_dir path = |
19473 | 101 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
16261 | 102 |
|
103 |
||
28028 | 104 |
(* open files *) |
6224 | 105 |
|
16261 | 106 |
local |
107 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
108 |
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
|
109 |
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
|
110 |
in Exn.release (Exn.capture f file before close_file file) end; |
6224 | 111 |
|
16261 | 112 |
in |
6218 | 113 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
114 |
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
|
115 |
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
|
116 |
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path; |
6224 | 117 |
|
28028 | 118 |
end; |
119 |
||
120 |
||
121 |
(* input *) |
|
122 |
||
123 |
(*scalable iterator -- avoid size limit of TextIO.inputAll, and overhead of many TextIO.inputLine*) |
|
124 |
fun fold_lines f path a = open_input (fn file => |
|
125 |
let |
|
28510
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
126 |
fun read buf x = |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
127 |
(case TextIO.input file of |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
128 |
"" => (case Buffer.content buf of "" => x | line => f line x) |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
129 |
| input => |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
130 |
(case String.fields (fn c => c = #"\n") input of |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
131 |
[rest] => read (Buffer.add rest buf) x |
66b95e857bde
fold_lines: more tuning, avoiding extra split_last;
wenzelm
parents:
28500
diff
changeset
|
132 |
| 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
|
133 |
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
|
134 |
| read_lines (line :: more) x = read_lines more (f line x); |
28028 | 135 |
in read Buffer.empty a end) path; |
136 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
137 |
val read = open_input TextIO.inputAll; |
5009 | 138 |
|
28028 | 139 |
|
140 |
(* output *) |
|
141 |
||
142 |
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts; |
|
143 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
144 |
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
|
145 |
fun append_list path txts = open_append (output txts) path; |
16713 | 146 |
|
147 |
fun write path txt = write_list path [txt]; |
|
148 |
fun append path txt = append_list path [txt]; |
|
6182 | 149 |
|
28028 | 150 |
fun write_buffer path buf = open_output (Buffer.output buf) path; |
151 |
||
152 |
||
153 |
(* copy *) |
|
5009 | 154 |
|
16603 | 155 |
fun eq paths = |
156 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
26656 | 157 |
SOME ids => is_equal (OS.FileSys.compare ids) |
16603 | 158 |
| NONE => false); |
159 |
||
21962 | 160 |
fun copy src dst = |
161 |
if eq (src, dst) then () |
|
162 |
else |
|
163 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
164 |
in write target (read src) end; |
|
6318 | 165 |
|
21962 | 166 |
fun copy_dir src dst = |
167 |
if eq (src, dst) then () |
|
168 |
else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ()); |
|
6640 | 169 |
|
5009 | 170 |
end; |