LCOV - code coverage report
Current view: top level - src - repo.c (source / functions) Hit Total Coverage
Test: cov.info Lines: 17 56 30.4 %
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             :  * 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             : #ifdef HAVE_CONFIG_H
      28             : #include "pkg_config.h"
      29             : #endif
      30             : 
      31             : #include <bsd_compat.h>
      32             : #include <getopt.h>
      33             : #include <signal.h>
      34             : #include <sysexits.h>
      35             : #include <stdio.h>
      36             : #include <string.h>
      37             : 
      38             : #ifdef HAVE_READPASSPHRASE_H
      39             : #include <readpassphrase.h>
      40             : #elif defined(HAVE_BSD_READPASSPHRASE_H)
      41             : #include <bsd/readpassphrase.h>
      42             : #else
      43             : #include "readpassphrase_compat.h"
      44             : #endif
      45             : 
      46             : #include <unistd.h>
      47             : 
      48             : #include <pkg.h>
      49             : #include "pkgcli.h"
      50             : 
      51             : void
      52           0 : usage_repo(void)
      53             : {
      54           0 :         fprintf(stderr, "Usage: pkg repo [-lqL] [-o output-dir] <repo-path> "
      55             :             "[<rsa-key>|signing_command: <the command>]\n\n");
      56           0 :         fprintf(stderr, "For more information see 'pkg help repo'.\n");
      57           0 : }
      58             : 
      59             : static int
      60           0 : password_cb(char *buf, int size, int rwflag, void *key)
      61             : {
      62           0 :         int len = 0;
      63             :         char pass[BUFSIZ];
      64             :         sigset_t sig, oldsig;
      65             : 
      66             :         (void)rwflag;
      67             :         (void)key;
      68             : 
      69             :         /* Block sigalarm temporary */
      70           0 :         sigemptyset(&sig);
      71           0 :         sigaddset(&sig, SIGALRM);
      72           0 :         sigprocmask(SIG_BLOCK, &sig, &oldsig);
      73             : 
      74           0 :         if (readpassphrase("\nEnter passphrase: ", pass, BUFSIZ, RPP_ECHO_OFF) == NULL)
      75           0 :                 return 0;
      76             : 
      77           0 :         len = strlen(pass);
      78             : 
      79           0 :         if (len <= 0)  return 0;
      80           0 :         if (len > size) len = size;
      81             : 
      82           0 :         memset(buf, '\0', size);
      83           0 :         memcpy(buf, pass, len);
      84           0 :         memset(pass, 0, BUFSIZ);
      85             : 
      86           0 :         sigprocmask(SIG_SETMASK, &oldsig, NULL);
      87             : 
      88           0 :         return (len);
      89             : }
      90             : 
      91             : int
      92          33 : exec_repo(int argc, char **argv)
      93             : {
      94             :         int      ret;
      95             :         int      ch;
      96          33 :         bool     filelist = false;
      97          33 :         char    *output_dir = NULL;
      98          33 :         char    *meta_file = NULL;
      99          33 :         bool    legacy = false;
     100             : 
     101          33 :         struct option longopts[] = {
     102             :                 { "list-files", no_argument,          NULL,   'l' },
     103             :                 { "output-dir", required_argument,    NULL,   'o' },
     104             :                 { "quiet",    no_argument,            NULL,   'q' },
     105             :                 { "meta-file",        required_argument,      NULL,   'm' },
     106             :                 { "legacy",   no_argument,    NULL,   'L' },
     107             :                 { NULL,         0,                      NULL,   0   },
     108             :         };
     109             : 
     110          66 :         while ((ch = getopt_long(argc, argv, "+lo:qm:L", longopts, NULL)) != -1) {
     111           0 :                 switch (ch) {
     112             :                 case 'l':
     113           0 :                         filelist = true;
     114           0 :                         break;
     115             :                 case 'o':
     116           0 :                         output_dir = optarg;
     117           0 :                         break;
     118             :                 case 'q':
     119           0 :                         quiet = true;
     120           0 :                         break;
     121             :                 case 'm':
     122           0 :                         meta_file = optarg;
     123           0 :                         break;
     124             :                 case 'L':
     125           0 :                         legacy = true;
     126           0 :                         break;
     127             :                 default:
     128           0 :                         usage_repo();
     129           0 :                         return (EX_USAGE);
     130             :                 }
     131             :         }
     132          33 :         argc -= optind;
     133          33 :         argv += optind;
     134             : 
     135          33 :         if (argc < 1) {
     136           0 :                 usage_repo();
     137           0 :                 return (EX_USAGE);
     138             :         }
     139             : 
     140          33 :         if (argc > 2 && strcmp(argv[1], "signing_command:") != 0) {
     141           0 :                 usage_repo();
     142           0 :                 return (EX_USAGE);
     143             :         }
     144             : 
     145          33 :         if (output_dir == NULL)
     146          33 :                 output_dir = argv[0];
     147             : 
     148          33 :         ret = pkg_create_repo(argv[0], output_dir, filelist, meta_file, legacy);
     149             : 
     150          11 :         if (ret != EPKG_OK) {
     151           0 :                 printf("Cannot create repository catalogue\n");
     152           0 :                 return (EX_IOERR);
     153             :         }
     154             : 
     155          11 :         if (pkg_finish_repo(output_dir, password_cb, argv + 1, argc - 1,
     156             :             filelist) != EPKG_OK)
     157           0 :                 return (EX_DATAERR);
     158             : 
     159          11 :         return (EX_OK);
     160             : }

Generated by: LCOV version 1.10