summaryrefslogtreecommitdiff
path: root/discmgr.c
blob: 11b32668c5525b714d9988c2bf8c88f2e0de6594 (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
137
138
139
140
141
/*
 * discmgr.c: BluRay disc / drive manager
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include <unistd.h>

#include <vdr/thread.h>
#include <vdr/tools.h>
#include <vdr/skins.h>

#include "discmgr.h"


static bool DeviceOk(const char *DevName)
{
  struct stat ds;

  if (stat(DevName, &ds) == 0) {
    if (S_ISBLK(ds.st_mode)) {
      if (access(DevName, R_OK) == 0)
        return true;
      else
        esyslog("ERROR: can't access device %s", DevName);
    }
    else
      esyslog("ERROR: %s is not a device", DevName);
  }
  else
    LOG_ERROR_STR(DevName);

  return false;
}

static bool PathOk(const char *DirName)
{
  struct stat ds;

  if (stat(DirName, &ds) == 0) {
    if (S_ISDIR(ds.st_mode)) {
      if (access(DirName, R_OK | X_OK) == 0)
        return true;
      else
        esyslog("ERROR: can't access mount point %s", DirName);
    }
    else
      esyslog("ERROR: mount point %s is not a directory", DirName);
  }
  else
    LOG_ERROR_STR(DirName);

  return false;
}

cDiscMgr::cDiscMgr()
{
  Device     = DEFAULT_DEVICE;
  Path       = DEFAULT_PATH;
  MountCmd   = DEFAULT_MOUNTER;
  UnMountCmd = DEFAULT_UNMOUNTER;
  EjectCmd   = DEFAULT_EJECT;
}

bool cDiscMgr::IsMounted()
{
  return PathOk(cString::sprintf("%s/BDMV", *Path));
}

void cDiscMgr::Mount(bool Retry)
{
  cString cmd = cString::sprintf("%s \"%s\" \"%s\"", *MountCmd, *Device, *Path);
  isyslog("executing '%s'", *cmd);
  SystemExec(cmd);

  if (Retry && !IsMounted()) {
    /* retry */

    cCondWait::SleepMs(3000);

    isyslog("executing '%s'", *cmd);
    SystemExec(cmd);
  }
}

void cDiscMgr::UnMount()
{
  cString cmd = cString::sprintf("%s \"%s\"", *UnMountCmd, *Device);
  isyslog("executing '%s'", *cmd);
  SystemExec(cmd);
}

void cDiscMgr::CloseTray()
{
  cString cmd = cString::sprintf("%s -t \"%s\"", *EjectCmd, *Device);
  isyslog("executing '%s'", *cmd);
  SystemExec(cmd);

  cCondWait::SleepMs(3000);
}

void cDiscMgr::Eject()
{
  cString cmd = cString::sprintf("%s \"%s\"", *EjectCmd, *Device);
  isyslog("executing '%s'", *cmd);
  SystemExec(cmd);
}

bool cDiscMgr::CheckDisc()
{
  if (!PathOk(Path)) {
    Skins.Message(mtError, tr("Mount point does not exist!"));
    return false;
  }

  if (!IsMounted()) {

    if (!DeviceOk(Device)) {
      Skins.Message(mtError, tr("Can't access device!"));
      return false;
    }

    Mount(false);

    if (!IsMounted()) {
      Skins.Message(mtWarning, tr("Failed to mount BluRay disc, retry..."));

      CloseTray();
      Mount();

      if (!PathOk(cString::sprintf("%s/BDMV/", *Path))) {
        Skins.Message(mtError, tr("Failed to mount BluRay disc!"));

        return false;
      }
    }
  }

  return true;
}