Nimbin[12]?Linux From Scratch / dependency_collecter_for_lfs_packageuser_system / commits / 178ca4c

dependency_collecter_for_lfs_packageuser_system git · main

Dependency collector for the package-user system

lfs blfs package-manager dependencies · first commit 2025-07-07 · last commit 2026-07-05 (4 months ago) · synced 1 hour ago · upstream: codeberg.org/Nimbin_Linux/dependency_collecter_for_lfs_packageuser_system

Markdown 67.4% Makefile 32.6%
git clone https://git.christianimmanuel.de/linux-from-scratch/dependency_collecter_for_lfs_packageuser_system.gitwget https://git.christianimmanuel.de/linux-from-scratch/dependency_collecter_for_lfs_packageuser_system/archive/dependency_collecter_for_lfs_packageuser_system.tar.gz

init

Christian Immanuel · 2025-07-07 13:06 · 178ca4c838f89b5d36054f83366f7e607b6a4e2f

 Makefile      |  35 ++++++++
 README.md     |  53 ++++++++++++
 depscollector | 254 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 depslister    |  88 ++++++++++++++++++++
 4 files changed, 430 insertions(+)

diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..c1f201c
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,35 @@
+PREFIX ?= /usr
+BINDIR := $(PREFIX)/bin
+INSTALL := install
+DBDIR := /var/lib/depscollector
+DBFILE := $(DBDIR)/depscollector.db
+
+TARGETS := depscollector depslister
+
+.PHONY: all install install-db uninstall clean
+
+all:
+	@echo "Nothing to build."
+
+install: $(TARGETS) install-db
+	@echo "Installing to $(BINDIR)"
+	@for bin in $(TARGETS); do \
+		$(INSTALL) -m 0755 $$bin "$(BINDIR)/$$bin"; \
+	done
+
+install-db:
+	@echo "Installing DB to $(DBFILE)"
+	install -d "$(DBDIR)"
+	install -m 0644 depscollector.db "$(DBFILE)"
+
+uninstall:
+	@echo "Uninstalling from $(BINDIR)"
+	@for bin in $(TARGETS); do \
+		rm -f "$(BINDIR)/$$bin"; \
+	done
+	@echo "Removing database file if present"
+	@rm -f "$(DBFILE)"
+	@rmdir --ignore-fail-on-non-empty "$(DBDIR)" 2>/dev/null || true
+
+clean:
+	@echo "Nothing to clean."
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..d711720
--- /dev/null
+++ b/README.md
@@ -0,0 +1,53 @@
+# depscollector
+
+I created two bash scripts, `depscollector` and `depslister`.
+`depscollector` collects all binary files and it dependencies within a sqlite db, `depslister` is used to get dependencies of a package.
+
+As i use the packageuser system for LFS the database keeps track of the user and groups of each binary.
+
+
+## install
+
+```
+make install
+```
+
+# man depscollector
+
+**SYNOPSIS**<br>
+**depscollector** [OPTION]
+
+**DESCRIPTION**
+
+    -u
+        Update the database by removing stale and adding new binaries.
+    -d DB_PATH
+        Use specified database file (default: /var/lib/depscollector/depscollector.db).
+    -b BIN_DIR
+        Scan binaries from specified directory (default: /usr/bin). Can be used multiple times.
+    -h
+        Show this help message.
+
+
+
+# man depslister
+
+**SYNOPSIS**<br>
+**depslister** [OPTION] [ARGUMENT]
+
+**DESCRIPTION**
+
+    -u USER
+        List all binaries owned by USER
+    -b BIN_NAME
+        List libraries required by binary BIN_NAME
+    -lu USER
+        List all libraries used by binaries owned by USER
+    -ldu USER
+        List owners of libraries used by USER's binaries (who USER depends on)
+    -ldr USER
+        List owners of binaries depending on libraries owned by USER (who depends on USER)
+    -a
+        List all binaries in the database
+    -h
+        Show this help message
diff --git a/depscollector b/depscollector
new file mode 100755
index 0000000..51127b9
--- /dev/null
+++ b/depscollector
@@ -0,0 +1,254 @@
+#!/bin/bash
+
+init_db() {
+    echo "INIT DB"
+    mkdir -p "$(dirname "$db_path")"
+
+    echo "Creating DB"
+    sqlite3 "$db_path" <<'EOF'
+CREATE TABLE IF NOT EXISTS sources (
+    id INTEGER PRIMARY KEY AUTOINCREMENT,
+    dir TEXT UNIQUE NOT NULL
+);
+
+CREATE TABLE IF NOT EXISTS binaries (
+    id INTEGER PRIMARY KEY AUTOINCREMENT,
+    path TEXT UNIQUE NOT NULL,
+    name TEXT NOT NULL,
+    owner_user TEXT,
+    owner_group TEXT,
+    source_id INTEGER NOT NULL,
+    FOREIGN KEY (source_id) REFERENCES sources(id)
+);
+
+CREATE TABLE IF NOT EXISTS libraries (
+    id INTEGER PRIMARY KEY AUTOINCREMENT,
+    path TEXT UNIQUE NOT NULL,
+    name TEXT NOT NULL,
+    owner_user TEXT,
+    owner_group TEXT
+);
+
+CREATE TABLE IF NOT EXISTS binary_dependencies (
+    binary_id INTEGER NOT NULL,
+    library_id INTEGER NOT NULL,
+    PRIMARY KEY (binary_id, library_id),
+    FOREIGN KEY (binary_id) REFERENCES binaries(id) ON DELETE CASCADE,
+    FOREIGN KEY (library_id) REFERENCES libraries(id) ON DELETE CASCADE
+);
+EOF
+
+
+    local expected_tables=("sources" "binaries" "libraries" "binary_dependencies")
+    for table in "${expected_tables[@]}"; do
+        if ! sqlite3 "$db_path" ".schema $table" | grep -q "^CREATE TABLE"; then
+            echo "Error: Table '$table' was not created successfully in $db_path" >&2
+            rm -f "$db_path"
+            exit 1
+        else
+            echo "Table $table exists."
+        fi
+    done
+}
+
+get_owner() {
+    stat -c '%U:%G' "$1"
+}
+
+insert_binary() {
+    local path="$1"
+    local name owner_user owner_group source_dir source_id
+
+    name="$(basename "$path")"
+    IFS=: read -r owner_user owner_group < <(get_owner "$path")
+    source_dir="$(dirname "$path")"
+
+    # Insert or find source_id
+    sqlite3 "$db_path" <<EOF
+INSERT OR IGNORE INTO sources (dir) VALUES ('$source_dir');
+EOF
+
+    source_id=$(sqlite3 "$db_path" "SELECT id FROM sources WHERE dir = '$source_dir';")
+
+    if [[ -z "$source_id" ]]; then
+        echo "Error: Could not resolve source_id for $source_dir" >&2
+        exit 1
+    fi
+
+    echo "## $name u:$owner_user g:$owner_group"
+    echo "   $path ($source_dir, id=$source_id)"
+
+    sqlite3 "$db_path" <<EOF
+INSERT OR IGNORE INTO binaries (path, name, owner_user, owner_group, source_id)
+VALUES ('$path', '$name', '$owner_user', '$owner_group', $source_id);
+EOF
+}
+
+insert_library() {
+    local lib_path="$1"
+    [[ ! -e "$lib_path" ]] && return
+
+    local name owner_user owner_group
+    name="$(basename "$lib_path")"
+    IFS=: read -r owner_user owner_group < <(get_owner "$lib_path")
+
+    echo "      $name $owner_user $owner_group"
+
+    sqlite3 "$db_path" <<EOF
+INSERT OR IGNORE INTO libraries (path, name, owner_user, owner_group)
+VALUES ('$lib_path', '$name', '$owner_user', '$owner_group');
+EOF
+}
+
+link_binary_to_library() {
+    local binary_path="$1"
+    local lib_path="$2"
+
+    local binary_id lib_id
+    binary_id=$(sqlite3 "$db_path" "SELECT id FROM binaries WHERE path = '$binary_path';")
+    lib_id=$(sqlite3 "$db_path" "SELECT id FROM libraries WHERE path = '$lib_path';")
+
+    [[ -n "$binary_id" && -n "$lib_id" ]] || return
+
+    sqlite3 "$db_path" <<EOF
+INSERT OR IGNORE INTO binary_dependencies (binary_id, library_id)
+VALUES ($binary_id, $lib_id);
+EOF
+}
+
+find_library_path() {
+    local lib="$1"
+    ldconfig -p | awk -v lib="$lib" '$1 == lib { print $NF; exit }'
+}
+
+process_binary() {
+    local binary="$1"
+    insert_binary "$binary"
+
+    if file "$binary" | grep -q ELF; then
+        local libs
+        libs=$(readelf -d "$binary" 2>/dev/null | awk '/NEEDED/ { gsub(/\[|\]/, "", $NF); print $NF }')
+
+        echo
+        for lib in $libs; do
+            echo "   $lib"
+            local lib_path
+            lib_path="$(find_library_path "$lib")"
+            if [[ -n "$lib_path" ]]; then
+                insert_library "$lib_path"
+                link_binary_to_library "$binary" "$lib_path"
+            fi
+        done
+    fi
+}
+
+remove_stale_entries() {
+    echo "Collecting current binaries..."
+    current_bins=()
+    for dir in "${bin_dirs[@]}"; do
+        [[ -d "$dir" ]] && mapfile -t found < <(find "$dir" -type f -executable)
+        current_bins+=("${found[@]}")
+    done
+
+    echo "Collecting tracked binaries from DB..."
+    mapfile -t db_bins < <(sqlite3 "$db_path" "SELECT path FROM binaries;" | sort)
+
+    # Use temp files to diff
+    local tmp_current tmp_db
+    tmp_current=$(mktemp)
+    tmp_db=$(mktemp)
+
+    printf "%s\n" "${current_bins[@]}" > "$tmp_current"
+    printf "%s\n" "${db_bins[@]}" > "$tmp_db"
+
+    # Remove binaries that are no longer found
+    while IFS= read -r stale_bin; do
+        echo "Removing stale binary: $stale_bin"
+        sqlite3 "$db_path" "DELETE FROM binaries WHERE path = '$stale_bin';"
+    done < <(comm -23 "$tmp_db" "$tmp_current")
+
+    rm -f "$tmp_current" "$tmp_db"
+
+    echo "Cleaning unused libraries..."
+    sqlite3 "$db_path" <<EOF
+DELETE FROM libraries
+WHERE id NOT IN (SELECT library_id FROM binary_dependencies);
+EOF
+}
+
+main() {
+    local update=0
+    db_path="/var/lib/depscollector/depscollector.db"
+    bin_dirs=()
+    bin_dir="/usr/bin"
+
+    while [[ $# -gt 0 ]]; do
+        case "$1" in
+            -u) update=1; shift ;;
+            -d) db_path="$2"; shift 2 ;;
+            -b) bin_dirs+=("$2"); shift 2 ;;
+            -h)
+                echo "Usage: $0 [-u] [-d db_path] [-b bin_dir]"
+                echo "  -u           Update mode (remove stale entries, add new only)"
+                echo "  -d db_path   Path to SQLite database (default: /var/lib/depscollector/depscollector.db)"
+                echo "  -b bin_dir   Directory to scan for binaries (can be used multiple times)"
+                echo "  -h           Show this help message"
+                exit 0
+                ;;
+            *) echo "Invalid option: $1" >&2; echo "Try '$0 -h' for help." >&2; exit 1 ;;
+        esac
+    done
+
+
+    if [[ ${#bin_dirs[@]} -eq 0 ]]; then
+        mapfile -t bin_dirs < <(sqlite3 "$db_path" "SELECT DISTINCT dir FROM sources;")
+    fi
+
+    if [[ ${#bin_dirs[@]} -eq 0 ]]; then
+        bin_dirs=("/usr/bin")
+    fi
+
+    echo "Using database: $db_path"
+    echo "Scanning binaries in: ${bin_dirs[*]}"
+
+    init_db
+
+    echo "Collecting current binaries from ${bin_dirs[@]}..."
+    all_bins=()
+    for dir in "${bin_dirs[@]}"; do
+        [[ -d "$dir" ]] && mapfile -t found < <(find "$dir" -type f -executable)
+        all_bins+=("${found[@]}")
+    done
+    IFS=$'\n' all_bins=($(printf "%s\n" "${all_bins[@]}" | sort -u))
+
+    if (( update )); then
+        echo "Updating DB: removing stale entries..."
+        current_bins=("${all_bins[@]}")
+        remove_stale_entries
+
+        echo "Collecting tracked binaries from DB after cleanup..."
+        mapfile -t db_bins < <(sqlite3 "$db_path" "SELECT path FROM binaries ORDER BY path;")
+
+        # Save to tmp files to diff
+        tmp_all=$(mktemp)
+        tmp_db=$(mktemp)
+        printf "%s\n" "${all_bins[@]}" | sort > "$tmp_all"
+        printf "%s\n" "${db_bins[@]}" | sort > "$tmp_db"
+
+        echo "Processing new binaries only..."
+        while IFS= read -r new_bin; do
+            echo -e "\n###############################################"
+            process_binary "$new_bin"
+        done < <(comm -13 "$tmp_db" "$tmp_all")
+
+        rm -f "$tmp_all" "$tmp_db"
+    else
+        # Full fresh scan: insert all
+        for bin in "${all_bins[@]}"; do
+            echo -e "\n###############################################"
+            process_binary "$bin"
+        done
+    fi
+}
+
+main "$@"
diff --git a/depslister b/depslister
new file mode 100755
index 0000000..970db2e
--- /dev/null
+++ b/depslister
@@ -0,0 +1,88 @@
+#!/bin/bash
+set -euo pipefail
+
+db_path="/var/lib/depscollector/depscollector.db"
+
+
+usage() {
+    cat <<EOF
+Usage: $0 [option] [argument]
+
+Options:
+  -u USER       List all binaries owned by USER
+  -b BIN_NAME   List libraries required by binary BIN_NAME
+  -lu USER      List all libraries used by binaries owned by USER
+  -ldu USER     List owners of libraries used by USER's binaries (who USER depends on)
+  -ldr USER     List owners of binaries depending on libraries owned by USER (who depends on USER)
+  -a            List all binaries in the database
+  -h            Show this help message
+
+Examples:
+  $0 -u ncurses
+  $0 -b bash
+  $0 -lu ncurses
+  $0 -ldu ncurses
+  $0 -ldr ncurses
+  $0 -a
+
+EOF
+    exit 1
+}
+
+list_binaries_by_user() {
+    sqlite3 "$db_path" "SELECT name, path FROM binaries WHERE owner_user = '$1';"
+}
+
+list_libs_for_binary() {
+    sqlite3 "$db_path" "
+    SELECT b.owner_user, l.name, l.path FROM libraries l
+    JOIN binary_dependencies bd ON bd.library_id = l.id
+    JOIN binaries b ON b.id = bd.binary_id
+    WHERE b.name = '$1';
+    "
+}
+
+list_libs_for_user() {
+    sqlite3 "$db_path" "
+    SELECT DISTINCT l.name, l.path FROM libraries l
+    JOIN binary_dependencies bd ON bd.library_id = l.id
+    JOIN binaries b ON b.id = bd.binary_id
+    WHERE b.owner_user = '$1';
+    "
+}
+
+list_users_libs_used_by_user() {
+    sqlite3 "$db_path" "
+    SELECT DISTINCT l.owner_user FROM libraries l
+    JOIN binary_dependencies bd ON bd.library_id = l.id
+    JOIN binaries b ON b.id = bd.binary_id
+    WHERE b.owner_user = '$1' AND l.owner_user IS NOT NULL;
+    "
+}
+
+list_users_binaries_relying_on_user() {
+    sqlite3 "$db_path" "
+    SELECT DISTINCT b.owner_user FROM binaries b
+    JOIN binary_dependencies bd ON bd.binary_id = b.id
+    JOIN libraries l ON l.id = bd.library_id
+    WHERE l.owner_user = '$1' AND b.owner_user IS NOT NULL;
+    "
+}
+
+list_all_binaries() {
+    sqlite3 "$db_path" "SELECT name, path FROM binaries;"
+}
+
+if [[ $# -eq 0 ]]; then
+    usage
+fi
+
+case "$1" in
+    -u) list_binaries_by_user "$2" ;;
+    -b) list_libs_for_binary "$2" ;;
+    -lu) list_libs_for_user "$2" ;;  # libs used by user's binaries
+    -ldu) list_users_libs_used_by_user "$2" ;; # users owning libs used by user
+    -ldr) list_users_binaries_relying_on_user "$2" ;; # users relying on this user
+    -a) list_all_binaries ;;
+    *) usage ;;
+esac