upgrade_sqlcipher.sh

Overview

upgrade_sqlcipher.sh is a shell script designed to automate the process of downloading, building, and integrating a specific version of the SQLCipher library (version 4.10.0) into a Rust project environment. SQLCipher is a specialized version of SQLite that provides transparent 256-bit AES encryption of database files. This script ensures that the project uses a freshly built SQLCipher amalgamation and regenerates Rust bindings accordingly. It also cleans previous build artifacts, manages environment variables for library paths, and runs sanity checks including cargo build and test commands with specific feature flags enabled.

Detailed Explanation

Script Workflow and Functions

The script is sequential and does not define explicit functions but performs a set of ordered operations described below:

  1. Set Script Directory and Change Working Directory

    • SCRIPT_DIR=$(cd "$(dirname "$0")" && pwd)

      • Resolves the absolute path of the directory containing the script.

    • cd "$SCRIPT_DIR" || { echo "fatal error" >&2; exit 1; }

      • Changes the current directory to the script directory, exiting with an error if unsuccessful.

  2. Clean Up Previous Build Artifacts

    • cargo clean -p libsqlite3-sys

      • Cleans build artifacts for the libsqlite3-sys package, ensuring a clean build state.

  3. Create Necessary Directories

    • mkdir -p "$SCRIPT_DIR/../target" "$SCRIPT_DIR/sqlcipher"

      • Creates the target build directory and a directory to hold the SQLCipher source and compiled files.

  4. Set Environment Variables for SQLCipher Library and Includes

    • export SQLCIPHER_LIB_DIR="$SCRIPT_DIR/sqlcipher"

    • export SQLCIPHER_INCLUDE_DIR="$SQLCIPHER_LIB_DIR"

      • These environment variables are used by the build process to locate the compiled SQLCipher library and header files.

  5. Download and Build SQLCipher Amalgamation

    • Defines the specific SQLCipher version: SQLCIPHER_VERSION="4.10.0"

    • Creates a source directory: mkdir -p $SCRIPT_DIR/sqlcipher.src

    • Downloads the source tarball if not already present:

      [ -e "v${SQLCIPHER_VERSION}.tar.gz" ] || curl -sfL -O "https://github.com/sqlcipher/sqlcipher/archive/v${SQLCIPHER_VERSION}.tar.gz"
      
    • Extracts the tarball with tar into the source directory, stripping the leading directory component.

    • Configures and builds the amalgamation C source with:

      ./configure
      make sqlite3.c
      
    • Copies generated source files (sqlite3.c, sqlite3.h, sqlite3ext.h) into the $SCRIPT_DIR/sqlcipher directory.

    • Cleans up by removing the downloaded tarball and source directory.

  6. Regenerate Rust Bindings

    • Removes any previous bundled bindgen file:

      rm -f "$SQLCIPHER_LIB_DIR/bindgen_bundled_version.rs"
      
    • Removes any existing bindgen.rs files from the target directory to avoid stale bindings.

    • Executes a cargo build with environment variable LIBSQLITE3_SYS_BUNDLING=1 and features "sqlcipher buildtime_bindgen session" enabled to regenerate the Rust bindings for SQLCipher.

    • Moves the generated bindgen.rs file to bindgen_bundled_version.rs within the SQLCipher library directory.

  7. Sanity Checks

    • Changes directory to project root ("$SCRIPT_DIR/..").

    • Runs cargo update --quiet to update dependencies quietly.

    • Runs cargo test with a comprehensive set of features enabled to verify the integration and functionality of the bundled SQLCipher:

      backup blob chrono functions limits load_extension serde_json trace vtab bundled-sqlcipher-vendored-openssl
      
    • Prints a colored message indicating successful completion of the SQLCipher tests.

Important Implementation Details

Interaction with Other Parts of the System

Usage Example

To upgrade and rebuild the SQLCipher library in the project, simply execute:

./upgrade_sqlcipher.sh

This will download SQLCipher version 4.10.0 if not already present, build the amalgamation, regenerate Rust bindings, and run tests to verify the integration.

Mermaid Diagram: Script Workflow Flowchart

flowchart TD
A[Start: Set SCRIPT_DIR] --> B[Change to SCRIPT_DIR]
B --> C[Clean libsqlite3-sys build artifacts]
C --> D[Create target and sqlcipher directories]
D --> E["Set environment variables (SQLCIPHER_LIB_DIR, SQLCIPHER_INCLUDE_DIR)"]
E --> F[Download SQLCipher source if missing]
F --> G[Extract source tarball]
G --> H[Configure and build sqlite3.c amalgamation]
H --> I[Copy sqlite3.c, sqlite3.h, sqlite3ext.h to sqlcipher directory]
I --> J[Cleanup source and tarball]
J --> K[Remove old bindgen_bundled_version.rs]
K --> L[Remove target bindgen.rs files]
L --> M[Build with cargo to regenerate bindings]
M --> N[Move generated bindgen.rs to bindgen_bundled_version.rs]
N --> O[Change to project root directory]
O --> P[Run cargo update]
P --> Q[Run cargo test with features]
Q --> R[Print finished message]
R --> S[End]

This flowchart illustrates the main steps and their sequence in the upgrade and build process handled by the script.