author | wenzelm |
Fri, 20 Jan 2023 21:52:29 +0100 | |
changeset 77034 | abd4a0f48e49 |
parent 77000 | ffc0774e0efe |
child 77035 | 28ac56e59d23 |
permissions | -rw-r--r-- |
43600 | 1 |
/* Title: Pure/General/path.scala |
2 |
Author: Makarius |
|
3 |
||
43601 | 4 |
Algebra of file-system paths: basic POSIX notation, extended by named |
5 |
roots (e.g. //foo) and variables (e.g. $BAR). |
|
43600 | 6 |
*/ |
7 |
||
8 |
package isabelle |
|
9 |
||
10 |
||
73897 | 11 |
import java.util.{Map => JMap} |
48409 | 12 |
import java.io.{File => JFile} |
73945 | 13 |
import java.nio.file.{Path => JPath} |
48373 | 14 |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
15 |
import scala.util.matching.Regex |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
16 |
|
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
17 |
|
75393 | 18 |
object Path { |
43600 | 19 |
/* path elements */ |
20 |
||
45244 | 21 |
sealed abstract class Elem |
60215 | 22 |
private case class Root(name: String) extends Elem |
23 |
private case class Basic(name: String) extends Elem |
|
24 |
private case class Variable(name: String) extends Elem |
|
43600 | 25 |
private case object Parent extends Elem |
26 |
||
27 |
private def err_elem(msg: String, s: String): Nothing = |
|
69547 | 28 |
error(msg + " path element " + quote(s)) |
29 |
||
69548
415dc92050a6
more strict check: avoid confusion of Path.basic with Path.current / Path.parent;
wenzelm
parents:
69547
diff
changeset
|
30 |
private val illegal_elem = Set("", "~", "~~", ".", "..") |
69550
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
31 |
private val illegal_char = "/\\$:\"'<>|?*" |
43600 | 32 |
|
33 |
private def check_elem(s: String): String = |
|
69547 | 34 |
if (illegal_elem.contains(s)) err_elem("Illegal", s) |
52106 | 35 |
else { |
69550
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
36 |
for (c <- s) { |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
37 |
if (c.toInt < 32) |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
38 |
err_elem("Illegal control character " + c.toInt + " in", s) |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
39 |
if (illegal_char.contains(c)) |
57ff523d9008
reject further illegal chars according to https://docs.microsoft.com/en-us/windows/desktop/fileio/naming-a-file
wenzelm
parents:
69549
diff
changeset
|
40 |
err_elem("Illegal character " + quote(c.toString) + " in", s) |
69547 | 41 |
} |
52106 | 42 |
s |
43 |
} |
|
43600 | 44 |
|
45 |
private def root_elem(s: String): Elem = Root(check_elem(s)) |
|
46 |
private def basic_elem(s: String): Elem = Basic(check_elem(s)) |
|
47 |
private def variable_elem(s: String): Elem = Variable(check_elem(s)) |
|
48 |
||
49 |
private def apply_elem(y: Elem, xs: List[Elem]): List[Elem] = |
|
50 |
(y, xs) match { |
|
51 |
case (Root(_), _) => List(y) |
|
52 |
case (Parent, Root(_) :: _) => xs |
|
53 |
case (Parent, Basic(_) :: rest) => rest |
|
54 |
case _ => y :: xs |
|
55 |
} |
|
56 |
||
57 |
private def norm_elems(elems: List[Elem]): List[Elem] = |
|
73361 | 58 |
elems.foldRight(List.empty[Elem])(apply_elem) |
43600 | 59 |
|
56844 | 60 |
private def implode_elem(elem: Elem, short: Boolean): String = |
43600 | 61 |
elem match { |
62 |
case Root("") => "" |
|
63 |
case Root(s) => "//" + s |
|
64 |
case Basic(s) => s |
|
56844 | 65 |
case Variable("USER_HOME") if short => "~" |
66 |
case Variable("ISABELLE_HOME") if short => "~~" |
|
43600 | 67 |
case Variable(s) => "$" + s |
68 |
case Parent => ".." |
|
69 |
} |
|
70 |
||
72962 | 71 |
private def squash_elem(elem: Elem): String = |
72 |
elem match { |
|
73 |
case Root("") => "ROOT" |
|
74 |
case Root(s) => "SERVER_" + s |
|
75 |
case Basic(s) => s |
|
76 |
case Variable(s) => s |
|
77 |
case Parent => "PARENT" |
|
78 |
} |
|
79 |
||
43600 | 80 |
|
81 |
/* path constructors */ |
|
82 |
||
45244 | 83 |
val current: Path = new Path(Nil) |
84 |
val root: Path = new Path(List(Root(""))) |
|
85 |
def named_root(s: String): Path = new Path(List(root_elem(s))) |
|
71601 | 86 |
def make(elems: List[String]): Path = new Path(elems.reverse.map(basic_elem)) |
45244 | 87 |
def basic(s: String): Path = new Path(List(basic_elem(s))) |
88 |
def variable(s: String): Path = new Path(List(variable_elem(s))) |
|
89 |
val parent: Path = new Path(List(Parent)) |
|
43600 | 90 |
|
73522 | 91 |
val USER_HOME: Path = variable("USER_HOME") |
92 |
val ISABELLE_HOME: Path = variable("ISABELLE_HOME") |
|
93 |
||
75926
b8ee1ef948c2
more thorough checks of browser_info file conflicts;
wenzelm
parents:
75701
diff
changeset
|
94 |
val index_html: Path = basic("index.html") |
b8ee1ef948c2
more thorough checks of browser_info file conflicts;
wenzelm
parents:
75701
diff
changeset
|
95 |
|
43600 | 96 |
|
97 |
/* explode */ |
|
98 |
||
75393 | 99 |
def explode(str: String): Path = { |
52106 | 100 |
def explode_elem(s: String): Elem = |
101 |
try { |
|
102 |
if (s == "..") Parent |
|
103 |
else if (s == "~") Variable("USER_HOME") |
|
104 |
else if (s == "~~") Variable("ISABELLE_HOME") |
|
105 |
else if (s.startsWith("$")) variable_elem(s.substring(1)) |
|
106 |
else basic_elem(s) |
|
107 |
} |
|
108 |
catch { case ERROR(msg) => cat_error(msg, "The error(s) above occurred in " + quote(str)) } |
|
109 |
||
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43669
diff
changeset
|
110 |
val ss = space_explode('/', str) |
43600 | 111 |
val r = ss.takeWhile(_.isEmpty).length |
112 |
val es = ss.dropWhile(_.isEmpty) |
|
113 |
val (roots, raw_elems) = |
|
114 |
if (r == 0) (Nil, es) |
|
115 |
else if (r == 1) (List(Root("")), es) |
|
116 |
else if (es.isEmpty) (List(Root("")), Nil) |
|
117 |
else (List(root_elem(es.head)), es.tail) |
|
52106 | 118 |
val elems = raw_elems.filterNot(s => s.isEmpty || s == ".").map(explode_elem) |
119 |
||
63866 | 120 |
new Path(norm_elems(elems reverse_::: roots)) |
43600 | 121 |
} |
43669 | 122 |
|
55879
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
123 |
def is_wellformed(str: String): Boolean = |
48484 | 124 |
try { explode(str); true } catch { case ERROR(_) => false } |
125 |
||
55879
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
126 |
def is_valid(str: String): Boolean = |
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
127 |
try { explode(str).expand; true } catch { case ERROR(_) => false } |
ac979f750c1a
clarified path checks: avoid crash of rendering due to spurious errors;
wenzelm
parents:
55555
diff
changeset
|
128 |
|
43669 | 129 |
def split(str: String): List[Path] = |
43670
7f933761764b
prefer space_explode/split_lines as in Isabelle/ML;
wenzelm
parents:
43669
diff
changeset
|
130 |
space_explode(':', str).filterNot(_.isEmpty).map(explode) |
48457 | 131 |
|
132 |
||
133 |
/* encode */ |
|
134 |
||
135 |
val encode: XML.Encode.T[Path] = (path => XML.Encode.string(path.implode)) |
|
69551
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
136 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
137 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
138 |
/* reserved names */ |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
139 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
140 |
private val reserved_windows: Set[String] = |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
141 |
Set("CON", "PRN", "AUX", "NUL", |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
142 |
"COM1", "COM2", "COM3", "COM4", "COM5", "COM6", "COM7", "COM8", "COM9", |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
143 |
"LPT1", "LPT2", "LPT3", "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9") |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
144 |
|
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
145 |
def is_reserved(name: String): Boolean = |
adb52af5ba55
exclude file name components that are special on Windows;
wenzelm
parents:
69550
diff
changeset
|
146 |
Long_Name.explode(name).exists(a => reserved_windows.contains(Word.uppercase(a))) |
69904 | 147 |
|
148 |
||
149 |
/* case-insensitive names */ |
|
150 |
||
75393 | 151 |
def check_case_insensitive(paths: List[Path]): Unit = { |
69904 | 152 |
val table = |
73359 | 153 |
paths.foldLeft(Multi_Map.empty[String, String]) { case (tab, path) => |
69904 | 154 |
val name = path.expand.implode |
155 |
tab.insert(Word.lowercase(name), name) |
|
73359 | 156 |
} |
69904 | 157 |
val collisions = |
158 |
(for { (_, coll) <- table.iterator_list if coll.length > 1 } yield coll).toList.flatten |
|
159 |
if (collisions.nonEmpty) { |
|
160 |
error(("Collision of file names due case-insensitivity:" :: collisions).mkString("\n ")) |
|
161 |
} |
|
162 |
} |
|
75926
b8ee1ef948c2
more thorough checks of browser_info file conflicts;
wenzelm
parents:
75701
diff
changeset
|
163 |
|
b8ee1ef948c2
more thorough checks of browser_info file conflicts;
wenzelm
parents:
75701
diff
changeset
|
164 |
def eq_case_insensitive(path1: Path, path2: Path): Boolean = |
b8ee1ef948c2
more thorough checks of browser_info file conflicts;
wenzelm
parents:
75701
diff
changeset
|
165 |
path1 == path2 || |
b8ee1ef948c2
more thorough checks of browser_info file conflicts;
wenzelm
parents:
75701
diff
changeset
|
166 |
Word.lowercase(path1.expand.implode) == Word.lowercase(path2.expand.implode) |
43600 | 167 |
} |
168 |
||
43669 | 169 |
|
75393 | 170 |
final class Path private( |
171 |
protected val elems: List[Path.Elem] // reversed elements |
|
172 |
) { |
|
72746 | 173 |
override def hashCode: Int = elems.hashCode |
174 |
override def equals(that: Any): Boolean = |
|
175 |
that match { |
|
176 |
case other: Path => elems == other.elems |
|
177 |
case _ => false |
|
178 |
} |
|
179 |
||
43600 | 180 |
def is_current: Boolean = elems.isEmpty |
59319 | 181 |
def is_absolute: Boolean = elems.nonEmpty && elems.last.isInstanceOf[Path.Root] |
65559 | 182 |
def is_root: Boolean = elems match { case List(Path.Root(_)) => true case _ => false } |
43600 | 183 |
def is_basic: Boolean = elems match { case List(Path.Basic(_)) => true case _ => false } |
75107 | 184 |
def all_basic: Boolean = elems.forall(_.isInstanceOf[Path.Basic]) |
72572 | 185 |
def starts_basic: Boolean = elems.nonEmpty && elems.last.isInstanceOf[Path.Basic] |
43600 | 186 |
|
73360 | 187 |
def +(other: Path): Path = new Path(other.elems.foldRight(elems)(Path.apply_elem)) |
43600 | 188 |
|
189 |
||
43604 | 190 |
/* implode */ |
43600 | 191 |
|
56844 | 192 |
private def gen_implode(short: Boolean): String = |
43600 | 193 |
elems match { |
194 |
case Nil => "." |
|
195 |
case List(Path.Root("")) => "/" |
|
56844 | 196 |
case _ => elems.map(Path.implode_elem(_, short)).reverse.mkString("/") |
43600 | 197 |
} |
56844 | 198 |
def implode: String = gen_implode(false) |
199 |
def implode_short: String = gen_implode(true) |
|
43600 | 200 |
|
43652 | 201 |
override def toString: String = quote(implode) |
43600 | 202 |
|
203 |
||
204 |
/* base element */ |
|
205 |
||
206 |
private def split_path: (Path, String) = |
|
207 |
elems match { |
|
45244 | 208 |
case Path.Basic(s) :: xs => (new Path(xs), s) |
43604 | 209 |
case _ => error("Cannot split path into dir/base: " + toString) |
43600 | 210 |
} |
211 |
||
212 |
def dir: Path = split_path._1 |
|
45244 | 213 |
def base: Path = new Path(List(Path.Basic(split_path._2))) |
43600 | 214 |
|
74056 | 215 |
def ends_with(a: String): Boolean = |
216 |
elems match { |
|
217 |
case Path.Basic(b) :: _ => b.endsWith(a) |
|
218 |
case _ => false |
|
219 |
} |
|
220 |
def is_java: Boolean = ends_with(".java") |
|
221 |
def is_scala: Boolean = ends_with(".scala") |
|
75119 | 222 |
def is_pdf: Boolean = ends_with(".pdf") |
77000
ffc0774e0efe
clarified file positions: retain original source path;
wenzelm
parents:
76884
diff
changeset
|
223 |
def is_latex: Boolean = |
ffc0774e0efe
clarified file positions: retain original source path;
wenzelm
parents:
76884
diff
changeset
|
224 |
ends_with(".tex") || |
ffc0774e0efe
clarified file positions: retain original source path;
wenzelm
parents:
76884
diff
changeset
|
225 |
ends_with(".sty") || |
ffc0774e0efe
clarified file positions: retain original source path;
wenzelm
parents:
76884
diff
changeset
|
226 |
ends_with(".cls") || |
ffc0774e0efe
clarified file positions: retain original source path;
wenzelm
parents:
76884
diff
changeset
|
227 |
ends_with(".clo") |
74056 | 228 |
|
43600 | 229 |
def ext(e: String): Path = |
230 |
if (e == "") this |
|
231 |
else { |
|
232 |
val (prfx, s) = split_path |
|
43604 | 233 |
prfx + Path.basic(s + "." + e) |
43600 | 234 |
} |
43604 | 235 |
|
77034 | 236 |
def gz: Path = ext("gz") |
72962 | 237 |
def html: Path = ext("html") |
73691
2f9877db82a1
reimplemented Mirabelle as Isabelle/ML presentation hook + Isabelle/Scala tool, but sledgehammer is still inactive;
wenzelm
parents:
73660
diff
changeset
|
238 |
def log: Path = ext("log") |
75230
bbbee54b1198
prepare patched version more thoroughly, with explicit patches;
wenzelm
parents:
75220
diff
changeset
|
239 |
def orig: Path = ext("orig") |
75220 | 240 |
def patch: Path = ext("patch") |
77034 | 241 |
def pdf: Path = ext("pdf") |
75312
e641ac92b489
more formal extension_manifest, with shasum for sources;
wenzelm
parents:
75273
diff
changeset
|
242 |
def shasum: Path = ext("shasum") |
77034 | 243 |
def tar: Path = ext("tar") |
244 |
def tex: Path = ext("tex") |
|
245 |
def thy: Path = ext("thy") |
|
246 |
def xml: Path = ext("xml") |
|
247 |
def xz: Path = ext("xz") |
|
76348 | 248 |
def zst: Path = ext("zst") |
72574
d892f6d66402
build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents:
72572
diff
changeset
|
249 |
|
75393 | 250 |
def backup: Path = { |
53336 | 251 |
val (prfx, s) = split_path |
252 |
prfx + Path.basic(s + "~") |
|
253 |
} |
|
254 |
||
75393 | 255 |
def backup2: Path = { |
58610 | 256 |
val (prfx, s) = split_path |
257 |
prfx + Path.basic(s + "~~") |
|
258 |
} |
|
259 |
||
75273 | 260 |
def exe: Path = ext("exe") |
76017 | 261 |
def exe_if(b: Boolean): Path = if (b) exe else this |
262 |
def platform_exe: Path = exe_if(Platform.is_windows) |
|
72464 | 263 |
|
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
264 |
private val Ext = new Regex("(.*)\\.([^.]*)") |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
265 |
|
75393 | 266 |
def split_ext: (Path, String) = { |
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
267 |
val (prefix, base) = split_path |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
268 |
base match { |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
269 |
case Ext(b, e) => (prefix + Path.basic(b), e) |
56556 | 270 |
case _ => (prefix + Path.basic(base), "") |
43697
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
271 |
} |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
272 |
} |
77ce24aa1770
explicit Document.Node.Header, with master_dir and thy_name;
wenzelm
parents:
43670
diff
changeset
|
273 |
|
69367 | 274 |
def drop_ext: Path = split_ext._1 |
275 |
def get_ext: String = split_ext._2 |
|
276 |
||
72962 | 277 |
def squash: Path = new Path(elems.map(elem => Path.Basic(Path.squash_elem(elem)))) |
278 |
||
43604 | 279 |
|
280 |
/* expand */ |
|
281 |
||
75393 | 282 |
def expand_env(env: JMap[String, String]): Path = { |
43604 | 283 |
def eval(elem: Path.Elem): List[Path.Elem] = |
284 |
elem match { |
|
43664 | 285 |
case Path.Variable(s) => |
64228
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
63866
diff
changeset
|
286 |
val path = Path.explode(Isabelle_System.getenv_strict(s, env)) |
48658 | 287 |
if (path.elems.exists(_.isInstanceOf[Path.Variable])) |
73715
bf51c23f3f99
clarified signature -- avoid odd warning about scala/bug#6675;
wenzelm
parents:
73712
diff
changeset
|
288 |
error("Illegal path variable nesting: " + Properties.Eq(s, path.toString)) |
48658 | 289 |
else path.elems |
43604 | 290 |
case x => List(x) |
291 |
} |
|
292 |
||
71383 | 293 |
new Path(Path.norm_elems(elems.flatMap(eval))) |
43604 | 294 |
} |
48373 | 295 |
|
64228
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
63866
diff
changeset
|
296 |
def expand: Path = expand_env(Isabelle_System.settings()) |
b46969a851a9
expand relatively to given environment, notably remote HOME;
wenzelm
parents:
63866
diff
changeset
|
297 |
|
69366 | 298 |
def file_name: String = expand.base.implode |
299 |
||
48373 | 300 |
|
66232 | 301 |
/* platform files */ |
48373 | 302 |
|
60988 | 303 |
def file: JFile = File.platform_file(this) |
48548 | 304 |
def is_file: Boolean = file.isFile |
305 |
def is_dir: Boolean = file.isDirectory |
|
65833 | 306 |
|
73945 | 307 |
def java_path: JPath = file.toPath |
308 |
||
66232 | 309 |
def absolute_file: JFile = File.absolute(file) |
310 |
def canonical_file: JFile = File.canonical(file) |
|
67181 | 311 |
|
312 |
def absolute: Path = File.path(absolute_file) |
|
313 |
def canonical: Path = File.path(canonical_file) |
|
43600 | 314 |
} |