From 9454b41d5e1b9fb10743840b48942aa4f3bd6c81 Mon Sep 17 00:00:00 2001 From: Josh Coalson Date: Fri, 23 Jul 2004 05:11:52 +0000 Subject: [PATCH] consolidate chain error reporting in a single function, add extra explanation for common errors --- src/metaflac/operations.c | 10 ++-- src/metaflac/operations_shorthand_cuesheet.c | 2 +- src/metaflac/operations_shorthand_seektable.c | 2 +- src/metaflac/operations_shorthand_vorbiscomment.c | 2 +- src/metaflac/utils.c | 56 +++++++++++++++++++++++ src/metaflac/utils.h | 1 + 6 files changed, 65 insertions(+), 8 deletions(-) diff --git a/src/metaflac/operations.c b/src/metaflac/operations.c index 903e84b..89dc6fb 100644 --- a/src/metaflac/operations.c +++ b/src/metaflac/operations.c @@ -109,7 +109,7 @@ FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOpt die("out of memory allocating chain"); if(!FLAC__metadata_chain_read(chain, filename)) { - fprintf(stderr, "%s: ERROR: reading metadata, status = \"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename); return false; } @@ -147,7 +147,7 @@ FLAC__bool do_major_operation_on_file(const char *filename, const CommandLineOpt FLAC__metadata_chain_sort_padding(chain); ok = FLAC__metadata_chain_write(chain, options->use_padding, options->preserve_modtime); if(!ok) - fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename); } FLAC__metadata_chain_delete(chain); @@ -267,7 +267,7 @@ FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLi die("out of memory allocating chain"); if(!FLAC__metadata_chain_read(chain, filename)) { - fprintf(stderr, "%s: ERROR: reading metadata, status = \"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: reading metadata", filename); return false; } @@ -304,7 +304,7 @@ FLAC__bool do_shorthand_operations_on_file(const char *filename, const CommandLi FLAC__metadata_chain_sort_padding(chain); ok = FLAC__metadata_chain_write(chain, use_padding, options->preserve_modtime); if(!ok) - fprintf(stderr, "%s: ERROR: writing FLAC file, error = %s\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: writing FLAC file", filename); } FLAC__metadata_chain_delete(chain); @@ -478,7 +478,7 @@ FLAC__bool do_shorthand_operation__add_padding(const char *filename, FLAC__Metad padding->length = length; if(!FLAC__metadata_iterator_insert_block_after(iterator, padding)) { - fprintf(stderr, "%s: ERROR: adding new PADDING block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: adding new PADDING block to metadata", filename); FLAC__metadata_object_delete(padding); FLAC__metadata_iterator_delete(iterator); return false; diff --git a/src/metaflac/operations_shorthand_cuesheet.c b/src/metaflac/operations_shorthand_cuesheet.c index 730f66e..119f060 100644 --- a/src/metaflac/operations_shorthand_cuesheet.c +++ b/src/metaflac/operations_shorthand_cuesheet.c @@ -75,7 +75,7 @@ FLAC__bool do_shorthand_operation__cuesheet(const char *filename, FLAC__Metadata while(FLAC__metadata_iterator_next(iterator)) ; if(!FLAC__metadata_iterator_insert_block_after(iterator, cuesheet)) { - fprintf(stderr, "%s: ERROR: adding new CUESHEET block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: adding new CUESHEET block to metadata", filename); FLAC__metadata_object_delete(cuesheet); ok = false; } diff --git a/src/metaflac/operations_shorthand_seektable.c b/src/metaflac/operations_shorthand_seektable.c index 22a7d82..03d7c38 100644 --- a/src/metaflac/operations_shorthand_seektable.c +++ b/src/metaflac/operations_shorthand_seektable.c @@ -60,7 +60,7 @@ FLAC__bool do_shorthand_operation__add_seekpoints(const char *filename, FLAC__Me while(FLAC__metadata_iterator_prev(iterator)) ; if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) { - fprintf(stderr, "%s: ERROR: adding new SEEKTABLE block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: adding new SEEKTABLE block to metadata", filename); FLAC__metadata_object_delete(block); return false; } diff --git a/src/metaflac/operations_shorthand_vorbiscomment.c b/src/metaflac/operations_shorthand_vorbiscomment.c index 94ffddc..6f14681 100644 --- a/src/metaflac/operations_shorthand_vorbiscomment.c +++ b/src/metaflac/operations_shorthand_vorbiscomment.c @@ -56,7 +56,7 @@ FLAC__bool do_shorthand_operation__vorbis_comment(const char *filename, FLAC__bo while(FLAC__metadata_iterator_next(iterator)) ; if(!FLAC__metadata_iterator_insert_block_after(iterator, block)) { - fprintf(stderr, "%s: ERROR: adding new VORBIS_COMMENT block to metadata, status =\"%s\"\n", filename, FLAC__Metadata_ChainStatusString[FLAC__metadata_chain_status(chain)]); + print_error_with_chain_status(chain, "%s: ERROR: adding new VORBIS_COMMENT block to metadata", filename); return false; } /* iterator is left pointing to new block */ diff --git a/src/metaflac/utils.c b/src/metaflac/utils.c index 2b0fc58..9983d9b 100644 --- a/src/metaflac/utils.c +++ b/src/metaflac/utils.c @@ -20,6 +20,7 @@ #include "FLAC/assert.h" #include "share/utf8.h" #include +#include #include #include #include @@ -119,6 +120,61 @@ void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const } } +void print_error_with_chain_status(FLAC__Metadata_Chain *chain, const char *format, ...) +{ + const FLAC__Metadata_ChainStatus status = FLAC__metadata_chain_status(chain); + va_list args; + + FLAC__ASSERT(0 != format); + + va_start(args, format); + + (void) vfprintf(stderr, format, args); + + va_end(args); + + fprintf(stderr, ", status = \"%s\"\n", FLAC__Metadata_ChainStatusString[status]); + + if(status == FLAC__METADATA_CHAIN_STATUS_ERROR_OPENING_FILE) { + fprintf(stderr, "\n" + "The FLAC file could not be opened. Most likely the file does not exist\n" + "or is not readable.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_A_FLAC_FILE) { + fprintf(stderr, "\n" + "The file does not appear to be a FLAC file.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_NOT_WRITABLE) { + fprintf(stderr, "\n" + "The FLAC file does not have write permissions.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_BAD_METADATA) { + fprintf(stderr, "\n" + "The metadata to be writted does not conform to the FLAC metadata\n" + "specifications.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_READ_ERROR) { + fprintf(stderr, "\n" + "There was an error while reading the FLAC file.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_WRITE_ERROR) { + fprintf(stderr, "\n" + "There was an error while writing FLAC file; most probably the disk is\n" + "full.\n" + ); + } + else if(status == FLAC__METADATA_CHAIN_STATUS_UNLINK_ERROR) { + fprintf(stderr, "\n" + "There was an error removing the temporary FLAC file.\n" + ); + } +} + FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation) { static const char * const violations[] = { diff --git a/src/metaflac/utils.h b/src/metaflac/utils.h index c60aedd..aa233be 100644 --- a/src/metaflac/utils.h +++ b/src/metaflac/utils.h @@ -31,6 +31,7 @@ size_t local_fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); char *local_strdup(const char *source); void local_strcat(char **dest, const char *source); void hexdump(const char *filename, const FLAC__byte *buf, unsigned bytes, const char *indent); +void print_error_with_chain_status(FLAC__Metadata_Chain *chain, const char *format, ...); FLAC__bool parse_vorbis_comment_field(const char *field_ref, char **field, char **name, char **value, unsigned *length, const char **violation); -- 2.7.4