summaryrefslogtreecommitdiff
path: root/videodir.c
diff options
context:
space:
mode:
Diffstat (limited to 'videodir.c')
-rw-r--r--videodir.c268
1 files changed, 84 insertions, 184 deletions
diff --git a/videodir.c b/videodir.c
index 9ad31b6f..cd739ea1 100644
--- a/videodir.c
+++ b/videodir.c
@@ -1,10 +1,10 @@
/*
- * videodir.c: Functions to maintain a distributed video directory
+ * videodir.c: Functions to maintain the video directory
*
* See the main source file 'vdr.c' for copyright information and
* how to reach the author.
*
- * $Id: videodir.c 3.1 2013/08/23 12:28:06 kls Exp $
+ * $Id: videodir.c 3.2 2013/09/11 12:20:37 kls Exp $
*/
#include "videodir.h"
@@ -19,213 +19,129 @@
#include "recording.h"
#include "tools.h"
-//#define DEPRECATED_DISTRIBUTED_VIDEODIR // Code enclosed with this macro is deprecated and will be removed in a future version
-
+#ifdef DEPRECATED_VIDEODIR
const char *VideoDirectory = VIDEODIR;
-
-void SetVideoDirectory(const char *Directory)
-{
- VideoDirectory = strdup(Directory);
-}
-
-class cVideoDirectory {
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
-private:
- char *name, *stored, *adjusted;
- int length, number, digits;
#endif
-public:
- cVideoDirectory(void);
- ~cVideoDirectory();
- int FreeMB(int *UsedMB = NULL);
- const char *Name(void) { return
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- name ? name :
-#endif
- VideoDirectory; }
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- const char *Stored(void) { return stored; }
- int Length(void) { return length; }
- bool IsDistributed(void) { return name != NULL; }
- bool Next(void);
- void Store(void);
- const char *Adjust(const char *FileName);
-#endif
- };
+cString cVideoDirectory::name;
+cVideoDirectory *cVideoDirectory::current = NULL;
cVideoDirectory::cVideoDirectory(void)
{
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- length = strlen(VideoDirectory);
- name = (VideoDirectory[length - 1] == '0') ? strdup(VideoDirectory) : NULL;
- stored = adjusted = NULL;
- number = -1;
- digits = 0;
-#endif
+ delete current;
+ current = this;
}
cVideoDirectory::~cVideoDirectory()
{
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- free(name);
- free(stored);
- free(adjusted);
-#endif
+ current = NULL;
}
-int cVideoDirectory::FreeMB(int *UsedMB)
+cVideoDirectory *cVideoDirectory::Current(void)
{
- return FreeDiskSpaceMB(
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- name ? name :
-#endif
- VideoDirectory, UsedMB);
+ if (!current)
+ current = new cVideoDirectory;
+ return current;
}
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
-bool cVideoDirectory::Next(void)
+void cVideoDirectory::Destroy(void)
{
- if (name) {
- if (number < 0) {
- int l = length;
- while (l-- > 0 && isdigit(name[l]))
- ;
- l++;
- digits = length - l;
- int n = atoi(&name[l]);
- if (n == 0)
- number = n;
- else
- return false; // base video directory must end with zero
- }
- if (++number > 0) {
- char buf[16];
- if (sprintf(buf, "%0*d", digits, number) == digits) {
- strcpy(&name[length - digits], buf);
- return DirectoryOk(name);
- }
- }
- }
- return false;
+ delete current;
}
-void cVideoDirectory::Store(void)
+int cVideoDirectory::FreeMB(int *UsedMB)
{
- if (name) {
- free(stored);
- stored = strdup(name);
- }
+ return FreeDiskSpaceMB(Name(), UsedMB);
}
-const char *cVideoDirectory::Adjust(const char *FileName)
+const char *cVideoDirectory::Name(void)
{
- if (stored) {
- free(adjusted);
- adjusted = strdup(FileName);
- return strncpy(adjusted, stored, length);
- }
- return NULL;
+ return name;
}
-#endif
-cUnbufferedFile *OpenVideoFile(const char *FileName, int Flags)
+void cVideoDirectory::SetName(const char *Name)
{
- const char *ActualFileName = FileName;
+ name = Name;
+#ifdef DEPRECATED_VIDEODIR
+ VideoDirectory = Name;
+#endif
+}
+bool cVideoDirectory::Register(const char *FileName)
+{
// Incoming name must be in base video directory:
- if (strstr(FileName, VideoDirectory) != FileName) {
- esyslog("ERROR: %s not in %s", FileName, VideoDirectory);
+ if (strstr(FileName, Name()) != FileName) {
+ esyslog("ERROR: %s not in %s", FileName, Name());
errno = ENOENT; // must set 'errno' - any ideas for a better value?
- return NULL;
- }
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- // Are we going to create a new file?
- if ((Flags & O_CREAT) != 0) {
- cVideoDirectory Dir;
- if (Dir.IsDistributed()) {
- // Find the directory with the most free space:
- int MaxFree = Dir.FreeMB();
- while (Dir.Next()) {
- int Free = FreeDiskSpaceMB(Dir.Name());
- if (Free > MaxFree) {
- Dir.Store();
- MaxFree = Free;
- }
- }
- if (Dir.Stored()) {
- ActualFileName = Dir.Adjust(FileName);
- if (!MakeDirs(ActualFileName, false))
- return NULL; // errno has been set by MakeDirs()
- if (symlink(ActualFileName, FileName) < 0) {
- LOG_ERROR_STR(FileName);
- return NULL;
- }
- ActualFileName = strdup(ActualFileName); // must survive Dir!
- }
- }
+ return false;
}
-#endif
- cUnbufferedFile *File = cUnbufferedFile::Create(ActualFileName, Flags, DEFFILEMODE);
- if (ActualFileName != FileName)
- free((char *)ActualFileName);
- return File;
+ return true;
}
-int CloseVideoFile(cUnbufferedFile *File)
+bool cVideoDirectory::Rename(const char *OldName, const char *NewName)
{
- int Result = File->Close();
- delete File;
- return Result;
+ if (rename(OldName, NewName) == -1) {
+ LOG_ERROR_STR(NewName);
+ return false;
+ }
+ return true;
}
-bool RenameVideoFile(const char *OldName, const char *NewName)
+bool cVideoDirectory::Move(const char *FromName, const char *ToName)
{
- // Only the base video directory entry will be renamed, leaving the
- // possible symlinks untouched. Going through all the symlinks and disks
- // would be unnecessary work - maybe later...
- if (rename(OldName, NewName) == -1) {
- LOG_ERROR_STR(OldName);
+ if (rename(FromName, ToName) == -1) {
+ LOG_ERROR_STR(ToName);
return false;
}
return true;
}
-bool RemoveVideoFile(const char *FileName)
+bool cVideoDirectory::Remove(const char *Name)
{
- return RemoveFileOrDir(FileName, true);
+ return RemoveFileOrDir(Name);
}
-bool VideoFileSpaceAvailable(int SizeMB)
+void cVideoDirectory::Cleanup(const char *IgnoreFiles[])
{
- cVideoDirectory Dir;
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- if (Dir.IsDistributed()) {
- if (Dir.FreeMB() >= SizeMB * 2) // base directory needs additional space
- return true;
- while (Dir.Next()) {
- if (Dir.FreeMB() >= SizeMB)
- return true;
- }
- return false;
- }
-#endif
- return Dir.FreeMB() >= SizeMB;
+ RemoveEmptyDirectories(Name(), false, IgnoreFiles);
+}
+
+bool cVideoDirectory::Contains(const char *Name)
+{
+ return EntriesOnSameFileSystem(this->Name(), Name);
+}
+
+cUnbufferedFile *cVideoDirectory::OpenVideoFile(const char *FileName, int Flags)
+{
+ if (Current()->Register(FileName))
+ return cUnbufferedFile::Create(FileName, Flags, DEFFILEMODE);
+ return NULL;
+}
+
+bool cVideoDirectory::RenameVideoFile(const char *OldName, const char *NewName)
+{
+ return Current()->Rename(OldName, NewName);
}
-int VideoDiskSpace(int *FreeMB, int *UsedMB)
+bool cVideoDirectory::MoveVideoFile(const char *FromName, const char *ToName)
{
- int free = 0, used = 0;
+ return Current()->Move(FromName, ToName);
+}
+
+bool cVideoDirectory::RemoveVideoFile(const char *FileName)
+{
+ return Current()->Remove(FileName);
+}
+
+bool cVideoDirectory::VideoFileSpaceAvailable(int SizeMB)
+{
+ return Current()->FreeMB() >= SizeMB;
+}
+
+int cVideoDirectory::VideoDiskSpace(int *FreeMB, int *UsedMB)
+{
+ int used = 0;
+ int free = Current()->FreeMB(&used);
int deleted = DeletedRecordings.TotalFileSizeMB();
- cVideoDirectory Dir;
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- do {
-#endif
- int u;
- free += Dir.FreeMB(&u);
- used += u;
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- } while (Dir.Next());
-#endif
if (deleted > used)
deleted = used; // let's not get beyond 100%
free += deleted;
@@ -237,7 +153,7 @@ int VideoDiskSpace(int *FreeMB, int *UsedMB)
return (free + used) ? used * 100 / (free + used) : 0;
}
-cString PrefixVideoFileName(const char *FileName, char Prefix)
+cString cVideoDirectory::PrefixVideoFileName(const char *FileName, char Prefix)
{
char PrefixedName[strlen(FileName) + 2];
@@ -257,30 +173,14 @@ cString PrefixVideoFileName(const char *FileName, char Prefix)
return NULL;
}
-void RemoveEmptyVideoDirectories(const char *IgnoreFiles[])
+void cVideoDirectory::RemoveEmptyVideoDirectories(const char *IgnoreFiles[])
{
- cVideoDirectory Dir;
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- do {
-#endif
- RemoveEmptyDirectories(Dir.Name(), false, IgnoreFiles);
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- } while (Dir.Next());
-#endif
+ Current()->Cleanup(IgnoreFiles);
}
-bool IsOnVideoDirectoryFileSystem(const char *FileName)
+bool cVideoDirectory::IsOnVideoDirectoryFileSystem(const char *FileName)
{
- cVideoDirectory Dir;
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- do {
-#endif
- if (EntriesOnSameFileSystem(Dir.Name(), FileName))
- return true;
-#ifdef DEPRECATED_DISTRIBUTED_VIDEODIR
- } while (Dir.Next());
-#endif
- return false;
+ return Current()->Contains(FileName);
}
// --- cVideoDiskUsage -------------------------------------------------------
@@ -298,7 +198,7 @@ bool cVideoDiskUsage::HasChanged(int &State)
{
if (time(NULL) - lastChecked > DISKSPACECHEK) {
int FreeMB;
- int UsedPercent = VideoDiskSpace(&FreeMB);
+ int UsedPercent = cVideoDirectory::VideoDiskSpace(&FreeMB);
if (FreeMB != freeMB) {
usedPercent = UsedPercent;
freeMB = FreeMB;