summaryrefslogtreecommitdiff
path: root/depdb.h
blob: 5b29f574b2985c8430f43599554c39c9f2c85135 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
 * pkg - Dependency database
 * Tracks package dependencies and installation reasons
 * https://rawnix.org
 *
 * Copyright (c) 2025 zorz@gmx.com
 * SPDX-License-Identifier: BSD-2-Clause
 */

#ifndef DEPDB_H
#define DEPDB_H

#include "pkg.h"
#include "pkgdb.h"
#include <time.h>

/* Installation reason */
typedef enum {
    REASON_MANUAL,      /* user explicitly requested */
    REASON_AUTO         /* installed as dependency   */
} install_reason_t;

/* Dependency database entry */
typedef struct {
    char            *name;
    char            *version;
    char            *release;
    install_reason_t reason;
    time_t           install_time;
    strarray_t      *depends;   /* runtime deps — used for orphan detection */
} dep_entry_t;

/* Hash table entry */
typedef struct depdb_hash {
    char            *key;
    dep_entry_t     *entry;
    struct depdb_hash *next;
} depdb_hash_t;

/* Dependency database */
typedef struct {
    depdb_hash_t   **buckets;
    size_t           bucket_count;
    size_t           size;
    char            *path;
} depdb_t;

/* Database management */
depdb_t        *depdb_open(const char *root);
void            depdb_close(depdb_t *db);
int             depdb_save(depdb_t *db);

/* Entry management */
dep_entry_t    *dep_entry_new(const char *name, const char *version,
                              const char *release, install_reason_t reason);
void            dep_entry_free(dep_entry_t *entry);

/* Database operations */
int             depdb_add(depdb_t *db, dep_entry_t *entry);
dep_entry_t    *depdb_find(depdb_t *db, const char *name);
int             depdb_remove(depdb_t *db, const char *name);

/* Query operations */
strarray_t     *depdb_list(depdb_t *db);
strarray_t     *depdb_rdeps(depdb_t *db, const char *pkg);
strarray_t     *depdb_auto_installed(depdb_t *db);

/* Bootstrap: populate depdb from pkgdb for untracked packages.
 * Never overwrites existing entries.  Returns number added. */
int             depdb_initdb(depdb_t *db, pkgdb_t *pkgdb);

/* Helpers */
const char     *reason_str(install_reason_t reason);
install_reason_t str_reason(const char *str);

#endif /* DEPDB_H */