2007-12-08 17:42:33 -05:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
/// \file suffix.c
|
|
|
|
/// \brief Checks filename suffix and creates the destination filename
|
|
|
|
//
|
|
|
|
// Copyright (C) 2007 Lasse Collin
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// 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
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#include "private.h"
|
|
|
|
|
2009-02-13 10:29:02 -05:00
|
|
|
// For case-insensitive filename suffix on case-insensitive systems
|
|
|
|
#ifdef DOSLIKE
|
|
|
|
# define strcmp strcasecmp
|
|
|
|
#endif
|
|
|
|
|
2007-12-08 17:42:33 -05:00
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
static char *custom_suffix = NULL;
|
|
|
|
|
|
|
|
|
2008-10-02 15:51:46 -04:00
|
|
|
struct suffix_pair {
|
2007-12-08 17:42:33 -05:00
|
|
|
const char *compressed;
|
|
|
|
const char *uncompressed;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Checks if src_name has given compressed_suffix
|
|
|
|
///
|
|
|
|
/// \param suffix Filename suffix to look for
|
|
|
|
/// \param src_name Input filename
|
|
|
|
/// \param src_len strlen(src_name)
|
|
|
|
///
|
|
|
|
/// \return If src_name has the suffix, src_len - strlen(suffix) is
|
|
|
|
/// returned. It's always a positive integer. Otherwise zero
|
|
|
|
/// is returned.
|
|
|
|
static size_t
|
|
|
|
test_suffix(const char *suffix, const char *src_name, size_t src_len)
|
|
|
|
{
|
|
|
|
const size_t suffix_len = strlen(suffix);
|
|
|
|
|
|
|
|
// The filename must have at least one character in addition to
|
|
|
|
// the suffix. src_name may contain path to the filename, so we
|
|
|
|
// need to check for directory separator too.
|
|
|
|
if (src_len <= suffix_len || src_name[src_len - suffix_len - 1] == '/')
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (strcmp(suffix, src_name + src_len - suffix_len) == 0)
|
|
|
|
return src_len - suffix_len;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Removes the filename suffix of the compressed file
|
|
|
|
///
|
|
|
|
/// \return Name of the uncompressed file, or NULL if file has unknown
|
|
|
|
/// suffix.
|
|
|
|
static char *
|
2008-10-02 15:51:46 -04:00
|
|
|
uncompressed_name(const char *src_name, const size_t src_len)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-10-02 15:51:46 -04:00
|
|
|
static const struct suffix_pair suffixes[] = {
|
|
|
|
{ ".xz", "" },
|
|
|
|
{ ".txz", ".tar" }, // .txz abbreviation for .txt.gz is rare.
|
|
|
|
{ ".lzma", "" },
|
|
|
|
{ ".tlz", ".tar" },
|
|
|
|
// { ".gz", "" },
|
|
|
|
// { ".tgz", ".tar" },
|
|
|
|
};
|
|
|
|
|
2007-12-08 17:42:33 -05:00
|
|
|
const char *new_suffix = "";
|
|
|
|
size_t new_len = 0;
|
|
|
|
|
2008-10-03 00:06:48 -04:00
|
|
|
if (opt_format == FORMAT_RAW) {
|
|
|
|
// Don't check for known suffixes when --format=raw was used.
|
2008-11-19 13:46:52 -05:00
|
|
|
if (custom_suffix == NULL) {
|
|
|
|
message_error(_("%s: With --format=raw, "
|
2008-10-03 00:06:48 -04:00
|
|
|
"--suffix=.SUF is required unless "
|
|
|
|
"writing to stdout"), src_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (size_t i = 0; i < ARRAY_SIZE(suffixes); ++i) {
|
|
|
|
new_len = test_suffix(suffixes[i].compressed,
|
|
|
|
src_name, src_len);
|
|
|
|
if (new_len != 0) {
|
|
|
|
new_suffix = suffixes[i].uncompressed;
|
|
|
|
break;
|
|
|
|
}
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
if (new_len == 0 && custom_suffix != NULL)
|
|
|
|
new_len = test_suffix(custom_suffix, src_name, src_len);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
if (new_len == 0) {
|
2008-11-19 13:46:52 -05:00
|
|
|
message_warning(_("%s: Filename has an unknown suffix, "
|
2007-12-08 17:42:33 -05:00
|
|
|
"skipping"), src_name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
const size_t new_suffix_len = strlen(new_suffix);
|
2008-11-19 13:46:52 -05:00
|
|
|
char *dest_name = xmalloc(new_len + new_suffix_len + 1);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
memcpy(dest_name, src_name, new_len);
|
|
|
|
memcpy(dest_name + new_len, new_suffix, new_suffix_len);
|
|
|
|
dest_name[new_len + new_suffix_len] = '\0';
|
|
|
|
|
|
|
|
return dest_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Appends suffix to src_name
|
2008-10-02 15:51:46 -04:00
|
|
|
///
|
|
|
|
/// In contrast to uncompressed_name(), we check only suffixes that are valid
|
|
|
|
/// for the specified file format.
|
2007-12-08 17:42:33 -05:00
|
|
|
static char *
|
2008-10-02 15:51:46 -04:00
|
|
|
compressed_name(const char *src_name, const size_t src_len)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-10-02 15:51:46 -04:00
|
|
|
// The order of these must match the order in args.h.
|
|
|
|
static const struct suffix_pair all_suffixes[][3] = {
|
|
|
|
{
|
|
|
|
{ ".xz", "" },
|
|
|
|
{ ".txz", ".tar" },
|
|
|
|
{ NULL, NULL }
|
|
|
|
}, {
|
|
|
|
{ ".lzma", "" },
|
|
|
|
{ ".tlz", ".tar" },
|
|
|
|
{ NULL, NULL }
|
|
|
|
/*
|
|
|
|
}, {
|
|
|
|
{ ".gz", "" },
|
|
|
|
{ ".tgz", ".tar" },
|
|
|
|
{ NULL, NULL }
|
|
|
|
*/
|
|
|
|
}, {
|
|
|
|
// --format=raw requires specifying the suffix
|
|
|
|
// manually or using stdout.
|
|
|
|
{ NULL, NULL }
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// args.c ensures this.
|
|
|
|
assert(opt_format != FORMAT_AUTO);
|
|
|
|
|
|
|
|
const size_t format = opt_format - 1;
|
|
|
|
const struct suffix_pair *const suffixes = all_suffixes[format];
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
for (size_t i = 0; suffixes[i].compressed != NULL; ++i) {
|
|
|
|
if (test_suffix(suffixes[i].compressed, src_name, src_len)
|
|
|
|
!= 0) {
|
2008-11-19 13:46:52 -05:00
|
|
|
message_warning(_("%s: File already has `%s' "
|
2007-12-08 17:42:33 -05:00
|
|
|
"suffix, skipping"), src_name,
|
|
|
|
suffixes[i].compressed);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-10-02 15:51:46 -04:00
|
|
|
// TODO: Hmm, maybe it would be better to validate this in args.c,
|
|
|
|
// since the suffix handling when decoding is weird now.
|
2008-11-19 13:46:52 -05:00
|
|
|
if (opt_format == FORMAT_RAW && custom_suffix == NULL) {
|
|
|
|
message_error(_("%s: With --format=raw, "
|
2008-10-03 00:06:48 -04:00
|
|
|
"--suffix=.SUF is required unless "
|
|
|
|
"writing to stdout"), src_name);
|
2008-10-02 15:51:46 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
const char *suffix = custom_suffix != NULL
|
|
|
|
? custom_suffix : suffixes[0].compressed;
|
2007-12-08 17:42:33 -05:00
|
|
|
const size_t suffix_len = strlen(suffix);
|
|
|
|
|
2008-11-19 13:46:52 -05:00
|
|
|
char *dest_name = xmalloc(src_len + suffix_len + 1);
|
2007-12-08 17:42:33 -05:00
|
|
|
|
|
|
|
memcpy(dest_name, src_name, src_len);
|
|
|
|
memcpy(dest_name + src_len, suffix, suffix_len);
|
|
|
|
dest_name[src_len + suffix_len] = '\0';
|
|
|
|
|
|
|
|
return dest_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
extern char *
|
2008-11-19 13:46:52 -05:00
|
|
|
suffix_get_dest_name(const char *src_name)
|
2007-12-08 17:42:33 -05:00
|
|
|
{
|
2008-10-02 15:51:46 -04:00
|
|
|
assert(src_name != NULL);
|
|
|
|
|
|
|
|
// Length of the name is needed in all cases to locate the end of
|
|
|
|
// the string to compare the suffix, so calculate the length here.
|
|
|
|
const size_t src_len = strlen(src_name);
|
|
|
|
|
2007-12-08 17:42:33 -05:00
|
|
|
return opt_mode == MODE_COMPRESS
|
2008-10-02 15:51:46 -04:00
|
|
|
? compressed_name(src_name, src_len)
|
|
|
|
: uncompressed_name(src_name, src_len);
|
2007-12-08 17:42:33 -05:00
|
|
|
}
|
2008-11-19 13:46:52 -05:00
|
|
|
|
|
|
|
|
|
|
|
extern void
|
|
|
|
suffix_set(const char *suffix)
|
|
|
|
{
|
|
|
|
// Empty suffix and suffixes having a slash are rejected. Such
|
|
|
|
// suffixes would break things later.
|
|
|
|
if (suffix[0] == '\0' || strchr(suffix, '/') != NULL)
|
|
|
|
message_fatal(_("%s: Invalid filename suffix"), optarg);
|
|
|
|
|
|
|
|
// Replace the old custom_suffix (if any) with the new suffix.
|
|
|
|
free(custom_suffix);
|
|
|
|
custom_suffix = xstrdup(suffix);
|
|
|
|
return;
|
|
|
|
}
|