some refactoring, cleaning, types fixed in fprintf

This commit is contained in:
Yakov Olkhovskiy 2022-06-13 20:25:19 -04:00
parent ce395dc68d
commit 539100fc4a
2 changed files with 33 additions and 21 deletions

View File

@ -28,6 +28,25 @@ ssize_t write_data(int fd, const void *buf, size_t count)
return count;
}
/// blocking read
ssize_t read_data(int fd, void *buf, size_t count)
{
for (size_t n = 0; n < count;)
{
ssize_t sz = read(fd, reinterpret_cast<char*>(buf) + n, count - n);
if (sz < 0)
{
if (errno == EINTR)
continue;
return sz;
}
if (sz == 0)
return count - n;
n += sz;
}
return count;
}
/// Main compression part
int doCompress(char * input, char * output, off_t & in_offset, off_t & out_offset,
off_t input_size, off_t output_size, ZSTD_CCtx * cctx)
@ -235,7 +254,7 @@ int compressFiles(char* filenames[], int count, int output_fd, int level, const
continue;
}
printf("Size: %lld\n", info_in.st_size);
printf("Size: %td\n", info_in.st_size);
/// Save umask
files_data[i].umask = info_in.st_mode;
@ -287,32 +306,28 @@ int copy_decompressor(const char *self, int output_fd)
if (-1 == lseek(input_fd, -15, SEEK_END))
{
close(input_fd);
perror(nullptr);
close(input_fd);
return 1;
}
char size_str[16] = {0};
for (size_t s_sz = sizeof(size_str) - 1; s_sz;)
if (ssize_t sz = read_data(input_fd, size_str, 15); sz < 15)
{
ssize_t sz = read(input_fd, size_str + sizeof(size_str) - (s_sz + 1), s_sz);
if (sz <= 0)
{
if (errno == EINTR)
continue;
close(input_fd);
if (sz < 0)
perror(nullptr);
return 1;
}
s_sz -= sz;
else
fprintf(stderr, "Error: unable to extract decompressor.\n");
close(input_fd);
return 1;
}
int decompressor_size = atoi(size_str);
if (-1 == lseek(input_fd, -(decompressor_size + 15), SEEK_END))
{
close(input_fd);
perror(nullptr);
close(input_fd);
return 1;
}
@ -330,15 +345,15 @@ int copy_decompressor(const char *self, int output_fd)
{
if (errno == EINTR)
continue;
close(input_fd);
perror(nullptr);
close(input_fd);
return 1;
}
if (n != write_data(output_fd, buf, n))
{
close(input_fd);
perror(nullptr);
close(input_fd);
return 1;
}
} while (true);

View File

@ -1,6 +1,3 @@
//#include <cstddef>
//#include <cstdio>
//#include <cstring>
#include <zstd.h>
#include <sys/mman.h>
#if defined __APPLE__
@ -55,7 +52,7 @@ int decompress(char * input, char * output, off_t start, off_t end, size_t max_n
size = ZSTD_findFrameCompressedSize(input + in_pointer, max_block_size);
if (ZSTD_isError(size))
{
fprintf(stderr, "Error (ZSTD): %lld %s\n", size, ZSTD_getErrorName(size));
fprintf(stderr, "Error (ZSTD): %td %s\n", size, ZSTD_getErrorName(size));
error_happened = true;
break;
}
@ -63,7 +60,7 @@ int decompress(char * input, char * output, off_t start, off_t end, size_t max_n
decompressed_size = ZSTD_getFrameContentSize(input + in_pointer, max_block_size);
if (ZSTD_isError(decompressed_size))
{
fprintf(stderr, "Error (ZSTD): %lld %s\n", decompressed_size, ZSTD_getErrorName(decompressed_size));
fprintf(stderr, "Error (ZSTD): %td %s\n", decompressed_size, ZSTD_getErrorName(decompressed_size));
error_happened = true;
break;
}
@ -174,7 +171,7 @@ int decompressFiles(int input_fd, char * path, char * name, bool & have_compress
}
if (fs_info.f_blocks * info_in.st_blksize < decompressed_full_size)
{
fprintf(stderr, "Not enough space for decompression. Have %llu, need %zu.",
fprintf(stderr, "Not enough space for decompression. Have %tu, need %zu.",
fs_info.f_blocks * info_in.st_blksize, decompressed_full_size);
return 1;
}