author | paulson |
Fri, 05 Oct 2007 09:59:03 +0200 | |
changeset 24854 | 0ebcd575d3c6 |
parent 24446 | bb7a85ea57cf |
child 26220 | d34b68c21f9a |
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 => |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
101 |
let val id = execute ("\"$ISABELLE_HOME/lib/scripts/fileident\" " ^ shell_path path) in |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
102 |
if id <> "" then id |
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
103 |
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
|
104 |
end))); |
16261 | 105 |
|
23861
72bb3494746f
replaced info by ident (for full identification, potentially content-based);
wenzelm
parents:
23826
diff
changeset
|
106 |
val exists = can OS.FileSys.modTime o platform_path; |
16261 | 107 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
108 |
fun check path = |
17826 | 109 |
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
|
110 |
else error ("No such file or directory: " ^ quote (Path.implode path)); |
17826 | 111 |
|
16261 | 112 |
val rm = OS.FileSys.remove o platform_path; |
113 |
||
114 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
115 |
||
116 |
fun is_dir path = |
|
19473 | 117 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
16261 | 118 |
|
119 |
||
6224 | 120 |
(* read / write files *) |
121 |
||
16261 | 122 |
local |
123 |
||
6224 | 124 |
fun fail_safe f g x = |
125 |
let val y = f x handle exn => (g x; raise exn) |
|
126 |
in g x; y end; |
|
127 |
||
16713 | 128 |
fun output txts stream = List.app (fn txt => TextIO.output (stream, txt)) txts; |
6182 | 129 |
|
16261 | 130 |
in |
6218 | 131 |
|
24360
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
132 |
fun read path = |
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
133 |
fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (platform_path path)); |
6224 | 134 |
|
24360
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
135 |
fun write_list path txts = |
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
136 |
fail_safe (output txts) TextIO.closeOut (TextIO.openOut (platform_path path)); |
5009 | 137 |
|
24360
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
138 |
fun append_list path txts = |
a0da34cc081c
File.read/write/append: non-critical (basic IO operations already thread-safe);
wenzelm
parents:
24058
diff
changeset
|
139 |
fail_safe (output txts) TextIO.closeOut (TextIO.openAppend (platform_path path)); |
16713 | 140 |
|
141 |
fun write path txt = write_list path [txt]; |
|
142 |
fun append path txt = append_list path [txt]; |
|
6182 | 143 |
|
16261 | 144 |
end; |
5009 | 145 |
|
16603 | 146 |
fun eq paths = |
147 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
148 |
SOME ids => OS.FileSys.compare ids = EQUAL |
|
149 |
| NONE => false); |
|
150 |
||
21962 | 151 |
fun copy src dst = |
152 |
if eq (src, dst) then () |
|
153 |
else |
|
154 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
155 |
in write target (read src) end; |
|
6318 | 156 |
|
21962 | 157 |
fun copy_dir src dst = |
158 |
if eq (src, dst) then () |
|
159 |
else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ()); |
|
6640 | 160 |
|
5009 | 161 |
end; |