diff --git a/Cargo.lock b/Cargo.lock index 47d69b8..7cdb83e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -10,7 +10,7 @@ dependencies = [ [[package]] name = "anyhow" -version = "1.0.28" +version = "1.0.31" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -29,7 +29,7 @@ version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "hermit-abi 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.70 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -49,7 +49,7 @@ version = "1.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -69,7 +69,7 @@ dependencies = [ [[package]] name = "bstr" -version = "0.2.12" +version = "0.2.13" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -89,7 +89,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "clap" -version = "2.33.0" +version = "2.33.1" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -98,7 +98,7 @@ dependencies = [ "strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", - "vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", + "vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -131,7 +131,7 @@ version = "0.3.4" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.70 (registry+https://github.com/rust-lang/crates.io-index)", "redox_users 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", "winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -147,7 +147,7 @@ version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.70 (registry+https://github.com/rust-lang/crates.io-index)", "wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -164,7 +164,7 @@ name = "hermit-abi" version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.70 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -174,7 +174,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] name = "libc" -version = "0.2.69" +version = "0.2.70" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -194,8 +194,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro-error-attr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -205,8 +205,8 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", "syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -221,7 +221,7 @@ dependencies = [ [[package]] name = "quote" -version = "1.0.4" +version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)", @@ -233,7 +233,7 @@ version = "0.7.3" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "getrandom 0.1.14 (registry+https://github.com/rust-lang/crates.io-index)", - "libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.70 (registry+https://github.com/rust-lang/crates.io-index)", "rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)", "rand_hc 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", @@ -300,20 +300,20 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.106" +version = "1.0.110" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_derive 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "serde_derive" -version = "1.0.106" +version = "1.0.110" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -326,7 +326,7 @@ name = "structopt" version = "0.3.14" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ - "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)", "lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "structopt-derive 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -339,17 +339,17 @@ dependencies = [ "heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro-error 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] name = "syn" -version = "1.0.18" +version = "1.0.21" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", ] @@ -359,8 +359,8 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" dependencies = [ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)", - "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", - "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -396,7 +396,7 @@ dependencies = [ [[package]] name = "vec_map" -version = "0.8.1" +version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" [[package]] @@ -432,20 +432,20 @@ source = "registry+https://github.com/rust-lang/crates.io-index" name = "zoxide" version = "0.4.0" dependencies = [ - "anyhow 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)", + "anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)", "bincode 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)", - "bstr 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)", - "clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)", + "bstr 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)", + "clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)", "dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)", "dunce 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)", - "serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)", "structopt 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)", "uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)", ] [metadata] "checksum ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -"checksum anyhow 1.0.28 (registry+https://github.com/rust-lang/crates.io-index)" = "d9a60d744a80c30fcb657dfe2c1b22bcb3e814c1a1e3674f32bf5820b570fbff" +"checksum anyhow 1.0.31 (registry+https://github.com/rust-lang/crates.io-index)" = "85bb70cc08ec97ca5450e6eba421deeea5f172c0fc61f78b5357b2a8e8be195f" "checksum arrayref 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544" "checksum arrayvec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cff77d8686867eceff3105329d4698d96c2391c176d5d03adc90c7389162b5b8" "checksum atty 0.2.14 (registry+https://github.com/rust-lang/crates.io-index)" = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" @@ -454,10 +454,10 @@ dependencies = [ "checksum bincode 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "5753e2a71534719bf3f4e57006c3a4f0d2c672a4b676eec84161f763eca87dbf" "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" "checksum blake2b_simd 0.5.10 (registry+https://github.com/rust-lang/crates.io-index)" = "d8fb2d74254a3a0b5cac33ac9f8ed0e44aa50378d9dbb2e5d83bd21ed1dc2c8a" -"checksum bstr 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "2889e6d50f394968c8bf4240dc3f2a7eb4680844d27308f798229ac9d4725f41" +"checksum bstr 0.2.13 (registry+https://github.com/rust-lang/crates.io-index)" = "31accafdb70df7871592c058eca3985b71104e15ac32f64706022c58867da931" "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de" "checksum cfg-if 0.1.10 (registry+https://github.com/rust-lang/crates.io-index)" = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" -"checksum clap 2.33.0 (registry+https://github.com/rust-lang/crates.io-index)" = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" +"checksum clap 2.33.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bdfa80d47f954d53a35a64987ca1422f495b8d6483c0fe9f7117b36c2a792129" "checksum constant_time_eq 0.1.5 (registry+https://github.com/rust-lang/crates.io-index)" = "245097e9a4535ee1e3e3931fcfcd55a796a44c643e8596ff6566d68f09b87bbc" "checksum crossbeam-utils 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)" = "c3c7c73a2d1e9fc0886a08b93e98eb643461230d5f1925e4036204d5f2e261a8" "checksum dirs 2.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "13aea89a5c93364a98e9b37b2fa237effbb694d5cfe01c5b70941f7eb087d5e3" @@ -467,13 +467,13 @@ dependencies = [ "checksum heck 0.3.1 (registry+https://github.com/rust-lang/crates.io-index)" = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" "checksum hermit-abi 0.1.12 (registry+https://github.com/rust-lang/crates.io-index)" = "61565ff7aaace3525556587bd2dc31d4a07071957be715e63ce7b1eccf51a8f4" "checksum lazy_static 1.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" -"checksum libc 0.2.69 (registry+https://github.com/rust-lang/crates.io-index)" = "99e85c08494b21a9054e7fe1374a732aeadaff3980b6990b94bfd3a70f690005" +"checksum libc 0.2.70 (registry+https://github.com/rust-lang/crates.io-index)" = "3baa92041a6fec78c687fa0cc2b3fae8884f743d672cf551bed1d6dac6988d0f" "checksum memchr 2.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400" "checksum ppv-lite86 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "74490b50b9fbe561ac330df47c08f3f33073d2d00c150f719147d7c54522fa1b" "checksum proc-macro-error 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678" "checksum proc-macro-error-attr 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53" "checksum proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)" = "8872cf6f48eee44265156c111456a700ab3483686b3f96df4cf5481c89157319" -"checksum quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4c1f4b0efa5fc5e8ceb705136bfee52cfdb6a4e3509f770b478cd6ed434232a7" +"checksum quote 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "42934bc9c8ab0d3b273a16d8551c8f0fcff46be73276ca083ec2414c15c4ba5e" "checksum rand 0.7.3 (registry+https://github.com/rust-lang/crates.io-index)" = "6a6b1679d49b24bbfe0c803429aa1874472f50d9b363131f0e89fc356b544d03" "checksum rand_chacha 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f4c8ed856279c9737206bf725bf36935d8666ead7aa69b52be55af369d193402" "checksum rand_core 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "90bde5296fc891b0cef12a6d03ddccc162ce7b2aff54160af9338f8d40df6d19" @@ -482,19 +482,19 @@ dependencies = [ "checksum redox_users 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "09b23093265f8d200fa7b4c2c76297f47e681c655f6f1285a8780d6a022f7431" "checksum regex-automata 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)" = "ae1ded71d66a4a97f5e961fd0cb25a5f366a42a41570d16a763a69c092c26ae4" "checksum rust-argon2 0.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "2bc8af4bda8e1ff4932523b94d3dd20ee30a87232323eda55903ffd71d2fb017" -"checksum serde 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)" = "36df6ac6412072f67cf767ebbde4133a5b2e88e76dc6187fa7104cd16f783399" -"checksum serde_derive 1.0.106 (registry+https://github.com/rust-lang/crates.io-index)" = "9e549e3abf4fb8621bd1609f11dfc9f5e50320802273b12f3811a67e6716ea6c" +"checksum serde 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)" = "99e7b308464d16b56eba9964e4972a3eee817760ab60d88c3f86e1fecb08204c" +"checksum serde_derive 1.0.110 (registry+https://github.com/rust-lang/crates.io-index)" = "818fbf6bfa9a42d3bfcaca148547aa00c7b915bec71d1757aa2d44ca68771984" "checksum strsim 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" "checksum structopt 0.3.14 (registry+https://github.com/rust-lang/crates.io-index)" = "863246aaf5ddd0d6928dfeb1a9ca65f505599e4e1b399935ef7e75107516b4ef" "checksum structopt-derive 0.4.7 (registry+https://github.com/rust-lang/crates.io-index)" = "d239ca4b13aee7a2142e6795cbd69e457665ff8037aed33b3effdc430d2f927a" -"checksum syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)" = "410a7488c0a728c7ceb4ad59b9567eb4053d02e8cc7f5c0e0eeeb39518369213" +"checksum syn 1.0.21 (registry+https://github.com/rust-lang/crates.io-index)" = "4696caa4048ac7ce2bcd2e484b3cef88c1004e41b8e945a277e2c25dc0b72060" "checksum syn-mid 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" "checksum textwrap 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" "checksum unicode-segmentation 1.6.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" "checksum unicode-width 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" "checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum uuid 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" -"checksum vec_map 0.8.1 (registry+https://github.com/rust-lang/crates.io-index)" = "05c78687fb1a80548ae3250346c3db86a80a7cdd77bda190189f2d0a0987c81a" +"checksum vec_map 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" "checksum version_check 0.9.1 (registry+https://github.com/rust-lang/crates.io-index)" = "078775d0255232fb988e6fccf26ddc9d1ac274299aaedcedce21c6f72cc533ce" "checksum wasi 0.9.0+wasi-snapshot-preview1 (registry+https://github.com/rust-lang/crates.io-index)" = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519" "checksum winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)" = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6" diff --git a/src/config.rs b/src/config.rs index 61ca135..fa71d19 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1,4 +1,3 @@ -use crate::db::DBVersion; use crate::dir::Rank; use anyhow::{bail, Context, Result}; @@ -7,9 +6,6 @@ use std::env; use std::fs; use std::path::PathBuf; -pub const DB_MAX_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB -pub const DB_VERSION: DBVersion = 3; - pub fn zo_data_dir() -> Result { let data_dir = match env::var_os("_ZO_DATA_DIR") { Some(data_osstr) => PathBuf::from(data_osstr), diff --git a/src/db.rs b/src/db.rs index a3f2802..0c5422a 100644 --- a/src/db.rs +++ b/src/db.rs @@ -1,77 +1,165 @@ -use crate::config; use crate::dir::{Dir, Epoch, Rank}; -use anyhow::{anyhow, bail, Context, Result}; +use anyhow::{bail, Context, Result}; use serde::{Deserialize, Serialize}; use uuid::Uuid; use std::cmp::Ordering; use std::fs::{self, File, OpenOptions}; -use std::io::{self, BufRead, BufReader, BufWriter}; +use std::io::{self, BufRead, BufReader, Write}; use std::path::{Path, PathBuf}; pub use i32 as DBVersion; -pub struct DB { - data: DBData, +#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)] +struct DbVersion(u32); + +pub struct Db { + data_dir: PathBuf, + dirs: Vec, modified: bool, - path: PathBuf, } -impl DB { - pub fn open>(path: P) -> Result { - let data = match File::open(&path) { - Ok(file) => { - let reader = BufReader::new(&file); - bincode::config() - .limit(config::DB_MAX_SIZE) - .deserialize_from(reader) - .context("could not deserialize database")? +impl Db { + const CURRENT_VERSION: DbVersion = DbVersion(3); + const MAX_SIZE: u64 = 8 * 1024 * 1024; // 8 MiB + + pub fn open(data_dir: PathBuf) -> Result { + fs::create_dir_all(&data_dir) + .with_context(|| format!("unable to create data directory: {}", data_dir.display()))?; + + let file_path = Self::get_path(&data_dir); + + let buffer = match fs::read(&file_path) { + Ok(buffer) => buffer, + Err(e) if e.kind() == io::ErrorKind::NotFound => { + return Ok(Db { + data_dir, + modified: false, + dirs: Vec::new(), + }) + } + Err(e) => { + return Err(e).with_context(|| { + format!("could not read from database: {}", file_path.display()) + }) } - Err(err) => match err.kind() { - io::ErrorKind::NotFound => DBData::default(), - _ => return Err(err).context("could not open database file"), - }, }; - if data.version != config::DB_VERSION { - bail!("database version '{}' is unsupported", data.version); + if buffer.is_empty() { + return Ok(Db { + data_dir, + modified: false, + dirs: Vec::new(), + }); } - Ok(DB { - data, + let version_size = bincode::serialized_size(&Self::CURRENT_VERSION) + .context("could not determine size of database version field")? + as _; + + if buffer.len() < version_size { + bail!("database is corrupted: {}", file_path.display()); + } + + let (buffer_version, buffer_dirs) = buffer.split_at(version_size); + + let mut deserializer = bincode::config(); + deserializer.limit(Self::MAX_SIZE); + + let version = deserializer.deserialize(buffer_version).with_context(|| { + format!( + "could not deserialize database version: {}", + file_path.display(), + ) + })?; + + let dirs = match version { + Self::CURRENT_VERSION => deserializer.deserialize(buffer_dirs).with_context(|| { + format!("could not deserialize database: {}", file_path.display()) + })?, + DbVersion(version_num) => bail!( + "zoxide {} does not support schema v{}: {}", + env!("ZOXIDE_VERSION"), + version_num, + file_path.display(), + ), + }; + + Ok(Db { + data_dir, modified: false, - path: path.as_ref().to_path_buf(), + dirs, }) } pub fn save(&mut self) -> Result<()> { - if self.modified { - let path_tmp = self.get_path_tmp(); - - let file_tmp = OpenOptions::new() - .write(true) - .create_new(true) - .open(&path_tmp) - .context("could not open temporary database file")?; - - let writer = BufWriter::new(&file_tmp); - bincode::serialize_into(writer, &self.data).context("could not serialize database")?; - - if let Err(e) = fs::rename(&path_tmp, &self.path) { - fs::remove_file(&path_tmp) - .context("could not move or delete temporary database file")?; - return Err(e).context("could not move temporary database file"); - } - - self.modified = false; + if !self.modified { + return Ok(()); } + let (buffer, buffer_size) = (|| -> bincode::Result<_> { + let version_size = bincode::serialized_size(&Self::CURRENT_VERSION)?; + let dirs_size = bincode::serialized_size(&self.dirs)?; + + let buffer_size = version_size + dirs_size; + let mut buffer = Vec::with_capacity(buffer_size as _); + + bincode::serialize_into(&mut buffer, &Self::CURRENT_VERSION)?; + bincode::serialize_into(&mut buffer, &self.dirs)?; + + Ok((buffer, buffer_size)) + })() + .context("could not serialize database")?; + + let db_path_tmp = Self::get_path_tmp(&self.data_dir); + + let mut db_file_tmp = OpenOptions::new() + .create_new(true) + .write(true) + .open(&db_path_tmp) + .with_context(|| { + format!( + "could not create temporary database: {}", + db_path_tmp.display() + ) + })?; + + // File::set_len() can fail on some filesystems, so we ignore errors + let _ = db_file_tmp.set_len(buffer_size); + + (|| -> anyhow::Result<()> { + db_file_tmp.write_all(&buffer).with_context(|| { + format!( + "could not write to temporary database: {}", + db_path_tmp.display() + ) + })?; + + let db_path = Self::get_path(&self.data_dir); + + fs::rename(&db_path_tmp, &db_path) + .with_context(|| format!("could not create database: {}", db_path.display())) + })() + .map_err(|e| { + fs::remove_file(&db_path_tmp) + .with_context(|| { + format!( + "could not remove temporary database: {}", + db_path_tmp.display() + ) + }) + .err() + .unwrap_or(e) + })?; + + self.modified = true; + Ok(()) } pub fn import>(&mut self, path: P, merge: bool) -> Result<()> { - if !self.data.dirs.is_empty() && !merge { + if !self.dirs.is_empty() && !merge { bail!( "To prevent conflicts, you can only import from z with an empty zoxide database!\n\ If you wish to merge the two, specify the `--merge` flag." @@ -125,9 +213,7 @@ impl DB { if merge { // If the path exists in the database, add the ranks and set the epoch to // the largest of the parsed epoch and the already present epoch. - if let Some(dir) = - self.data.dirs.iter_mut().find(|dir| dir.path == path_abs) - { + if let Some(dir) = self.dirs.iter_mut().find(|dir| dir.path == path_abs) { dir.rank += rank; dir.last_accessed = Epoch::max(epoch, dir.last_accessed); @@ -135,7 +221,7 @@ impl DB { }; } - self.data.dirs.push(Dir { + self.dirs.push(Dir { path: path_abs, rank, last_accessed: epoch, @@ -156,10 +242,10 @@ impl DB { pub fn add>(&mut self, path: P, max_age: Rank, now: Epoch) -> Result<()> { let path_abs = dunce::canonicalize(&path) - .with_context(|| anyhow!("could not access directory: {}", path.as_ref().display()))?; + .with_context(|| format!("could not access directory: {}", path.as_ref().display()))?; - match self.data.dirs.iter_mut().find(|dir| dir.path == path_abs) { - None => self.data.dirs.push(Dir { + match self.dirs.iter_mut().find(|dir| dir.path == path_abs) { + None => self.dirs.push(Dir { path: path_abs, last_accessed: now, rank: 1.0, @@ -170,15 +256,15 @@ impl DB { } }; - let sum_age = self.data.dirs.iter().map(|dir| dir.rank).sum::(); + let sum_age = self.dirs.iter().map(|dir| dir.rank).sum::(); if sum_age > max_age { let factor = 0.9 * max_age / sum_age; - for dir in &mut self.data.dirs { + for dir in &mut self.dirs { dir.rank *= factor; } - self.data.dirs.retain(|dir| dir.rank >= 1.0); + self.dirs.retain(|dir| dir.rank >= 1.0); } self.modified = true; @@ -187,7 +273,6 @@ impl DB { pub fn query(&mut self, keywords: &[String], now: Epoch) -> Option<&Dir> { let (idx, dir, _) = self - .data .dirs .iter() .enumerate() @@ -199,9 +284,9 @@ impl DB { if dir.is_dir() { // FIXME: change this to Some(dir) once the MIR borrow checker comes to stable Rust - Some(&self.data.dirs[idx]) + Some(&self.dirs[idx]) } else { - self.data.dirs.swap_remove(idx); + self.dirs.swap_remove(idx); self.modified = true; self.query(keywords, now) } @@ -214,14 +299,14 @@ impl DB { } pub fn query_all(&mut self) -> &[Dir] { - let orig_len = self.data.dirs.len(); - self.data.dirs.retain(Dir::is_dir); + let orig_len = self.dirs.len(); + self.dirs.retain(Dir::is_dir); - if orig_len != self.data.dirs.len() { + if orig_len != self.dirs.len() { self.modified = true; } - self.data.dirs.as_slice() + self.dirs.as_slice() } pub fn remove>(&mut self, path: P) -> Result<()> { @@ -234,13 +319,8 @@ impl DB { } pub fn remove_exact>(&mut self, path: P) -> Result<()> { - if let Some(idx) = self - .data - .dirs - .iter() - .position(|dir| dir.path == path.as_ref()) - { - self.data.dirs.swap_remove(idx); + if let Some(idx) = self.dirs.iter().position(|dir| dir.path == path.as_ref()) { + self.dirs.swap_remove(idx); self.modified = true; Ok(()) } else { @@ -251,35 +331,20 @@ impl DB { } } - fn get_path_tmp(&self) -> PathBuf { - let file_name = format!(".{}.zo", Uuid::new_v4()); + fn get_path>(data_dir: P) -> PathBuf { + data_dir.as_ref().join("db.zo") + } - let mut path_tmp = self.path.clone(); - path_tmp.set_file_name(file_name); - - path_tmp + fn get_path_tmp>(data_dir: P) -> PathBuf { + let file_name = format!("db-{}.zo.tmp", Uuid::new_v4()); + data_dir.as_ref().join(file_name) } } -impl Drop for DB { +impl Drop for Db { fn drop(&mut self) { if let Err(e) = self.save() { eprintln!("{:#}", e); } } } - -#[derive(Debug, Deserialize, Serialize)] -struct DBData { - version: DBVersion, - dirs: Vec, -} - -impl Default for DBData { - fn default() -> DBData { - DBData { - version: config::DB_VERSION, - dirs: Vec::new(), - } - } -} diff --git a/src/subcommand/init/shell/posix.rs b/src/subcommand/init/shell/posix.rs index 520e964..5c19f7f 100644 --- a/src/subcommand/init/shell/posix.rs +++ b/src/subcommand/init/shell/posix.rs @@ -1,6 +1,6 @@ use super::{HookConfig, ShellConfig}; -use anyhow::{anyhow, Result}; +use anyhow::{Context, Result}; use uuid::Uuid; use std::borrow::Cow; @@ -83,13 +83,13 @@ fn hook_pwd() -> Result> { let tmp_path_str = tmp_path .to_str() - .ok_or_else(|| anyhow!("invalid Unicode in zoxide tmp path"))?; + .context("invalid Unicode in zoxide tmp path")?; let pwd_path = tmp_path.join(format!("pwd-{}", Uuid::new_v4())); let pwd_path_str = pwd_path .to_str() - .ok_or_else(|| anyhow!("invalid Unicode in zoxide pwd path"))?; + .context("invalid Unicode in zoxide pwd path")?; let hook_pwd = format!( r#" diff --git a/src/subcommand/init/shell/powershell.rs b/src/subcommand/init/shell/powershell.rs index 142dd7e..a9fa919 100644 --- a/src/subcommand/init/shell/powershell.rs +++ b/src/subcommand/init/shell/powershell.rs @@ -82,4 +82,3 @@ if ($PSVersionTable.PSVersion.Major -ge 6) { Ok(Cow::Borrowed(HOOK_PWD)) } - diff --git a/src/util.rs b/src/util.rs index d8cbcb1..c760013 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,9 +1,9 @@ use crate::config; -use crate::db::DB; +use crate::db::Db; use crate::dir::{Dir, Epoch}; use crate::error::SilentExit; -use anyhow::{anyhow, bail, Context, Result}; +use anyhow::{bail, Context, Result}; use std::cmp::Reverse; use std::io::{Read, Write}; @@ -43,11 +43,9 @@ pub fn bytes_to_path(bytes: &[u8]) -> Result<&Path> { .context("invalid Unicode in path") } -pub fn get_db() -> Result { - let mut db_path = config::zo_data_dir()?; - db_path.push("db.zo"); - - DB::open(db_path) +pub fn get_db() -> Result { + let data_dir = config::zo_data_dir()?; + Db::open(data_dir) } pub fn get_current_time() -> Result { @@ -73,7 +71,7 @@ where let fzf_stdin = fzf .stdin .as_mut() - .ok_or_else(|| anyhow!("could not connect to fzf stdin"))?; + .context("could not connect to fzf stdin")?; let mut dir_frecencies = dirs .into_iter() @@ -96,7 +94,7 @@ where let fzf_stdout = fzf .stdout .as_mut() - .ok_or_else(|| anyhow!("could not connect to fzf stdout"))?; + .context("could not connect to fzf stdout")?; let mut buffer = Vec::new(); fzf_stdout