summaryrefslogtreecommitdiff
path: root/src/DeviceAccess.cc
blob: 1273b782f7ef3e22d789a665a0f1f87a9646966a (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
// Copyright © 2012, 2015-17, 2019 Richard Kettlewell.
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
#include <config.h>
#include "DeviceAccess.h"
#include "Conf.h"
#include "Subprocess.h"
#include "Errors.h"
#include "Command.h"
#include "IO.h"
#include "Utils.h"

static bool devicesReady;
static std::vector<IO *> filesToClose;

static void runDeviceAccessHook(const std::vector<std::string> &cmd,
                                const std::string &name) {
  if(cmd.size() == 0)
    return;
  Subprocess sp("device-access", cmd);
  sp.setenv("RSBACKUP_HOOK", name);
  sp.setenv("RSBACKUP_ACT", globalCommand.act ? "true" : "false");
  std::string devices;
  for(auto &d: globalConfig.devices) {
    if(devices.size() != 0)
      devices += " ";
    devices += d.first;
  }
  sp.setenv("RSBACKUP_DEVICES", devices);
  sp.reporting(globalWarningMask & WARNING_VERBOSE, false);
  sp.runAndWait(Subprocess::THROW_ON_ERROR
                |Subprocess::THROW_ON_CRASH
                |Subprocess::THROW_ON_SIGPIPE);
}

void preDeviceAccess() {
  if(!devicesReady) {
    runDeviceAccessHook(globalConfig.preAccess, "pre-access-hook");
    devicesReady = true;
  }
}

void closeOnUnmount(IO *f) {
  filesToClose.push_back(f);
}

void postDeviceAccess() {
  if(devicesReady) {
    deleteAll(filesToClose);
    runDeviceAccessHook(globalConfig.postAccess, "post-access-hook");
    devicesReady = false;
  }
}