From 6ceefaf85f1866e15a90b961cdc589a4fc38b3ae Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 16 Sep 2001 08:57:58 +0200 Subject: [PATCH] Better error handling when writing configuration files --- HISTORY | 1 + config.c | 31 ++++++++++--------------------- config.h | 5 +++-- tools.c | 22 ++++++++++++++++++---- tools.h | 4 ++-- 5 files changed, 34 insertions(+), 29 deletions(-) diff --git a/HISTORY b/HISTORY index 5c464b2c..ff036fad 100644 --- a/HISTORY +++ b/HISTORY @@ -750,3 +750,4 @@ Video Disk Recorder Revision History - Changed obsolete macro VIDEO_WINDOW_CHROMAKEY to VID_TYPE_CHROMAKEY (thanks to Guido Fiala). - New version of the "Master-Timer" tool (thanks to Matthias Schniedermeyer). +- Better error handling when writing configuration files. diff --git a/config.c b/config.c index dee8eccb..15042ef0 100644 --- a/config.c +++ b/config.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: config.c 1.70 2001/09/14 14:35:30 kls Exp $ + * $Id: config.c 1.71 2001/09/16 08:57:58 kls Exp $ */ #include "config.h" @@ -122,24 +122,14 @@ bool cKeys::Load(const char *FileName) bool cKeys::Save(void) { - bool result = true; cSafeFile f(fileName); if (f.Open()) { - if (fprintf(f, "Code\t%c\nAddress\t%04X\n", code, address) > 0) { - for (tKey *k = keys; k->type != kNone; k++) { - if (fprintf(f, "%s\t%08X\n", k->name, k->code) <= 0) { - result = false; - break; - } - } - } - else - result = false; - f.Close(); + fprintf(f, "Code\t%c\nAddress\t%04X\n", code, address); + for (tKey *k = keys; k->type != kNone; k++) + fprintf(f, "%s\t%08X\n", k->name, k->code); + return f.Close(); } - else - result = false; - return result; + return false; } eKeys cKeys::Get(unsigned int Code) @@ -928,12 +918,11 @@ bool cSetup::Save(const char *FileName) fprintf(f, "MultiSpeedMode = %d\n", MultiSpeedMode); fprintf(f, "ShowReplayMode = %d\n", ShowReplayMode); fprintf(f, "CurrentChannel = %d\n", CurrentChannel); - f.Close(); - isyslog(LOG_INFO, "saved setup to %s", FileName); - return true; + if (f.Close()) { + isyslog(LOG_INFO, "saved setup to %s", FileName); + return true; + } } - else - LOG_ERROR_STR(FileName); } else esyslog(LOG_ERR, "attempt to save setup without file name"); diff --git a/config.h b/config.h index 2e039548..a4bfa5bf 100644 --- a/config.h +++ b/config.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: config.h 1.77 2001/09/14 14:35:32 kls Exp $ + * $Id: config.h 1.78 2001/09/15 15:38:40 kls Exp $ */ #ifndef __CONFIG_H @@ -226,7 +226,8 @@ public: } l = (T *)l->Next(); } - f.Close(); + if (!f.Close()) + result = false; } else result = false; diff --git a/tools.c b/tools.c index 0adf03e2..d6b65e55 100644 --- a/tools.c +++ b/tools.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.c 1.44 2001/09/14 14:35:37 kls Exp $ + * $Id: tools.c 1.45 2001/09/15 15:41:16 kls Exp $ */ #define _GNU_SOURCE @@ -554,13 +554,27 @@ bool cSafeFile::Open(void) return f != NULL; } -void cSafeFile::Close(void) +bool cSafeFile::Close(void) { + bool result = true; if (f) { - fclose(f); + if (ferror(f) != 0) { + LOG_ERROR_STR(tempName); + result = false; + } + if (fclose(f) < 0) { + LOG_ERROR_STR(tempName); + result = false; + } f = NULL; - rename(tempName, fileName); + if (result && rename(tempName, fileName) < 0) { + LOG_ERROR_STR(fileName); + result = false; + } } + else + result = false; + return result; } // --- cListObject ----------------------------------------------------------- diff --git a/tools.h b/tools.h index 6e945e18..188adb45 100644 --- a/tools.h +++ b/tools.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: tools.h 1.33 2001/09/14 14:35:40 kls Exp $ + * $Id: tools.h 1.34 2001/09/15 15:22:57 kls Exp $ */ #ifndef __TOOLS_H @@ -93,7 +93,7 @@ public: ~cSafeFile(); operator FILE* () { return f; } bool Open(void); - void Close(void); + bool Close(void); }; class cListObject {