| author | wenzelm |
| Tue, 11 Mar 2008 22:31:09 +0100 | |
| changeset 26256 | 3e7939e978c6 |
| parent 26220 | d34b68c21f9a |
| child 26503 | 4dec4460244f |
| 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 |
|
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
10 |
val explode_platform_path_fn: (string -> Path.T) ref |
|
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
11 |
val explode_platform_path: string -> Path.T |
| 16261 | 12 |
val platform_path_fn: (Path.T -> string) ref |
13 |
val platform_path: Path.T -> string |
|
14 |
val shell_path_fn: (Path.T -> string) ref |
|
15 |
val shell_path: Path.T -> string |
|
| 6224 | 16 |
val cd: Path.T -> unit |
17 |
val pwd: unit -> Path.T |
|
18 |
val full_path: Path.T -> Path.T |
|
| 6182 | 19 |
val tmp_path: Path.T -> Path.T |
| 16261 | 20 |
val isatool: string -> int |
21 |
val system_command: string -> unit |
|
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
22 |
eqtype ident |
| 23874 | 23 |
val rep_ident: ident -> string |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
24 |
val ident: Path.T -> ident option |
| 17826 | 25 |
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
|
26 |
val check: Path.T -> unit |
| 17826 | 27 |
val rm: Path.T -> unit |
28 |
val mkdir: Path.T -> unit |
|
| 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 |
|
| 16603 | 34 |
val eq: Path.T * Path.T -> bool |
| 6182 | 35 |
val copy: Path.T -> Path.T -> unit |
| 16261 | 36 |
val copy_dir: Path.T -> Path.T -> unit |
| 5009 | 37 |
end; |
38 |
||
39 |
structure File: FILE = |
|
40 |
struct |
|
41 |
||
| 16261 | 42 |
(* platform specific path representations *) |
| 6224 | 43 |
|
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
44 |
val explode_platform_path_fn = ref Path.explode; |
|
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
45 |
fun explode_platform_path s = ! explode_platform_path_fn s; |
| 6224 | 46 |
|
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
47 |
val platform_path_fn = ref (Path.implode o Path.expand); |
| 16261 | 48 |
fun platform_path path = ! platform_path_fn path; |
| 6224 | 49 |
|
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
50 |
val shell_path_fn = ref (enclose "'" "'" o Path.implode o Path.expand); |
| 16261 | 51 |
fun shell_path path = ! shell_path_fn path; |
| 6224 | 52 |
|
53 |
||
54 |
(* current path *) |
|
55 |
||
|
23826
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22145
diff
changeset
|
56 |
val cd = cd o platform_path; |
|
463903573934
moved cd/pwd to ML compatibility layer (simplifies bootstrapping with Alice);
wenzelm
parents:
22145
diff
changeset
|
57 |
val pwd = explode_platform_path o pwd; |
| 6224 | 58 |
|
| 24446 | 59 |
fun norm_absolute p = NAMED_CRITICAL "IO" (fn () => |
|
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
60 |
let |
|
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
61 |
val p' = pwd (); |
|
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
62 |
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
|
63 |
else Path.append (norm (Path.dir p)) (Path.base p); |
| 16261 | 64 |
val p'' = norm p; |
| 24446 | 65 |
in (cd p'; p'') end); |
|
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
66 |
|
| 16261 | 67 |
fun full_path path = |
|
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
68 |
(if Path.is_absolute path then path |
| 16261 | 69 |
else Path.append (pwd ()) path) |
70 |
|> norm_absolute; |
|
| 5009 | 71 |
|
| 6182 | 72 |
|
73 |
(* tmp_path *) |
|
74 |
||
75 |
fun tmp_path path = |
|
76 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
| 5009 | 77 |
|
78 |
||
| 16261 | 79 |
(* system commands *) |
80 |
||
81 |
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd);
|
|
82 |
||
83 |
fun system_command cmd = |
|
84 |
if system cmd <> 0 then error ("System command failed: " ^ cmd)
|
|
85 |
else (); |
|
86 |
||
87 |
||
88 |
(* directory entries *) |
|
89 |
||
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
90 |
datatype ident = Ident of string; |
| 16261 | 91 |
|
| 23874 | 92 |
fun rep_ident (Ident s) = s; |
93 |
||
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
94 |
fun ident path = |
|
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
95 |
(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
|
96 |
NONE => NONE |
|
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
97 |
| SOME time => SOME (Ident |
|
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
98 |
(case getenv "ISABELLE_FILE_IDENT" of |
|
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
99 |
"" => Path.implode (full_path path) ^ ": " ^ Time.toString time |
|
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
100 |
| cmd => |
| 26220 | 101 |
let |
102 |
val (id, rc) = system_out ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ shell_path path)
|
|
103 |
in |
|
104 |
if id <> "" andalso rc = 0 then id |
|
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
105 |
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
|
106 |
end))); |
| 16261 | 107 |
|
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
108 |
val exists = can OS.FileSys.modTime o platform_path; |
| 16261 | 109 |
|
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
110 |
fun check path = |
| 17826 | 111 |
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
|
112 |
else error ("No such file or directory: " ^ quote (Path.implode path));
|
| 17826 | 113 |
|
| 16261 | 114 |
val rm = OS.FileSys.remove o platform_path; |
115 |
||
116 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path);
|
|
117 |
||
118 |
fun is_dir path = |
|
| 19473 | 119 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
| 16261 | 120 |
|
121 |
||
| 6224 | 122 |
(* read / write files *) |
123 |
||
| 16261 | 124 |
local |
125 |
||
| 6224 | 126 |
fun fail_safe f g x = |
127 |
let val y = f x handle exn => (g x; raise exn) |
|
128 |
in g x; y end; |
|
129 |
||
| 16713 | 130 |
fun output txts stream = List.app (fn txt => TextIO.output (stream, txt)) txts; |
| 6182 | 131 |
|
| 16261 | 132 |
in |
| 6218 | 133 |
|
|
24360
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
134 |
fun read path = |
|
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
135 |
fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (platform_path path)); |
| 6224 | 136 |
|
|
24360
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
137 |
fun write_list path txts = |
|
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
138 |
fail_safe (output txts) TextIO.closeOut (TextIO.openOut (platform_path path)); |
| 5009 | 139 |
|
|
24360
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
140 |
fun append_list path txts = |
|
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
141 |
fail_safe (output txts) TextIO.closeOut (TextIO.openAppend (platform_path path)); |
| 16713 | 142 |
|
143 |
fun write path txt = write_list path [txt]; |
|
144 |
fun append path txt = append_list path [txt]; |
|
| 6182 | 145 |
|
| 16261 | 146 |
end; |
| 5009 | 147 |
|
| 16603 | 148 |
fun eq paths = |
149 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
150 |
SOME ids => OS.FileSys.compare ids = EQUAL |
|
151 |
| NONE => false); |
|
152 |
||
| 21962 | 153 |
fun copy src dst = |
154 |
if eq (src, dst) then () |
|
155 |
else |
|
156 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
157 |
in write target (read src) end; |
|
| 6318 | 158 |
|
| 21962 | 159 |
fun copy_dir src dst = |
160 |
if eq (src, dst) then () |
|
161 |
else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ());
|
|
| 6640 | 162 |
|
| 5009 | 163 |
end; |