summaryrefslogtreecommitdiff
path: root/infopkg.c
blob: 99bf88005289ae739466b921579ee51ac3365880 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * infopkg.c - query the package database
 * https://rawnix.org
 *
 * Copyright (c) 2025 zorz@gmx.com
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include "db.h"
#include "archive.h"
#include "util.h"

#include <err.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

static void usage(void)
{
    fprintf(stderr,
        "usage: infopkg <operation> [options]\n"
        "  -i           list all installed packages\n"
        "  -l <pkg>     list files owned by package or in archive\n"
        "  -o <pattern> find which package owns a file (regex)\n"
        "  -f <file>    print footprint of package archive\n"
        "  -r <root>    alternative installation root\n"
    );
    exit(1);
}

static int pkg_cmp(const void *a, const void *b)
{
    return strcmp((*(pkg_t **)a)->name, (*(pkg_t **)b)->name);
}

int main(int argc, char **argv)
{
    int         opt_installed = 0, opt_list = 0;
    int         opt_owner = 0, opt_footprint = 0;
    const char *opt_root = NULL, *opt_arg = NULL;
    const char *root;
    pkgdb_t    *db;
    pkg_t      *p, **sorted;
    size_t      i, j, count;
    int         c;

    while ((c = getopt(argc, argv, "il:o:f:r:vh")) != -1) {
        switch (c) {
        case 'i': opt_installed = 1;      break;
        case 'l': opt_list      = 1; opt_arg = optarg; break;
        case 'o': opt_owner     = 1; opt_arg = optarg; break;
        case 'f': opt_footprint = 1; opt_arg = optarg; break;
        case 'r': opt_root      = optarg; break;
        case 'v':
            printf("infopkg (mkpkg) %s\n", MKPKG_VERSION);
            return 0;
        case 'h': usage(); /* FALLTHROUGH */
        default:  usage();
        }
    }

    if (opt_installed + opt_list + opt_owner + opt_footprint == 0)
        errx(1, "no operation specified — try -h");
    if (opt_installed + opt_list + opt_owner + opt_footprint > 1)
        errx(1, "only one operation at a time");

    /* footprint does not need the database */
    if (opt_footprint) {
        if (!file_exists(opt_arg))
            errx(1, "file not found: %s", opt_arg);
        return arc_footprint(opt_arg);
    }

    root = opt_root ? opt_root : (getenv("PKG_ROOT") ? getenv("PKG_ROOT") : "/");
    db   = db_open(root, 0);   /* shared lock — read only */

    if (opt_installed) {
        count = 0;
        for (p = db->head; p; p = p->next)
            count++;

        if (count == 0) {
            db_close(db);
            return 0;
        }

        sorted = xmalloc(count * sizeof(pkg_t *));
        i = 0;
        for (p = db->head; p; p = p->next)
            sorted[i++] = p;
        qsort(sorted, count, sizeof(pkg_t *), pkg_cmp);

        for (i = 0; i < count; i++)
            printf("%s %s\n", sorted[i]->name, sorted[i]->version);

        free(sorted);
    }

    else if (opt_list) {
        p = db_find(db, opt_arg);
        if (p) {
            /* installed package — list from database */
            strarray_sort(p->files);
            for (i = 0; i < p->files->count; i++)
                printf("%s\n", p->files->items[i]);
        } else if (file_exists(opt_arg)) {
            /* package archive on disk */
            char       *name, *version;
            strarray_t *files;

            if (arc_list(opt_arg, &name, &version, &files) < 0) {
                db_close(db);
                return 1;
            }
            strarray_sort(files);
            for (i = 0; i < files->count; i++)
                printf("%s\n", files->items[i]);
            strarray_free(files);
            free(name);
            free(version);
        } else {
            errx(1, "%s is not installed and not a package file", opt_arg);
        }
    }

    else if (opt_owner) {
        regex_t re;
        int     found = 0;
        char    filepath[PATH_MAX_LEN];

        if (regcomp(&re, opt_arg, REG_EXTENDED | REG_NOSUB) != 0)
            errx(1, "invalid regex: %s", opt_arg);

        for (p = db->head; p; p = p->next) {
            for (j = 0; j < p->files->count; j++) {
                /* match against /path as stored, with leading / prepended */
                snprintf(filepath, sizeof(filepath),
                         "/%s", p->files->items[j]);
                if (regexec(&re, filepath, 0, NULL, 0) == 0) {
                    printf("%-20s %s\n", p->name, p->files->items[j]);
                    found = 1;
                }
            }
        }

        regfree(&re);

        if (!found)
            puts("no owner found");
    }

    db_close(db);
    return 0;
}