LCOV - code coverage report
Current view: top level - import - import-tar.c (source / functions) Hit Total Coverage
Test: main_coverage.info Lines: 0 182 0.0 %
Date: 2019-08-22 15:41:25 Functions: 0 10 0.0 %

          Line data    Source code
       1             : /* SPDX-License-Identifier: LGPL-2.1+ */
       2             : 
       3             : #include <linux/fs.h>
       4             : 
       5             : #include "sd-daemon.h"
       6             : #include "sd-event.h"
       7             : 
       8             : #include "alloc-util.h"
       9             : #include "btrfs-util.h"
      10             : #include "copy.h"
      11             : #include "fd-util.h"
      12             : #include "fileio.h"
      13             : #include "fs-util.h"
      14             : #include "hostname-util.h"
      15             : #include "import-common.h"
      16             : #include "import-compress.h"
      17             : #include "import-tar.h"
      18             : #include "io-util.h"
      19             : #include "machine-pool.h"
      20             : #include "mkdir.h"
      21             : #include "path-util.h"
      22             : #include "process-util.h"
      23             : #include "qcow2-util.h"
      24             : #include "ratelimit.h"
      25             : #include "rm-rf.h"
      26             : #include "string-util.h"
      27             : #include "tmpfile-util.h"
      28             : #include "util.h"
      29             : 
      30             : struct TarImport {
      31             :         sd_event *event;
      32             : 
      33             :         char *image_root;
      34             : 
      35             :         TarImportFinished on_finished;
      36             :         void *userdata;
      37             : 
      38             :         char *local;
      39             :         bool force_local;
      40             :         bool read_only;
      41             : 
      42             :         char *temp_path;
      43             :         char *final_path;
      44             : 
      45             :         int input_fd;
      46             :         int tar_fd;
      47             : 
      48             :         ImportCompress compress;
      49             : 
      50             :         sd_event_source *input_event_source;
      51             : 
      52             :         uint8_t buffer[16*1024];
      53             :         size_t buffer_size;
      54             : 
      55             :         uint64_t written_compressed;
      56             :         uint64_t written_uncompressed;
      57             : 
      58             :         struct stat st;
      59             : 
      60             :         pid_t tar_pid;
      61             : 
      62             :         unsigned last_percent;
      63             :         RateLimit progress_rate_limit;
      64             : };
      65             : 
      66           0 : TarImport* tar_import_unref(TarImport *i) {
      67           0 :         if (!i)
      68           0 :                 return NULL;
      69             : 
      70           0 :         sd_event_source_unref(i->input_event_source);
      71             : 
      72           0 :         if (i->tar_pid > 1) {
      73           0 :                 (void) kill_and_sigcont(i->tar_pid, SIGKILL);
      74           0 :                 (void) wait_for_terminate(i->tar_pid, NULL);
      75             :         }
      76             : 
      77           0 :         if (i->temp_path) {
      78           0 :                 (void) rm_rf(i->temp_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
      79           0 :                 free(i->temp_path);
      80             :         }
      81             : 
      82           0 :         import_compress_free(&i->compress);
      83             : 
      84           0 :         sd_event_unref(i->event);
      85             : 
      86           0 :         safe_close(i->tar_fd);
      87             : 
      88           0 :         free(i->final_path);
      89           0 :         free(i->image_root);
      90           0 :         free(i->local);
      91           0 :         return mfree(i);
      92             : }
      93             : 
      94           0 : int tar_import_new(
      95             :                 TarImport **ret,
      96             :                 sd_event *event,
      97             :                 const char *image_root,
      98             :                 TarImportFinished on_finished,
      99             :                 void *userdata) {
     100             : 
     101           0 :         _cleanup_(tar_import_unrefp) TarImport *i = NULL;
     102           0 :         _cleanup_free_ char *root = NULL;
     103             :         int r;
     104             : 
     105           0 :         assert(ret);
     106             : 
     107           0 :         root = strdup(image_root ?: "/var/lib/machines");
     108           0 :         if (!root)
     109           0 :                 return -ENOMEM;
     110             : 
     111           0 :         i = new(TarImport, 1);
     112           0 :         if (!i)
     113           0 :                 return -ENOMEM;
     114             : 
     115           0 :         *i = (TarImport) {
     116             :                 .input_fd = -1,
     117             :                 .tar_fd = -1,
     118             :                 .on_finished = on_finished,
     119             :                 .userdata = userdata,
     120             :                 .last_percent = (unsigned) -1,
     121           0 :                 .image_root = TAKE_PTR(root),
     122             :         };
     123             : 
     124           0 :         RATELIMIT_INIT(i->progress_rate_limit, 100 * USEC_PER_MSEC, 1);
     125             : 
     126           0 :         if (event)
     127           0 :                 i->event = sd_event_ref(event);
     128             :         else {
     129           0 :                 r = sd_event_default(&i->event);
     130           0 :                 if (r < 0)
     131           0 :                         return r;
     132             :         }
     133             : 
     134           0 :         *ret = TAKE_PTR(i);
     135             : 
     136           0 :         return 0;
     137             : }
     138             : 
     139           0 : static void tar_import_report_progress(TarImport *i) {
     140             :         unsigned percent;
     141           0 :         assert(i);
     142             : 
     143             :         /* We have no size information, unless the source is a regular file */
     144           0 :         if (!S_ISREG(i->st.st_mode))
     145           0 :                 return;
     146             : 
     147           0 :         if (i->written_compressed >= (uint64_t) i->st.st_size)
     148           0 :                 percent = 100;
     149             :         else
     150           0 :                 percent = (unsigned) ((i->written_compressed * UINT64_C(100)) / (uint64_t) i->st.st_size);
     151             : 
     152           0 :         if (percent == i->last_percent)
     153           0 :                 return;
     154             : 
     155           0 :         if (!ratelimit_below(&i->progress_rate_limit))
     156           0 :                 return;
     157             : 
     158           0 :         sd_notifyf(false, "X_IMPORT_PROGRESS=%u", percent);
     159           0 :         log_info("Imported %u%%.", percent);
     160             : 
     161           0 :         i->last_percent = percent;
     162             : }
     163             : 
     164           0 : static int tar_import_finish(TarImport *i) {
     165             :         int r;
     166             : 
     167           0 :         assert(i);
     168           0 :         assert(i->tar_fd >= 0);
     169           0 :         assert(i->temp_path);
     170           0 :         assert(i->final_path);
     171             : 
     172           0 :         i->tar_fd = safe_close(i->tar_fd);
     173             : 
     174           0 :         if (i->tar_pid > 0) {
     175           0 :                 r = wait_for_terminate_and_check("tar", i->tar_pid, WAIT_LOG);
     176           0 :                 i->tar_pid = 0;
     177           0 :                 if (r < 0)
     178           0 :                         return r;
     179           0 :                 if (r != EXIT_SUCCESS)
     180           0 :                         return -EPROTO;
     181             :         }
     182             : 
     183           0 :         r = import_mangle_os_tree(i->temp_path);
     184           0 :         if (r < 0)
     185           0 :                 return r;
     186             : 
     187           0 :         if (i->read_only) {
     188           0 :                 r = import_make_read_only(i->temp_path);
     189           0 :                 if (r < 0)
     190           0 :                         return r;
     191             :         }
     192             : 
     193           0 :         if (i->force_local)
     194           0 :                 (void) rm_rf(i->final_path, REMOVE_ROOT|REMOVE_PHYSICAL|REMOVE_SUBVOLUME);
     195             : 
     196           0 :         r = rename_noreplace(AT_FDCWD, i->temp_path, AT_FDCWD, i->final_path);
     197           0 :         if (r < 0)
     198           0 :                 return log_error_errno(r, "Failed to move image into place: %m");
     199             : 
     200           0 :         i->temp_path = mfree(i->temp_path);
     201             : 
     202           0 :         return 0;
     203             : }
     204             : 
     205           0 : static int tar_import_fork_tar(TarImport *i) {
     206             :         int r;
     207             : 
     208           0 :         assert(i);
     209             : 
     210           0 :         assert(!i->final_path);
     211           0 :         assert(!i->temp_path);
     212           0 :         assert(i->tar_fd < 0);
     213             : 
     214           0 :         i->final_path = path_join(i->image_root, i->local);
     215           0 :         if (!i->final_path)
     216           0 :                 return log_oom();
     217             : 
     218           0 :         r = tempfn_random(i->final_path, NULL, &i->temp_path);
     219           0 :         if (r < 0)
     220           0 :                 return log_oom();
     221             : 
     222           0 :         (void) mkdir_parents_label(i->temp_path, 0700);
     223             : 
     224           0 :         r = btrfs_subvol_make(i->temp_path);
     225           0 :         if (r == -ENOTTY) {
     226           0 :                 if (mkdir(i->temp_path, 0755) < 0)
     227           0 :                         return log_error_errno(errno, "Failed to create directory %s: %m", i->temp_path);
     228           0 :         } else if (r < 0)
     229           0 :                 return log_error_errno(r, "Failed to create subvolume %s: %m", i->temp_path);
     230             :         else
     231           0 :                 (void) import_assign_pool_quota_and_warn(i->temp_path);
     232             : 
     233           0 :         i->tar_fd = import_fork_tar_x(i->temp_path, &i->tar_pid);
     234           0 :         if (i->tar_fd < 0)
     235           0 :                 return i->tar_fd;
     236             : 
     237           0 :         return 0;
     238             : }
     239             : 
     240           0 : static int tar_import_write(const void *p, size_t sz, void *userdata) {
     241           0 :         TarImport *i = userdata;
     242             :         int r;
     243             : 
     244           0 :         r = loop_write(i->tar_fd, p, sz, false);
     245           0 :         if (r < 0)
     246           0 :                 return r;
     247             : 
     248           0 :         i->written_uncompressed += sz;
     249             : 
     250           0 :         return 0;
     251             : }
     252             : 
     253           0 : static int tar_import_process(TarImport *i) {
     254             :         ssize_t l;
     255             :         int r;
     256             : 
     257           0 :         assert(i);
     258           0 :         assert(i->buffer_size < sizeof(i->buffer));
     259             : 
     260           0 :         l = read(i->input_fd, i->buffer + i->buffer_size, sizeof(i->buffer) - i->buffer_size);
     261           0 :         if (l < 0) {
     262           0 :                 if (errno == EAGAIN)
     263           0 :                         return 0;
     264             : 
     265           0 :                 r = log_error_errno(errno, "Failed to read input file: %m");
     266           0 :                 goto finish;
     267             :         }
     268           0 :         if (l == 0) {
     269           0 :                 if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
     270           0 :                         log_error("Premature end of file.");
     271           0 :                         r = -EIO;
     272           0 :                         goto finish;
     273             :                 }
     274             : 
     275           0 :                 r = tar_import_finish(i);
     276           0 :                 goto finish;
     277             :         }
     278             : 
     279           0 :         i->buffer_size += l;
     280             : 
     281           0 :         if (i->compress.type == IMPORT_COMPRESS_UNKNOWN) {
     282           0 :                 r = import_uncompress_detect(&i->compress, i->buffer, i->buffer_size);
     283           0 :                 if (r < 0) {
     284           0 :                         log_error_errno(r, "Failed to detect file compression: %m");
     285           0 :                         goto finish;
     286             :                 }
     287           0 :                 if (r == 0) /* Need more data */
     288           0 :                         return 0;
     289             : 
     290           0 :                 r = tar_import_fork_tar(i);
     291           0 :                 if (r < 0)
     292           0 :                         goto finish;
     293             :         }
     294             : 
     295           0 :         r = import_uncompress(&i->compress, i->buffer, i->buffer_size, tar_import_write, i);
     296           0 :         if (r < 0) {
     297           0 :                 log_error_errno(r, "Failed to decode and write: %m");
     298           0 :                 goto finish;
     299             :         }
     300             : 
     301           0 :         i->written_compressed += i->buffer_size;
     302           0 :         i->buffer_size = 0;
     303             : 
     304           0 :         tar_import_report_progress(i);
     305             : 
     306           0 :         return 0;
     307             : 
     308           0 : finish:
     309           0 :         if (i->on_finished)
     310           0 :                 i->on_finished(i, r, i->userdata);
     311             :         else
     312           0 :                 sd_event_exit(i->event, r);
     313             : 
     314           0 :         return 0;
     315             : }
     316             : 
     317           0 : static int tar_import_on_input(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
     318           0 :         TarImport *i = userdata;
     319             : 
     320           0 :         return tar_import_process(i);
     321             : }
     322             : 
     323           0 : static int tar_import_on_defer(sd_event_source *s, void *userdata) {
     324           0 :         TarImport *i = userdata;
     325             : 
     326           0 :         return tar_import_process(i);
     327             : }
     328             : 
     329           0 : int tar_import_start(TarImport *i, int fd, const char *local, bool force_local, bool read_only) {
     330             :         int r;
     331             : 
     332           0 :         assert(i);
     333           0 :         assert(fd >= 0);
     334           0 :         assert(local);
     335             : 
     336           0 :         if (!machine_name_is_valid(local))
     337           0 :                 return -EINVAL;
     338             : 
     339           0 :         if (i->input_fd >= 0)
     340           0 :                 return -EBUSY;
     341             : 
     342           0 :         r = fd_nonblock(fd, true);
     343           0 :         if (r < 0)
     344           0 :                 return r;
     345             : 
     346           0 :         r = free_and_strdup(&i->local, local);
     347           0 :         if (r < 0)
     348           0 :                 return r;
     349           0 :         i->force_local = force_local;
     350           0 :         i->read_only = read_only;
     351             : 
     352           0 :         if (fstat(fd, &i->st) < 0)
     353           0 :                 return -errno;
     354             : 
     355           0 :         r = sd_event_add_io(i->event, &i->input_event_source, fd, EPOLLIN, tar_import_on_input, i);
     356           0 :         if (r == -EPERM) {
     357             :                 /* This fd does not support epoll, for example because it is a regular file. Busy read in that case */
     358           0 :                 r = sd_event_add_defer(i->event, &i->input_event_source, tar_import_on_defer, i);
     359           0 :                 if (r < 0)
     360           0 :                         return r;
     361             : 
     362           0 :                 r = sd_event_source_set_enabled(i->input_event_source, SD_EVENT_ON);
     363             :         }
     364           0 :         if (r < 0)
     365           0 :                 return r;
     366             : 
     367           0 :         i->input_fd = fd;
     368           0 :         return r;
     369             : }

Generated by: LCOV version 1.14