| 1 |
(**************************************************************************) |
| 2 |
(* *) |
| 3 |
(* Copyright 2016-2020 OCamlPro *) |
| 4 |
(* *) |
| 5 |
(* All rights reserved. This file is distributed under the terms of the *) |
| 6 |
(* GNU Lesser General Public License version 2.1, with the special *) |
| 7 |
(* exception on linking described in the file LICENSE. *) |
| 8 |
(* *) |
| 9 |
(**************************************************************************) |
| 10 |
|
| 11 |
open OpamStd.Op |
| 12 |
|
| 13 |
let log ?level fmt = OpamConsole.log ?level "TRACK" fmt |
| 14 |
let slog = OpamConsole.slog |
| 15 |
|
| 16 |
module SM = OpamStd.String.Map |
| 17 |
|
| 18 |
type digest = string |
| 19 |
|
| 20 |
let digest_of_string dg = dg |
| 21 |
let string_of_digest dg = dg |
| 22 |
|
| 23 |
type change = |
| 24 |
| Added of digest |
| 25 |
| Removed |
| 26 |
| Contents_changed of digest |
| 27 |
| Perm_changed of digest |
| 28 |
| Kind_changed of digest |
| 29 |
|
| 30 |
type t = change SM.t |
| 31 |
|
| 32 |
let string_of_change ?(full=false) = |
| 33 |
let str s d = |
| 34 |
if not full then s else |
| 35 |
Printf.sprintf "%s %s" s (string_of_digest d) |
| 36 |
in |
| 37 |
function |
| 38 |
| Added d -> str "addition" d |
| 39 |
| Removed -> "removal" |
| 40 |
| Contents_changed d -> str "modifications" d |
| 41 |
| Perm_changed d -> str "permission change" d |
| 42 |
| Kind_changed d -> str "kind change" d |
| 43 |
|
| 44 |
let to_string t = |
| 45 |
OpamStd.Format.itemize (fun (f, change) -> |
| 46 |
Printf.sprintf "%s of %s" |
| 47 |
(String.capitalize_ascii (string_of_change change)) f) |
| 48 |
(SM.bindings t) |
| 49 |
|
| 50 |
let to_summary_string t = |
| 51 |
let freq_table = |
| 52 |
SM.fold (fun _ change -> |
| 53 |
SM.union (+) (SM.singleton (string_of_change ~full:false change) 1)) |
| 54 |
t |
| 55 |
SM.empty |
| 56 |
in |
| 57 |
let freq_list = |
| 58 |
OpamStd.List.concat_map ~left:" (" ~right:")" ~nil:"" "; " (fun (change, freq) -> |
| 59 |
Printf.sprintf "%s: %d" change freq) |
| 60 |
(SM.bindings freq_table) |
| 61 |
in |
| 62 |
Printf.sprintf "%d items%s" |
| 63 |
(SM.cardinal t) |
| 64 |
freq_list |
| 65 |
|
| 66 |
(** uid, gid, perm *) |
| 67 |
type perms = int * int * int |
| 68 |
|
| 69 |
type item_value = |
| 70 |
| File of string |
| 71 |
| Dir |
| 72 |
| Link of string |
| 73 |
| Special of (int * int) |
| 74 |
|
| 75 |
type item = perms * item_value |
| 76 |
|
| 77 |
let cached_digest = |
| 78 |
let item_cache = Hashtbl.create 749 in |
| 79 |
fun f size mtime -> |
| 80 |
try |
| 81 |
let csize, cmtime, digest = Hashtbl.find item_cache f in |
| 82 |
if csize = size && mtime = cmtime then Digest.to_hex digest |
| 83 |
else raise Not_found |
| 84 |
with Not_found -> |
| 85 |
let digest = Digest.file f in |
| 86 |
Hashtbl.replace item_cache f (size, mtime, digest); |
| 87 |
Digest.to_hex digest |
| 88 |
|
| 89 |
let quick_digest _f size mtime = |
| 90 |
Printf.sprintf "S%dT%s" size (string_of_float mtime) |
| 91 |
|
| 92 |
let get_digest ?(precise=OpamCoreConfig.(!r.precise_tracking)) f size mtime = |
| 93 |
if precise then cached_digest f size mtime |
| 94 |
else quick_digest f size mtime |
| 95 |
|
| 96 |
let item_of_filename ?precise f : item = |
| 97 |
let stats = Unix.lstat f in |
| 98 |
Unix.(stats.st_uid, stats.st_gid, stats.st_perm), |
| 99 |
match stats.Unix.st_kind with |
| 100 |
| Unix.S_REG -> |
| 101 |
File (get_digest ?precise f stats.Unix.st_size stats.Unix.st_mtime) |
| 102 |
| Unix.S_DIR -> Dir |
| 103 |
| Unix.S_LNK -> Link (Unix.readlink f) |
| 104 |
| Unix.S_CHR | Unix.S_BLK | Unix.S_FIFO | Unix.S_SOCK -> |
| 105 |
Special Unix.(stats.st_dev, stats.st_rdev) |
| 106 |
|
| 107 |
let item_of_filename_opt ?precise f = |
| 108 |
try Some (item_of_filename ?precise f) |
| 109 |
with Unix.Unix_error _ -> None |
| 110 |
|
| 111 |
let item_digest = function |
| 112 |
| _perms, File d -> "F:" ^ d |
| 113 |
| _perms, Dir -> "D" |
| 114 |
| _perms, Link l -> "L:" ^ l |
| 115 |
| _perms, Special (a,b) -> Printf.sprintf "S:%d:%d" a b |
| 116 |
|
| 117 |
let is_precise_digest d = |
| 118 |
not (OpamStd.String.starts_with ~prefix:"F:S" d) |
| 119 |
|
| 120 |
let check_digest file digest = |
| 121 |
let precise = is_precise_digest digest in |
| 122 |
let it = item_of_filename ~precise file in |
| 123 |
try if item_digest it = digest then `Unchanged else `Changed |
| 124 |
with Unix.Unix_error _ -> `Removed |
| 125 |
|
| 126 |
let check prefix changes = |
| 127 |
let str_pfx = OpamFilename.Dir.to_string prefix in |
| 128 |
SM.fold (fun fname op acc -> |
| 129 |
let f = Filename.concat str_pfx fname in |
| 130 |
match op with |
| 131 |
| Added dg | Kind_changed dg | Contents_changed dg -> |
| 132 |
(OpamFilename.of_string f, check_digest f dg) :: acc |
| 133 |
| Perm_changed _ | Removed -> acc) |
| 134 |
changes [] |
| 135 |
|> List.rev |
| 136 |
|
| 137 |
let revert ?title ?(verbose=OpamConsole.verbose()) ?(force=false) |
| 138 |
?(dryrun=false) prefix changes = |
| 139 |
let title = match title with |
| 140 |
| None -> "" |
| 141 |
| Some t -> t ^ ": " |
| 142 |
in |
| 143 |
let rmdir d = if not dryrun then OpamFilename.rmdir d in |
| 144 |
let rmfile f = if not dryrun then OpamFilename.remove f in |
| 145 |
let changes = |
| 146 |
(* Reverse the list so that dirnames come after the files they contain *) |
| 147 |
List.rev (OpamStd.String.Map.bindings changes) |
| 148 |
in |
| 149 |
let already, modified, nonempty, cannot = |
| 150 |
List.fold_left (fun (already,modified,nonempty,cannot as acc) (fname,op) -> |
| 151 |
let f = Filename.concat (OpamFilename.Dir.to_string prefix) fname in |
| 152 |
match op with |
| 153 |
| Added dg | Kind_changed dg -> |
| 154 |
let cur_item_ct, cur_dg = |
| 155 |
try |
| 156 |
let precise = is_precise_digest dg in |
| 157 |
let item = item_of_filename ~precise f in |
| 158 |
Some (snd item), Some (item_digest item) |
| 159 |
with Unix.Unix_error _ -> None, None |
| 160 |
in |
| 161 |
if cur_dg = None then (fname::already, modified, nonempty, cannot) |
| 162 |
else if cur_dg <> Some dg && not force then |
| 163 |
(already, fname::modified, nonempty, cannot) |
| 164 |
else if cur_item_ct = Some Dir then |
| 165 |
let d = OpamFilename.Dir.of_string f in |
| 166 |
if OpamFilename.dir_is_empty d then |
| 167 |
(rmdir d; acc) |
| 168 |
else |
| 169 |
let nonempty = |
| 170 |
if List.exists |
| 171 |
(OpamStd.String.starts_with ~prefix:fname) nonempty |
| 172 |
then nonempty else fname::nonempty |
| 173 |
in |
| 174 |
(already, modified, nonempty, cannot) |
| 175 |
else |
| 176 |
let f = OpamFilename.of_string f in |
| 177 |
rmfile f; |
| 178 |
acc |
| 179 |
| Contents_changed dg -> |
| 180 |
if check_digest f dg = `Changed then |
| 181 |
(already, modified, nonempty, (op,fname)::cannot) |
| 182 |
else |
| 183 |
acc (* File has changed, assume the removal script reverted it *) |
| 184 |
| (Removed | Perm_changed _) -> |
| 185 |
(already, modified, nonempty, (op,fname)::cannot)) |
| 186 |
([], [], [], []) changes |
| 187 |
in |
| 188 |
if already <> [] then |
| 189 |
log ~level:2 "%sfiles %s were already removed" title |
| 190 |
(String.concat ", " (List.rev already)); |
| 191 |
if modified <> [] then |
| 192 |
if OpamConsole.confirm ~default:false |
| 193 |
"%sthese files have been modified since installation:\n%s\ |
| 194 |
Remove them anyway?" title |
| 195 |
(OpamStd.Format.itemize (fun s -> s) (List.rev modified)) then |
| 196 |
List.iter (fun f -> OpamFilename.remove (OpamFilename.Op.(prefix // f))) |
| 197 |
modified; |
| 198 |
if nonempty <> [] && verbose then |
| 199 |
OpamConsole.note "%snot removing non-empty directories:\n%s" title |
| 200 |
(OpamStd.Format.itemize (fun s -> s) (List.rev nonempty)); |
| 201 |
if cannot <> [] && verbose then |
| 202 |
let cannot = |
| 203 |
let rem, modf, perm = |
| 204 |
List.fold_left (fun (rem, modf, perm as acc) (op,f) -> |
| 205 |
match op with |
| 206 |
| Removed -> (None, f)::rem, modf, perm |
| 207 |
| Contents_changed dg -> |
| 208 |
let precise = Some (is_precise_digest dg) in |
| 209 |
rem, (precise, f)::modf, perm |
| 210 |
| Perm_changed dg -> |
| 211 |
let precise = Some (is_precise_digest dg) in |
| 212 |
rem, modf, (precise, f)::perm |
| 213 |
| _ -> acc) |
| 214 |
([],[],[]) cannot |
| 215 |
in |
| 216 |
(if rem = [] then [] else [Removed, rem]) |
| 217 |
@ (if modf = [] then [] else [Contents_changed "_", modf]) |
| 218 |
@ (if perm = [] then [] else [Perm_changed "_", perm]) |
| 219 |
in |
| 220 |
(OpamConsole.warning "%scannot revert:" title; |
| 221 |
OpamConsole.errmsg "%s" |
| 222 |
(OpamStd.Format.itemize |
| 223 |
(fun (op,lf) -> |
| 224 |
Printf.sprintf "%s of:\n%s" |
| 225 |
(string_of_change op) |
| 226 |
(OpamStd.Format.itemize (fun (pre,x) -> |
| 227 |
(OpamStd.Option.to_string (fun pr -> |
| 228 |
if pr then "[hash] " else "[tms] ") pre) ^ x) lf)) |
| 229 |
cannot)) |
| 230 |
|
| 231 |
let update prefix t = |
| 232 |
let removed = ref [] in |
| 233 |
let prefix = OpamFilename.Dir.to_string prefix in |
| 234 |
let update_digest file digest = |
| 235 |
match |
| 236 |
let filename = Filename.concat prefix file in |
| 237 |
let precise = is_precise_digest digest in |
| 238 |
item_digest ( item_of_filename ~precise filename ) |
| 239 |
with |
| 240 |
| exception Unix.Unix_error ( ENOENT, _, _) -> |
| 241 |
removed := file :: !removed; |
| 242 |
digest |
| 243 |
| exception _exn -> digest |
| 244 |
| digest -> digest |
| 245 |
in |
| 246 |
let t = |
| 247 |
SM.mapi (fun file change -> |
| 248 |
match change with |
| 249 |
| Added digest -> Added (update_digest file digest) |
| 250 |
| Removed -> Removed |
| 251 |
| Contents_changed digest -> |
| 252 |
Contents_changed (update_digest file digest) |
| 253 |
| Perm_changed digest -> Perm_changed (update_digest file digest) |
| 254 |
| Kind_changed digest -> Kind_changed (update_digest file digest) |
| 255 |
) t |
| 256 |
in |
| 257 |
List.fold_left (fun t file -> |
| 258 |
SM.remove file t |
| 259 |
) t !removed |