33 unsigned char buf[1024];
39 have = fread(buf, 1, 1024, fsrc);
40 if (fwrite(buf, 1, have, fdest) != have)
42 }
while (have == 1024);
50 FILE *fsrc = fopen(src.c_str(),
"r");
51 if (!fsrc)
goto file_copy_final;
55 retval |= fchmod(fileno(fdest), info.st_mode);
58 if (fsrc) fclose(fsrc);
69 fsrc = fopen(src.c_str(),
"r");
70 if (!fsrc)
goto file_copy_final;
72 fdest = fopen(dest.c_str(),
"w");
73 if (!fdest)
goto file_copy_final;
77 retval |= fchmod(fileno(fdest), info.st_mode);
80 if (fsrc) fclose(fsrc);
81 if (fdest) fclose(fdest);
86 bool CopyMem2File(
const unsigned char *buffer,
const unsigned buffer_size,
89 int written = fwrite(buffer, 1, buffer_size, fdest);
90 return (written >=0) && (unsigned(written) == buffer_size);
94 bool CopyMem2Path(
const unsigned char *buffer,
const unsigned buffer_size,
101 int written = write(fd, buffer, buffer_size);
104 return (written >=0) && (unsigned(written) == buffer_size);
109 unsigned char **buffer,
unsigned *buffer_size)
111 const int fd = open(path.c_str(), O_RDONLY);
116 *buffer =
reinterpret_cast<unsigned char *
>(smalloc(*buffer_size));
117 unsigned total_bytes = 0;
119 int num_bytes = read(fd, *buffer + total_bytes, *buffer_size - total_bytes);
128 total_bytes += num_bytes;
129 if (total_bytes >= *buffer_size) {
132 reinterpret_cast<unsigned char *
>(srealloc(*buffer, *buffer_size));
137 *buffer_size = total_bytes;
149 if ((algorithm_option ==
"default") || (algorithm_option ==
"zlib"))
151 if (algorithm_option ==
"none")
154 algorithm_option.c_str());
175 strm->zalloc = Z_NULL;
176 strm->zfree = Z_NULL;
177 strm->opaque = Z_NULL;
178 strm->next_in = Z_NULL;
180 int retval = deflateInit(strm, Z_DEFAULT_COMPRESSION);
186 strm->zalloc = Z_NULL;
187 strm->zfree = Z_NULL;
188 strm->opaque = Z_NULL;
190 strm->next_in = Z_NULL;
191 int retval = inflateInit(strm);
197 (void)deflateEnd(strm);
202 (void)inflateEnd(strm);
216 strm->avail_in =
size;
217 strm->next_in =
static_cast<unsigned char *
>(
const_cast<void *
>(buf));
222 strm->next_out = out;
223 z_ret = deflate(strm, eof ? Z_FINISH : Z_NO_FLUSH);
224 if (z_ret == Z_STREAM_ERROR)
226 size_t have =
kZChunk - strm->avail_out;
228 }
while (strm->avail_out == 0);
246 strm->next_in = ((
unsigned char *)buf)+pos;
251 strm->next_out = out;
252 z_ret = inflate(strm, Z_NO_FLUSH);
255 z_ret = Z_DATA_ERROR;
262 size_t have =
kZChunk - strm->avail_out;
263 int64_t written = sink->
Write(out, have);
264 if ((written < 0) || (static_cast<uint64_t>(written) != have))
266 }
while (strm->avail_out == 0);
269 }
while (pos < size);
287 strm->next_in = ((
unsigned char *)buf)+pos;
292 strm->next_out = out;
293 z_ret = inflate(strm, Z_NO_FLUSH);
296 z_ret = Z_DATA_ERROR;
303 size_t have =
kZChunk - strm->avail_out;
304 if (fwrite(out, 1, have, f) != have || ferror(f)) {
306 "(errno=%d)", strerror(errno), errno);
309 }
while (strm->avail_out == 0);
312 }
while (pos < size);
319 FILE *fsrc = fopen(src.c_str(),
"r");
326 FILE *fdest = fopen(dest.c_str(),
"w");
329 "failed with errno=%d", dest.c_str(), errno);
335 src.c_str(), dest.c_str());
347 FILE *fsrc = fopen(src.c_str(),
"r");
354 FILE *fdest = fopen(dest.c_str(),
"w");
357 "failed with errno=%d", dest.c_str(), errno);
363 src.c_str(), dest.c_str());
366 goto compress_path2path_final;
368 if (
platform_fstat(fileno(fsrc), &info) != 0)
goto compress_path2path_final;
370 if (fchmod(fileno(fdest), info.st_mode) != 0)
goto compress_path2path_final;
374 compress_path2path_final:
386 fsrc = fopen(src.c_str(),
"r");
387 if (!fsrc)
goto decompress_path2path_final;
389 fdest = fopen(dest.c_str(),
"w");
390 if (!fdest)
goto decompress_path2path_final;
394 decompress_path2path_final:
395 if (fsrc) fclose(fsrc);
396 if (fdest) fclose(fdest);
412 hash_context.
buffer = alloca(hash_context.
size);
417 strm.avail_in = fread(in, 1,
kZChunk, fsrc);
418 if (ferror(fsrc))
goto compress_file2null_final;
420 flush = feof(fsrc) ? Z_FINISH : Z_NO_FLUSH;
428 z_ret = deflate(&strm, flush);
429 if (z_ret == Z_STREAM_ERROR)
430 goto compress_file2null_final;
431 have =
kZChunk - strm.avail_out;
433 }
while (strm.avail_out == 0);
436 }
while (flush != Z_FINISH);
439 if (z_ret != Z_STREAM_END)
goto compress_file2null_final;
445 compress_file2null_final:
454 uint64_t *processed_bytes) {
462 off_t cksum_bytes = 0;
466 hash_context.
buffer = alloca(hash_context.
size);
471 ssize_t bytes_read = read(fd_src, in,
kZChunk);
472 if (bytes_read < 0) {
473 if (errno == EINTR) {
continue;}
474 goto compress_fd2null_final;
476 cksum_bytes += bytes_read;
477 strm.avail_in = bytes_read;
479 flush = (
static_cast<size_t>(bytes_read) <
kZChunk) ? Z_FINISH : Z_NO_FLUSH;
487 z_ret = deflate(&strm, flush);
488 if (z_ret == Z_STREAM_ERROR)
489 goto compress_fd2null_final;
490 have =
kZChunk - strm.avail_out;
492 }
while (strm.avail_out == 0);
495 }
while (flush != Z_FINISH);
498 if (z_ret != Z_STREAM_END)
goto compress_fd2null_final;
501 if (processed_bytes) {
502 *processed_bytes = cksum_bytes;
507 compress_fd2null_final:
516 FILE *fsrc = fopen(src.c_str(),
"r");
538 strm.avail_in = fread(in, 1,
kZChunk, fsrc);
539 if (ferror(fsrc))
goto compress_file2file_final;
541 flush = feof(fsrc) ? Z_FINISH : Z_NO_FLUSH;
549 z_ret = deflate(&strm, flush);
550 if (z_ret == Z_STREAM_ERROR)
551 goto compress_file2file_final;
552 have =
kZChunk - strm.avail_out;
553 if (fwrite(out, 1, have, fdest) != have || ferror(fdest))
554 goto compress_file2file_final;
555 }
while (strm.avail_out == 0);
558 }
while (flush != Z_FINISH);
561 if (z_ret != Z_STREAM_END)
goto compress_file2file_final;
566 compress_file2file_final:
576 FILE *fsrc = fopen(src.c_str(),
"r");
597 hash_context.
buffer = alloca(hash_context.
size);
602 strm.avail_in = fread(in, 1,
kZChunk, fsrc);
603 if (ferror(fsrc))
goto compress_file2file_hashed_final;
605 flush = feof(fsrc) ? Z_FINISH : Z_NO_FLUSH;
613 z_ret = deflate(&strm, flush);
614 if (z_ret == Z_STREAM_ERROR)
615 goto compress_file2file_hashed_final;
616 have =
kZChunk - strm.avail_out;
617 if (fwrite(out, 1, have, fdest) != have || ferror(fdest))
618 goto compress_file2file_hashed_final;
620 }
while (strm.avail_out == 0);
623 }
while (flush != Z_FINISH);
626 if (z_ret != Z_STREAM_END)
goto compress_file2file_hashed_final;
632 compress_file2file_hashed_final:
649 while ((have = fread(buf, 1,
kBufferSize, fsrc)) > 0) {
652 goto decompress_file2file_final;
655 stream_state, ferror(fsrc));
656 if ((stream_state !=
kStreamEnd) || ferror(fsrc))
657 goto decompress_file2file_final;
661 decompress_file2file_final:
668 FILE *fsrc = fopen(src.c_str(),
"r");
691 hash_context.
buffer = alloca(hash_context.
size);
696 used = min(static_cast<size_t>(
kZChunk), size - offset);
697 strm.avail_in = used;
699 flush = (strm.avail_in <
kZChunk) ? Z_FINISH : Z_NO_FLUSH;
700 strm.next_in =
const_cast<unsigned char*
>(buf + offset);
707 z_ret = deflate(&strm, flush);
708 if (z_ret == Z_STREAM_ERROR)
709 goto compress_file2file_hashed_final;
710 have =
kZChunk - strm.avail_out;
711 if (fwrite(out, 1, have, fdest) != have || ferror(fdest))
712 goto compress_file2file_hashed_final;
714 }
while (strm.avail_out == 0);
719 }
while (flush != Z_FINISH);
722 if (z_ret != Z_STREAM_END)
goto compress_file2file_hashed_final;
728 compress_file2file_hashed_final:
740 void **out_buf, uint64_t *out_size)
750 *out_buf = smalloc(alloc_size);
755 flush = (pos +
kZChunk) >= size ? Z_FINISH : Z_NO_FLUSH;
756 strm.next_in = ((
unsigned char *)buf) + pos;
762 z_ret = deflate(&strm, flush);
763 if (z_ret == Z_STREAM_ERROR) {
770 size_t have =
kZChunk - strm.avail_out;
771 if (*out_size+have > alloc_size) {
773 *out_buf = srealloc(*out_buf, alloc_size);
775 memcpy(static_cast<unsigned char *>(*out_buf) + *out_size, out, have);
777 }
while (strm.avail_out == 0);
780 }
while (flush != Z_FINISH);
783 if (z_ret != Z_STREAM_END) {
798 void **out_buf, uint64_t *out_size)
807 *out_buf = smalloc(alloc_size);
812 strm.next_in = ((
unsigned char *)buf)+pos;
818 z_ret = inflate(&strm, Z_NO_FLUSH);
821 z_ret = Z_DATA_ERROR;
831 size_t have =
kZChunk - strm.avail_out;
832 if (*out_size+have > alloc_size) {
834 *out_buf = srealloc(*out_buf, alloc_size);
836 memcpy(static_cast<unsigned char *>(*out_buf) + *out_size, out, have);
838 }
while (strm.avail_out == 0);
841 }
while (pos < size);
844 if (z_ret != Z_STREAM_END) {
858 void Compressor::RegisterPlugins() {
859 RegisterPlugin<ZlibCompressor>();
860 RegisterPlugin<EchoCompressor>();
880 const int zlib_retval = deflateInit(&
stream_, Z_DEFAULT_COMPRESSION);
889 int retcode = deflateEnd(&other->
stream_);
891 retcode = deflateCopy(const_cast<z_streamp>(&other->
stream_), &
stream_);
898 unsigned char **inbuf,
size_t *inbufsize,
899 unsigned char **outbuf,
size_t *outbufsize)
904 const int flush_int = (flush) ? Z_FINISH : Z_NO_FLUSH;
911 stream_.avail_out = *outbufsize;
915 retcode = deflate(&
stream_, flush_int);
916 assert(retcode == Z_OK || retcode == Z_STREAM_END);
918 *outbufsize -=
stream_.avail_out;
922 return (flush_int == Z_NO_FLUSH && retcode == Z_OK &&
stream_.avail_in == 0)
923 || (flush_int == Z_FINISH && retcode == Z_STREAM_END);
928 int retcode = deflateEnd(&
stream_);
935 return deflateBound(&
stream_, bytes);
960 unsigned char **inbuf,
size_t *inbufsize,
961 unsigned char **outbuf,
size_t *outbufsize)
963 size_t bytes_to_copy = min(*outbufsize, *inbufsize);
964 memcpy(*outbuf, *inbuf, bytes_to_copy);
965 const bool done = (bytes_to_copy == *inbufsize);
968 *inbuf += bytes_to_copy;
969 *outbufsize = bytes_to_copy;
970 *inbufsize -= bytes_to_copy;
979 return (bytes == 0) ? 1 : bytes;
bool CompressPath2Null(const string &src, shash::Any *compressed_hash)
Algorithms ParseCompressionAlgorithm(const std::string &algorithm_option)
StreamStates DecompressZStream2Sink(const void *buf, const int64_t size, z_stream *strm, cvmfs::Sink *sink)
const int kDefaultFileMode
const unsigned kBufferSize
bool CompressPath2File(const string &src, FILE *fdest, shash::Any *compressed_hash)
EchoCompressor(const Algorithms &alg)
bool CopyMem2File(const unsigned char *buffer, const unsigned buffer_size, FILE *fdest)
static bool WillHandle(const zlib::Algorithms &alg)
void DecompressInit(z_stream *strm)
bool DecompressMem2Mem(const void *buf, const int64_t size, void **out_buf, uint64_t *out_size)
StreamStates DecompressZStream2File(const void *buf, const int64_t size, z_stream *strm, FILE *f)
std::string AlgorithmName(const zlib::Algorithms alg)
bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize, unsigned char **outbuf, size_t *outbufsize)
assert((mem||(size==0))&&"Out Of Memory")
void DecompressFini(z_stream *strm)
bool CopyPath2Path(const string &src, const string &dest)
bool CopyPath2Mem(const string &path, unsigned char **buffer, unsigned *buffer_size)
bool CopyPath2File(const std::string &src, FILE *fdest)
void Init(ContextPtr context)
StreamStates CompressZStream2Null(const void *buf, const int64_t size, const bool eof, z_stream *strm, shash::ContextPtr *hash_context)
bool DecompressFile2File(FILE *fsrc, FILE *fdest)
bool CompressFile2File(FILE *fsrc, FILE *fdest)
size_t DeflateBound(const size_t bytes)
bool CopyMem2Path(const unsigned char *buffer, const unsigned buffer_size, const string &path)
bool CompressFd2Null(int fd_src, shash::Any *compressed_hash, uint64_t *processed_bytes)
void CompressFini(z_stream *strm)
bool DecompressPath2File(const string &src, FILE *fdest)
void Final(ContextPtr context, Any *any_digest)
void CompressInit(z_stream *strm)
bool Deflate(const bool flush, unsigned char **inbuf, size_t *inbufsize, unsigned char **outbuf, size_t *outbufsize)
void Update(const unsigned char *buffer, const unsigned buffer_length, ContextPtr context)
bool CompressFile2Null(FILE *fsrc, shash::Any *compressed_hash)
static bool CopyFile2File(FILE *fsrc, FILE *fdest)
bool CompressMem2File(const unsigned char *buf, const size_t size, FILE *fdest, shash::Any *compressed_hash)
bool DecompressPath2Path(const string &src, const string &dest)
bool CompressMem2Mem(const void *buf, const int64_t size, void **out_buf, uint64_t *out_size)
ZlibCompressor(const Algorithms &alg)
size_t DeflateBound(const size_t bytes)
bool CompressPath2Path(const string &src, const string &dest)
virtual int64_t Write(const void *buf, uint64_t sz)=0
CVMFS_EXPORT void LogCvmfs(const LogSource source, const int mask, const char *format,...)