Branch data Line data Source code
1 : : /*-
2 : : * Copyright (c) 2012 Matthew Seaman <matthew@FreeBSD.org>
3 : : * All rights reserved.
4 : : *
5 : : * Redistribution and use in source and binary forms, with or without
6 : : * modification, are permitted provided that the following conditions
7 : : * are met:
8 : : * 1. Redistributions of source code must retain the above copyright
9 : : * notice, this list of conditions and the following disclaimer
10 : : * in this position and unchanged.
11 : : * 2. Redistributions in binary form must reproduce the above copyright
12 : : * notice, this list of conditions and the following disclaimer in the
13 : : * documentation and/or other materials provided with the distribution.
14 : : *
15 : : * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 : : * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 : : * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 : : * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 : : * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 : : * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 : : * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 : : * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 : : * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 : : * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 : : */
26 : :
27 : : #include <sys/param.h>
28 : :
29 : : #include <stdio.h>
30 : : #include <stdlib.h>
31 : : #include <string.h>
32 : : #include <unistd.h>
33 : :
34 : : #include <sqlite3.h>
35 : :
36 : : #include <bsd_compat.h>
37 : :
38 : : #include "pkg.h"
39 : : #include "private/pkg.h"
40 : :
41 : : extern struct pkg_ctx ctx;
42 : :
43 : : #ifndef _LOCALBASE
44 : : #define _LOCALBASE "/usr/local"
45 : : #endif
46 : :
47 : : static bool is_exec_at_localbase(const char *progname);
48 : :
49 : : pkg_status_t
50 : 13 : pkg_status(int *count)
51 : : {
52 : : const char *progname;
53 : : char dbpath[MAXPATHLEN];
54 : 13 : int numpkgs = 0;
55 : 13 : sqlite3 *db = NULL;
56 : 13 : sqlite3_stmt *stmt = NULL;
57 : 13 : const char *sql = "SELECT COUNT(*) FROM packages";
58 : : bool dbsuccess;
59 : :
60 : : /* Is this executable called pkg, or does pkg exist at
61 : : $LOCALBASE/sbin/pkg. Ditto: pkg-static. Portability:
62 : : assumes setprogname() has been called */
63 : :
64 : 13 : progname = getprogname();
65 [ + - ]: 13 : if (progname == NULL)
66 : 0 : return (PKG_STATUS_UNINSTALLED);
67 : :
68 [ - + # # ]: 13 : if (strcmp(progname, PKG_EXEC_NAME) != 0 &&
69 [ # # ]: 0 : strcmp(progname, PKG_STATIC_NAME) != 0 &&
70 [ # # ]: 0 : !is_exec_at_localbase(PKG_EXEC_NAME) &&
71 : 0 : !is_exec_at_localbase(PKG_STATIC_NAME))
72 : 0 : return (PKG_STATUS_UNINSTALLED);
73 : :
74 : : /* Does the local.sqlite pkg database exist, and can we open
75 : : it for reading? */
76 : :
77 : 13 : snprintf(dbpath, sizeof(dbpath), "%s/local.sqlite", ctx.dbdir);
78 : :
79 [ - + ]: 13 : if (eaccess(dbpath, R_OK) == -1)
80 : 13 : return (PKG_STATUS_NODB);
81 : :
82 : : /* Try opening the DB and preparing and running a simple query. */
83 : :
84 : 0 : dbsuccess = (sqlite3_initialize() == SQLITE_OK);
85 [ # # ]: 0 : if (dbsuccess) {
86 : 0 : dbsuccess = (sqlite3_open_v2(dbpath, &db, SQLITE_OPEN_READONLY, NULL) == SQLITE_OK);
87 [ # # ]: 0 : if (dbsuccess) {
88 : 0 : dbsuccess = (sqlite3_prepare_v2(db, sql, -1, &stmt, NULL) == SQLITE_OK);
89 [ # # ]: 0 : if (dbsuccess) {
90 : 0 : dbsuccess = (sqlite3_step(stmt) == SQLITE_ROW);
91 [ # # ]: 0 : if (dbsuccess) {
92 : 0 : numpkgs = sqlite3_column_int64(stmt, 0);
93 : 0 : }
94 : 0 : sqlite3_finalize(stmt);
95 : 0 : }
96 : 0 : sqlite3_close(db);
97 : 0 : }
98 : 0 : sqlite3_shutdown();
99 : 0 : }
100 : :
101 [ # # ]: 0 : if (!dbsuccess)
102 : 0 : return (PKG_STATUS_NODB);
103 : :
104 : : /* Save result, if requested */
105 [ # # ]: 0 : if (count != NULL)
106 : 0 : *count = numpkgs;
107 : :
108 : 0 : return (numpkgs == 0 ? PKG_STATUS_NOPACKAGES : PKG_STATUS_ACTIVE);
109 : 13 : }
110 : :
111 : : static bool
112 : 0 : is_exec_at_localbase(const char *progname)
113 : : {
114 : : char pkgpath[MAXPATHLEN];
115 : 0 : bool result = true;
116 : :
117 : 0 : snprintf(pkgpath, sizeof(pkgpath), "%s/sbin/%s",
118 [ # # ]: 0 : getenv("LOCALBASE") ? getenv("LOCALBASE") : _LOCALBASE,
119 : 0 : progname);
120 [ # # ]: 0 : if (access(pkgpath, X_OK) == -1)
121 : 0 : result = false;
122 : :
123 : 0 : return (result);
124 : : }
|