LCOV - code coverage report
Current view: top level - src - fetch.c (source / functions) Hit Total Coverage
Test: plop Lines: 64 114 56.1 %
Date: 2024-12-30 07:09:03 Functions: 1 2 50.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 39 79 49.4 %

           Branch data     Line data    Source code
       1                 :            : /*-
       2                 :            :  * Copyright (c) 2011-2012 Marin Atanasov Nikolov <dnaeon@gmail.com>
       3                 :            :  * Copyright (c) 2013-2014 Matthew Seaman <matthew@FreeBSD.org>
       4                 :            :  * Copyright (c) 2012-2013 Bryan Drewery <bdrewery@FreeBSD.org>
       5                 :            :  * Copyright (c) 2016 Vsevolod Stakhov <vsevolod@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/types.h>
      31                 :            : 
      32                 :            : #include <err.h>
      33                 :            : #include <getopt.h>
      34                 :            : #include <libgen.h>
      35                 :            : #include <stdbool.h>
      36                 :            : #include <stdio.h>
      37                 :            : #include <string.h>
      38                 :            : #include <unistd.h>
      39                 :            : 
      40                 :            : #include <pkg.h>
      41                 :            : 
      42                 :            : #include "pkgcli.h"
      43                 :            : 
      44                 :            : void
      45                 :          0 : usage_fetch(void)
      46                 :            : {
      47                 :          0 :         fprintf(stderr, "Usage: pkg fetch [-r reponame] [-o destdir] [-dqUy] "
      48                 :            :                                         "[-Cgix] <pkg-name> <...>\n");
      49                 :          0 :         fprintf(stderr, "       pkg fetch [-r reponame] [-dqUy] -a\n");
      50                 :          0 :         fprintf(stderr, "       pkg fetch [-r reponame] [-dqUy] -u\n\n");
      51                 :          0 :         fprintf(stderr, "For more information see 'pkg help fetch'.\n");
      52                 :          0 : }
      53                 :            : 
      54                 :            : int
      55                 :          6 : exec_fetch(int argc, char **argv)
      56                 :            : {
      57                 :          6 :         struct pkgdb    *db = NULL;
      58                 :          6 :         struct pkg_jobs *jobs = NULL;
      59                 :          6 :         const char *destdir = NULL;
      60                 :            :         int              ch;
      61                 :            :         int              retcode;
      62                 :          6 :         int              status = EXIT_FAILURE;
      63                 :          6 :         bool             upgrades_for_installed = false, rc, csum_only = false;
      64                 :            :         unsigned         mode;
      65                 :          6 :         match_t          match = MATCH_EXACT;
      66                 :          6 :         pkg_flags        f = PKG_FLAG_NONE;
      67                 :            :         c_charv_t       reponames;
      68                 :            : 
      69                 :          6 :         struct option longopts[] = {
      70                 :            :                 { "all",              no_argument,            NULL,   'a' },
      71                 :            :                 { "case-sensitive",   no_argument,            NULL,   'C' },
      72                 :            :                 { "dependencies",     no_argument,            NULL,   'd' },
      73                 :            :                 { "glob",             no_argument,            NULL,   'g' },
      74                 :            :                 { "case-insensitive", no_argument,            NULL,   'i' },
      75                 :            :                 { "quiet",            no_argument,            NULL,   'q' },
      76                 :            :                 { "repository",               required_argument,      NULL,   'r' },
      77                 :            :                 { "available-updates",        no_argument,            NULL,   'u' },
      78                 :            :                 { "no-repo-update",   no_argument,            NULL,   'U' },
      79                 :            :                 { "regex",            no_argument,            NULL,   'x' },
      80                 :            :                 { "yes",              no_argument,            NULL,   'y' },
      81                 :            :                 { "output",           required_argument,      NULL,   'o' },
      82                 :            :                 { NULL,                 0,                      NULL,   0   },
      83                 :            :         };
      84                 :            : 
      85                 :          6 :         pkgvec_init(&reponames);
      86         [ +  + ]:         22 :         while ((ch = getopt_long(argc, argv, "+aCdgiqr:Uuxyo:", longopts, NULL)) != -1) {
      87   [ -  -  +  -  :         16 :                 switch (ch) {
          -  -  -  +  -  
             +  +  -  - ]
      88                 :            :                 case 'a':
      89                 :          0 :                         match = MATCH_ALL;
      90                 :          0 :                         break;
      91                 :            :                 case 'C':
      92                 :          0 :                         pkgdb_set_case_sensitivity(true);
      93                 :          0 :                         break;
      94                 :            :                 case 'd':
      95                 :          4 :                         f |= PKG_FLAG_WITH_DEPS | PKG_FLAG_RECURSIVE;
      96                 :          4 :                         break;
      97                 :            :                 case 'g':
      98                 :          0 :                         match = MATCH_GLOB;
      99                 :          0 :                         break;
     100                 :            :                 case 'i':
     101                 :          0 :                         pkgdb_set_case_sensitivity(false);
     102                 :          0 :                         break;
     103                 :            :                 case 'q':
     104                 :          0 :                         quiet = true;
     105                 :          0 :                         break;
     106                 :            :                 case 'r':
     107   [ -  +  -  +  :          5 :                         pkgvec_push(&reponames, optarg);
                   +  - ]
     108                 :          5 :                         break;
     109                 :            :                 case 'u':
     110                 :          0 :                         f |= PKG_FLAG_UPGRADES_FOR_INSTALLED;
     111                 :          0 :                         upgrades_for_installed = true;
     112                 :          0 :                         break;
     113                 :            :                 case 'U':
     114                 :          1 :                         auto_update = false;
     115                 :          1 :                         break;
     116                 :            :                 case 'x':
     117                 :          0 :                         match = MATCH_REGEX;
     118                 :          0 :                         break;
     119                 :            :                 case 'y':
     120                 :          6 :                         yes = true;
     121                 :          6 :                         break;
     122                 :            :                 case 'o':
     123                 :          0 :                         f |= PKG_FLAG_FETCH_MIRROR;
     124                 :          0 :                         destdir = optarg;
     125                 :          0 :                         break;
     126                 :            :                 default:
     127                 :          0 :                         usage_fetch();
     128                 :          0 :                         return (EXIT_FAILURE);
     129                 :            :                 }
     130                 :            :         }
     131                 :          6 :         argc -= optind;
     132                 :          6 :         argv += optind;
     133                 :            : 
     134   [ -  +  #  #  :          6 :         if (argc < 1 && match != MATCH_ALL && !upgrades_for_installed) {
                   #  # ]
     135                 :          0 :                 usage_fetch();
     136                 :          0 :                 return (EXIT_FAILURE);
     137                 :            :         }
     138                 :            : 
     139   [ -  +  #  # ]:          6 :         if (match == MATCH_ALL && upgrades_for_installed) {
     140                 :          0 :                 usage_fetch();
     141                 :          0 :                 return (EXIT_FAILURE);
     142                 :            :         }
     143                 :            : 
     144         [ +  + ]:          6 :         if (auto_update)
     145                 :          5 :                 mode = PKGDB_MODE_READ|PKGDB_MODE_WRITE|PKGDB_MODE_CREATE;
     146                 :            :         else
     147                 :          1 :                 mode = PKGDB_MODE_READ;
     148                 :            : 
     149                 :          6 :         retcode = pkgdb_access2(mode, PKGDB_DB_REPO, &reponames);
     150                 :            : 
     151         [ -  + ]:          6 :         if (retcode == EPKG_ENOACCESS) {
     152                 :          0 :                 warnx("Insufficient privileges to access repo catalogue");
     153                 :          0 :                 return (EXIT_FAILURE);
     154         [ -  + ]:          6 :         } else if (retcode != EPKG_OK)
     155                 :          0 :                 return (EXIT_FAILURE);
     156                 :            : 
     157         [ +  - ]:          6 :         if (upgrades_for_installed) {
     158                 :          0 :                 retcode = pkgdb_access(PKGDB_MODE_READ, PKGDB_DB_LOCAL);
     159                 :            : 
     160         [ #  # ]:          0 :                 if (retcode == EPKG_ENOACCESS) {
     161                 :          0 :                         warnx("Insufficient privileges to access the package database");
     162                 :          0 :                         return (EXIT_FAILURE);
     163         [ #  # ]:          0 :                 } else if (retcode != EPKG_OK)
     164                 :          0 :                         return (EXIT_FAILURE);
     165                 :          0 :         }
     166                 :            : 
     167                 :            :         /* first update the remote repositories if needed */
     168   [ +  +  +  - ]:          6 :         if (auto_update &&
     169                 :          5 :             (retcode = pkgcli_update(false, false, &reponames)) != EPKG_OK)
     170                 :          0 :                 return (retcode);
     171                 :            : 
     172         [ -  + ]:          6 :         if (pkgdb_open_all2(&db, PKGDB_REMOTE, &reponames) != EPKG_OK)
     173                 :          0 :                 return (EXIT_FAILURE);
     174                 :            : 
     175         [ -  + ]:          6 :         if (pkgdb_obtain_lock(db, PKGDB_LOCK_READONLY) != EPKG_OK) {
     176                 :          0 :                 pkgdb_close(db);
     177                 :          0 :                 warnx("Cannot get a read lock on a database, it is locked by another process");
     178                 :          0 :                 return (EXIT_FAILURE);
     179                 :            :         }
     180                 :            : 
     181                 :            : 
     182         [ -  + ]:          6 :         if (pkg_jobs_new(&jobs, PKG_JOBS_FETCH, db) != EPKG_OK)
     183                 :          0 :                 goto cleanup;
     184                 :            : 
     185         [ -  + ]:          6 :         if (pkg_jobs_set_repositories(jobs, &reponames) != EPKG_OK)
     186                 :          0 :                 goto cleanup;
     187                 :            : 
     188   [ -  +  #  # ]:          6 :         if (destdir != NULL && pkg_jobs_set_destdir(jobs, destdir) != EPKG_OK)
     189                 :          0 :                 goto cleanup;
     190                 :            : 
     191                 :          6 :         pkg_jobs_set_flags(jobs, f);
     192                 :            : 
     193   [ +  -  +  - ]:          6 :         if (!upgrades_for_installed &&
     194                 :          6 :             pkg_jobs_add(jobs, match, argv, argc) != EPKG_OK)
     195                 :          0 :                 goto cleanup;
     196                 :            : 
     197         [ +  + ]:          6 :         if (pkg_jobs_solve(jobs) != EPKG_OK)
     198                 :          1 :                 goto cleanup;
     199                 :            : 
     200         [ +  + ]:          5 :         if (pkg_jobs_count(jobs) == 0)
     201                 :          1 :                 goto cleanup;
     202                 :            : 
     203         [ -  + ]:          4 :         if (!quiet) {
     204                 :            : 
     205                 :          4 :                 rc = print_jobs_summary(jobs,
     206                 :            :                     "The following packages will be fetched:\n\n");
     207                 :            : 
     208         [ +  + ]:          4 :                 if (rc != 0) {
     209                 :          3 :                         rc = query_yesno(false, "\nProceed with fetching "
     210                 :            :                                         "packages? ");
     211                 :          3 :                 } else {
     212                 :          1 :                         printf("No packages are required to be fetched.\n");
     213                 :          1 :                         rc = query_yesno(false, "Check the integrity of packages "
     214                 :            :                                                         "downloaded? ");
     215                 :          1 :                         csum_only = true;
     216                 :            :                 }
     217                 :          4 :         }
     218                 :            :         else {
     219                 :          0 :                 rc = true;
     220                 :            :         }
     221                 :            : 
     222   [ +  -  +  + ]:          4 :         if (!rc || (retcode = pkg_jobs_apply(jobs)) != EPKG_OK)
     223                 :          2 :                 goto cleanup;
     224                 :            : 
     225   [ +  +  -  + ]:          2 :         if (csum_only && !quiet)
     226                 :          1 :                 printf("Integrity check was successful.\n");
     227                 :            : 
     228                 :          2 :         status = EXIT_SUCCESS;
     229                 :            : 
     230                 :            : cleanup:
     231                 :          6 :         pkg_jobs_free(jobs);
     232                 :          6 :         pkgdb_release_lock(db, PKGDB_LOCK_READONLY);
     233                 :          6 :         pkgdb_close(db);
     234                 :            : 
     235                 :          6 :         return (status);
     236                 :          6 : }

Generated by: LCOV version 1.15