+ f->written_compressed += sz;
+
+ return 0;
+}
+
+static int raw_import_file_detect_xz(RawImportFile *f) {
+ static const uint8_t xz_signature[] = {
+ '\xfd', '7', 'z', 'X', 'Z', '\x00'
+ };
+ lzma_ret lzr;
+ int r;
+
+ assert(f);
+
+ if (f->payload_size < sizeof(xz_signature))
+ return 0;
+
+ f->compressed = memcmp(f->payload, xz_signature, sizeof(xz_signature)) == 0;
+ log_debug("Stream is XZ compressed: %s", yes_no(f->compressed));
+
+ if (f->compressed) {
+ lzr = lzma_stream_decoder(&f->lzma, UINT64_MAX, LZMA_TELL_UNSUPPORTED_CHECK);
+ if (lzr != LZMA_OK) {
+ log_error("Failed to initialize LZMA decoder.");
+ return -EIO;
+ }
+ }
+
+ r = raw_import_file_open_disk_for_write(f);
+ if (r < 0)
+ return r;
+
+ r = raw_import_file_write_compressed(f, f->payload, f->payload_size);
+ if (r < 0)
+ return r;
+
+ free(f->payload);
+ f->payload = NULL;
+ f->payload_size = 0;
+
+ return 0;
+}
+
+static size_t raw_import_file_write_callback(void *contents, size_t size, size_t nmemb, void *userdata) {
+ RawImportFile *f = userdata;
+ size_t sz = size * nmemb;
+ int r;
+
+ assert(contents);
+ assert(f);
+
+ if (f->done) {
+ r = -ESTALE;