summaryrefslogtreecommitdiffstats
path: root/zupdate.cc
diff options
context:
space:
mode:
Diffstat (limited to 'zupdate.cc')
-rw-r--r--zupdate.cc55
1 files changed, 38 insertions, 17 deletions
diff --git a/zupdate.cc b/zupdate.cc
index a605f35..e87d9e1 100644
--- a/zupdate.cc
+++ b/zupdate.cc
@@ -1,5 +1,5 @@
-/* Zupdate - recompress bzip2, gzip, xz files to lzip format
- Copyright (C) 2013-2021 Antonio Diaz Diaz.
+/* Zupdate - recompress bzip2, gzip, xz, zstd files to lzip format
+ Copyright (C) 2013-2022 Antonio Diaz Diaz.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -33,7 +33,7 @@
#include <utime.h>
#include <sys/stat.h>
#include <sys/wait.h>
-#if defined(__MSVCRT__) || defined(__OS2__)
+#if defined __MSVCRT__ || defined __OS2__
#include <io.h>
#endif
@@ -51,7 +51,7 @@ namespace {
void show_help()
{
- std::printf( "zupdate recompresses files from bzip2, gzip, and xz formats to lzip\n"
+ std::printf( "zupdate recompresses files from bzip2, gzip, xz, and zstd formats to lzip\n"
"format. Each original is compared with the new file and then deleted.\n"
"Only regular files with standard file name extensions are recompressed,\n"
"other files are ignored. Compressed files are decompressed and then\n"
@@ -67,8 +67,8 @@ void show_help()
"to be safe and not cause any data loss. Therefore, existing lzip\n"
"compressed files are never overwritten nor deleted.\n"
"\nThe names of the original files must have one of the following extensions:\n"
- "'.bz2', '.gz', or '.xz', which are recompressed to '.lz';\n"
- "'.tbz', '.tbz2', '.tgz', or '.txz', which are recompressed to '.tlz'.\n"
+ "\n'.bz2', '.gz', '.xz', or '.zst', which are recompressed to '.lz'.\n"
+ "\n'.tbz', '.tbz2', '.tgz', '.txz', or '.tzst', which are recompressed to '.tlz'.\n"
"\nUsage: zupdate [options] [files]\n"
"\nExit status is 0 if all the compressed files were successfully recompressed\n"
"(if needed), compared, and deleted (if requested). Non-zero otherwise.\n"
@@ -88,7 +88,8 @@ void show_help()
" --bz2=<command> set compressor and options for bzip2 format\n"
" --gz=<command> set compressor and options for gzip format\n"
" --lz=<command> set compressor and options for lzip format\n"
- " --xz=<command> set compressor and options for xz format\n" );
+ " --xz=<command> set compressor and options for xz format\n"
+ " --zst=<command> set compressor and options for zstd format\n" );
show_help_addr();
}
@@ -125,17 +126,19 @@ void set_permissions( const char * const rname, const struct stat & in_stats )
t.modtime = in_stats.st_mtime;
if( utime( rname, &t ) != 0 ) warning = true;
if( warning && verbosity >= 2 )
- show_error( "Can't change output file attributes." );
+ show_file_error( rname, "Can't change output file attributes.", errno );
}
- // Returns 0 for success, -1 for file skipped, 1 for error.
+ // Return 0 if success, -1 if file skipped, 1 if error.
int zupdate_file( const std::string & name, const char * const lzip_name,
const std::vector< std::string > & lzip_args2,
const bool force, const bool keep_input_files,
const bool no_rcfile )
{
+ // bzip2, gzip, and lzip are the primary formats. xz and zstd are optional.
static int disable_xz = -1; // tri-state bool
+ static int disable_zst = -1; // tri-state bool
int format_index = -1;
std::string rname; // recompressed name
@@ -198,9 +201,24 @@ int zupdate_file( const std::string & name, const char * const lzip_name,
{
std::string command( compressor_name ); command += " -V > /dev/null 2>&1";
disable_xz = ( std::system( command.c_str() ) != 0 );
+ if( disable_xz && verbosity >= 2 )
+ std::fprintf( stderr, "%s: '%s' not found. Ignoring xz files.\n",
+ program_name, compressor_name );
}
if( disable_xz ) return 0; // ignore this file if no xz installed
}
+ else if( format_index == fmt_zst )
+ {
+ if( disable_zst < 0 )
+ {
+ std::string command( compressor_name ); command += " -V > /dev/null 2>&1";
+ disable_zst = ( std::system( command.c_str() ) != 0 );
+ if( disable_zst && verbosity >= 2 )
+ std::fprintf( stderr, "%s: '%s' not found. Ignoring zstd files.\n",
+ program_name, compressor_name );
+ }
+ if( disable_zst ) return 0; // ignore this file if no zstd installed
+ }
if( !lz_exists ) // recompress
{
@@ -307,7 +325,7 @@ int zupdate_file( const std::string & name, const char * const lzip_name,
int main( const int argc, const char * const argv[] )
{
- enum { bz2_opt = 256, gz_opt, lz_opt, xz_opt };
+ enum { bz2_opt = 256, gz_opt, lz_opt, xz_opt, zst_opt };
int recursive = 0; // 1 = '-r', 2 = '-R'
std::list< std::string > filenames;
std::vector< std::string > lzip_args2; // args to lzip, maybe empty
@@ -340,11 +358,12 @@ int main( const int argc, const char * const argv[] )
{ 'R', "dereference-recursive", Arg_parser::no },
{ 'v', "verbose", Arg_parser::no },
{ 'V', "version", Arg_parser::no },
- { bz2_opt, "bz2", Arg_parser::yes },
- { gz_opt, "gz", Arg_parser::yes },
- { lz_opt, "lz", Arg_parser::yes },
- { xz_opt, "xz", Arg_parser::yes },
- { 0 , 0, Arg_parser::no } };
+ { bz2_opt, "bz2", Arg_parser::yes },
+ { gz_opt, "gz", Arg_parser::yes },
+ { lz_opt, "lz", Arg_parser::yes },
+ { xz_opt, "xz", Arg_parser::yes },
+ { zst_opt, "zst", Arg_parser::yes },
+ { 0, 0, Arg_parser::no } };
const Arg_parser parser( argc, argv, options );
if( parser.error().size() ) // bad option
@@ -357,6 +376,7 @@ int main( const int argc, const char * const argv[] )
{
const int code = parser.code( argind );
if( !code ) break; // no more options
+ const char * const pn = parser.parsed_name( argind ).c_str();
const std::string & arg = parser.argument( argind );
switch( code )
{
@@ -367,7 +387,7 @@ int main( const int argc, const char * const argv[] )
case 'h': show_help(); return 0;
case 'k': keep_input_files = true; break;
case 'l': lzip_args2.push_back( "-v" ); break;
- case 'M': parse_format_list( arg ); break;
+ case 'M': parse_format_list( arg, pn ); break;
case 'N': no_rcfile = true; break;
case 'q': verbosity = -1; lzip_args2.push_back( "-q" ); break;
case 'r': recursive = 1; break;
@@ -378,11 +398,12 @@ int main( const int argc, const char * const argv[] )
case gz_opt: parse_compressor( arg, fmt_gz, 1 ); break;
case lz_opt: parse_compressor( arg, fmt_lz, 1 ); break;
case xz_opt: parse_compressor( arg, fmt_xz, 1 ); break;
+ case zst_opt: parse_compressor( arg, fmt_zst, 1 ); break;
default : internal_error( "uncaught option." );
}
} // end process options
-#if defined(__MSVCRT__) || defined(__OS2__)
+#if defined __MSVCRT__ || defined __OS2__
setmode( STDIN_FILENO, O_BINARY );
setmode( STDOUT_FILENO, O_BINARY );
#endif