author | wenzelm |
Wed, 06 Jul 2005 10:41:47 +0200 | |
changeset 16713 | be5763901788 |
parent 16603 | 1776d276f848 |
child 17826 | afa2696eacce |
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 unpack_platform_path_fn: (string -> Path.T) ref |
11 |
val unpack_platform_path: string -> Path.T |
|
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 |
|
6182 | 22 |
val read: Path.T -> string |
23 |
val write: Path.T -> string -> unit |
|
24 |
val append: Path.T -> string -> unit |
|
16713 | 25 |
val write_list: Path.T -> string list -> unit |
26 |
val append_list: Path.T -> string list -> unit |
|
16603 | 27 |
val eq: Path.T * Path.T -> bool |
6182 | 28 |
val copy: Path.T -> Path.T -> unit |
16261 | 29 |
val copy_dir: Path.T -> Path.T -> unit |
6224 | 30 |
eqtype info |
31 |
val info: Path.T -> info option |
|
32 |
val exists: Path.T -> bool |
|
16261 | 33 |
val rm: Path.T -> unit |
6318 | 34 |
val mkdir: Path.T -> unit |
12116 | 35 |
val use: Path.T -> unit |
5009 | 36 |
end; |
37 |
||
38 |
structure File: FILE = |
|
39 |
struct |
|
40 |
||
16261 | 41 |
(* platform specific path representations *) |
6224 | 42 |
|
16261 | 43 |
val unpack_platform_path_fn = ref Path.unpack; |
44 |
fun unpack_platform_path s = ! unpack_platform_path_fn s; |
|
6224 | 45 |
|
16261 | 46 |
val platform_path_fn = ref (Path.pack o Path.expand); |
47 |
fun platform_path path = ! platform_path_fn path; |
|
6224 | 48 |
|
16261 | 49 |
val shell_path_fn = ref (enclose "'" "'" o Path.pack o Path.expand); |
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; |
56 |
val pwd = unpack_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 |
||
96 |
val rm = OS.FileSys.remove o platform_path; |
|
97 |
||
98 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
99 |
||
100 |
fun is_dir path = |
|
101 |
if_none (try OS.FileSys.isDir (platform_path path)) false; |
|
102 |
||
103 |
||
6224 | 104 |
(* read / write files *) |
105 |
||
16261 | 106 |
local |
107 |
||
6224 | 108 |
fun fail_safe f g x = |
109 |
let val y = f x handle exn => (g x; raise exn) |
|
110 |
in g x; y end; |
|
111 |
||
16713 | 112 |
fun output txts stream = List.app (fn txt => TextIO.output (stream, txt)) txts; |
6182 | 113 |
|
16261 | 114 |
in |
6218 | 115 |
|
16261 | 116 |
fun read path = |
117 |
fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (platform_path path)); |
|
6224 | 118 |
|
16713 | 119 |
fun write_list path txts = |
120 |
fail_safe (output txts) TextIO.closeOut (TextIO.openOut (platform_path path)); |
|
5009 | 121 |
|
16713 | 122 |
fun append_list path txts = |
123 |
fail_safe (output txts) TextIO.closeOut (TextIO.openAppend (platform_path path)); |
|
124 |
||
125 |
fun write path txt = write_list path [txt]; |
|
126 |
fun append path txt = append_list path [txt]; |
|
6182 | 127 |
|
16261 | 128 |
end; |
5009 | 129 |
|
16603 | 130 |
fun eq paths = |
131 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
132 |
SOME ids => OS.FileSys.compare ids = EQUAL |
|
133 |
| NONE => false); |
|
134 |
||
135 |
fun copy from to = conditional (not (eq (from, to))) (fn () => |
|
16261 | 136 |
let val target = if is_dir to then Path.append to (Path.base from) else to |
16603 | 137 |
in write target (read from) end); |
6318 | 138 |
|
16603 | 139 |
fun copy_dir from to = conditional (not (eq (from, to))) (fn () => |
140 |
(system_command ("cp -r " ^ shell_path from ^ "/. " ^ shell_path to); ())) |
|
6640 | 141 |
|
142 |
||
12116 | 143 |
(* use ML files *) |
6640 | 144 |
|
16261 | 145 |
val use = use o platform_path; |
6640 | 146 |
|
5009 | 147 |
end; |