Skip to content

Commit c36ae69

Browse files
authored
Merge branch 'main' into feat-add-list-namespaces-admin-endpoint
2 parents 03407c6 + cda864e commit c36ae69

File tree

12 files changed

+201
-75
lines changed

12 files changed

+201
-75
lines changed

Cargo.lock

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,18 @@ exclude = [
2525
]
2626

2727
[workspace.package]
28-
version = "0.9.14"
28+
version = "0.9.17"
2929
authors = ["the libSQL authors"]
3030
edition = "2021"
3131
license = "MIT"
3232
repository = "https://github.com/tursodatabase/libsql"
3333

3434
[workspace.dependencies]
35-
libsql-ffi = { path = "libsql-ffi", version = "0.9.14" }
36-
libsql-sys = { path = "libsql-sys", version = "0.9.14", default-features = false }
37-
libsql-hrana = { path = "libsql-hrana", version = "0.9.14" }
38-
libsql_replication = { path = "libsql-replication", version = "0.9.14" }
39-
rusqlite = { package = "libsql-rusqlite", path = "vendored/rusqlite", version = "0.9.14", default-features = false, features = [
35+
libsql-ffi = { path = "libsql-ffi", version = "0.9.17" }
36+
libsql-sys = { path = "libsql-sys", version = "0.9.17", default-features = false }
37+
libsql-hrana = { path = "libsql-hrana", version = "0.9.17" }
38+
libsql_replication = { path = "libsql-replication", version = "0.9.17" }
39+
rusqlite = { package = "libsql-rusqlite", path = "vendored/rusqlite", version = "0.9.17", default-features = false, features = [
4040
"libsql-experimental",
4141
"column_decltype",
4242
"load_extension",

bindings/c/Cargo.toml

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,8 @@ tracing = "0.1.40"
1919
tracing-subscriber = "0.3.18"
2020
http = "1.1.0"
2121
anyhow = "1.0.86"
22-
23-
[target.'cfg(not(any(target_os = "ios", target_os = "android")))'.dependencies]
2422
libsql = { path = "../../libsql", features = ["encryption"] }
2523

26-
# Disable encryption for ios and android targets
27-
[target.'cfg(any(target_os = "ios", target_os = "android"))'.dependencies]
28-
libsql = { path = "../../libsql"}
29-
3024

3125
# The produced binaries are too large for mobiles
3226
# When compiling for iOS or Android, you should turn on symbol stripping, lto and cut debug symbols

bindings/c/Makefile

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
OS := $(shell uname)
22
CFLAGS := -Iinclude
33
LDFLAGS := -lm
4-
ARCHS_IOS = x86_64-apple-ios aarch64-apple-ios aarch64-apple-ios-sim
4+
ARCHS_IOS = aarch64-apple-ios aarch64-apple-ios-sim
55
ARCHS_ANDROID = aarch64-linux-android armv7-linux-androideabi x86_64-linux-android i686-linux-android
66
LIB = libsql_experimental.a
77
HEADER = libsql.h
@@ -40,11 +40,21 @@ $(ARCHS_ANDROID): %:
4040
ios: $(XCFRAMEWORK)
4141

4242
$(ARCHS_IOS): %:
43-
cargo build --release --target $@
44-
45-
$(XCFRAMEWORK): $(ARCHS_IOS)
43+
cargo build --release --target $@
44+
45+
x86_64-apple-ios:
46+
SDKROOT=$$(xcrun --sdk iphonesimulator --show-sdk-path) \
47+
CFLAGS_x86_64_apple_ios="-target x86_64-apple-ios-simulator" \
48+
CXXFLAGS_x86_64_apple_ios="-target x86_64-apple-ios-simulator" \
49+
CC_x86_64_apple_ios=$$(xcrun --sdk iphonesimulator --find clang) \
50+
CXX_x86_64_apple_ios=$$(xcrun --sdk iphonesimulator --find clang++) \
51+
CMAKE_SYSTEM_NAME=iOS \
52+
CMAKE_OSX_SYSROOT=$$(xcrun --sdk iphonesimulator --show-sdk-path) \
53+
cargo build --release --target x86_64-apple-ios
54+
55+
$(XCFRAMEWORK): $(ARCHS_IOS) x86_64-apple-ios
4656
rm -rf generated
4757
mkdir -p generated/simulator_fat
4858
rm -rf $@
4959
lipo -create $(wildcard ../../target/x86_64-apple-ios/release/$(LIB)) $(wildcard ../../target/aarch64-apple-ios-sim/release/$(LIB)) -output generated/simulator_fat/$(LIB)
50-
xcodebuild -create-xcframework -library $(wildcard ../../target/aarch64-apple-ios/release/$(LIB)) -headers include -library generated/simulator_fat/$(LIB) -headers include -output $@
60+
xcodebuild -create-xcframework -library $(wildcard ../../target/aarch64-apple-ios/release/$(LIB)) -headers include -library generated/simulator_fat/$(LIB) -headers include -output $@

bindings/c/include/libsql.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ typedef struct {
4141
int sync_interval;
4242
char with_webpki;
4343
char offline;
44+
const char *remote_encryption_key;
4445
} libsql_config;
4546

4647
typedef const libsql_connection *libsql_connection_t;

bindings/c/src/lib.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ pub unsafe extern "C" fn libsql_open_sync(
9898
sync_interval: 0,
9999
with_webpki: 0,
100100
offline: 0,
101+
remote_encryption_key: std::ptr::null(),
101102
};
102103
libsql_open_sync_with_config(config, out_db, out_err_msg)
103104
}
@@ -121,6 +122,7 @@ pub unsafe extern "C" fn libsql_open_sync_with_webpki(
121122
sync_interval: 0,
122123
with_webpki: 1,
123124
offline: 0,
125+
remote_encryption_key: std::ptr::null(),
124126
};
125127
libsql_open_sync_with_config(config, out_db, out_err_msg)
126128
}
@@ -271,6 +273,19 @@ pub unsafe extern "C" fn libsql_open_sync_with_config(
271273
.build();
272274
builder = builder.connector(https);
273275
}
276+
if !config.remote_encryption_key.is_null() {
277+
let key = unsafe { std::ffi::CStr::from_ptr(config.remote_encryption_key) };
278+
let key = match key.to_str() {
279+
Ok(k) => k,
280+
Err(e) => {
281+
set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg);
282+
return 5;
283+
}
284+
};
285+
builder = builder.remote_encryption(libsql::EncryptionContext {
286+
key: libsql::EncryptionKey::Base64Encoded(key.to_string()),
287+
});
288+
};
274289
match RT.block_on(builder.build()) {
275290
Ok(db) => {
276291
let db = Box::leak(Box::new(libsql_database { db }));
@@ -325,6 +340,19 @@ pub unsafe extern "C" fn libsql_open_sync_with_config(
325340
let config = libsql::EncryptionConfig::new(libsql::Cipher::Aes256Cbc, key);
326341
builder = builder.encryption_config(config)
327342
};
343+
if !config.remote_encryption_key.is_null() {
344+
let key = unsafe { std::ffi::CStr::from_ptr(config.remote_encryption_key) };
345+
let key = match key.to_str() {
346+
Ok(k) => k,
347+
Err(e) => {
348+
set_err_msg(format!("Wrong encryption key: {e}"), out_err_msg);
349+
return 5;
350+
}
351+
};
352+
builder = builder.remote_encryption(libsql::EncryptionContext {
353+
key: libsql::EncryptionKey::Base64Encoded(key.to_string()),
354+
});
355+
};
328356
match RT.block_on(builder.build()) {
329357
Ok(db) => {
330358
let db = Box::leak(Box::new(libsql_database { db }));

bindings/c/src/types.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct libsql_config {
1515
pub sync_interval: std::ffi::c_int,
1616
pub with_webpki: std::ffi::c_char,
1717
pub offline: std::ffi::c_char,
18+
pub remote_encryption_key: *const std::ffi::c_char,
1819
}
1920

2021
#[derive(Clone, Debug)]

libsql-ffi/build.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,11 @@ fn build_multiple_ciphers(out_path: &Path) -> PathBuf {
485485
build.compiler(cxx);
486486
config.init_cxx_cfg(build);
487487
}
488+
let target = env::var("TARGET").unwrap();
489+
490+
if target.ends_with("apple-ios") {
491+
config.define("SQLITE3MC_OMIT_AES_HARDWARE_SUPPORT", "ON");
492+
}
488493

489494
if cfg!(feature = "wasmtime-bindings") {
490495
config.define("LIBSQL_ENABLE_WASM_RUNTIME", "1");
@@ -496,6 +501,14 @@ fn build_multiple_ciphers(out_path: &Path) -> PathBuf {
496501
.define("SQLITE_ENABLE_SESSION", "ON");
497502
}
498503

504+
if let Ok(abi) = env::var("CARGO_NDK_ANDROID_TARGET") {
505+
config.define("ANDROID_ABI", abi);
506+
}
507+
508+
if let Ok(platform) = env::var("ANDROID_PLATFORM") {
509+
config.define("ANDROID_PLATFORM", platform);
510+
}
511+
499512
config.build()
500513
}
501514

libsql-ffi/bundled/SQLite3MultipleCiphers/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,12 @@ if (CMAKE_C_COMPILER_ID STREQUAL "Clang" AND NOT (
313313
CMAKE_SYSTEM_PROCESSOR STREQUAL "aarch64"
314314
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm64"
315315
OR CMAKE_SYSTEM_PROCESSOR STREQUAL "arm"
316+
OR CMAKE_ANDROID_ARCH_ABI STREQUAL "arm64-v8a"
317+
OR CMAKE_ANDROID_ARCH_ABI STREQUAL "armeabi-v7a"
316318
))
317319
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -msse4.2 -maes -Wno-error=incompatible-function-pointer-types")
318320
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -msse4.2 -maes")
319321
endif()
320-
321322
set(_LIB_DIFINITIONS
322323
_LIB
323324
)

libsql/src/local/connection.rs

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ impl Connection {
7676
// disabled so that we can sync our changes back to a remote
7777
// server.
7878
conn.query("PRAGMA journal_mode = WAL", Params::None)?;
79-
unsafe {
80-
ffi::libsql_wal_disable_checkpoint(conn.raw);
81-
}
79+
conn.wal_disable_checkpoint()?;
8280
}
8381
Ok(conn)
8482
}
@@ -554,6 +552,16 @@ impl Connection {
554552
Ok(buf)
555553
}
556554

555+
fn wal_disable_checkpoint(&self) -> Result<()> {
556+
let rc = unsafe { libsql_sys::ffi::libsql_wal_disable_checkpoint(self.handle()) };
557+
if rc != 0 {
558+
return Err(crate::errors::Error::SqliteFailure(
559+
rc as std::ffi::c_int,
560+
format!("wal_disable_checkpoint failed"),
561+
));
562+
}
563+
Ok(())
564+
}
557565
fn wal_insert_begin(&self) -> Result<()> {
558566
let rc = unsafe { libsql_sys::ffi::libsql_wal_insert_begin(self.handle()) };
559567
if rc != 0 {
@@ -576,14 +584,14 @@ impl Connection {
576584
Ok(())
577585
}
578586

579-
fn wal_insert_frame(&self, frame: &[u8]) -> Result<()> {
587+
fn wal_insert_frame(&self, frame_no: u32, frame: &[u8]) -> Result<()> {
580588
let mut conflict = 0i32;
581589
let rc = unsafe {
582590
libsql_sys::ffi::libsql_wal_insert_frame(
583591
self.handle(),
584-
frame.len() as u32,
592+
frame_no,
585593
frame.as_ptr() as *mut std::ffi::c_void,
586-
0,
594+
frame.len() as u32,
587595
&mut conflict,
588596
)
589597
};
@@ -658,13 +666,17 @@ unsafe extern "C" fn authorizer_callback(
658666

659667
pub(crate) struct WalInsertHandle<'a> {
660668
conn: &'a Connection,
661-
in_session: RefCell<bool>
669+
in_session: RefCell<bool>,
662670
}
663671

664672
impl WalInsertHandle<'_> {
665-
pub fn insert(&self, frame: &[u8]) -> Result<()> {
673+
pub fn insert_at(&self, frame_no: u32, frame: &[u8]) -> Result<()> {
666674
assert!(*self.in_session.borrow());
667-
self.conn.wal_insert_frame(frame)
675+
self.conn.wal_insert_frame(frame_no, frame)
676+
}
677+
678+
pub fn in_session(&self) -> bool {
679+
*self.in_session.borrow()
668680
}
669681

670682
pub fn begin(&self) -> Result<()> {
@@ -698,3 +710,54 @@ impl fmt::Debug for Connection {
698710
f.debug_struct("Connection").finish()
699711
}
700712
}
713+
714+
#[cfg(test)]
715+
mod tests {
716+
use crate::{
717+
local::{Connection, Database},
718+
params::Params,
719+
OpenFlags,
720+
};
721+
722+
#[tokio::test]
723+
pub async fn test_kek() {
724+
let temp_dir = tempfile::tempdir().unwrap();
725+
let path1 = temp_dir.path().join("local1.db");
726+
let db1 = Database::new(path1.to_str().unwrap().to_string(), OpenFlags::default());
727+
let conn1 = Connection::connect(&db1).unwrap();
728+
conn1
729+
.query("PRAGMA journal_mode = WAL", Params::None)
730+
.unwrap();
731+
conn1.wal_disable_checkpoint().unwrap();
732+
733+
let path2 = temp_dir.path().join("local2.db");
734+
let db2 = Database::new(path2.to_str().unwrap().to_string(), OpenFlags::default());
735+
let conn2 = Connection::connect(&db2).unwrap();
736+
conn2
737+
.query("PRAGMA journal_mode = WAL", Params::None)
738+
.unwrap();
739+
conn2.wal_disable_checkpoint().unwrap();
740+
741+
conn1.execute("CREATE TABLE t(x)", Params::None).unwrap();
742+
const CNT: usize = 32;
743+
for _ in 0..CNT {
744+
conn1
745+
.execute(
746+
"INSERT INTO t VALUES (randomblob(1024 * 1024))",
747+
Params::None,
748+
)
749+
.unwrap();
750+
}
751+
let handle = conn2.wal_insert_handle().unwrap();
752+
let frame_count = conn1.wal_frame_count();
753+
for frame_no in 0..frame_count {
754+
let frame = conn1.wal_get_frame(frame_no + 1, 4096).unwrap();
755+
handle.insert_at(frame_no as u32 + 1, &frame).unwrap();
756+
}
757+
let result = conn2.query("SELECT COUNT(*) FROM t", Params::None).unwrap();
758+
let row = result.unwrap().next().unwrap().unwrap();
759+
let column = row.get_value(0).unwrap();
760+
let cnt = *column.as_integer().unwrap();
761+
assert_eq!(cnt, 32 as i64);
762+
}
763+
}

0 commit comments

Comments
 (0)