summaryrefslogtreecommitdiff
path: root/archive.c
blob: c15aa38773deac2bdb5340a154cd84a5db16cc99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
 * archive.c: A plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 * $Id$
 */

#include <vdr/interface.h>
#include <vdr/videodir.h>
#include "archive.h"

// --- cArchive -----------------------------------------------------------

cString cArchive::archivePath = NULL;
char cArchive::buf[8] = "";

bool cArchive::Filecopy(const char *Sourcefile, const char *Destfile)
{
   FILE *source = fopen(Sourcefile, "r");
   FILE *dest = fopen(Destfile, "w");
   if (!source)
      return false;
   if (!dest) {
      fclose(source);
      return false;
   }
   size_t l1;
   size_t l2;
   unsigned char buffer[8192];
   while ((l1 = fread(buffer, 1, sizeof buffer, source)) > 0) {
      l2 = fwrite(buffer, 1, l1, dest);
      if (l2 < 0 || l2 < l1) {
         fclose(source);
         fclose(dest);
         return false;
      }
   }
   fclose(source);
   fclose(dest);
   return true;
}

const char *cArchive::GetArchiveId(const cRecording *Recording)
{
   buf[0] = 0;
   cString archiveFileName = cString::sprintf("%s%s", Recording->FileName(), "/hdd");
   FILE *f = fopen(archiveFileName, "r");
   if (f) {
      if (fgets(buf, sizeof(buf), f)) {
         char *p = strchr(buf, '\n');
         if (p)
            *p = 0;
      }
      fclose(f);
   }
   return buf;
}

bool cArchive::MountArchive(void)
{
   char *cmd = NULL;
   asprintf(&cmd, "mount %s", HddArchiveConfig.ArchiveMountpoint);
   if (SystemExec(cmd)) {
      free(cmd);
      return false;
   }
   free(cmd);
   return true;
}

bool cArchive::UnmountArchive(void)
{
   char *cmd = NULL;
   asprintf(&cmd, "umount %s", HddArchiveConfig.ArchiveMountpoint);
   if (SystemExec(cmd)) {
      free(cmd);
      return false;
   }
   free(cmd);
   return true;
}

bool cArchive::LinkArchive(const cRecording *Recording)
{
   const char *uniqueFolder = strrchr(Recording->FileName(), '/') + 1;
   if (!FindUniqueFolder(HddArchiveConfig.ArchiveMountpoint, uniqueFolder))
      return false;
   cString videoPath = Recording->FileName();
   cReadDir dir(archivePath);
   struct dirent *e;
   cString sourcefile;
   cString destfile;
   while (e = dir.Next()) {
      if (e->d_type == DT_REG) {
         sourcefile = AddDirectory(archivePath, e->d_name);
         destfile = AddDirectory(videoPath, e->d_name);
         symlink((const char*)sourcefile, (const char*)destfile);
      }
   }
   return true;
}

bool cArchive::UnlinkArchive(const cRecording *Recording)
{
   cString videoPath = Recording->FileName();
   cReadDir dir(videoPath);
   struct dirent *e;
   cString file;
   while (e = dir.Next()) {
      if (e->d_type == DT_LNK) {
         file = AddDirectory(videoPath, e->d_name);
         if (remove((const char*)file))
            return false;
      }
   }
   return true;
}

bool cArchive::FindUniqueFolder(const char *Parent, const char *Target)
{
   cReadDir dir(Parent);
   struct dirent *e;
   while (e = dir.Next()) {
      if (!e->d_type == DT_DIR || e->d_type == DT_LNK)
         continue;
      cString child = AddDirectory(Parent, e->d_name);
      if (!strcmp(e->d_name, Target)) {
         archivePath = child;
         return true;
      }
      else if (FindUniqueFolder(child, Target))
         return true;
      }
   return false;
}