|
8 | 8 | [clojure.string :as str] |
9 | 9 | [clj-commons.ansi :refer [perr]] |
10 | 10 | #?(:bb [babashka.classpath :as cp])) |
11 | | - (:import [java.io File] |
12 | | - [java.security MessageDigest])) |
| 11 | + (:import (java.io File) |
| 12 | + (java.nio ByteBuffer) |
| 13 | + (java.security MessageDigest))) |
13 | 14 |
|
14 | 15 | (defn- get-classpath |
15 | 16 | [] |
16 | | - #?( |
17 | | - :bb (cp/get-classpath) |
| 17 | + #?(:bb (cp/get-classpath) |
18 | 18 | :clj (System/getProperty "java.class.path"))) |
19 | 19 |
|
20 | 20 | (defn- get-split-classpath |
21 | 21 | [] |
22 | 22 | (let [sep fs/path-separator |
23 | 23 | paths (-> (get-classpath) |
24 | 24 | (str/split (re-pattern sep)))] |
25 | | - |
26 | 25 | (->> paths |
27 | 26 | sort |
28 | 27 | (mapv io/file)))) |
|
34 | 33 | (let [bytes (.getBytes value "UTF-8")] |
35 | 34 | (.update digest bytes))) |
36 | 35 |
|
37 | | -(defn- update-digest-from-file-contents |
| 36 | +(defn- update-digest-from-file |
38 | 37 | [^MessageDigest digest f] |
39 | | - (let [f' (fs/file f)] |
40 | | - ;; Would be better to digest the 8 raw bytes, but this is easier. |
41 | | - (update-digest-from-string digest (Long/toHexString (.lastModified f'))))) |
| 38 | + (let [f' (fs/file f) |
| 39 | + last-modified (.lastModified f') |
| 40 | + b (ByteBuffer/allocate 8)] |
| 41 | + (.putLong b last-modified) |
| 42 | + (.flip b) |
| 43 | + (.update digest b))) |
42 | 44 |
|
43 | 45 | (defn- update-digest-recursively |
44 | 46 | [digest ^File root] |
45 | 47 | (let [paths (fs/glob root "**")] |
46 | | - (run! #(update-digest-from-file-contents digest %) paths))) |
| 48 | + (run! #(update-digest-from-file digest %) paths))) |
47 | 49 |
|
48 | 50 | (defn- update-digest |
49 | 51 | [digest source] |
|
52 | 54 | ;; The assumption is that files are .jar files and the name will change if |
53 | 55 | ;; the contents change. |
54 | 56 | (update-digest-from-string digest (.getCanonicalPath f)) |
55 | | - ;; But for a source directory, find all the sources (and digest their contents). |
| 57 | + ;; But for a source directory, find all the sources (and digest their file time stamps). |
| 58 | + ;; Adding or removing a file (even if no other files are touched) will change the digest. |
56 | 59 | (update-digest-recursively digest f)))) |
57 | 60 |
|
58 | 61 | (defn- hex-string [^bytes input] |
|
0 commit comments