programs: reformat using new code style

Signed-off-by: Marcel Cornu <marcel.d.cornu@intel.com>
This commit is contained in:
Marcel Cornu 2024-04-19 17:09:46 +01:00 committed by Pablo de Lara
parent aaa78d6a7c
commit cf6105271a

View File

@ -41,17 +41,17 @@
#include <errno.h>
#include "igzip_lib.h" /* Normally you use isa-l.h instead for external programs */
#if defined (HAVE_THREADS)
# include <pthread.h>
# include "crc.h"
#if defined(HAVE_THREADS)
#include <pthread.h>
#include "crc.h"
#endif
#if !defined (VERSION)
# if defined (ISAL_VERSION)
# define VERSION ISAL_VERSION
# else
# define VERSION "unknown version"
# endif
#if !defined(VERSION)
#if defined(ISAL_VERSION)
#define VERSION ISAL_VERSION
#else
#define VERSION "unknown version"
#endif
#endif
#define BAD_OPTION 1
@ -84,21 +84,11 @@ int default_suffixes_lens[] = { 3, 2 };
char stdin_file_name[] = "-";
int stdin_file_name_len = 1;
enum compression_modes {
COMPRESS_MODE,
DECOMPRESS_MODE
};
enum compression_modes { COMPRESS_MODE, DECOMPRESS_MODE };
enum long_only_opt_val {
RM
};
enum long_only_opt_val { RM };
enum log_types {
INFORM,
WARN,
ERROR,
VERBOSE
};
enum log_types { INFORM, WARN, ERROR, VERBOSE };
int level_size_buf[10] = {
#ifdef ISAL_DEF_LVL0_DEFAULT
@ -180,7 +170,8 @@ struct cli_options {
struct cli_options global_options;
void init_options(struct cli_options *options)
void
init_options(struct cli_options *options)
{
options->infile_name = NULL;
options->infile_name_len = 0;
@ -206,14 +197,16 @@ void init_options(struct cli_options *options)
options->threads = 1;
};
int is_interactive(void)
int
is_interactive(void)
{
int ret;
ret = !global_options.force && !global_options.quiet_level && isatty(fileno(stdin));
return ret;
}
size_t get_filesize(FILE * fp)
size_t
get_filesize(FILE *fp)
{
size_t file_size;
fpos_t pos, pos_curr;
@ -225,13 +218,14 @@ size_t get_filesize(FILE * fp)
fseeko(fp, 0, SEEK_END);
#endif
fgetpos(fp, &pos);
file_size = *(size_t *)&pos;
file_size = *(size_t *) &pos;
fsetpos(fp, &pos_curr); /* Restore position */
return file_size;
}
int get_posix_filetime(FILE * fp, uint32_t * time)
int
get_posix_filetime(FILE *fp, uint32_t *time)
{
struct stat file_stats;
const int ret = fstat(fileno(fp), &file_stats);
@ -240,7 +234,8 @@ int get_posix_filetime(FILE * fp, uint32_t * time)
return ret;
}
uint32_t set_filetime(char *file_name, uint32_t posix_time)
uint32_t
set_filetime(char *file_name, uint32_t posix_time)
{
struct utimbuf new_time;
new_time.actime = posix_time;
@ -248,7 +243,8 @@ uint32_t set_filetime(char *file_name, uint32_t posix_time)
return utime(file_name, &new_time);
}
void log_print(int log_type, char *format, ...)
void
log_print(int log_type, char *format, ...)
{
va_list args;
va_start(args, format);
@ -274,7 +270,8 @@ void log_print(int log_type, char *format, ...)
va_end(args);
}
int usage(int exit_code)
int
usage(int exit_code)
{
int log_type = exit_code ? WARN : INFORM;
log_print(log_type,
@ -293,7 +290,8 @@ int usage(int exit_code)
" -V, --version show version number\n"
" -v, --verbose verbose mode\n"
" -N, --name save/use file name and timestamp in compress/decompress\n"
" -n, --no-name do not save/use file name and timestamp in compress/decompress\n"
" -n, --no-name do not save/use file name and timestamp in "
"compress/decompress\n"
" -t, --test test compressed file integrity\n"
" -T, --threads <n> use n threads to compress if enabled\n"
" -q, --quiet suppress warnings\n\n"
@ -303,12 +301,14 @@ int usage(int exit_code)
exit(exit_code);
}
void print_version(void)
void
print_version(void)
{
log_print(INFORM, "igzip command line interface %s\n", VERSION);
}
void *malloc_safe(size_t size)
void *
malloc_safe(size_t size)
{
void *ptr = NULL;
if (size == 0)
@ -323,7 +323,8 @@ void *malloc_safe(size_t size)
return ptr;
}
FILE *fopen_safe(const char *file_name, const char *mode)
FILE *
fopen_safe(const char *file_name, const char *mode)
{
FILE *file;
@ -363,31 +364,32 @@ FILE *fopen_safe(const char *file_name, const char *mode)
return file;
}
size_t fread_safe(void *buf, size_t word_size, size_t buf_size, FILE * in, char *file_name)
size_t
fread_safe(void *buf, size_t word_size, size_t buf_size, FILE *in, char *file_name)
{
size_t read_size;
read_size = fread(buf, word_size, buf_size, in);
if (ferror(in)) {
log_print(ERROR, "igzip: Error encountered while reading file %s\n",
file_name);
log_print(ERROR, "igzip: Error encountered while reading file %s\n", file_name);
exit(FILE_READ_ERROR);
}
return read_size;
}
size_t fwrite_safe(void *buf, size_t word_size, size_t buf_size, FILE * out, char *file_name)
size_t
fwrite_safe(void *buf, size_t word_size, size_t buf_size, FILE *out, char *file_name)
{
size_t write_size;
write_size = fwrite(buf, word_size, buf_size, out);
if (ferror(out)) {
log_print(ERROR, "igzip: Error encountered while writing to file %s\n",
file_name);
log_print(ERROR, "igzip: Error encountered while writing to file %s\n", file_name);
exit(FILE_WRITE_ERROR);
}
return write_size;
}
void open_in_file(FILE ** in, char *infile_name)
void
open_in_file(FILE **in, char *infile_name)
{
*in = NULL;
if (infile_name == NULL)
@ -396,7 +398,8 @@ void open_in_file(FILE ** in, char *infile_name)
*in = fopen_safe(infile_name, "rb");
}
void open_out_file(FILE ** out, char *outfile_name)
void
open_out_file(FILE **out, char *outfile_name)
{
*out = NULL;
if (global_options.use_stdout)
@ -416,12 +419,7 @@ void open_out_file(FILE ** out, char *outfile_name)
#define MAX_THREADS 8
#define MAX_JOBQUEUE 16 /* must be a power of 2 */
enum job_status {
JOB_UNALLOCATED = 0,
JOB_ALLOCATED,
JOB_SUCCESS,
JOB_FAIL
};
enum job_status { JOB_UNALLOCATED = 0, JOB_ALLOCATED, JOB_SUCCESS, JOB_FAIL };
struct thread_job {
uint8_t *next_in;
@ -446,24 +444,28 @@ struct thread_pool {
// Globals for thread pool
struct thread_pool pool;
static inline int pool_has_space(void)
static inline int
pool_has_space(void)
{
return ((pool.head + 1) % MAX_JOBQUEUE) != pool.tail;
}
static inline int pool_has_work(void)
static inline int
pool_has_work(void)
{
return (pool.queue != pool.head);
}
int pool_get_work(void)
int
pool_get_work(void)
{
assert(pool.queue != pool.head);
pool.queue = (pool.queue + 1) % MAX_JOBQUEUE;
return pool.queue;
}
int pool_put_work(struct isal_zstream *stream)
int
pool_put_work(struct isal_zstream *stream)
{
pthread_mutex_lock(&pool.mutex);
if (!pool_has_space() || pool.shutdown) {
@ -483,7 +485,8 @@ int pool_put_work(struct isal_zstream *stream)
return 0;
}
void *thread_worker(void *none)
void *
thread_worker(void *none)
{
struct isal_zstream wstream;
int check;
@ -519,8 +522,7 @@ void *thread_worker(void *none)
wstream.level_buf_size = level_size;
check = isal_deflate_stateless(&wstream);
log_print(VERBOSE, "Worker finished job %d, out=%d\n",
work_idx, wstream.total_out);
log_print(VERBOSE, "Worker finished job %d, out=%d\n", work_idx, wstream.total_out);
pool.job[work_idx].total_out = wstream.total_out;
pool.job[work_idx].status = JOB_SUCCESS + check; // complete or fail
@ -532,7 +534,8 @@ void *thread_worker(void *none)
pthread_exit(NULL);
}
int pool_create(void)
int
pool_create(void)
{
int i;
int nthreads = global_options.threads - 1;
@ -547,7 +550,8 @@ int pool_create(void)
return 0;
}
void pool_quit(void)
void
pool_quit(void)
{
int i;
pthread_mutex_lock(&pool.mutex);
@ -561,7 +565,8 @@ void pool_quit(void)
#endif // defined(HAVE_THREADS)
int compress_file(void)
int
compress_file(void)
{
FILE *in = NULL, *out = NULL;
unsigned char *inbuf = NULL, *outbuf = NULL, *level_buf = NULL;
@ -586,8 +591,7 @@ int compress_file(void)
suffix_len = default_suffixes_lens[0];
}
if (infile_name_len == stdin_file_name_len &&
infile_name != NULL &&
if (infile_name_len == stdin_file_name_len && infile_name != NULL &&
memcmp(infile_name, stdin_file_name, infile_name_len) == 0) {
infile_name = NULL;
infile_name_len = 0;
@ -605,9 +609,8 @@ int compress_file(void)
if (in == NULL)
goto compress_file_cleanup;
if (infile_name_len != 0 && infile_name_len == outfile_name_len
&& infile_name != NULL && outfile_name != NULL
&& strncmp(infile_name, outfile_name, infile_name_len) == 0) {
if (infile_name_len != 0 && infile_name_len == outfile_name_len && infile_name != NULL &&
outfile_name != NULL && strncmp(infile_name, outfile_name, infile_name_len) == 0) {
log_print(ERROR, "igzip: Error input and output file names must differ\n");
goto compress_file_cleanup;
}
@ -691,12 +694,13 @@ int compress_file(void)
if (pool.job[t].status > JOB_SUCCESS) {
success = 0;
log_print(ERROR,
"igzip: Error encountered while compressing file %s\n",
"igzip: Error encountered while "
"compressing file %s\n",
infile_name);
goto compress_file_cleanup;
}
fwrite_safe(pool.job[t].next_out, 1,
pool.job[t].total_out, out, outfile_name);
fwrite_safe(pool.job[t].next_out, 1, pool.job[t].total_out,
out, outfile_name);
pool.job[t].total_out = 0;
pool.job[t].status = 0;
@ -725,8 +729,8 @@ int compress_file(void)
stream.level_buf = level_buf;
stream.level_buf_size = level_size;
int check = isal_deflate_stateless(&stream);
log_print(VERBOSE, "Self finished job %d, out=%d\n",
work_idx, stream.total_out);
log_print(VERBOSE, "Self finished job %d, out=%d\n", work_idx,
stream.total_out);
pool.job[work_idx].total_out = stream.total_out;
pool.job[work_idx].status = JOB_SUCCESS + check; // complete or fail
}
@ -770,7 +774,7 @@ int compress_file(void)
success = 1;
compress_file_cleanup:
compress_file_cleanup:
if (out != NULL && out != stdout)
fclose(out);
@ -786,7 +790,8 @@ int compress_file(void)
return (success == 0);
}
int decompress_file(void)
int
decompress_file(void)
{
FILE *in = NULL, *out = NULL;
unsigned char *inbuf = NULL, *outbuf = NULL;
@ -807,8 +812,7 @@ int decompress_file(void)
uint32_t file_time;
// Allocate mem and setup to hold gzip header info
if (infile_name_len == stdin_file_name_len &&
infile_name != NULL &&
if (infile_name_len == stdin_file_name_len && infile_name != NULL &&
memcmp(infile_name, stdin_file_name, infile_name_len) == 0) {
infile_name = NULL;
infile_name_len = 0;
@ -826,9 +830,8 @@ int decompress_file(void)
}
outfile_name_len = infile_name_len - suffix_len;
if (infile_name_len >= suffix_len
&& memcmp(infile_name + outfile_name_len, suffix,
suffix_len) == 0)
if (infile_name_len >= suffix_len &&
memcmp(infile_name + outfile_name_len, suffix, suffix_len) == 0)
break;
suffix = NULL;
suffix_len = 0;
@ -845,9 +848,9 @@ int decompress_file(void)
outfile_type = implicit;
}
if (outfile_type != terminal) {
allocated_name = malloc_safe(outfile_name_len >=
MAX_FILEPATH_BUF ? outfile_name_len +
1 : MAX_FILEPATH_BUF);
allocated_name = malloc_safe(outfile_name_len >= MAX_FILEPATH_BUF
? outfile_name_len + 1
: MAX_FILEPATH_BUF);
outfile_name = allocated_name;
}
}
@ -886,17 +889,15 @@ int decompress_file(void)
if (outfile_type == implicit)
file_time = gz_hdr.time;
if (outfile_name != NULL && infile_name != NULL
&& (outfile_type == stripped
|| (outfile_type == implicit && outfile_name[0] == 0))) {
if (outfile_name != NULL && infile_name != NULL &&
(outfile_type == stripped || (outfile_type == implicit && outfile_name[0] == 0))) {
outfile_name_len = infile_name_len - suffix_len;
memcpy(outfile_name, infile_name, outfile_name_len);
outfile_name[outfile_name_len] = 0;
}
if (infile_name_len != 0 && infile_name_len == outfile_name_len
&& infile_name != NULL && outfile_name != NULL
&& strncmp(infile_name, outfile_name, infile_name_len) == 0) {
if (infile_name_len != 0 && infile_name_len == outfile_name_len && infile_name != NULL &&
outfile_name != NULL && strncmp(infile_name, outfile_name, infile_name_len) == 0) {
log_print(ERROR, "igzip: Error input and output file names must differ\n");
goto decompress_file_cleanup;
}
@ -911,8 +912,7 @@ int decompress_file(void)
do {
if (state.avail_in == 0) {
state.next_in = inbuf;
state.avail_in =
fread_safe(state.next_in, 1, inbuf_size, in, infile_name);
state.avail_in = fread_safe(state.next_in, 1, inbuf_size, in, infile_name);
}
state.next_out = outbuf;
@ -920,8 +920,7 @@ int decompress_file(void)
ret = isal_inflate(&state);
if (ret != ISAL_DECOMP_OK) {
log_print(ERROR,
"igzip: Error encountered while decompressing file %s\n",
log_print(ERROR, "igzip: Error encountered while decompressing file %s\n",
infile_name);
goto decompress_file_cleanup;
}
@ -961,21 +960,20 @@ int decompress_file(void)
if (ret != ISAL_DECOMP_OK) {
log_print(ERROR,
"igzip: Error while decompressing extra concatenated"
"gzip files on %s\n", infile_name);
"gzip files on %s\n",
infile_name);
goto decompress_file_cleanup;
}
if (out != NULL)
fwrite_safe(outbuf, 1, state.next_out - outbuf, out,
outfile_name);
fwrite_safe(outbuf, 1, state.next_out - outbuf, out, outfile_name);
} while (state.block_state != ISAL_BLOCK_FINISH
&& (!feof(in) || state.avail_out == 0));
} while (state.block_state != ISAL_BLOCK_FINISH &&
(!feof(in) || state.avail_out == 0));
if (!feof(in) && state.avail_in == 0) {
state.next_in = inbuf;
state.avail_in =
fread_safe(state.next_in, 1, inbuf_size, in, infile_name);
state.avail_in = fread_safe(state.next_in, 1, inbuf_size, in, infile_name);
}
}
@ -985,7 +983,7 @@ int decompress_file(void)
else
success = 1;
decompress_file_cleanup:
decompress_file_cleanup:
if (out != NULL && out != stdout) {
fclose(out);
if (success)
@ -1004,7 +1002,8 @@ int decompress_file(void)
return (success == 0);
}
int main(int argc, char *argv[])
int
main(int argc, char *argv[])
{
int c;
char optstring[] = "hcdz0123456789o:S:kfqVvNntT:";
@ -1014,34 +1013,32 @@ int main(int argc, char *argv[])
int bad_level = 0;
int bad_c = 0;
struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
{"stdout", no_argument, NULL, 'c'},
{"to-stdout", no_argument, NULL, 'c'},
{"compress", no_argument, NULL, 'z'},
{"decompress", no_argument, NULL, 'd'},
{"uncompress", no_argument, NULL, 'd'},
{"keep", no_argument, NULL, 'k'},
{"rm", no_argument, &long_only_flag, RM},
{"suffix", no_argument, NULL, 'S'},
{"fast", no_argument, NULL, '1'},
{"best", no_argument, NULL, '0' + ISAL_DEF_MAX_LEVEL},
{"force", no_argument, NULL, 'f'},
{"quiet", no_argument, NULL, 'q'},
{"version", no_argument, NULL, 'V'},
{"verbose", no_argument, NULL, 'v'},
{"no-name", no_argument, NULL, 'n'},
{"name", no_argument, NULL, 'N'},
{"test", no_argument, NULL, 't'},
{"threads", required_argument, NULL, 'T'},
struct option long_options[] = { { "help", no_argument, NULL, 'h' },
{ "stdout", no_argument, NULL, 'c' },
{ "to-stdout", no_argument, NULL, 'c' },
{ "compress", no_argument, NULL, 'z' },
{ "decompress", no_argument, NULL, 'd' },
{ "uncompress", no_argument, NULL, 'd' },
{ "keep", no_argument, NULL, 'k' },
{ "rm", no_argument, &long_only_flag, RM },
{ "suffix", no_argument, NULL, 'S' },
{ "fast", no_argument, NULL, '1' },
{ "best", no_argument, NULL, '0' + ISAL_DEF_MAX_LEVEL },
{ "force", no_argument, NULL, 'f' },
{ "quiet", no_argument, NULL, 'q' },
{ "version", no_argument, NULL, 'V' },
{ "verbose", no_argument, NULL, 'v' },
{ "no-name", no_argument, NULL, 'n' },
{ "name", no_argument, NULL, 'N' },
{ "test", no_argument, NULL, 't' },
{ "threads", required_argument, NULL, 'T' },
/* Possible future extensions
{"recursive, no_argument, NULL, 'r'},
{"list", no_argument, NULL, 'l'},
{"benchmark", optional_argument, NULL, 'b'},
{"benchmark_end", required_argument, NULL, 'e'},
*/
{0, 0, 0, 0}
};
{ 0, 0, 0, 0 } };
init_options(&global_options);