author | wenzelm |
Sun, 21 Jan 2007 16:43:44 +0100 | |
changeset 22145 | fedad292f738 |
parent 21962 | 279b129498b6 |
child 23826 | 463903573934 |
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 |
|
17826 | 22 |
eqtype info |
23 |
val info: Path.T -> info option |
|
24 |
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
|
25 |
val check: Path.T -> unit |
17826 | 26 |
val rm: Path.T -> unit |
27 |
val mkdir: Path.T -> unit |
|
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 |
||
16261 | 41 |
(* platform specific path representations *) |
6224 | 42 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
43 |
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
|
44 |
fun explode_platform_path s = ! explode_platform_path_fn s; |
6224 | 45 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
46 |
val platform_path_fn = ref (Path.implode o Path.expand); |
16261 | 47 |
fun platform_path path = ! platform_path_fn path; |
6224 | 48 |
|
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
49 |
val shell_path_fn = ref (enclose "'" "'" o Path.implode o Path.expand); |
16261 | 50 |
fun shell_path path = ! shell_path_fn path; |
6224 | 51 |
|
52 |
||
53 |
(* current path *) |
|
54 |
||
16261 | 55 |
val cd = Library.cd o platform_path; |
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
56 |
val pwd = explode_platform_path o Library.pwd; |
6224 | 57 |
|
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
58 |
fun norm_absolute p = |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
59 |
let |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
60 |
val p' = pwd (); |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
61 |
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
|
62 |
else Path.append (norm (Path.dir p)) (Path.base p); |
16261 | 63 |
val p'' = norm p; |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
64 |
in (cd p'; p'') end |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
65 |
|
16261 | 66 |
fun full_path path = |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
67 |
(if Path.is_absolute path then path |
16261 | 68 |
else Path.append (pwd ()) path) |
69 |
|> norm_absolute; |
|
5009 | 70 |
|
6182 | 71 |
|
72 |
(* tmp_path *) |
|
73 |
||
74 |
fun tmp_path path = |
|
75 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 76 |
|
77 |
||
16261 | 78 |
(* system commands *) |
79 |
||
80 |
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd); |
|
81 |
||
82 |
fun system_command cmd = |
|
83 |
if system cmd <> 0 then error ("System command failed: " ^ cmd) |
|
84 |
else (); |
|
85 |
||
86 |
||
87 |
(* directory entries *) |
|
88 |
||
89 |
datatype info = Info of string; |
|
90 |
||
91 |
fun info path = |
|
92 |
Option.map (Info o Time.toString) (try OS.FileSys.modTime (platform_path path)); |
|
93 |
||
94 |
val exists = is_some o info; |
|
95 |
||
21858
05f57309170c
avoid conflict with Alice keywords: renamed pack -> implode, unpack -> explode, any -> many, avoided assert;
wenzelm
parents:
19960
diff
changeset
|
96 |
fun check path = |
17826 | 97 |
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
|
98 |
else error ("No such file or directory: " ^ quote (Path.implode path)); |
17826 | 99 |
|
16261 | 100 |
val rm = OS.FileSys.remove o platform_path; |
101 |
||
102 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
103 |
||
104 |
fun is_dir path = |
|
19473 | 105 |
the_default false (try OS.FileSys.isDir (platform_path path)); |
16261 | 106 |
|
107 |
||
6224 | 108 |
(* read / write files *) |
109 |
||
16261 | 110 |
local |
111 |
||
6224 | 112 |
fun fail_safe f g x = |
113 |
let val y = f x handle exn => (g x; raise exn) |
|
114 |
in g x; y end; |
|
115 |
||
16713 | 116 |
fun output txts stream = List.app (fn txt => TextIO.output (stream, txt)) txts; |
6182 | 117 |
|
16261 | 118 |
in |
6218 | 119 |
|
16261 | 120 |
fun read path = |
121 |
fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (platform_path path)); |
|
6224 | 122 |
|
16713 | 123 |
fun write_list path txts = |
124 |
fail_safe (output txts) TextIO.closeOut (TextIO.openOut (platform_path path)); |
|
5009 | 125 |
|
16713 | 126 |
fun append_list path txts = |
127 |
fail_safe (output txts) TextIO.closeOut (TextIO.openAppend (platform_path path)); |
|
128 |
||
129 |
fun write path txt = write_list path [txt]; |
|
130 |
fun append path txt = append_list path [txt]; |
|
6182 | 131 |
|
16261 | 132 |
end; |
5009 | 133 |
|
16603 | 134 |
fun eq paths = |
135 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
136 |
SOME ids => OS.FileSys.compare ids = EQUAL |
|
137 |
| NONE => false); |
|
138 |
||
21962 | 139 |
fun copy src dst = |
140 |
if eq (src, dst) then () |
|
141 |
else |
|
142 |
let val target = if is_dir dst then Path.append dst (Path.base src) else dst |
|
143 |
in write target (read src) end; |
|
6318 | 144 |
|
21962 | 145 |
fun copy_dir src dst = |
146 |
if eq (src, dst) then () |
|
147 |
else (system_command ("cp -r -f " ^ shell_path src ^ "/. " ^ shell_path dst); ()); |
|
6640 | 148 |
|
5009 | 149 |
end; |