From 9c9435e6f539e6ac4f65fc406f792cfa34b0eea8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Jacke?= Date: Mon, 28 Oct 2024 02:24:23 +0100 Subject: [PATCH] fix fileAddDate with SIGUSR1 If fileAddDate = yes is set and if we send darkice the USR1 signal, it should write the data into the file with the up-to-date date, not override the initial one. My patch is based on the patch from neingeist. This fixes https://github.com/rafael2k/darkice/issues/141 --- darkice/trunk/src/DarkIce.cpp | 58 +++++----------------------------- darkice/trunk/src/FileSink.cpp | 44 +++++++++++++++++++++----- darkice/trunk/src/FileSink.h | 30 ++++++++++++++++-- 3 files changed, 71 insertions(+), 61 deletions(-) diff --git a/darkice/trunk/src/DarkIce.cpp b/darkice/trunk/src/DarkIce.cpp index 8313e7f..c701569 100644 --- a/darkice/trunk/src/DarkIce.cpp +++ b/darkice/trunk/src/DarkIce.cpp @@ -345,17 +345,9 @@ DarkIce :: configIceCast ( const Config & config, // check for and create the local dump file if needed if ( localDumpName != 0 ) { - if ( fileAddDate ) { - if (fileDateFormat == 0) { - localDumpName = Util::fileAddDate(localDumpName); - } - else { - localDumpName = Util::fileAddDate( localDumpName, - fileDateFormat ); - } - } + localDumpFile = new FileSink( stream, localDumpName, + fileAddDate, fileDateFormat ); - localDumpFile = new FileSink( stream, localDumpName); if ( !localDumpFile->exists() ) { if ( !localDumpFile->create() ) { reportEvent( 1, "can't create local dump file", @@ -363,9 +355,6 @@ DarkIce :: configIceCast ( const Config & config, localDumpFile = 0; } } - if ( fileAddDate ) { - delete[] localDumpName; - } } // streaming related stuff audioOuts[u].socket = new TcpSocket( server, port); @@ -575,17 +564,8 @@ DarkIce :: configIceCast2 ( const Config & config, // check for and create the local dump file if needed if ( localDumpName != 0 ) { - if ( fileAddDate ) { - if (fileDateFormat == 0) { - localDumpName = Util::fileAddDate(localDumpName); - } - else { - localDumpName = Util::fileAddDate( localDumpName, - fileDateFormat ); - } - } - - localDumpFile = new FileSink( stream, localDumpName); + localDumpFile = new FileSink( stream, localDumpName, + fileAddDate, fileDateFormat ); if ( !localDumpFile->exists() ) { if ( !localDumpFile->create() ) { reportEvent( 1, "can't create local dump file", @@ -593,9 +573,6 @@ DarkIce :: configIceCast2 ( const Config & config, localDumpFile = 0; } } - if ( fileAddDate ) { - delete[] localDumpName; - } } // streaming related stuff @@ -902,17 +879,9 @@ DarkIce :: configShoutCast ( const Config & config, // check for and create the local dump file if needed if ( localDumpName != 0 ) { - if ( fileAddDate ) { - if (fileDateFormat == 0) { - localDumpName = Util::fileAddDate(localDumpName); - } - else { - localDumpName = Util::fileAddDate( localDumpName, - fileDateFormat ); - } - } + localDumpFile = new FileSink( stream, localDumpName, + fileAddDate, fileDateFormat ); - localDumpFile = new FileSink( stream, localDumpName); if ( !localDumpFile->exists() ) { if ( !localDumpFile->create() ) { reportEvent( 1, "can't create local dump file", @@ -920,9 +889,6 @@ DarkIce :: configShoutCast ( const Config & config, localDumpFile = 0; } } - if ( fileAddDate ) { - delete[] localDumpName; - } } // streaming related stuff @@ -1074,17 +1040,9 @@ DarkIce :: configFileCast ( const Config & config ) // go on and create the things // the underlying file - if ( fileAddDate ) { - if (fileDateFormat == 0) { - targetFileName = Util::fileAddDate( targetFileName); - } - else { - targetFileName = Util::fileAddDate( targetFileName, - fileDateFormat ); - } - } + FileSink * targetFile = new FileSink( stream, targetFileName, + fileAddDate, fileDateFormat ); - FileSink * targetFile = new FileSink( stream, targetFileName); if ( !targetFile->exists() ) { if ( !targetFile->create() ) { throw Exception( __FILE__, __LINE__, diff --git a/darkice/trunk/src/FileSink.cpp b/darkice/trunk/src/FileSink.cpp index 0d44268..19063b3 100644 --- a/darkice/trunk/src/FileSink.cpp +++ b/darkice/trunk/src/FileSink.cpp @@ -119,11 +119,18 @@ static const char fileid[] = "$Id$"; *----------------------------------------------------------------------------*/ void FileSink :: init ( const char * configName, - const char * name ) + const char * name, + const bool nameAddDate, + const char * fileDateFormat ) { this->configName = Util::strDup(configName); fileName = Util::strDup(name); + addDate = nameAddDate; + if (fileDateFormat) { + this->fileDateFormat = Util::strDup(fileDateFormat); + } fileDescriptor = 0; + fileNameActual = 0; } @@ -138,6 +145,9 @@ FileSink :: strip ( void) } delete[] fileName; + delete[] fileNameActual; + if (fileDateFormat) + delete[] fileDateFormat; } @@ -149,7 +159,7 @@ FileSink :: FileSink ( const FileSink & fs ) { int fd; - init( fs.configName, fs.fileName); + init( fs.configName, fs.fileName, fs.addDate, fs.fileDateFormat); if ( (fd = fs.fileDescriptor ? dup( fs.fileDescriptor) : 0) == -1 ) { strip(); @@ -176,7 +186,7 @@ FileSink :: operator= ( const FileSink & fs ) /* then build up */ Sink::operator=( fs ); - init( fs.configName, fs.fileName); + init( fs.configName, fs.fileName, fs.addDate, fs.fileDateFormat); if ( (fd = fs.fileDescriptor ? dup( fs.fileDescriptor) : 0) == -1 ) { strip(); @@ -220,8 +230,26 @@ FileSink :: create ( void ) /* filemode default to 0666 */ const int filemode = (S_IRUSR|S_IWUSR|S_IWGRP|S_IRGRP|S_IROTH|S_IWOTH) ; /* create the file */ - if ( (fd = ::creat( fileName, filemode )) == -1 ) { - reportEvent( 3, "can't create file", fileName, errno); + if (fileNameActual != 0) { + delete[] fileNameActual; + } + + if ( addDate ) { + if (fileDateFormat == 0) { + fileNameActual = Util::fileAddDate(fileName); + } + else { + fileNameActual = Util::fileAddDate( fileName, + fileDateFormat ); + } + } + else { + fileNameActual = Util::strDup(fileName); + } + + + if ( (fd = ::creat( fileNameActual, filemode)) == -1 ) { + reportEvent( 3, "can't create file", fileNameActual, errno); return false; } @@ -240,7 +268,7 @@ FileSink :: open ( void ) return false; } - if ( (fileDescriptor = ::open( fileName, O_WRONLY | O_TRUNC, 0)) == -1 ) { + if ( (fileDescriptor = ::open( fileNameActual, O_WRONLY | O_TRUNC, 0)) == -1 ) { fileDescriptor = 0; return false; } @@ -352,8 +380,8 @@ FileSink :: cut ( void ) throw () try { std::string archiveFileName = getArchiveFileName(); - if (::rename(fileName, archiveFileName.c_str()) != 0) { - reportEvent(2, "couldn't move file", fileName, + if (::rename(fileNameActual, archiveFileName.c_str()) != 0) { + reportEvent(2, "couldn't move file", fileNameActual, "to", archiveFileName); } diff --git a/darkice/trunk/src/FileSink.h b/darkice/trunk/src/FileSink.h index c246de6..44c8f89 100644 --- a/darkice/trunk/src/FileSink.h +++ b/darkice/trunk/src/FileSink.h @@ -75,11 +75,15 @@ class FileSink : public Sink, public virtual Reporter * @param configName the name of the configuration related to * this file sink. something like "file-0" or "file-2". * @param name name of the file to be represented by the object. + * @param addDate add a date to the filename. + * @param fileDateFormat optional date format of the added date * @exception Exception */ void init ( const char * configName, - const char * name ) ; + const char * name, + const bool addDate, + const char * fileDateFormat ); /** * De-initialize the object. @@ -108,6 +112,22 @@ class FileSink : public Sink, public virtual Reporter int fileDescriptor; /** + * Actual filename f.e. when using fileAddDate. + */ + char * fileNameActual; + + /** + * Whether a date is added to the filename or not. + */ + bool addDate; + + /** + * Date format of the added date. + */ + char * fileDateFormat; + + /** + * Default constructor. Always throws an Exception. * * @exception Exception @@ -127,13 +147,17 @@ class FileSink : public Sink, public virtual Reporter * @param configName the name of the configuration related to * this file sink. something like "file-0" or "file-2". * @param name name of the file to be represented by the object. + * @param addDate add a date to the filename. + * @param fileDateFormat optional date format of the added date * @exception Exception */ inline FileSink( const char * configName, - const char * name ) + const char * name, + const bool addDate, + const char * fileDateFormat ) { - init( configName, name); + init( configName, name, addDate, fileDateFormat ); } /**