Skip to content

Commit

Permalink
Rename to libstone in root
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Oct 30, 2024
1 parent b550cd3 commit 8fa216d
Show file tree
Hide file tree
Showing 15 changed files with 613 additions and 84 deletions.
253 changes: 196 additions & 57 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"boulder",
"moss",
"libstone",
"crates/*",
]
default-members = [
Expand Down Expand Up @@ -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"
Expand Down
16 changes: 10 additions & 6 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -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"
15 changes: 12 additions & 3 deletions crates/stone-ffi/Cargo.toml → libstone/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
9 changes: 9 additions & 0 deletions libstone/build.rs
Original file line number Diff line number Diff line change
@@ -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"));
}
File renamed without changes.
41 changes: 27 additions & 14 deletions crates/stone-ffi/test.c → libstone/examples/read.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 *),
Expand Down Expand Up @@ -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);
}
Expand All @@ -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 <stone>\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;
}
23 changes: 22 additions & 1 deletion crates/stone-ffi/src/lib.rs → libstone/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8fa216d

Please sign in to comment.