author | wenzelm |
Mon, 31 Mar 2008 23:08:54 +0200 | |
changeset 26503 | 4dec4460244f |
parent 26220 | d34b68c21f9a |
child 26656 | 62fff5feb756 |
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 |
6182 | 28 |
val read: Path.T -> string |
29 |
val write: Path.T -> string -> unit |
|
30 |
val append: Path.T -> string -> unit |
|
16713 | 31 |
val write_list: Path.T -> string list -> unit |
32 |
val append_list: Path.T -> string list -> 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; |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
44 |
val shell_path = enclose "'" "'" o Path.implode o Path.expand; |
6224 | 45 |
|
46 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
47 |
(* current working directory *) |
6224 | 48 |
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22145
diff
changeset
|
49 |
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
|
50 |
val pwd = Path.explode o pwd; |
6224 | 51 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
52 |
(* FIXME not thread-safe! *) |
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
53 |
fun norm_absolute p = |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
54 |
let |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
55 |
val p' = pwd (); |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
56 |
fun norm p = if can cd p then pwd () |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
57 |
else Path.append (norm (Path.dir p)) (Path.base p); |
16261 | 58 |
val p'' = norm p; |
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
59 |
in (cd p'; p'') end; |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
60 |
|
16261 | 61 |
fun full_path path = |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
62 |
(if Path.is_absolute path then path |
16261 | 63 |
else Path.append (pwd ()) path) |
64 |
|> norm_absolute; |
|
5009 | 65 |
|
6182 | 66 |
|
67 |
(* tmp_path *) |
|
68 |
||
69 |
fun tmp_path path = |
|
70 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 71 |
|
72 |
||
16261 | 73 |
(* system commands *) |
74 |
||
75 |
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd); |
|
76 |
||
77 |
fun system_command cmd = |
|
78 |
if system cmd <> 0 then error ("System command failed: " ^ cmd) |
|
79 |
else (); |
|
80 |
||
81 |
||
82 |
(* directory entries *) |
|
83 |
||
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
84 |
datatype ident = Ident of string; |
16261 | 85 |
|
23874 | 86 |
fun rep_ident (Ident s) = s; |
87 |
||
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
88 |
fun ident path = |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
89 |
(case try OS.FileSys.modTime (platform_path path) of |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
90 |
NONE => NONE |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
91 |
| SOME time => SOME (Ident |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
92 |
(case getenv "ISABELLE_FILE_IDENT" of |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
93 |
"" => Path.implode (full_path path) ^ ": " ^ Time.toString time |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
94 |
| cmd => |
26220 | 95 |
let |
96 |
val (id, rc) = system_out ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ shell_path path) |
|
97 |
in |
|
98 |
if id <> "" andalso rc = 0 then id |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
99 |
else error ("Failed to identify file " ^ quote (Path.implode path) ^ " by " ^ cmd) |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
100 |
end))); |
16261 | 101 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
102 |
val exists = can OS.FileSys.modTime o platform_path; |
16261 | 103 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
104 |
fun check path = |
17826 | 105 |
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
|
106 |
else error ("No such file or directory: " ^ quote (Path.implode path)); |
17826 | 107 |
|
16261 | 108 |
val rm = OS.FileSys.remove o platform_path; |
109 |
||
110 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
111 |
||
112 |
fun is_dir path = |
|
19473 | 113 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
16261 | 114 |
|
115 |
||
6224 | 116 |
(* read / write files *) |
117 |
||
16261 | 118 |
local |
119 |
||
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
120 |
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
|
121 |
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
|
122 |
in Exn.release (Exn.capture f file before close_file file) end; |
6224 | 123 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
124 |
fun output txts file = List.app (fn txt => TextIO.output (file, txt)) txts; |
6182 | 125 |
|
16261 | 126 |
in |
6218 | 127 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
128 |
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
|
129 |
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
|
130 |
fun open_append f = with_file TextIO.openAppend TextIO.closeOut f o platform_path; |
6224 | 131 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
132 |
val read = open_input TextIO.inputAll; |
5009 | 133 |
|
26503
4dec4460244f
discontinued unused hooks explode_platform_path_fn, platform_path_fn, shell_path_fn;
wenzelm
parents:
26220
diff
changeset
|
134 |
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
|
135 |
fun append_list path txts = open_append (output txts) path; |
16713 | 136 |
|
137 |
fun write path txt = write_list path [txt]; |
|
138 |
fun append path txt = append_list path [txt]; |
|
6182 | 139 |
|
16261 | 140 |
end; |
5009 | 141 |
|
16603 | 142 |
fun eq paths = |
143 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
144 |
SOME ids => OS.FileSys.compare ids = EQUAL |
|
145 |
| NONE => false); |
|
146 |
||
21962 | 147 |
fun copy src dst = |
148 |
if eq (src, dst) then () |
|
149 |
else |
|
150 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
151 |
in write target (read src) end; |
|
6318 | 152 |
|
21962 | 153 |
fun copy_dir src dst = |
154 |
if eq (src, dst) then () |
|
155 |
else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ()); |
|
6640 | 156 |
|
5009 | 157 |
end; |