LCOV - code coverage report
Current view: top level - libpkg - pkg_status.c (source / functions) Hit Total Coverage
Test: cov.info Lines: 12 41 29.3 %
Date: 2015-08-15 Functions: 1 2 50.0 %

          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             : 
      40             : 
      41             : #ifndef _LOCALBASE
      42             : #define _LOCALBASE      "/usr/local"
      43             : #endif
      44             : 
      45             : static bool is_exec_at_localbase(const char *progname);
      46             : 
      47             : pkg_status_t
      48           1 : pkg_status(int *count)
      49             : {
      50             :         const pkg_object        *o;
      51             :         const char              *progname;
      52             :         char                     dbpath[MAXPATHLEN];
      53           1 :         int                      numpkgs = 0;
      54           1 :         sqlite3                 *db = NULL;
      55           1 :         sqlite3_stmt            *stmt = NULL;
      56           1 :         const char              *sql = "SELECT COUNT(*) FROM packages";
      57             :         bool                     dbsuccess;
      58             : 
      59             :         /* Is this executable called pkg, or does pkg exist at
      60             :            $LOCALBASE/sbin/pkg.  Ditto: pkg-static. Portability:
      61             :            assumes setprogname() has been called */
      62             : 
      63           1 :         progname = getprogname();
      64           1 :         if (progname == NULL)
      65           0 :                 return (PKG_STATUS_UNINSTALLED);
      66             : 
      67           1 :         if (strcmp(progname, PKG_EXEC_NAME) != 0   &&
      68           0 :             strcmp(progname, PKG_STATIC_NAME) != 0 &&
      69           0 :             !is_exec_at_localbase(PKG_EXEC_NAME)   &&
      70           0 :             !is_exec_at_localbase(PKG_STATIC_NAME))
      71           0 :                 return (PKG_STATUS_UNINSTALLED);
      72             : 
      73             :         /* Does the local.sqlite pkg database exist, and can we open
      74             :            it for reading? */
      75             : 
      76           1 :         o = pkg_config_get("PKG_DBDIR");
      77           1 :         snprintf(dbpath, sizeof(dbpath), "%s/local.sqlite", pkg_object_string(o));
      78             : 
      79           1 :         if (eaccess(dbpath, R_OK) == -1)
      80           1 :                 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             :                                 }
      94           0 :                                 sqlite3_finalize(stmt);
      95             :                         }
      96           0 :                         sqlite3_close(db);
      97             :                 }
      98           0 :                 sqlite3_shutdown();
      99             :         }
     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             : }
     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             :                  progname);
     120           0 :         if (access(pkgpath, X_OK) == -1)
     121           0 :                 result = false;
     122             : 
     123           0 :         return (result);
     124             : }

Generated by: LCOV version 1.10