LCOV - code coverage report
Current view: top level - shared - import-util.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 1 84 1.2 %
Date: 2019-08-22 15:41:25 Functions: 2 7 28.6 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <errno.h>
       4             : #include <string.h>
       5             : 
       6             : #include "alloc-util.h"
       7             : #include "btrfs-util.h"
       8             : #include "import-util.h"
       9             : #include "log.h"
      10             : #include "macro.h"
      11             : #include "nulstr-util.h"
      12             : #include "path-util.h"
      13             : #include "string-table.h"
      14             : #include "string-util.h"
      15             : 
      16           0 : int import_url_last_component(const char *url, char **ret) {
      17             :         const char *e, *p;
      18             :         char *s;
      19             : 
      20           0 :         e = strchrnul(url, '?');
      21             : 
      22           0 :         while (e > url && e[-1] == '/')
      23           0 :                 e--;
      24             : 
      25           0 :         p = e;
      26           0 :         while (p > url && p[-1] != '/')
      27           0 :                 p--;
      28             : 
      29           0 :         if (e <= p)
      30           0 :                 return -EINVAL;
      31             : 
      32           0 :         s = strndup(p, e - p);
      33           0 :         if (!s)
      34           0 :                 return -ENOMEM;
      35             : 
      36           0 :         *ret = s;
      37           0 :         return 0;
      38             : }
      39             : 
      40           0 : int import_url_change_last_component(const char *url, const char *suffix, char **ret) {
      41             :         const char *e;
      42             :         char *s;
      43             : 
      44           0 :         assert(url);
      45           0 :         assert(ret);
      46             : 
      47           0 :         e = strchrnul(url, '?');
      48             : 
      49           0 :         while (e > url && e[-1] == '/')
      50           0 :                 e--;
      51             : 
      52           0 :         while (e > url && e[-1] != '/')
      53           0 :                 e--;
      54             : 
      55           0 :         if (e <= url)
      56           0 :                 return -EINVAL;
      57             : 
      58           0 :         s = new(char, (e - url) + strlen(suffix) + 1);
      59           0 :         if (!s)
      60           0 :                 return -ENOMEM;
      61             : 
      62           0 :         strcpy(mempcpy(s, url, e - url), suffix);
      63           0 :         *ret = s;
      64           0 :         return 0;
      65             : }
      66             : 
      67             : static const char* const import_verify_table[_IMPORT_VERIFY_MAX] = {
      68             :         [IMPORT_VERIFY_NO] = "no",
      69             :         [IMPORT_VERIFY_CHECKSUM] = "checksum",
      70             :         [IMPORT_VERIFY_SIGNATURE] = "signature",
      71             : };
      72             : 
      73          10 : DEFINE_STRING_TABLE_LOOKUP(import_verify, ImportVerify);
      74             : 
      75           0 : int tar_strip_suffixes(const char *name, char **ret) {
      76             :         const char *e;
      77             :         char *s;
      78             : 
      79           0 :         e = endswith(name, ".tar");
      80           0 :         if (!e)
      81           0 :                 e = endswith(name, ".tar.xz");
      82           0 :         if (!e)
      83           0 :                 e = endswith(name, ".tar.gz");
      84           0 :         if (!e)
      85           0 :                 e = endswith(name, ".tar.bz2");
      86           0 :         if (!e)
      87           0 :                 e = endswith(name, ".tgz");
      88           0 :         if (!e)
      89           0 :                 e = strchr(name, 0);
      90             : 
      91           0 :         if (e <= name)
      92           0 :                 return -EINVAL;
      93             : 
      94           0 :         s = strndup(name, e - name);
      95           0 :         if (!s)
      96           0 :                 return -ENOMEM;
      97             : 
      98           0 :         *ret = s;
      99           0 :         return 0;
     100             : }
     101             : 
     102           0 : int raw_strip_suffixes(const char *p, char **ret) {
     103             : 
     104             :         static const char suffixes[] =
     105             :                 ".xz\0"
     106             :                 ".gz\0"
     107             :                 ".bz2\0"
     108             :                 ".raw\0"
     109             :                 ".qcow2\0"
     110             :                 ".img\0"
     111             :                 ".bin\0";
     112             : 
     113           0 :         _cleanup_free_ char *q = NULL;
     114             : 
     115           0 :         q = strdup(p);
     116           0 :         if (!q)
     117           0 :                 return -ENOMEM;
     118             : 
     119           0 :         for (;;) {
     120             :                 const char *sfx;
     121           0 :                 bool changed = false;
     122             : 
     123           0 :                 NULSTR_FOREACH(sfx, suffixes) {
     124             :                         char *e;
     125             : 
     126           0 :                         e = endswith(q, sfx);
     127           0 :                         if (e) {
     128           0 :                                 *e = 0;
     129           0 :                                 changed = true;
     130             :                         }
     131             :                 }
     132             : 
     133           0 :                 if (!changed)
     134           0 :                         break;
     135             :         }
     136             : 
     137           0 :         *ret = TAKE_PTR(q);
     138             : 
     139           0 :         return 0;
     140             : }
     141             : 
     142           0 : int import_assign_pool_quota_and_warn(const char *path) {
     143             :         int r;
     144             : 
     145           0 :         r = btrfs_subvol_auto_qgroup("/var/lib/machines", 0, true);
     146           0 :         if (r == -ENOTTY)  {
     147           0 :                 log_debug_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines, as directory is not on btrfs or not a subvolume. Ignoring.");
     148           0 :                 return 0;
     149             :         }
     150           0 :         if (r < 0)
     151           0 :                 return log_error_errno(r, "Failed to set up default quota hierarchy for /var/lib/machines: %m");
     152           0 :         if (r > 0)
     153           0 :                 log_info("Set up default quota hierarchy for /var/lib/machines.");
     154             : 
     155           0 :         r = btrfs_subvol_auto_qgroup(path, 0, true);
     156           0 :         if (r == -ENOTTY) {
     157           0 :                 log_debug_errno(r, "Failed to set up quota hierarchy for %s, as directory is not on btrfs or not a subvolume. Ignoring.", path);
     158           0 :                 return 0;
     159             :         }
     160           0 :         if (r < 0)
     161           0 :                 return log_error_errno(r, "Failed to set up default quota hierarchy for %s: %m", path);
     162           0 :         if (r > 0)
     163           0 :                 log_debug("Set up default quota hierarchy for %s.", path);
     164             : 
     165           0 :         return 0;
     166             : }

Generated by: LCOV version 1.14