From 882691e32f4b2488aa23e0844cc6185cd89e3301 Mon Sep 17 00:00:00 2001 From: Klaus Schmidinger Date: Fri, 17 Feb 2012 14:02:13 +0100 Subject: [PATCH] cReadDir::Next() now skips directory entries "." and ".." --- HISTORY | 1 + PLUGINS/src/pictures/HISTORY | 4 ++ PLUGINS/src/pictures/entry.c | 14 ++--- PLUGINS/src/pictures/pictures.c | 4 +- recording.c | 58 +++++++++-------- themes.c | 60 +++++++++--------- tools.c | 108 ++++++++++++++++---------------- 7 files changed, 124 insertions(+), 125 deletions(-) diff --git a/HISTORY b/HISTORY index 6f818079..725450fd 100644 --- a/HISTORY +++ b/HISTORY @@ -6884,3 +6884,4 @@ Video Disk Recorder Revision History - The DVB device adapters/frontends are now probed by scanning the /dev/dvb directory instead of looping through adapter/frontend numbers. This allows for "holes" in the device numbering. +- cReadDir::Next() now skips directory entries "." and "..". diff --git a/PLUGINS/src/pictures/HISTORY b/PLUGINS/src/pictures/HISTORY index 7c2f89f8..5ea878fd 100644 --- a/PLUGINS/src/pictures/HISTORY +++ b/PLUGINS/src/pictures/HISTORY @@ -63,3 +63,7 @@ VDR Plugin 'pictures' Revision History 2012-01-08: - Added option -o to pic2mpg. + +2012-02-17: + +- cReadDir::Next() now skips directory entries "." and "..". diff --git a/PLUGINS/src/pictures/entry.c b/PLUGINS/src/pictures/entry.c index 427585e6..8c124b5d 100644 --- a/PLUGINS/src/pictures/entry.c +++ b/PLUGINS/src/pictures/entry.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: entry.c 1.3 2008/02/17 13:42:34 kls Exp $ + * $Id: entry.c 2.1 2012/02/17 14:00:28 kls Exp $ */ #include "entry.h" @@ -48,13 +48,11 @@ void cPictureEntry::Load(void) const if (d.Ok()) { struct dirent *e; while ((e = d.Next()) != NULL) { - if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - struct stat ds; - if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) { - if (!entries) - entries = new cList; - entries->Add(new cPictureEntry(e->d_name, this, S_ISDIR(ds.st_mode))); - } + struct stat ds; + if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) { + if (!entries) + entries = new cList; + entries->Add(new cPictureEntry(e->d_name, this, S_ISDIR(ds.st_mode))); } } if (entries) diff --git a/PLUGINS/src/pictures/pictures.c b/PLUGINS/src/pictures/pictures.c index 8a890b1c..77fb4216 100644 --- a/PLUGINS/src/pictures/pictures.c +++ b/PLUGINS/src/pictures/pictures.c @@ -3,7 +3,7 @@ * * See the README file for copyright information and how to reach the author. * - * $Id: pictures.c 2.3 2011/02/20 16:50:01 kls Exp $ + * $Id: pictures.c 2.4 2012/02/17 14:00:48 kls Exp $ */ #include @@ -11,7 +11,7 @@ #include "menu.h" #include "player.h" -static const char *VERSION = "0.1.0"; +static const char *VERSION = "0.1.1"; static const char *DESCRIPTION = trNOOP("A simple picture viewer"); static const char *MAINMENUENTRY = trNOOP("Pictures"); diff --git a/recording.c b/recording.c index 6f7d8850..ac1ff2c7 100644 --- a/recording.c +++ b/recording.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: recording.c 2.48 2012/02/16 11:53:13 kls Exp $ + * $Id: recording.c 2.49 2012/02/17 13:57:05 kls Exp $ */ #include "recording.h" @@ -1109,40 +1109,38 @@ void cRecordings::ScanVideoDir(const char *DirName, bool Foreground, int LinkLev cReadDir d(DirName); struct dirent *e; while ((Foreground || Running()) && (e = d.Next()) != NULL) { - if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - cString buffer = AddDirectory(DirName, e->d_name); - struct stat st; - if (lstat(buffer, &st) == 0) { - int Link = 0; - if (S_ISLNK(st.st_mode)) { - if (LinkLevel > MAX_LINK_LEVEL) { - isyslog("max link level exceeded - not scanning %s", *buffer); - continue; - } - Link = 1; - if (stat(buffer, &st) != 0) - continue; + cString buffer = AddDirectory(DirName, e->d_name); + struct stat st; + if (lstat(buffer, &st) == 0) { + int Link = 0; + if (S_ISLNK(st.st_mode)) { + if (LinkLevel > MAX_LINK_LEVEL) { + isyslog("max link level exceeded - not scanning %s", *buffer); + continue; } - if (S_ISDIR(st.st_mode)) { - if (endswith(buffer, deleted ? DELEXT : RECEXT)) { - cRecording *r = new cRecording(buffer); - if (r->Name()) { - r->NumFrames(); // initializes the numFrames member - Lock(); - Add(r); - ChangeState(); - Unlock(); - if (deleted) { - r->fileSizeMB = DirSizeMB(buffer); - r->deleted = time(NULL); - } + Link = 1; + if (stat(buffer, &st) != 0) + continue; + } + if (S_ISDIR(st.st_mode)) { + if (endswith(buffer, deleted ? DELEXT : RECEXT)) { + cRecording *r = new cRecording(buffer); + if (r->Name()) { + r->NumFrames(); // initializes the numFrames member + Lock(); + Add(r); + ChangeState(); + Unlock(); + if (deleted) { + r->fileSizeMB = DirSizeMB(buffer); + r->deleted = time(NULL); } - else - delete r; } else - ScanVideoDir(buffer, Foreground, LinkLevel + Link); + delete r; } + else + ScanVideoDir(buffer, Foreground, LinkLevel + Link); } } } diff --git a/themes.c b/themes.c index 034ec0b9..88c7005e 100644 --- a/themes.c +++ b/themes.c @@ -4,7 +4,7 @@ * See the main source file 'vdr.c' for copyright information and * how to reach the author. * - * $Id: themes.c 2.1 2011/02/25 14:45:18 kls Exp $ + * $Id: themes.c 2.2 2012/02/17 13:57:32 kls Exp $ */ #include "themes.h" @@ -243,37 +243,35 @@ bool cThemes::Load(const char *SkinName) cReadDir d(themesDirectory); struct dirent *e; while ((e = d.Next()) != NULL) { - if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - if (strstr(e->d_name, SkinName) == e->d_name && e->d_name[strlen(SkinName)] == '-') { - cString FileName = AddDirectory(themesDirectory, e->d_name); - cTheme Theme; - if (Theme.Load(*FileName, true)) { - if (char **NewBuffer = (char **)realloc(names, (numThemes + 1) * sizeof(char *))) { - names = NewBuffer; - names[numThemes] = strdup(Theme.Name()); - } - else { - esyslog("ERROR: out of memory"); - break; - } - if (char **NewBuffer = (char **)realloc(fileNames, (numThemes + 1) * sizeof(char *))) { - fileNames = NewBuffer; - fileNames[numThemes] = strdup(*FileName); - } - else { - esyslog("ERROR: out of memory"); - break; - } - if (char **NewBuffer = (char **)realloc(descriptions, (numThemes + 1) * sizeof(char *))) { - descriptions = NewBuffer; - descriptions[numThemes] = strdup(Theme.Description()); - } - else { - esyslog("ERROR: out of memory"); - break; - } - numThemes++; + if (strstr(e->d_name, SkinName) == e->d_name && e->d_name[strlen(SkinName)] == '-') { + cString FileName = AddDirectory(themesDirectory, e->d_name); + cTheme Theme; + if (Theme.Load(*FileName, true)) { + if (char **NewBuffer = (char **)realloc(names, (numThemes + 1) * sizeof(char *))) { + names = NewBuffer; + names[numThemes] = strdup(Theme.Name()); } + else { + esyslog("ERROR: out of memory"); + break; + } + if (char **NewBuffer = (char **)realloc(fileNames, (numThemes + 1) * sizeof(char *))) { + fileNames = NewBuffer; + fileNames[numThemes] = strdup(*FileName); + } + else { + esyslog("ERROR: out of memory"); + break; + } + if (char **NewBuffer = (char **)realloc(descriptions, (numThemes + 1) * sizeof(char *))) { + descriptions = NewBuffer; + descriptions[numThemes] = strdup(Theme.Description()); + } + else { + esyslog("ERROR: out of memory"); + break; + } + numThemes++; } } } diff --git a/tools.c b/tools.c index 778c4639..145af7af 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 2.20 2012/01/11 11:21:43 kls Exp $ + * $Id: tools.c 2.21 2012/02/17 13:58:49 kls Exp $ */ #include "tools.h" @@ -384,37 +384,35 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks) if (d.Ok()) { struct dirent *e; while ((e = d.Next()) != NULL) { - if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - cString buffer = AddDirectory(FileName, e->d_name); - if (FollowSymlinks) { - struct stat st2; - if (lstat(buffer, &st2) == 0) { - if (S_ISLNK(st2.st_mode)) { - int size = st2.st_size + 1; - char *l = MALLOC(char, size); - int n = readlink(buffer, l, size - 1); - if (n < 0) { - if (errno != EINVAL) - LOG_ERROR_STR(*buffer); - } - else { - l[n] = 0; - dsyslog("removing %s", l); - if (remove(l) < 0) - LOG_ERROR_STR(l); - } - free(l); + cString buffer = AddDirectory(FileName, e->d_name); + if (FollowSymlinks) { + struct stat st2; + if (lstat(buffer, &st2) == 0) { + if (S_ISLNK(st2.st_mode)) { + int size = st2.st_size + 1; + char *l = MALLOC(char, size); + int n = readlink(buffer, l, size - 1); + if (n < 0) { + if (errno != EINVAL) + LOG_ERROR_STR(*buffer); } - } - else if (errno != ENOENT) { - LOG_ERROR_STR(FileName); - return false; + else { + l[n] = 0; + dsyslog("removing %s", l); + if (remove(l) < 0) + LOG_ERROR_STR(l); + } + free(l); } } - dsyslog("removing %s", *buffer); - if (remove(buffer) < 0) - LOG_ERROR_STR(*buffer); + else if (errno != ENOENT) { + LOG_ERROR_STR(FileName); + return false; + } } + dsyslog("removing %s", *buffer); + if (remove(buffer) < 0) + LOG_ERROR_STR(*buffer); } } else { @@ -442,7 +440,7 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis) bool empty = true; struct dirent *e; while ((e = d.Next()) != NULL) { - if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..") && strcmp(e->d_name, "lost+found")) { + if (strcmp(e->d_name, "lost+found")) { cString buffer = AddDirectory(DirName, e->d_name); struct stat st; if (stat(buffer, &st) == 0) { @@ -480,24 +478,22 @@ int DirSizeMB(const char *DirName) int size = 0; struct dirent *e; while (size >= 0 && (e = d.Next()) != NULL) { - if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - cString buffer = AddDirectory(DirName, e->d_name); - struct stat st; - if (stat(buffer, &st) == 0) { - if (S_ISDIR(st.st_mode)) { - int n = DirSizeMB(buffer); - if (n >= 0) - size += n; - else - size = -1; - } + cString buffer = AddDirectory(DirName, e->d_name); + struct stat st; + if (stat(buffer, &st) == 0) { + if (S_ISDIR(st.st_mode)) { + int n = DirSizeMB(buffer); + if (n >= 0) + size += n; else - size += st.st_size / MEGABYTE(1); - } - else { - LOG_ERROR_STR(*buffer); - size = -1; + size = -1; } + else + size += st.st_size / MEGABYTE(1); + } + else { + LOG_ERROR_STR(*buffer); + size = -1; } } return size; @@ -1320,7 +1316,13 @@ cReadDir::~cReadDir() struct dirent *cReadDir::Next(void) { - return directory && readdir_r(directory, &u.d, &result) == 0 ? result : NULL; + if (directory) { + while (readdir_r(directory, &u.d, &result) == 0 && result) { + if (strcmp(result->d_name, ".") && strcmp(result->d_name, "..")) + return result; + } + } + return NULL; } // --- cStringList ----------------------------------------------------------- @@ -1362,16 +1364,14 @@ bool cFileNameList::Load(const char *Directory, bool DirsOnly) struct dirent *e; if (d.Ok()) { while ((e = d.Next()) != NULL) { - if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { - if (DirsOnly) { - struct stat ds; - if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) { - if (!S_ISDIR(ds.st_mode)) - continue; - } + if (DirsOnly) { + struct stat ds; + if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) { + if (!S_ISDIR(ds.st_mode)) + continue; } - Append(strdup(e->d_name)); } + Append(strdup(e->d_name)); } Sort(); return true;