author | wenzelm |
Wed, 29 Jun 2005 15:13:33 +0200 | |
changeset 16603 | 1776d276f848 |
parent 16304 | 5e845b75f94f |
child 16713 | be5763901788 |
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 |
|
16603 | 25 |
val eq: Path.T * Path.T -> bool |
6182 | 26 |
val copy: Path.T -> Path.T -> unit |
16261 | 27 |
val copy_dir: Path.T -> Path.T -> unit |
6224 | 28 |
eqtype info |
29 |
val info: Path.T -> info option |
|
30 |
val exists: Path.T -> bool |
|
16261 | 31 |
val rm: Path.T -> unit |
6318 | 32 |
val mkdir: Path.T -> unit |
12116 | 33 |
val use: Path.T -> unit |
5009 | 34 |
end; |
35 |
||
36 |
structure File: FILE = |
|
37 |
struct |
|
38 |
||
16261 | 39 |
(* platform specific path representations *) |
6224 | 40 |
|
16261 | 41 |
val unpack_platform_path_fn = ref Path.unpack; |
42 |
fun unpack_platform_path s = ! unpack_platform_path_fn s; |
|
6224 | 43 |
|
16261 | 44 |
val platform_path_fn = ref (Path.pack o Path.expand); |
45 |
fun platform_path path = ! platform_path_fn path; |
|
6224 | 46 |
|
16261 | 47 |
val shell_path_fn = ref (enclose "'" "'" o Path.pack o Path.expand); |
48 |
fun shell_path path = ! shell_path_fn path; |
|
6224 | 49 |
|
50 |
||
51 |
(* current path *) |
|
52 |
||
16261 | 53 |
val cd = Library.cd o platform_path; |
54 |
val pwd = unpack_platform_path o Library.pwd; |
|
6224 | 55 |
|
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
56 |
fun norm_absolute p = |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
57 |
let |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
58 |
val p' = pwd (); |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
59 |
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
|
60 |
else Path.append (norm (Path.dir p)) (Path.base p); |
16261 | 61 |
val p'' = norm p; |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
62 |
in (cd p'; p'') end |
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
63 |
|
16261 | 64 |
fun full_path path = |
15155
6cdd6a8da9b9
Added function for "normalizing" absolute paths (i.e. dereferencing of
berghofe
parents:
14981
diff
changeset
|
65 |
(if Path.is_absolute path then path |
16261 | 66 |
else Path.append (pwd ()) path) |
67 |
|> norm_absolute; |
|
5009 | 68 |
|
6182 | 69 |
|
70 |
(* tmp_path *) |
|
71 |
||
72 |
fun tmp_path path = |
|
73 |
Path.append (Path.variable "ISABELLE_TMP") (Path.base path); |
|
5009 | 74 |
|
75 |
||
16261 | 76 |
(* system commands *) |
77 |
||
78 |
fun isatool cmd = system ("\"$ISATOOL\" " ^ cmd); |
|
79 |
||
80 |
fun system_command cmd = |
|
81 |
if system cmd <> 0 then error ("System command failed: " ^ cmd) |
|
82 |
else (); |
|
83 |
||
84 |
||
85 |
(* directory entries *) |
|
86 |
||
87 |
datatype info = Info of string; |
|
88 |
||
89 |
fun info path = |
|
90 |
Option.map (Info o Time.toString) (try OS.FileSys.modTime (platform_path path)); |
|
91 |
||
92 |
val exists = is_some o info; |
|
93 |
||
94 |
val rm = OS.FileSys.remove o platform_path; |
|
95 |
||
96 |
fun mkdir path = system_command ("mkdir -p " ^ shell_path path); |
|
97 |
||
98 |
fun is_dir path = |
|
99 |
if_none (try OS.FileSys.isDir (platform_path path)) false; |
|
100 |
||
101 |
||
6224 | 102 |
(* read / write files *) |
103 |
||
16261 | 104 |
local |
105 |
||
6224 | 106 |
fun fail_safe f g x = |
107 |
let val y = f x handle exn => (g x; raise exn) |
|
108 |
in g x; y end; |
|
109 |
||
110 |
fun output txt stream = TextIO.output (stream, txt); |
|
6182 | 111 |
|
16261 | 112 |
in |
6218 | 113 |
|
16261 | 114 |
fun read path = |
115 |
fail_safe TextIO.inputAll TextIO.closeIn (TextIO.openIn (platform_path path)); |
|
6224 | 116 |
|
16261 | 117 |
fun write path txt = |
118 |
fail_safe (output txt) TextIO.closeOut (TextIO.openOut (platform_path path)); |
|
5009 | 119 |
|
16261 | 120 |
fun append path txt = |
121 |
fail_safe (output txt) TextIO.closeOut (TextIO.openAppend (platform_path path)); |
|
6182 | 122 |
|
16261 | 123 |
end; |
5009 | 124 |
|
16603 | 125 |
fun eq paths = |
126 |
(case try (pairself (OS.FileSys.fileId o platform_path)) paths of |
|
127 |
SOME ids => OS.FileSys.compare ids = EQUAL |
|
128 |
| NONE => false); |
|
129 |
||
130 |
fun copy from to = conditional (not (eq (from, to))) (fn () => |
|
16261 | 131 |
let val target = if is_dir to then Path.append to (Path.base from) else to |
16603 | 132 |
in write target (read from) end); |
6318 | 133 |
|
16603 | 134 |
fun copy_dir from to = conditional (not (eq (from, to))) (fn () => |
135 |
(system_command ("cp -r " ^ shell_path from ^ "/. " ^ shell_path to); ())) |
|
6640 | 136 |
|
137 |
||
12116 | 138 |
(* use ML files *) |
6640 | 139 |
|
16261 | 140 |
val use = use o platform_path; |
6640 | 141 |
|
5009 | 142 |
end; |