diff --git a/Cargo.lock b/Cargo.lock index bbe3eab1..9afae654 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -244,7 +244,7 @@ checksum = "3b43422f69d8ff38f95f1b2bb76517c91589a924d1559a0e935d7c8ce0274c11" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -279,7 +279,7 @@ checksum = "a27b8a3a6e1a44fa4c8baf1f653e4172e81486d4941f2237e20dc2d0cf4ddff1" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -288,6 +288,17 @@ version = "1.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" +[[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + [[package]] name = "autocfg" version = "1.3.0" @@ -315,6 +326,12 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "bitflags" +version = "1.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" + [[package]] name = "bitflags" version = "2.6.0" @@ -379,7 +396,7 @@ name = "boulder" version = "0.24.2" dependencies = [ "chrono", - "clap", + "clap 4.5.18", "config", "container", "derive_more", @@ -455,6 +472,25 @@ version = "0.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5" +[[package]] +name = "cbindgen" +version = "0.26.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "da6bc11b07529f16944307272d5bd9b22530bc7d05751717c9d416586cedab49" +dependencies = [ + "clap 3.2.25", + "heck 0.4.1", + "indexmap 1.9.3", + "log", + "proc-macro2", + "quote", + "serde", + "serde_json", + "syn 1.0.109", + "tempfile", + "toml 0.5.11", +] + [[package]] name = "cc" version = "1.1.21" @@ -535,6 +571,21 @@ dependencies = [ "half", ] +[[package]] +name = "clap" +version = "3.2.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123" +dependencies = [ + "atty", + "bitflags 1.3.2", + "clap_lex 0.2.4", + "indexmap 1.9.3", + "strsim 0.10.0", + "termcolor", + "textwrap", +] + [[package]] name = "clap" version = "4.5.18" @@ -553,8 +604,8 @@ checksum = "4d72166dd41634086d5803a47eb71ae740e61d84709c36f3c34110173db3961b" dependencies = [ "anstream", "anstyle", - "clap_lex", - "strsim", + "clap_lex 0.7.2", + "strsim 0.11.1", ] [[package]] @@ -563,10 +614,19 @@ version = "4.5.18" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4ac6a0c7b1a9e9a5186361f67dfa1b88213572f427fb9ab038efb2bd8c582dab" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", - "syn", + "syn 2.0.77", +] + +[[package]] +name = "clap_lex" +version = "0.2.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5" +dependencies = [ + "os_str_bytes", ] [[package]] @@ -684,7 +744,7 @@ dependencies = [ "anes", "cast", "ciborium", - "clap", + "clap 4.5.18", "criterion-plot", "is-terminal", "itertools 0.10.5", @@ -742,7 +802,7 @@ version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" dependencies = [ - "bitflags", + "bitflags 2.6.0", "crossterm_winapi", "libc", "mio 0.8.11", @@ -804,8 +864,8 @@ dependencies = [ "ident_case", "proc-macro2", "quote", - "strsim", - "syn", + "strsim 0.11.1", + "syn 2.0.77", ] [[package]] @@ -816,7 +876,7 @@ checksum = "d336a2a514f6ccccaa3e09b02d41d35330c07ddf03a62165fcec10bb561c7806" dependencies = [ "darling_core", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -844,7 +904,7 @@ dependencies = [ "proc-macro2", "quote", "rustc_version", - "syn", + "syn 2.0.77", ] [[package]] @@ -881,7 +941,7 @@ dependencies = [ "dsl_auto_type", "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -901,7 +961,7 @@ version = "0.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "209c735641a413bc68c4923a9d6ad4bcb3ca306b794edaa7eb0b3228a99ffb25" dependencies = [ - "syn", + "syn 2.0.77", ] [[package]] @@ -943,10 +1003,10 @@ checksum = "c5d9abe6314103864cc2d8901b7ae224e0ab1a103a0a416661b4097b0779b607" dependencies = [ "darling", "either", - "heck", + "heck 0.5.0", "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -1000,7 +1060,7 @@ checksum = "de0d48a183585823424a4ce1aa132d174a6a81bd540895822eb4c8373a8e49e8" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -1165,7 +1225,7 @@ checksum = "87750cf4b7a4c0625b1529e4c543c2182106e4dedc60a2a6455e00d212c489ac" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -1237,7 +1297,7 @@ version = "3.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8283e7331b8c93b9756e0cfdbcfb90312852f953c6faf9bf741e684cc3b6ad69" dependencies = [ - "bitflags", + "bitflags 2.6.0", "crc", "log", "uuid", @@ -1255,7 +1315,7 @@ dependencies = [ "futures-core", "futures-sink", "http", - "indexmap", + "indexmap 2.5.0", "slab", "tokio", "tokio-util", @@ -1272,18 +1332,39 @@ dependencies = [ "crunchy", ] +[[package]] +name = "hashbrown" +version = "0.12.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" + [[package]] name = "hashbrown" version = "0.14.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "heck" version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" +[[package]] +name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + [[package]] name = "hermit-abi" version = "0.3.9" @@ -1439,6 +1520,16 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indexmap" +version = "1.9.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99" +dependencies = [ + "autocfg", + "hashbrown 0.12.3", +] + [[package]] name = "indexmap" version = "2.5.0" @@ -1446,7 +1537,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "68b900aa2f7301e21c36462b170ee99994de34dff39a4a6a528e80e7376d07e5" dependencies = [ "equivalent", - "hashbrown", + "hashbrown 0.14.5", ] [[package]] @@ -1469,7 +1560,7 @@ dependencies = [ "proc-macro2", "quote", "strum", - "syn", + "syn 2.0.77", "thiserror", ] @@ -1578,7 +1669,7 @@ version = "0.1.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c0ff37bd590ca25063e35af745c343cb7a0271906fb7b37e4813e8f79f00268d" dependencies = [ - "bitflags", + "bitflags 2.6.0", "libc", ] @@ -1593,6 +1684,15 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "libstone" +version = "0.24.2" +dependencies = [ + "cbindgen", + "libc", + "stone", +] + [[package]] name = "linux-raw-sys" version = "0.4.14" @@ -1657,7 +1757,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "fd01039851e82f8799046eabbb354056283fb265c8ec0996af940f4e85a380ff" dependencies = [ "serde", - "toml", + "toml 0.8.19", ] [[package]] @@ -1723,7 +1823,7 @@ dependencies = [ "blsforme", "bytes", "chrono", - "clap", + "clap 4.5.18", "config", "container", "dag", @@ -1762,7 +1862,7 @@ version = "0.27.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2eb04e9c688eff1c89d72b407f168cf79bb9e867a9d3323ed6c01519eb9cc053" dependencies = [ - "bitflags", + "bitflags 2.6.0", "cfg-if", "libc", ] @@ -1773,7 +1873,7 @@ version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ab2156c4fce2f8df6c499cc1c763e4394b7482525bf2a9701c9d79d215f519e4" dependencies = [ - "bitflags", + "bitflags 2.6.0", "cfg-if", "cfg_aliases 0.1.1", "libc", @@ -1785,7 +1885,7 @@ version = "0.29.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "71e2746dc3a24dd78b3cfcb7be93368c6de9963d30f43a6a73998a9cf4b17b46" dependencies = [ - "bitflags", + "bitflags 2.6.0", "cfg-if", "cfg_aliases 0.2.1", "libc", @@ -1860,6 +1960,12 @@ dependencies = [ "pin-project-lite", ] +[[package]] +name = "os_str_bytes" +version = "6.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1" + [[package]] name = "parking" version = "2.2.1" @@ -1902,7 +2008,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b4c5cc86750666a3ed20bdaf5ca2a0344f9c67674cae0515bec2da16fbaa47db" dependencies = [ "fixedbitset", - "indexmap", + "indexmap 2.5.0", ] [[package]] @@ -1922,7 +2028,7 @@ checksum = "2f38a4412a78282e09a2cf38d195ea5420d15ba0602cb375210efbc877243965" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2155,7 +2261,7 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0884ad60e090bf1345b93da0a5de8923c93884cd03f40dfcfddd3b4bee661853" dependencies = [ - "bitflags", + "bitflags 2.6.0", ] [[package]] @@ -2287,7 +2393,7 @@ version = "0.38.37" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8acb788b847c24f28525660c4d7758620a7210875711f79e7f663cc152726811" dependencies = [ - "bitflags", + "bitflags 2.6.0", "errno", "libc", "linux-raw-sys", @@ -2385,7 +2491,7 @@ checksum = "243902eda00fad750862fc144cea25caca5e20d615af0a81bee94ca738f1df1f" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2408,7 +2514,7 @@ checksum = "6c64451ba24fc7a6a2d60fc75dd9c83c90903b19028d4eff35e88fc1e86564e9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2438,7 +2544,7 @@ version = "0.9.34+deprecated" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6a8b1a1a2ebf674015cc02edccce75287f1a0130d394307b36743c2f5d504b47" dependencies = [ - "indexmap", + "indexmap 2.5.0", "itoa", "ryu", "serde", @@ -2565,14 +2671,6 @@ dependencies = [ "zstd", ] -[[package]] -name = "stone-ffi" -version = "0.24.2" -dependencies = [ - "libc", - "stone", -] - [[package]] name = "stone_recipe" version = "0.24.2" @@ -2585,6 +2683,12 @@ dependencies = [ "url", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "strsim" version = "0.11.1" @@ -2606,11 +2710,11 @@ version = "0.26.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c6bee85a5a24955dc440386795aa378cd9cf82acd5f764469152d2270e581be" dependencies = [ - "heck", + "heck 0.5.0", "proc-macro2", "quote", "rustversion", - "syn", + "syn 2.0.77", ] [[package]] @@ -2629,6 +2733,17 @@ dependencies = [ "uuid", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.77" @@ -2662,6 +2777,21 @@ dependencies = [ "windows-sys 0.59.0", ] +[[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "textwrap" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9" + [[package]] name = "thiserror" version = "1.0.64" @@ -2679,7 +2809,7 @@ checksum = "08904e7672f5eb876eaaf87e0ce17857500934f4981c4a0ab2b4aa98baac7fc3" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2688,7 +2818,7 @@ version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d3b04d33c9633b8662b167b847c7ab521f83d1ae20f2321b65b5b925e532e36" dependencies = [ - "bitflags", + "bitflags 2.6.0", "cfg-if", "libc", "log", @@ -2778,7 +2908,7 @@ checksum = "693d596312e88961bc67d7f1f97af8a70227d9f90c31bba5806eec004978d752" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -2805,6 +2935,15 @@ dependencies = [ "tokio", ] +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "toml" version = "0.8.19" @@ -2832,7 +2971,7 @@ version = "0.22.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3b072cee73c449a636ffd6f32bd8de3a9f7119139aff882f44943ce2986dc5cf" dependencies = [ - "indexmap", + "indexmap 2.5.0", "serde", "serde_spanned", "toml_datetime", @@ -2897,7 +3036,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -3085,7 +3224,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn", + "syn 2.0.77", "wasm-bindgen-shared", ] @@ -3119,7 +3258,7 @@ checksum = "afc340c74d9005395cf9dd098506f7f44e38f2b4a21c6aaacf9a105ea5e1e836" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -3456,7 +3595,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn", + "syn 2.0.77", "zvariant_utils", ] @@ -3489,7 +3628,7 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] [[package]] @@ -3548,7 +3687,7 @@ dependencies = [ "proc-macro-crate", "proc-macro2", "quote", - "syn", + "syn 2.0.77", "zvariant_utils", ] @@ -3560,5 +3699,5 @@ checksum = "c51bcff7cc3dbb5055396bcf774748c3dab426b4b8659046963523cee4808340" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.77", ] diff --git a/Cargo.toml b/Cargo.toml index c347d340..55700e8b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "boulder", "moss", + "libstone", "crates/*", ] default-members = [ @@ -58,6 +59,7 @@ zstd = { version = "0.13.2", features = ["zstdmt"] } mailparse = "0.15.0" zbus = "4.4.0" libc = "0.2.62" +cbindgen = "0.26" [profile.release] lto = "thin" diff --git a/justfile b/justfile index 971721b7..6cab4839 100644 --- a/justfile +++ b/justfile @@ -85,10 +85,14 @@ diesel db +ARGS: --database-url sqlite://{{root-dir}}/moss/src/db/{{db}}/test.db \ {{ARGS}} -test-ffi: +# Run libstone example +libstone example="read" *ARGS="./test/bash-completion-2.11-1-1-x86_64.stone": #!/bin/bash - cargo cbuild -p stone-ffi - gcc -o stone-ffi-test ./crates/stone-ffi/test.c -L./target/x86_64-unknown-linux-gnu/debug/ -lstone -I./target/x86_64-unknown-linux-gnu/debug/ - ln -sf $(pwd)/target/x86_64-unknown-linux-gnu/debug/libstone.so ./target/x86_64-unknown-linux-gnu/debug/libstone.so.0.24 - LD_LIBRARY_PATH=$(pwd)/target/x86_64-unknown-linux-gnu/debug/ valgrind --track-origins=yes ./stone-ffi-test - rm stone-ffi-test + output=$(mktemp) + cargo build -p libstone --release + clang libstone/examples/{{example}}.c -o $output -I./libstone/src/ -lstone -L./target/release/ -Wl,-rpath,./target/release/ + if [ "$USE_VALGRIND" == "1" ]; + then valgrind --track-origins=yes $output {{ARGS}}; + else $output {{ARGS}}; + fi + rm "$output" diff --git a/crates/stone-ffi/Cargo.toml b/libstone/Cargo.toml similarity index 56% rename from crates/stone-ffi/Cargo.toml rename to libstone/Cargo.toml index 9e108e4b..33811bf0 100644 --- a/crates/stone-ffi/Cargo.toml +++ b/libstone/Cargo.toml @@ -1,22 +1,31 @@ [package] -name = "stone-ffi" +name = "libstone" version.workspace = true edition.workspace = true rust-version.workspace = true +[lib] +name = "stone" +crate-type = ["cdylib", "staticlib"] + [features] capi = [] [dependencies] -stone = { path = "../stone", features = ["ffi"] } +stone = { path = "../crates/stone", features = ["ffi"] } libc.workspace = true +[build-dependencies] +cbindgen.workspace = true + [package.metadata.capi.header] name = "stone" +subdirectory = "libstone" [package.metadata.capi.pkg_config] -name = "stone" +name = "libstone" +filename = "libstone" [package.metadata.capi.library] name = "stone" diff --git a/libstone/build.rs b/libstone/build.rs new file mode 100644 index 00000000..f6547cf2 --- /dev/null +++ b/libstone/build.rs @@ -0,0 +1,9 @@ +use std::path::Path; + +fn main() { + let root = Path::new(env!("CARGO_MANIFEST_DIR")); + + let bindings = cbindgen::generate(env!("CARGO_MANIFEST_DIR")).unwrap(); + + bindings.write_to_file(root.join("src/stone.h")); +} diff --git a/crates/stone-ffi/cbindgen.toml b/libstone/cbindgen.toml similarity index 100% rename from crates/stone-ffi/cbindgen.toml rename to libstone/cbindgen.toml diff --git a/crates/stone-ffi/test.c b/libstone/examples/read.c similarity index 93% rename from crates/stone-ffi/test.c rename to libstone/examples/read.c index 908bcf8d..504489d0 100644 --- a/crates/stone-ffi/test.c +++ b/libstone/examples/read.c @@ -170,6 +170,14 @@ void print_payload_attribute_record(StonePayloadAttributeRecord *record) { printf("}\n"); } +void print_inspect_output(StonePayloadMetaRecord *metas, int num_metas, + StonePayloadLayoutRecord *layouts, int num_layotus) { + + for (int i = 0; i < num_metas; i++) { + StonePayloadMetaRecord *meta = &metas[i]; + } +} + void process_records(StonePayload *payload, StonePayloadHeader *payload_header, void **records, int *num_records, int record_size, int (*next_record)(StonePayload *, void *), @@ -251,21 +259,22 @@ void process_reader(StoneReader *reader, StoneHeaderVersion version) { break; } case STONE_PAYLOAD_KIND_CONTENT: { - void *data = malloc(payload_header.plain_size); - - if (data == NULL) { - exit(1); - } + FILE *fptr; + fptr = fopen("/dev/null", "w+"); - stone_reader_unpack_content_payload(reader, payload, data); + stone_reader_unpack_content_payload_to_file(reader, payload, fileno(fptr)); - free(data); + fclose(fptr); } } current_payload += 1; } + if (num_metas + num_layouts > 0) { + print_inspect_output(metas, num_metas, layouts, num_layouts); + } + if (layouts != NULL) { free(layouts); } @@ -290,20 +299,24 @@ int main(int argc, char *argv[]) { FILE *fptr; StoneReader *reader; StoneHeaderVersion version; - char *file = "./test/bash-completion-2.11-1-1-x86_64.stone"; - printf("Reading stone from '%s'\n\n", file); - fptr = fopen(file, "r"); - stone_reader_read_file(fileno(fptr), &reader, &version); + if (argc != 2) { + printf("usage: %s \n", argv[0]); + exit(1); + } + + printf("Reading stone header from buffer\n\n"); + stone_reader_read_buf(HEADER_BUF, sizeof(HEADER_BUF), &reader, &version); process_reader(reader, version); stone_reader_destroy(reader); - fclose(fptr); printf("\n"); - printf("Reading stone header from buffer\n\n"); - stone_reader_read_buf(HEADER_BUF, sizeof(HEADER_BUF), &reader, &version); + printf("Reading stone from '%s'\n\n", argv[1]); + fptr = fopen(argv[1], "r"); + stone_reader_read_file(fileno(fptr), &reader, &version); process_reader(reader, version); stone_reader_destroy(reader); + fclose(fptr); return 0; } diff --git a/crates/stone-ffi/src/lib.rs b/libstone/src/lib.rs similarity index 92% rename from crates/stone-ffi/src/lib.rs rename to libstone/src/lib.rs index f43c4c20..24dee754 100644 --- a/crates/stone-ffi/src/lib.rs +++ b/libstone/src/lib.rs @@ -160,7 +160,28 @@ pub unsafe extern "C" fn stone_reader_next_payload( } #[no_mangle] -pub unsafe extern "C" fn stone_reader_unpack_content_payload( +pub unsafe extern "C" fn stone_reader_unpack_content_payload_to_file( + reader: *mut StoneReader, + payload: *const StonePayload, + file: c_int, +) -> c_int { + fallible(|| { + let mut reader = NonNull::new(reader).ok_or("")?; + let payload = NonNull::new(payload as *mut StonePayload).ok_or("")?; + let mut file = File::from_raw_fd(file); + + if let StoneDecodedPayload::Content(content) = &payload.as_ref().decoded { + reader.as_mut().unpack_content(content, &mut file)?; + } else { + Err("incorrect payload kind")?; + } + + Ok(()) + }) +} + +#[no_mangle] +pub unsafe extern "C" fn stone_reader_unpack_content_payload_to_buf( reader: *mut StoneReader, payload: *const StonePayload, data: *mut u8, diff --git a/crates/stone-ffi/src/payload.rs b/libstone/src/payload.rs similarity index 100% rename from crates/stone-ffi/src/payload.rs rename to libstone/src/payload.rs diff --git a/crates/stone-ffi/src/payload/attribute.rs b/libstone/src/payload/attribute.rs similarity index 100% rename from crates/stone-ffi/src/payload/attribute.rs rename to libstone/src/payload/attribute.rs diff --git a/crates/stone-ffi/src/payload/index.rs b/libstone/src/payload/index.rs similarity index 100% rename from crates/stone-ffi/src/payload/index.rs rename to libstone/src/payload/index.rs diff --git a/crates/stone-ffi/src/payload/layout.rs b/libstone/src/payload/layout.rs similarity index 100% rename from crates/stone-ffi/src/payload/layout.rs rename to libstone/src/payload/layout.rs diff --git a/crates/stone-ffi/src/payload/meta.rs b/libstone/src/payload/meta.rs similarity index 100% rename from crates/stone-ffi/src/payload/meta.rs rename to libstone/src/payload/meta.rs diff --git a/libstone/src/stone.h b/libstone/src/stone.h new file mode 100644 index 00000000..70900b4a --- /dev/null +++ b/libstone/src/stone.h @@ -0,0 +1,326 @@ +// SPDX-FileCopyrightText: Copyright © 2020-2024 Serpent OS Developers +// +// SPDX-License-Identifier: MPL-2.0 + + +#ifndef STONE_H +#define STONE_H + +#include +#include +#include +#include + +/** + * Well known file type for a v1 stone container + * + * Some types are now legacy as we're going to use Ion to define them. + * + */ +enum StoneHeaderV1FileType { + /** + * Binary package + */ + STONE_HEADER_V1_FILE_TYPE_BINARY = 1, + /** + * Delta package + */ + STONE_HEADER_V1_FILE_TYPE_DELTA, + /** + * (Legacy) repository index + */ + STONE_HEADER_V1_FILE_TYPE_REPOSITORY, + /** + * (Legacy) build manifest + */ + STONE_HEADER_V1_FILE_TYPE_BUILD_MANIFEST, +}; +typedef uint8_t StoneHeaderV1FileType; + +/** + * Format versions are defined as u32, to allow further mangling in future + */ +enum StoneHeaderVersion { + STONE_HEADER_VERSION_V1 = 1, +}; +typedef uint32_t StoneHeaderVersion; + +enum StonePayloadCompression { + STONE_PAYLOAD_COMPRESSION_NONE = 1, + STONE_PAYLOAD_COMPRESSION_ZSTD = 2, +}; +typedef uint8_t StonePayloadCompression; + +enum StonePayloadKind { + STONE_PAYLOAD_KIND_META = 1, + STONE_PAYLOAD_KIND_CONTENT = 2, + STONE_PAYLOAD_KIND_LAYOUT = 3, + STONE_PAYLOAD_KIND_INDEX = 4, + STONE_PAYLOAD_KIND_ATTRIBUTES = 5, + STONE_PAYLOAD_KIND_DUMB = 6, +}; +typedef uint8_t StonePayloadKind; + +/** + * Layout entries record their target file type so they can be rebuilt on + * the target installation. + */ +enum StonePayloadLayoutFileType { + /** + * Regular file + */ + STONE_PAYLOAD_LAYOUT_FILE_TYPE_REGULAR = 1, + /** + * Symbolic link (source + target set) + */ + STONE_PAYLOAD_LAYOUT_FILE_TYPE_SYMLINK, + /** + * Directory node + */ + STONE_PAYLOAD_LAYOUT_FILE_TYPE_DIRECTORY, + /** + * Character device + */ + STONE_PAYLOAD_LAYOUT_FILE_TYPE_CHARACTER_DEVICE, + /** + * Block device + */ + STONE_PAYLOAD_LAYOUT_FILE_TYPE_BLOCK_DEVICE, + /** + * FIFO node + */ + STONE_PAYLOAD_LAYOUT_FILE_TYPE_FIFO, + /** + * UNIX Socket + */ + STONE_PAYLOAD_LAYOUT_FILE_TYPE_SOCKET, +}; +typedef uint8_t StonePayloadLayoutFileType; + +enum StonePayloadMetaDependency { + /** + * Just the plain name of a package + */ + STONE_PAYLOAD_META_DEPENDENCY_PACKAGE_NAME = 0, + /** + * A soname based dependency + */ + STONE_PAYLOAD_META_DEPENDENCY_SHARED_LIBRARY, + /** + * A pkgconfig `.pc` based dependency + */ + STONE_PAYLOAD_META_DEPENDENCY_PKG_CONFIG, + /** + * Special interpreter (PT_INTERP/etc) to run the binaries + */ + STONE_PAYLOAD_META_DEPENDENCY_INTERPRETER, + /** + * A CMake module + */ + STONE_PAYLOAD_META_DEPENDENCY_C_MAKE, + /** + * A Python module + */ + STONE_PAYLOAD_META_DEPENDENCY_PYTHON, + /** + * A binary in /usr/bin + */ + STONE_PAYLOAD_META_DEPENDENCY_BINARY, + /** + * A binary in /usr/sbin + */ + STONE_PAYLOAD_META_DEPENDENCY_SYSTEM_BINARY, + /** + * An emul32-compatible pkgconfig .pc dependency (lib32/*.pc) + */ + STONE_PAYLOAD_META_DEPENDENCY_PKG_CONFIG32, +}; +typedef uint8_t StonePayloadMetaDependency; + +typedef enum StonePayloadMetaPrimitiveType { + STONE_PAYLOAD_META_PRIMITIVE_TYPE_INT8, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_UINT8, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_INT16, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_UINT16, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_INT32, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_UINT32, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_INT64, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_UINT64, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_STRING, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_DEPENDENCY, + STONE_PAYLOAD_META_PRIMITIVE_TYPE_PROVIDER, +} StonePayloadMetaPrimitiveType; + +enum StonePayloadMetaTag { + STONE_PAYLOAD_META_TAG_NAME = 1, + STONE_PAYLOAD_META_TAG_ARCHITECTURE = 2, + STONE_PAYLOAD_META_TAG_VERSION = 3, + STONE_PAYLOAD_META_TAG_SUMMARY = 4, + STONE_PAYLOAD_META_TAG_DESCRIPTION = 5, + STONE_PAYLOAD_META_TAG_HOMEPAGE = 6, + STONE_PAYLOAD_META_TAG_SOURCE_ID = 7, + STONE_PAYLOAD_META_TAG_DEPENDS = 8, + STONE_PAYLOAD_META_TAG_PROVIDES = 9, + STONE_PAYLOAD_META_TAG_CONFLICTS = 10, + STONE_PAYLOAD_META_TAG_RELEASE = 11, + STONE_PAYLOAD_META_TAG_LICENSE = 12, + STONE_PAYLOAD_META_TAG_BUILD_RELEASE = 13, + STONE_PAYLOAD_META_TAG_PACKAGE_URI = 14, + STONE_PAYLOAD_META_TAG_PACKAGE_HASH = 15, + STONE_PAYLOAD_META_TAG_PACKAGE_SIZE = 16, + STONE_PAYLOAD_META_TAG_BUILD_DEPENDS = 17, + STONE_PAYLOAD_META_TAG_SOURCE_URI = 18, + STONE_PAYLOAD_META_TAG_SOURCE_PATH = 19, + STONE_PAYLOAD_META_TAG_SOURCE_REF = 20, +}; +typedef uint16_t StonePayloadMetaTag; + +typedef struct StonePayload StonePayload; + +typedef struct StoneReader StoneReader; + +/** + * Header for the v1 format version + */ +typedef struct StoneHeaderV1 { + uint16_t num_payloads; + StoneHeaderV1FileType file_type; +} StoneHeaderV1; + +typedef struct StonePayloadHeader { + uint64_t stored_size; + uint64_t plain_size; + uint8_t checksum[8]; + uintptr_t num_records; + uint16_t version; + StonePayloadKind kind; + StonePayloadCompression compression; +} StonePayloadHeader; + +typedef struct StoneString { + const uint8_t *buf; + size_t size; +} StoneString; + +typedef struct StonePayloadLayoutFileRegular { + uint8_t hash[16]; + struct StoneString name; +} StonePayloadLayoutFileRegular; + +typedef struct StonePayloadLayoutFileSymlink { + struct StoneString source; + struct StoneString target; +} StonePayloadLayoutFileSymlink; + +typedef union StonePayloadLayoutFilePayload { + struct StonePayloadLayoutFileRegular regular; + struct StonePayloadLayoutFileSymlink symlink; + struct StoneString directory; + struct StoneString character_device; + struct StoneString block_device; + struct StoneString fifo; + struct StoneString socket; +} StonePayloadLayoutFilePayload; + +typedef struct StonePayloadLayoutRecord { + uint32_t uid; + uint32_t gid; + uint32_t mode; + uint32_t tag; + StonePayloadLayoutFileType file_type; + union StonePayloadLayoutFilePayload file_payload; +} StonePayloadLayoutRecord; + +typedef struct StonePayloadMetaDependencyValue { + StonePayloadMetaDependency kind; + struct StoneString name; +} StonePayloadMetaDependencyValue; + +typedef struct StonePayloadMetaProviderValue { + StonePayloadMetaDependency kind; + struct StoneString name; +} StonePayloadMetaProviderValue; + +typedef union StonePayloadMetaPrimitivePayload { + int8_t int8; + uint8_t uint8; + int16_t int16; + uint16_t uint16; + int32_t int32; + uint32_t uint32; + int64_t int64; + uint64_t uint64; + struct StoneString string; + struct StonePayloadMetaDependencyValue dependency; + struct StonePayloadMetaProviderValue provider; +} StonePayloadMetaPrimitivePayload; + +typedef struct StonePayloadMetaRecord { + StonePayloadMetaTag tag; + enum StonePayloadMetaPrimitiveType primitive_type; + union StonePayloadMetaPrimitivePayload primitive_payload; +} StonePayloadMetaRecord; + +typedef struct StonePayloadIndexRecord { + uint64_t start; + uint64_t end; + uint8_t digest[16]; +} StonePayloadIndexRecord; + +typedef struct StonePayloadAttributeRecord { + uintptr_t key_size; + const uint8_t *key_buf; + uintptr_t value_size; + const uint8_t *value_buf; +} StonePayloadAttributeRecord; + +int stone_reader_read_file(int file, struct StoneReader **reader_ptr, StoneHeaderVersion *version); + +int stone_reader_read_buf(const uint8_t *buf, + uintptr_t len, + struct StoneReader **reader_ptr, + StoneHeaderVersion *version); + +int stone_reader_header_v1(const struct StoneReader *reader, struct StoneHeaderV1 *header); + +int stone_reader_next_payload(struct StoneReader *reader, struct StonePayload **payload_ptr); + +int stone_reader_unpack_content_payload_to_file(struct StoneReader *reader, + const struct StonePayload *payload, + int file); + +int stone_reader_unpack_content_payload_to_buf(struct StoneReader *reader, + const struct StonePayload *payload, + uint8_t *data); + +void stone_reader_destroy(struct StoneReader *reader); + +int stone_payload_header(const struct StonePayload *payload, struct StonePayloadHeader *header); + +int stone_payload_next_layout_record(struct StonePayload *payload, + struct StonePayloadLayoutRecord *record); + +int stone_payload_next_meta_record(struct StonePayload *payload, + struct StonePayloadMetaRecord *record); + +int stone_payload_next_index_record(struct StonePayload *payload, + struct StonePayloadIndexRecord *record); + +int stone_payload_next_attribute_record(struct StonePayload *payload, + struct StonePayloadAttributeRecord *record); + +void stone_payload_destroy(struct StonePayload *payload); + +void stone_format_header_v1_file_type(StoneHeaderV1FileType file_type, uint8_t *buf); + +void stone_format_payload_compression(StonePayloadCompression compression, uint8_t *buf); + +void stone_format_payload_kind(StonePayloadKind kind, uint8_t *buf); + +void stone_format_payload_layout_file_type(StonePayloadLayoutFileType file_type, uint8_t *buf); + +void stone_format_payload_meta_tag(StonePayloadMetaTag tag, uint8_t *buf); + +void stone_format_payload_meta_dependency(StonePayloadMetaDependency dependency, uint8_t *buf); + +#endif /* STONE_H */ diff --git a/moss/src/cli/inspect.rs b/moss/src/cli/inspect.rs index 254b3447..88c1cfa7 100644 --- a/moss/src/cli/inspect.rs +++ b/moss/src/cli/inspect.rs @@ -5,7 +5,9 @@ use clap::{arg, ArgMatches, Command}; use fs_err::File; use std::path::PathBuf; -use stone::{StoneDecodedPayload, StonePayloadLayoutFile, StonePayloadMetaPrimitive, StonePayloadMetaTag, StoneReadError}; +use stone::{ + StoneDecodedPayload, StonePayloadLayoutFile, StonePayloadMetaPrimitive, StonePayloadMetaTag, StoneReadError, +}; use thiserror::Error; const COLUMN_WIDTH: usize = 20; @@ -56,10 +58,14 @@ pub fn handle(args: &ArgMatches) -> Result<(), Error> { let name = format!("{:?}", record.tag); match &record.primitive { - StonePayloadMetaPrimitive::Provider(k, p) if record.tag == StonePayloadMetaTag::Provides => { + StonePayloadMetaPrimitive::Provider(k, p) + if record.tag == StonePayloadMetaTag::Provides => + { provs.push(format!("{}({})", k, p)) } - StonePayloadMetaPrimitive::Provider(k, p) if record.tag == StonePayloadMetaTag::Conflicts => { + StonePayloadMetaPrimitive::Provider(k, p) + if record.tag == StonePayloadMetaTag::Conflicts => + { cnfls.push(format!("{}({})", k, p)) } StonePayloadMetaPrimitive::Dependency(k, d) => deps.push(format!("{}({})", k, d)),