cReadDir::Next() now skips directory entries "." and ".."

This commit is contained in:
Klaus Schmidinger 2012-02-17 14:02:13 +01:00
parent f1eecdeb1c
commit 882691e32f
7 changed files with 124 additions and 125 deletions

View File

@ -6884,3 +6884,4 @@ Video Disk Recorder Revision History
- The DVB device adapters/frontends are now probed by scanning the /dev/dvb directory - 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 instead of looping through adapter/frontend numbers. This allows for "holes" in the
device numbering. device numbering.
- cReadDir::Next() now skips directory entries "." and "..".

View File

@ -63,3 +63,7 @@ VDR Plugin 'pictures' Revision History
2012-01-08: 2012-01-08:
- Added option -o to pic2mpg. - Added option -o to pic2mpg.
2012-02-17:
- cReadDir::Next() now skips directory entries "." and "..".

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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" #include "entry.h"
@ -48,13 +48,11 @@ void cPictureEntry::Load(void) const
if (d.Ok()) { if (d.Ok()) {
struct dirent *e; struct dirent *e;
while ((e = d.Next()) != NULL) { while ((e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { struct stat ds;
struct stat ds; if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) {
if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) { if (!entries)
if (!entries) entries = new cList<cPictureEntry>;
entries = new cList<cPictureEntry>; entries->Add(new cPictureEntry(e->d_name, this, S_ISDIR(ds.st_mode)));
entries->Add(new cPictureEntry(e->d_name, this, S_ISDIR(ds.st_mode)));
}
} }
} }
if (entries) if (entries)

View File

@ -3,7 +3,7 @@
* *
* See the README file for copyright information and how to reach the author. * 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 <getopt.h> #include <getopt.h>
@ -11,7 +11,7 @@
#include "menu.h" #include "menu.h"
#include "player.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 *DESCRIPTION = trNOOP("A simple picture viewer");
static const char *MAINMENUENTRY = trNOOP("Pictures"); static const char *MAINMENUENTRY = trNOOP("Pictures");

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "recording.h"
@ -1109,40 +1109,38 @@ void cRecordings::ScanVideoDir(const char *DirName, bool Foreground, int LinkLev
cReadDir d(DirName); cReadDir d(DirName);
struct dirent *e; struct dirent *e;
while ((Foreground || Running()) && (e = d.Next()) != NULL) { while ((Foreground || Running()) && (e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { cString buffer = AddDirectory(DirName, e->d_name);
cString buffer = AddDirectory(DirName, e->d_name); struct stat st;
struct stat st; if (lstat(buffer, &st) == 0) {
if (lstat(buffer, &st) == 0) { int Link = 0;
int Link = 0; if (S_ISLNK(st.st_mode)) {
if (S_ISLNK(st.st_mode)) { if (LinkLevel > MAX_LINK_LEVEL) {
if (LinkLevel > MAX_LINK_LEVEL) { isyslog("max link level exceeded - not scanning %s", *buffer);
isyslog("max link level exceeded - not scanning %s", *buffer); continue;
continue;
}
Link = 1;
if (stat(buffer, &st) != 0)
continue;
} }
if (S_ISDIR(st.st_mode)) { Link = 1;
if (endswith(buffer, deleted ? DELEXT : RECEXT)) { if (stat(buffer, &st) != 0)
cRecording *r = new cRecording(buffer); continue;
if (r->Name()) { }
r->NumFrames(); // initializes the numFrames member if (S_ISDIR(st.st_mode)) {
Lock(); if (endswith(buffer, deleted ? DELEXT : RECEXT)) {
Add(r); cRecording *r = new cRecording(buffer);
ChangeState(); if (r->Name()) {
Unlock(); r->NumFrames(); // initializes the numFrames member
if (deleted) { Lock();
r->fileSizeMB = DirSizeMB(buffer); Add(r);
r->deleted = time(NULL); ChangeState();
} Unlock();
if (deleted) {
r->fileSizeMB = DirSizeMB(buffer);
r->deleted = time(NULL);
} }
else
delete r;
} }
else else
ScanVideoDir(buffer, Foreground, LinkLevel + Link); delete r;
} }
else
ScanVideoDir(buffer, Foreground, LinkLevel + Link);
} }
} }
} }

View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "themes.h"
@ -243,37 +243,35 @@ bool cThemes::Load(const char *SkinName)
cReadDir d(themesDirectory); cReadDir d(themesDirectory);
struct dirent *e; struct dirent *e;
while ((e = d.Next()) != NULL) { 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)] == '-') {
if (strstr(e->d_name, SkinName) == e->d_name && e->d_name[strlen(SkinName)] == '-') { cString FileName = AddDirectory(themesDirectory, e->d_name);
cString FileName = AddDirectory(themesDirectory, e->d_name); cTheme Theme;
cTheme Theme; if (Theme.Load(*FileName, true)) {
if (Theme.Load(*FileName, true)) { if (char **NewBuffer = (char **)realloc(names, (numThemes + 1) * sizeof(char *))) {
if (char **NewBuffer = (char **)realloc(names, (numThemes + 1) * sizeof(char *))) { names = NewBuffer;
names = NewBuffer; names[numThemes] = strdup(Theme.Name());
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++;
} }
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++;
} }
} }
} }

108
tools.c
View File

@ -4,7 +4,7 @@
* See the main source file 'vdr.c' for copyright information and * See the main source file 'vdr.c' for copyright information and
* how to reach the author. * 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" #include "tools.h"
@ -384,37 +384,35 @@ bool RemoveFileOrDir(const char *FileName, bool FollowSymlinks)
if (d.Ok()) { if (d.Ok()) {
struct dirent *e; struct dirent *e;
while ((e = d.Next()) != NULL) { while ((e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { cString buffer = AddDirectory(FileName, e->d_name);
cString buffer = AddDirectory(FileName, e->d_name); if (FollowSymlinks) {
if (FollowSymlinks) { struct stat st2;
struct stat st2; if (lstat(buffer, &st2) == 0) {
if (lstat(buffer, &st2) == 0) { if (S_ISLNK(st2.st_mode)) {
if (S_ISLNK(st2.st_mode)) { int size = st2.st_size + 1;
int size = st2.st_size + 1; char *l = MALLOC(char, size);
char *l = MALLOC(char, size); int n = readlink(buffer, l, size - 1);
int n = readlink(buffer, l, size - 1); if (n < 0) {
if (n < 0) { if (errno != EINVAL)
if (errno != EINVAL) LOG_ERROR_STR(*buffer);
LOG_ERROR_STR(*buffer);
}
else {
l[n] = 0;
dsyslog("removing %s", l);
if (remove(l) < 0)
LOG_ERROR_STR(l);
}
free(l);
} }
} else {
else if (errno != ENOENT) { l[n] = 0;
LOG_ERROR_STR(FileName); dsyslog("removing %s", l);
return false; if (remove(l) < 0)
LOG_ERROR_STR(l);
}
free(l);
} }
} }
dsyslog("removing %s", *buffer); else if (errno != ENOENT) {
if (remove(buffer) < 0) LOG_ERROR_STR(FileName);
LOG_ERROR_STR(*buffer); return false;
}
} }
dsyslog("removing %s", *buffer);
if (remove(buffer) < 0)
LOG_ERROR_STR(*buffer);
} }
} }
else { else {
@ -442,7 +440,7 @@ bool RemoveEmptyDirectories(const char *DirName, bool RemoveThis)
bool empty = true; bool empty = true;
struct dirent *e; struct dirent *e;
while ((e = d.Next()) != NULL) { 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); cString buffer = AddDirectory(DirName, e->d_name);
struct stat st; struct stat st;
if (stat(buffer, &st) == 0) { if (stat(buffer, &st) == 0) {
@ -480,24 +478,22 @@ int DirSizeMB(const char *DirName)
int size = 0; int size = 0;
struct dirent *e; struct dirent *e;
while (size >= 0 && (e = d.Next()) != NULL) { while (size >= 0 && (e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { cString buffer = AddDirectory(DirName, e->d_name);
cString buffer = AddDirectory(DirName, e->d_name); struct stat st;
struct stat st; if (stat(buffer, &st) == 0) {
if (stat(buffer, &st) == 0) { if (S_ISDIR(st.st_mode)) {
if (S_ISDIR(st.st_mode)) { int n = DirSizeMB(buffer);
int n = DirSizeMB(buffer); if (n >= 0)
if (n >= 0) size += n;
size += n;
else
size = -1;
}
else else
size += st.st_size / MEGABYTE(1); size = -1;
}
else {
LOG_ERROR_STR(*buffer);
size = -1;
} }
else
size += st.st_size / MEGABYTE(1);
}
else {
LOG_ERROR_STR(*buffer);
size = -1;
} }
} }
return size; return size;
@ -1320,7 +1316,13 @@ cReadDir::~cReadDir()
struct dirent *cReadDir::Next(void) 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 ----------------------------------------------------------- // --- cStringList -----------------------------------------------------------
@ -1362,16 +1364,14 @@ bool cFileNameList::Load(const char *Directory, bool DirsOnly)
struct dirent *e; struct dirent *e;
if (d.Ok()) { if (d.Ok()) {
while ((e = d.Next()) != NULL) { while ((e = d.Next()) != NULL) {
if (strcmp(e->d_name, ".") && strcmp(e->d_name, "..")) { if (DirsOnly) {
if (DirsOnly) { struct stat ds;
struct stat ds; if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) {
if (stat(AddDirectory(Directory, e->d_name), &ds) == 0) { if (!S_ISDIR(ds.st_mode))
if (!S_ISDIR(ds.st_mode)) continue;
continue;
}
} }
Append(strdup(e->d_name));
} }
Append(strdup(e->d_name));
} }
Sort(); Sort();
return true; return true;