Welcome to mirror list, hosted at ThFree Co, Russian Federation.

compression.cc « filed « src « core - github.com/bareos/bareos.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: de1bea4f86542cf6abc3f195394d004079973e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/*
   BAREOS® - Backup Archiving REcovery Open Sourced

   Copyright (C) 2000-2011 Free Software Foundation Europe e.V.
   Copyright (C) 2011-2012 Planets Communications B.V.
   Copyright (C) 2013-2019 Bareos GmbH & Co. KG

   This program is Free Software; you can redistribute it and/or
   modify it under the terms of version three of the GNU Affero General Public
   License as published by the Free Software Foundation and included
   in the file LICENSE.

   This program is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   Affero General Public License for more details.

   You should have received a copy of the GNU Affero General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301, USA.
*/
/*
 * Kern Sibbald, March MM
 * Extracted from other source files by Marco van Wieringen, June 2011
 */
/**
 * @file
 * Functions to handle compression/decompression of data.
 */

#include "include/bareos.h"
#include "filed/filed.h"
#include "filed/filed_globals.h"
#include "filed/jcr_private.h"

#if defined(HAVE_LIBZ)
#  include <zlib.h>
#endif

#include "fastlz/fastlzlib.h"

namespace filedaemon {

// For compression we enable all used compressors in the fileset.
bool AdjustCompressionBuffers(JobControlRecord* jcr)
{
  findFILESET* fileset = jcr->impl->ff->fileset;
  uint32_t compress_buf_size = 0;

  if (fileset) {
    int i, j;

    for (i = 0; i < fileset->include_list.size(); i++) {
      findIncludeExcludeItem* incexe
          = (findIncludeExcludeItem*)fileset->include_list.get(i);
      for (j = 0; j < incexe->opts_list.size(); j++) {
        findFOPTS* fo = (findFOPTS*)incexe->opts_list.get(j);

        if (!SetupCompressionBuffers(jcr, me->compatible, fo->Compress_algo,
                                     &compress_buf_size)) {
          return false;
        }
      }
    }

    if (compress_buf_size > 0) {
      jcr->compress.deflate_buffer = GetMemory(compress_buf_size);
      jcr->compress.deflate_buffer_size = compress_buf_size;
    }
  }

  return true;
}

// For decompression we use the same decompression buffer for each algorithm.
bool AdjustDecompressionBuffers(JobControlRecord* jcr)
{
  uint32_t decompress_buf_size;

  SetupDecompressionBuffers(jcr, &decompress_buf_size);

  if (decompress_buf_size > 0) {
    jcr->compress.inflate_buffer = GetMemory(decompress_buf_size);
    jcr->compress.inflate_buffer_size = decompress_buf_size;
  }

  return true;
}

bool SetupCompressionContext(b_ctx& bctx)
{
  bool retval = false;

  if (BitIsSet(FO_COMPRESS, bctx.ff_pkt->flags)) {
    // See if we need to be compatible with the old GZIP stream encoding.
    if (!me->compatible || bctx.ff_pkt->Compress_algo != COMPRESS_GZIP) {
      memset(&bctx.ch, 0, sizeof(comp_stream_header));

      // Calculate buffer offsets.
      if (BitIsSet(FO_SPARSE, bctx.ff_pkt->flags)
          || BitIsSet(FO_OFFSETS, bctx.ff_pkt->flags)) {
        bctx.chead
            = (uint8_t*)bctx.jcr->compress.deflate_buffer + OFFSET_FADDR_SIZE;
        bctx.cbuf = (uint8_t*)bctx.jcr->compress.deflate_buffer
                    + OFFSET_FADDR_SIZE + sizeof(comp_stream_header);
        bctx.max_compress_len
            = bctx.jcr->compress.deflate_buffer_size
              - (sizeof(comp_stream_header) + OFFSET_FADDR_SIZE);
      } else {
        bctx.chead = (uint8_t*)bctx.jcr->compress.deflate_buffer;
        bctx.cbuf = (uint8_t*)bctx.jcr->compress.deflate_buffer
                    + sizeof(comp_stream_header);
        bctx.max_compress_len = bctx.jcr->compress.deflate_buffer_size
                                - sizeof(comp_stream_header);
      }

      bctx.wbuf
          = bctx.jcr->compress.deflate_buffer; /* compressed output here */
      bctx.cipher_input
          = (uint8_t*)
                bctx.jcr->compress.deflate_buffer; /* encrypt compressed data */
      bctx.ch.magic = bctx.ff_pkt->Compress_algo;
      bctx.ch.version = COMP_HEAD_VERSION;
    } else {
      // Calculate buffer offsets.
      bctx.chead = NULL;
      if (BitIsSet(FO_SPARSE, bctx.ff_pkt->flags)
          || BitIsSet(FO_OFFSETS, bctx.ff_pkt->flags)) {
        bctx.cbuf
            = (uint8_t*)bctx.jcr->compress.deflate_buffer + OFFSET_FADDR_SIZE;
        bctx.max_compress_len
            = bctx.jcr->compress.deflate_buffer_size - OFFSET_FADDR_SIZE;
      } else {
        bctx.cbuf = (uint8_t*)bctx.jcr->compress.deflate_buffer;
        bctx.max_compress_len = bctx.jcr->compress.deflate_buffer_size;
      }
      bctx.wbuf
          = bctx.jcr->compress.deflate_buffer; /* compressed output here */
      bctx.cipher_input
          = (uint8_t*)
                bctx.jcr->compress.deflate_buffer; /* encrypt compressed data */
    }

    /*
     * Do compression specific actions and set the magic, header version and
     * compression level.
     */
    switch (bctx.ff_pkt->Compress_algo) {
#if defined(HAVE_LIBZ)
      case COMPRESS_GZIP: {
        z_stream* pZlibStream;

        /**
         * Only change zlib parameters if there is no pending operation.
         * This should never happen as deflateReset is called after each
         * deflate.
         */
        pZlibStream = (z_stream*)bctx.jcr->compress.workset.pZLIB;
        if (pZlibStream->total_in == 0) {
          int zstat;

          // Set gzip compression level - must be done per file
          if ((zstat = deflateParams(pZlibStream, bctx.ff_pkt->Compress_level,
                                     Z_DEFAULT_STRATEGY))
              != Z_OK) {
            Jmsg(bctx.jcr, M_FATAL, 0,
                 _("Compression deflateParams error: %d\n"), zstat);
            bctx.jcr->setJobStatus(JS_ErrorTerminated);
            goto bail_out;
          }
        }
        bctx.ch.level = bctx.ff_pkt->Compress_level;
        break;
      }
#endif
#if defined(HAVE_LZO)
      case COMPRESS_LZO1X:
        break;
#endif
      case COMPRESS_FZFZ:
      case COMPRESS_FZ4L:
      case COMPRESS_FZ4H: {
        int zstat;
        zfast_stream* pZfastStream;
        zfast_stream_compressor compressor = COMPRESSOR_FASTLZ;

        /**
         * Only change fastlz parameters if there is no pending operation.
         * This should never happen as fastlzlibCompressReset is called after
         * each fastlzlibCompress.
         */
        pZfastStream = (zfast_stream*)bctx.jcr->compress.workset.pZFAST;
        if (pZfastStream->total_in == 0) {
          switch (bctx.ff_pkt->Compress_algo) {
            case COMPRESS_FZ4L:
            case COMPRESS_FZ4H:
              compressor = COMPRESSOR_LZ4;
              break;
          }

          if ((zstat = fastlzlibSetCompressor(pZfastStream, compressor))
              != Z_OK) {
            Jmsg(bctx.jcr, M_FATAL, 0,
                 _("Compression fastlzlibSetCompressor error: %d\n"), zstat);
            bctx.jcr->setJobStatus(JS_ErrorTerminated);
            goto bail_out;
          }
        }
        bctx.ch.level = bctx.ff_pkt->Compress_level;
        break;
      }
      default:
        break;
    }
  }

  retval = true;

bail_out:
  return retval;
}

} /* namespace filedaemon */