LCOV - code coverage report
Current view: top level - src - update.c (source / functions) Hit Total Coverage
Test: cov.info Lines: 29 67 43.3 %
Date: 2015-08-15 Functions: 1 3 33.3 %

          Line data    Source code
       1             : /*-
       2             :  * Copyright (c) 2011-2012 Baptiste Daroussin <bapt@FreeBSD.org>
       3             :  * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
       4             :  * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
       5             :  * Copyright (c) 2014 Matthew Seaman <matthew@FreeBSD.org>
       6             :  * All rights reserved.
       7             :  * 
       8             :  * Redistribution and use in source and binary forms, with or without
       9             :  * modification, are permitted provided that the following conditions
      10             :  * are met:
      11             :  * 1. Redistributions of source code must retain the above copyright
      12             :  *    notice, this list of conditions and the following disclaimer
      13             :  *    in this position and unchanged.
      14             :  * 2. Redistributions in binary form must reproduce the above copyright
      15             :  *    notice, this list of conditions and the following disclaimer in the
      16             :  *    documentation and/or other materials provided with the distribution.
      17             :  * 
      18             :  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
      19             :  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
      20             :  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
      21             :  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
      22             :  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
      23             :  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      24             :  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      25             :  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      26             :  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
      27             :  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      28             :  */
      29             : 
      30             : #include <sys/stat.h>
      31             : #include <sys/param.h>
      32             : 
      33             : #include <err.h>
      34             : #include <getopt.h>
      35             : #include <stdio.h>
      36             : #include <stdlib.h>
      37             : #include <string.h>
      38             : #include <sysexits.h>
      39             : #include <unistd.h>
      40             : 
      41             : #include <pkg.h>
      42             : 
      43             : #include "pkgcli.h"
      44             : 
      45             : /**
      46             :  * Fetch repository calalogues.
      47             :  */
      48             : int
      49           9 : pkgcli_update(bool force, bool strict, const char *reponame)
      50             : {
      51           9 :         int retcode = EPKG_FATAL, update_count = 0, total_count = 0;
      52           9 :         struct pkg_repo *r = NULL;
      53             : 
      54             :         /* Only auto update if the user has write access. */
      55           9 :         if (pkgdb_access(PKGDB_MODE_READ|PKGDB_MODE_WRITE|PKGDB_MODE_CREATE,
      56             :             PKGDB_DB_REPO) == EPKG_ENOACCESS)
      57           0 :                 return (EPKG_OK);
      58             : 
      59           9 :         if (pkg_repos_total_count() == 0) {
      60           1 :                 fprintf(stderr, "No active remote repositories configured.\n");
      61           1 :                 return (EPKG_FATAL);
      62             :         }
      63             : 
      64          24 :         while (pkg_repos(&r) == EPKG_OK) {
      65           8 :                 if (reponame != NULL) {
      66           0 :                         if (strcmp(pkg_repo_name(r), reponame) != 0)
      67           0 :                                 continue;
      68             :                 } else {
      69           8 :                         if (!pkg_repo_enabled(r))
      70           0 :                                 continue;
      71             :                 }
      72             : 
      73           8 :                 if (!quiet)
      74           8 :                         printf("Updating %s repository catalogue...\n",
      75             :                             pkg_repo_name(r));
      76           8 :                 retcode = pkg_update(r, force);
      77           8 :                 if (retcode == EPKG_UPTODATE) {
      78           1 :                         if (!quiet)
      79           1 :                                 printf("%s repository is up-to-date.\n",
      80             :                                     pkg_repo_name(r));
      81             :                 }
      82           7 :                 else if (retcode != EPKG_OK && strict)
      83           0 :                         retcode = EPKG_FATAL;
      84             : 
      85           8 :                 total_count ++;
      86           8 :                 if (retcode != EPKG_OK)
      87           1 :                         continue;
      88             : 
      89           7 :                 update_count ++;
      90             :         }
      91             : 
      92           8 :         if (!strict || retcode == EPKG_UPTODATE)
      93           8 :                 retcode = EPKG_OK;
      94             : 
      95           8 :         if (total_count == 0) {
      96           0 :                 if (!quiet)
      97           0 :                         printf("No repositories are enabled.\n");
      98           0 :                 retcode = EPKG_FATAL;
      99             :         }
     100           8 :         else if (update_count == 0) {
     101           1 :                 if (!quiet)
     102           1 :                         if (retcode == EPKG_OK)
     103           1 :                                 printf("All repositories are up-to-date.\n");
     104             :         }
     105             : 
     106           8 :         return (retcode);
     107             : }
     108             : 
     109             : 
     110             : void
     111           0 : usage_update(void)
     112             : {
     113           0 :         fprintf(stderr, "Usage: pkg update [-fq] [-r reponame]\n\n");
     114           0 :         fprintf(stderr, "For more information, see 'pkg help update'.\n");
     115           0 : }
     116             : 
     117             : int
     118           0 : exec_update(int argc, char **argv)
     119             : {
     120             :         int              ret;
     121             :         int              ch;
     122           0 :         const char      *reponame = NULL;
     123             : 
     124           0 :         struct option longopts[] = {
     125             :                 { "force",    no_argument,            NULL,   'f' },
     126             :                 { "quiet",    no_argument,            NULL,   'q' },
     127             :                 { "repository", required_argument,    NULL,   'r' },
     128             :                 { NULL,         0,                      NULL,   0   },
     129             :         };
     130             : 
     131           0 :         while ((ch = getopt_long(argc, argv, "+fqr:", longopts, NULL)) != -1) {
     132           0 :                 switch (ch) {
     133             :                 case 'f':
     134           0 :                         force = true;
     135           0 :                         break;
     136             :                 case 'q':
     137           0 :                         quiet = true;
     138           0 :                         break;
     139             :                 case 'r':
     140           0 :                         reponame = optarg;
     141           0 :                         break;
     142             :                 default:
     143           0 :                         usage_update();
     144           0 :                         return (EX_USAGE);
     145             :                 }
     146             :         }
     147           0 :         argc -= optind;
     148           0 :         argv += optind;
     149             : 
     150           0 :         if (argc != 0) {
     151           0 :                 usage_update();
     152           0 :                 return (EX_USAGE);
     153             :         }
     154             : 
     155           0 :         ret = pkgdb_access(PKGDB_MODE_WRITE|PKGDB_MODE_CREATE,
     156             :                            PKGDB_DB_REPO);
     157           0 :         if (ret == EPKG_ENOACCESS) {
     158           0 :                 warnx("Insufficient privileges to update the repository "
     159             :                       "catalogue.");
     160           0 :                 return (EX_NOPERM);
     161           0 :         } else if (ret != EPKG_OK)
     162           0 :                 return (EX_IOERR);
     163             : 
     164             :         /* For pkg-update update op is strict */
     165           0 :         ret = pkgcli_update(force, true, reponame);
     166             : 
     167           0 :         return ((ret == EPKG_OK) ? EX_OK : EX_SOFTWARE);
     168             : }

Generated by: LCOV version 1.10