From 305abd449f33d3885d2167c05f33d3144ee05d15 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Sun, 20 Jan 2002 16:47:09 +0100 Subject: [PATCH] Now using 'ftw()' instead of an external 'find' to collect recordings --- CONTRIBUTORS | 3 +++ HISTORY | 3 +++ recording.c | 58 ++++++++++++++++++++++++++++++++-------------------- recording.h | 3 +-- tools.c | 13 +++++++++++- tools.h | 3 ++- 6 files changed, 57 insertions(+), 26 deletions(-) diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 7a077b69..38084be7 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -175,3 +175,6 @@ Stephan Schreiber Lauri Pesonen for avoiding linking in 'libncurses' if compiling without DEBUG_OSD=1 and REMOTE=KBD + +Emil Naepflein + for his hint about the 'ftw()' library function diff --git a/HISTORY b/HISTORY index 7b71a7b7..2ae78f37 100644 --- a/HISTORY +++ b/HISTORY @@ -900,3 +900,6 @@ Video Disk Recorder Revision History subdirectories for the recordings. This can be controlled through the "RecordingDirs" parameter in the "Setup" menu. See "MANUAL/Replaying a Recording" for details. +- Now using a call to the 'ftw()' library function instead of an external 'find' + command to collect the recordings (thanks to Emil Naepflein for his hint about + this function). diff --git a/recording.c b/recording.c index 2ce0cf78..faabd4ca 100644 --- a/recording.c +++ b/recording.c @@ -4,12 +4,13 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: recording.c 1.43 2002/01/20 12:14:25 kls Exp $ + * $Id: recording.c 1.44 2002/01/20 16:47:09 kls Exp $ */ #include "recording.h" #include #include +#include #include #include #include @@ -31,8 +32,6 @@ #define SUMMARYFILESUFFIX "/summary.vdr" #define MARKSFILESUFFIX "/marks.vdr" -#define FINDCMD "find %s -follow -type d -name '%s' 2> /dev/null" - #define MINDISKSPACE 1024 // MB #define DELETEDLIFETIME 1 // hours after which a deleted recording will be actually removed @@ -473,35 +472,50 @@ bool cRecording::Delete(void) bool cRecording::Remove(void) { + // let's do a final safety check here: + if (!endswith(FileName(), DELEXT)) { + esyslog(LOG_ERR, "attempt to remove recording %s", FileName()); + return false; + } isyslog(LOG_INFO, "removing recording %s", FileName()); return RemoveVideoFile(FileName()); } // --- cRecordings ----------------------------------------------------------- +cRecordings *FilterRecordings = NULL; +const char *FilterSuffix = NULL; + +static int Filter(const char *Name, const struct stat *Stat, int Status) +{ + if (FilterRecordings && FilterSuffix) { + if (Status == FTW_D) { + if (endswith(Name, FilterSuffix)) { + cRecording *r = new cRecording(Name); + FilterRecordings->Add(r); + } + } + return 0; + } + return 1; +} + bool cRecordings::Load(bool Deleted) { + if (FilterRecordings) + return false; // because of the static Filter() function only _one_ call at a time is allowed! Clear(); - bool result = false; - char *cmd = NULL; - asprintf(&cmd, FINDCMD, VideoDirectory, Deleted ? "*" DELEXT : "*" RECEXT); - FILE *p = popen(cmd, "r"); - if (p) { - char *s; - while ((s = readline(p)) != NULL) { - cRecording *r = new cRecording(s); - if (r->name) - Add(r); - else - delete r; - } - pclose(p); - Sort(); - result = Count() > 0; + bool result = true; + FilterRecordings = this; + FilterSuffix = Deleted ? DELEXT : RECEXT; + if (ftw(VideoDirectory, Filter, 10) < 0) { + LOG_ERROR; + result = false; } - else - Interface->Error("Error while opening pipe!"); - delete cmd; + Sort(); + result = result && Count() > 0; + FilterRecordings = NULL; + FilterSuffix = NULL; return result; } diff --git a/recording.h b/recording.h index c098271a..15a65e06 100644 --- a/recording.h +++ b/recording.h @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: recording.h 1.19 2002/01/20 11:35:32 kls Exp $ + * $Id: recording.h 1.20 2002/01/20 16:05:24 kls Exp $ */ #ifndef __RECORDING_H @@ -29,7 +29,6 @@ public: }; class cRecording : public cListObject { - friend class cRecordings; private: int resume; char *titleBuffer; diff --git a/tools.c b/tools.c index 472c50d7..de21af50 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.50 2001/10/19 13:12:45 kls Exp $ + * $Id: tools.c 1.51 2002/01/20 15:43:35 kls Exp $ */ #include "tools.h" @@ -145,6 +145,17 @@ bool startswith(const char *s, const char *p) return true; } +bool endswith(const char *s, const char *p) +{ + const char *se = s + strlen(s) - 1; + const char *pe = p + strlen(p) - 1; + while (pe >= p) { + if (*pe-- != *se-- || (se < s && pe >= p)) + return false; + } + return true; +} + bool isempty(const char *s) { return !(s && *skipspace(s)); diff --git a/tools.h b/tools.h index 843cbf1d..933b797e 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.36 2001/09/30 10:20:59 kls Exp $ + * $Id: tools.h 1.37 2002/01/20 15:39:53 kls Exp $ */ #ifndef __TOOLS_H @@ -51,6 +51,7 @@ char *skipspace(const char *s); char *stripspace(char *s); char *compactspace(char *s); bool startswith(const char *s, const char *p); +bool endswith(const char *s, const char *p); bool isempty(const char *s); int time_ms(void); void delay_ms(int ms);