| 1 |
structure Reruncheck :> sig |
| 2 |
datatype file_kind = INPUT | OUTPUT | AUXILIARY |
| 3 |
type file_info = { path : string |
| 4 |
, abspath : string |
| 5 |
, kind : file_kind |
| 6 |
} |
| 7 |
type recorded |
| 8 |
val parseRecorderFile : { file : string, options : AppOptions.options } -> recorded |
| 9 |
val parseRecorderFileContinued : { file : string, options : AppOptions.options, previousResult : recorded } -> recorded |
| 10 |
val getFileInfo : recorded -> file_info list * file_info StringMap.map |
| 11 |
type aux_status = { mtime : Time.time option |
| 12 |
, size : Position.int option |
| 13 |
, md5sum : MD5.hash option |
| 14 |
} |
| 15 |
val collectFileInfo : file_info list * aux_status StringMap.map -> aux_status StringMap.map |
| 16 |
val compareFileInfo : file_info list * aux_status StringMap.map -> bool * aux_status StringMap.map |
| 17 |
val compareFileTime : { srcAbs : string, dst : string, auxstatus : aux_status StringMap.map } -> bool |
| 18 |
end = struct |
| 19 |
datatype file_kind = INPUT | OUTPUT | AUXILIARY |
| 20 |
type file_info = { path : string |
| 21 |
, abspath : string |
| 22 |
, kind : file_kind |
| 23 |
} |
| 24 |
type recorded = file_info ref list * file_info ref StringMap.map |
| 25 |
fun getFileInfo ((fileInfo, fileMap) : recorded) = (List.foldl (fn (x, xs) => !x :: xs) [] fileInfo, StringMap.map ! fileMap) |
| 26 |
fun parseRecorderFileContinued { file, options : AppOptions.options, previousResult = (fileList, fileMap) } |
| 27 |
= let val ins = TextIO.openIn file |
| 28 |
fun go (fileList, fileMap) |
| 29 |
= case TextIO.inputLine ins of |
| 30 |
NONE => (TextIO.closeIn ins; (fileList, fileMap)) |
| 31 |
| SOME line => |
| 32 |
let val (t, rest) = Substring.splitl Char.isAlphaNum (Substring.full line) |
| 33 |
val path = Substring.string (Substring.dropl Char.isSpace (Substring.dropr Char.isSpace rest)) |
| 34 |
in case Substring.string t of |
| 35 |
"PWD" => go (fileList, fileMap) (* ignore *) |
| 36 |
| "INPUT" => |
| 37 |
let val abspath = PathUtil.abspath { path = path, cwd = NONE } |
| 38 |
in case StringMap.find (fileMap, abspath) of |
| 39 |
SOME (r as ref { path = path', abspath = abspath', kind }) => |
| 40 |
( r := { path = if String.size path < String.size path' then |
| 41 |
path |
| 42 |
else |
| 43 |
path' |
| 44 |
, abspath = abspath' |
| 45 |
, kind = if kind = OUTPUT then |
| 46 |
(* The files listed in both INPUT and OUTPUT are considered to be auxiliary files. *) |
| 47 |
AUXILIARY |
| 48 |
else |
| 49 |
kind |
| 50 |
} |
| 51 |
; go (fileList, fileMap) |
| 52 |
) |
| 53 |
| NONE => |
| 54 |
if FSUtil.isFile path then |
| 55 |
let val r = ref { path = path |
| 56 |
, abspath = abspath |
| 57 |
, kind = if PathUtil.ext path = "bbl" then |
| 58 |
AUXILIARY |
| 59 |
else |
| 60 |
INPUT |
| 61 |
} |
| 62 |
in go (r :: fileList, StringMap.insert (fileMap, abspath, r)) |
| 63 |
end |
| 64 |
else |
| 65 |
(* Maybe a command execution *) |
| 66 |
go (fileList, fileMap) |
| 67 |
end |
| 68 |
| "OUTPUT" => |
| 69 |
let val abspath = PathUtil.abspath { path = path, cwd = NONE } |
| 70 |
in case StringMap.find (fileMap, abspath) of |
| 71 |
SOME (r as ref { path = path', abspath = abspath', kind }) => |
| 72 |
( r := { path = if String.size path < String.size path' then |
| 73 |
path |
| 74 |
else |
| 75 |
path' |
| 76 |
, abspath = abspath' |
| 77 |
, kind = if kind = INPUT then |
| 78 |
(* The files listed in both INPUT and OUTPUT are considered to be auxiliary files. *) |
| 79 |
AUXILIARY |
| 80 |
else |
| 81 |
kind |
| 82 |
} |
| 83 |
; go (fileList, fileMap) |
| 84 |
) |
| 85 |
| NONE => |
| 86 |
let val ext = PathUtil.ext path |
| 87 |
val r = ref { path = path |
| 88 |
, abspath = abspath |
| 89 |
, kind = if ext = "out" orelse (#makeindex options <> NONE andalso ext = "idx") orelse ext = "bcf" orelse ext = "glo" then |
| 90 |
(* .out: hyperref bookmarks file |
| 91 |
* .idx: input for makeindex |
| 92 |
* .bcf: biber |
| 93 |
* .glo: makeglossaries |
| 94 |
*) |
| 95 |
AUXILIARY |
| 96 |
else |
| 97 |
OUTPUT |
| 98 |
} |
| 99 |
in go (r :: fileList, StringMap.insert (fileMap, abspath, r)) |
| 100 |
end |
| 101 |
end |
| 102 |
| t => ( Message.warn ("Unrecognized line in recorder file '" ^ file ^ "': " ^ t) |
| 103 |
; go (fileList, fileMap) |
| 104 |
) |
| 105 |
end |
| 106 |
in go (fileList, fileMap) |
| 107 |
end |
| 108 |
fun parseRecorderFile { file, options } = parseRecorderFileContinued { file = file, options = options, previousResult = ([], StringMap.empty) } |
| 109 |
|
| 110 |
type aux_status = { mtime : Time.time option |
| 111 |
, size : Position.int option |
| 112 |
, md5sum : MD5.hash option |
| 113 |
} |
| 114 |
|
| 115 |
fun md5sumOfFile (path : string) : MD5.hash |
| 116 |
= let val ins = BinIO.openIn path |
| 117 |
val data = BinIO.inputAll ins before BinIO.closeIn ins |
| 118 |
in MD5.compute data |
| 119 |
end |
| 120 |
|
| 121 |
fun collectFileInfo (fileList : file_info list, auxstatus : aux_status StringMap.map) : aux_status StringMap.map |
| 122 |
= let fun go ({ abspath, kind, ... } : file_info, auxstatus) : aux_status ref StringMap.map |
| 123 |
= if FSUtil.isFile abspath then |
| 124 |
let val (status, auxstatus) = case StringMap.find (auxstatus, abspath) of |
| 125 |
NONE => let val s = ref { mtime = NONE, size = NONE, md5sum = NONE } |
| 126 |
in (s, StringMap.insert (auxstatus, abspath, s)) |
| 127 |
end |
| 128 |
| SOME status => (status, auxstatus) |
| 129 |
in case kind of |
| 130 |
INPUT => (case status of |
| 131 |
ref (s as { mtime = NONE, ... }) => status := { s where mtime = SOME (OS.FileSys.modTime abspath) } |
| 132 |
| _ => () |
| 133 |
) |
| 134 |
| AUXILIARY => let val s = !status |
| 135 |
val s = case s of |
| 136 |
{ mtime = NONE, ... } => { s where mtime = SOME (OS.FileSys.modTime abspath) } |
| 137 |
| _ => s |
| 138 |
val s = case s of |
| 139 |
{ size = NONE, ... } => { s where size = SOME (OS.FileSys.fileSize abspath) } |
| 140 |
| _ => s |
| 141 |
val s = case s of |
| 142 |
{ md5sum = NONE, ... } => { s where md5sum = SOME (md5sumOfFile abspath) } |
| 143 |
| _ => s |
| 144 |
in status := s |
| 145 |
end |
| 146 |
| OUTPUT => () |
| 147 |
; auxstatus |
| 148 |
end |
| 149 |
else |
| 150 |
auxstatus |
| 151 |
in StringMap.map ! (List.foldl go (StringMap.map ref auxstatus) fileList) |
| 152 |
end |
| 153 |
|
| 154 |
fun compareFileInfo (fileList : file_info list, auxstatus : aux_status StringMap.map) : bool * aux_status StringMap.map |
| 155 |
= let fun go ([], newauxstatus) = (false, newauxstatus) |
| 156 |
| go ({ path = shortPath, abspath, kind } :: fileList, newauxstatus) |
| 157 |
= if FSUtil.isFile abspath then |
| 158 |
let val (shouldRerun, newauxstatus) |
| 159 |
= case kind of |
| 160 |
INPUT => (* Input file: User might have modified while running TeX. *) |
| 161 |
let val mtime = OS.FileSys.modTime abspath |
| 162 |
in case StringMap.find (auxstatus, abspath) of |
| 163 |
SOME { mtime = SOME mtime', ... } => |
| 164 |
if Time.< (mtime', mtime) then |
| 165 |
(* Input file was updated during execution *) |
| 166 |
( Message.info ("Input file '" ^ shortPath ^ "' was modified (by user, or some external commands).") |
| 167 |
; (true, StringMap.insert (newauxstatus, abspath, ref { mtime = SOME mtime, size = NONE, md5sum = NONE })) |
| 168 |
) |
| 169 |
else |
| 170 |
(false, newauxstatus) |
| 171 |
| _ => (* New input file *) |
| 172 |
(false, newauxstatus) |
| 173 |
end |
| 174 |
| AUXILIARY => (* Auxiliary file: Compare file contents. *) |
| 175 |
(case StringMap.find (auxstatus, abspath) of |
| 176 |
SOME s => |
| 177 |
let val size = OS.FileSys.fileSize abspath |
| 178 |
val sizeIsDifferent = case #size s of |
| 179 |
SOME z => z <> size |
| 180 |
| NONE => true |
| 181 |
val (modifiedBecause, newauxstatus) |
| 182 |
= if sizeIsDifferent then |
| 183 |
let val previousSize = case #size s of |
| 184 |
SOME z => Position.toString z |
| 185 |
| NONE => "(N/A)" |
| 186 |
in (SOME ("size: " ^ previousSize ^ " -> " ^ Position.toString size), StringMap.insert (newauxstatus, abspath, ref { mtime = NONE, size = SOME size, md5sum = NONE })) |
| 187 |
end |
| 188 |
else |
| 189 |
let val md5sum = md5sumOfFile abspath |
| 190 |
val md5sumIsDifferent = case #md5sum s of |
| 191 |
SOME h => h <> md5sum |
| 192 |
| NONE => true |
| 193 |
in if md5sumIsDifferent then |
| 194 |
let val previousMd5sum = case #md5sum s of |
| 195 |
SOME h => MD5.hashToLowerHexString h |
| 196 |
| NONE => "(N/A)" |
| 197 |
in (SOME ("md5: " ^ previousMd5sum ^ " -> " ^ MD5.hashToLowerHexString md5sum), StringMap.insert (newauxstatus, abspath, ref { mtime = NONE, size = SOME size, md5sum = SOME md5sum })) |
| 198 |
end |
| 199 |
else |
| 200 |
(NONE, newauxstatus) |
| 201 |
end |
| 202 |
in case modifiedBecause of |
| 203 |
SOME reason => ( Message.info ("File '" ^ shortPath ^ "' was modified (" ^ reason ^ ").") |
| 204 |
; (true, newauxstatus) |
| 205 |
) |
| 206 |
| NONE => ( if Message.getVerbosity () >= 1 then |
| 207 |
Message.info ("File '" ^ shortPath ^ "' unmodified (size and md5sum).") |
| 208 |
else |
| 209 |
() |
| 210 |
; (false, newauxstatus) |
| 211 |
) |
| 212 |
end |
| 213 |
| NONE => (* New file *) |
| 214 |
let val (shouldRerun, newauxstatus) |
| 215 |
= if String.isSuffix ".aux" abspath then |
| 216 |
let val size = OS.FileSys.fileSize abspath |
| 217 |
in if size = 8 then |
| 218 |
let val ins = BinIO.openIn abspath |
| 219 |
val contents = BinIO.inputAll ins before BinIO.closeIn ins |
| 220 |
val isTrivial = Byte.bytesToString contents = "\\relax \n" |
| 221 |
val newauxstatus = StringMap.insert (newauxstatus, abspath, ref { mtime = NONE, size = SOME size, md5sum = SOME (MD5.compute contents) }) |
| 222 |
in (not isTrivial, newauxstatus) |
| 223 |
end |
| 224 |
else |
| 225 |
let val newauxstatus = StringMap.insert (newauxstatus, abspath, ref { mtime = NONE, size = SOME size, md5sum = NONE }) |
| 226 |
in (true, newauxstatus) |
| 227 |
end |
| 228 |
end |
| 229 |
else |
| 230 |
(true, newauxstatus) |
| 231 |
in if shouldRerun then |
| 232 |
Message.info ("New auxiliary file '" ^ shortPath ^ "'.") |
| 233 |
else if Message.getVerbosity () >= 1 then |
| 234 |
Message.info ("Ignoring almost-empty auxiliary file '" ^ shortPath ^ "'.") |
| 235 |
else |
| 236 |
() |
| 237 |
; (shouldRerun, newauxstatus) |
| 238 |
end |
| 239 |
) |
| 240 |
| OUTPUT => (false, newauxstatus) |
| 241 |
in if shouldRerun then |
| 242 |
(true, newauxstatus) |
| 243 |
else |
| 244 |
go (fileList, newauxstatus) |
| 245 |
end |
| 246 |
else |
| 247 |
go (fileList, newauxstatus) |
| 248 |
val (shouldRerun, auxstatus) = go (fileList, StringMap.empty) |
| 249 |
in (shouldRerun, StringMap.map ! auxstatus) |
| 250 |
end |
| 251 |
|
| 252 |
(* true if src is newer than dst *) |
| 253 |
fun compareFileTime { srcAbs, dst, auxstatus : aux_status StringMap.map } |
| 254 |
= if not (FSUtil.isFile dst) then |
| 255 |
true |
| 256 |
else |
| 257 |
case StringMap.find (auxstatus, srcAbs) of |
| 258 |
SOME { mtime = SOME mtime, ... } => Time.> (mtime, OS.FileSys.modTime dst) |
| 259 |
| _ => false |
| 260 |
end; |