EmptyDriveProbe.cpp 4.04 KB
Newer Older
1
2
/*
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2003-2021 CERN
4
5
6
7
 * @license        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.
Daniele Kruse's avatar
Daniele Kruse committed
8
 *
9
10
11
12
 *                 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.
Daniele Kruse's avatar
Daniele Kruse committed
13
 *
14
15
16
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
Daniele Kruse's avatar
Daniele Kruse committed
17

18
#include "castor/tape/tapeserver/daemon/EmptyDriveProbe.hpp"
Daniele Kruse's avatar
Daniele Kruse committed
19
20
21
22

//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
23
castor::tape::tapeserver::daemon::EmptyDriveProbe::EmptyDriveProbe(
Victor Kotlyar's avatar
Victor Kotlyar committed
24
  cta::log::Logger &log,
25
  const cta::tape::daemon::TpconfigLine &driveConfig,
26
  System::virtualWrapper &sysWrapper):
Daniele Kruse's avatar
Daniele Kruse committed
27
28
  m_log(log),
  m_driveConfig(driveConfig),
29
  m_sysWrapper(sysWrapper) {
Daniele Kruse's avatar
Daniele Kruse committed
30
31
32
}

//------------------------------------------------------------------------------
33
// driveIsEmpty()
Daniele Kruse's avatar
Daniele Kruse committed
34
//------------------------------------------------------------------------------
35
bool castor::tape::tapeserver::daemon::EmptyDriveProbe::driveIsEmpty() throw() {
Daniele Kruse's avatar
Daniele Kruse committed
36
37
38
  std::string errorMessage;

  try {
39
    return exceptionThrowingDriveIsEmpty();
40
  } catch(cta::exception::Exception &ex) {
Daniele Kruse's avatar
Daniele Kruse committed
41
42
43
44
45
46
47
    errorMessage = ex.getMessage().str();
  } catch(std::exception &se) {
    errorMessage = se.what();
  } catch(...) {
    errorMessage = "Caught an unknown exception";
  }

48
  m_probeErrorMsg = std::string("EmptyDriveProbe: ") + errorMessage;
Daniele Kruse's avatar
Daniele Kruse committed
49
  // Reaching this point means the probe failed and an exception was thrown
Victor Kotlyar's avatar
Victor Kotlyar committed
50
  std::list<cta::log::Param> params = {
51
    cta::log::Param("tapeDrive", m_driveConfig.unitName),
Victor Kotlyar's avatar
Victor Kotlyar committed
52
53
    cta::log::Param("message", errorMessage)};
  m_log(cta::log::ERR, "Probe failed", params);
54
  return false;
Daniele Kruse's avatar
Daniele Kruse committed
55
56
}

57
58
59
60
61
62
63
//------------------------------------------------------------------------------
// getProbeErrorMsg()
//------------------------------------------------------------------------------
cta::optional<std::string> castor::tape::tapeserver::daemon::EmptyDriveProbe::getProbeErrorMsg(){
  return m_probeErrorMsg;
}

Daniele Kruse's avatar
Daniele Kruse committed
64
//------------------------------------------------------------------------------
65
// exceptionThrowingDriveIsEmpty
Daniele Kruse's avatar
Daniele Kruse committed
66
//------------------------------------------------------------------------------
67
68
bool castor::tape::tapeserver::daemon::EmptyDriveProbe::
  exceptionThrowingDriveIsEmpty() {
Victor Kotlyar's avatar
Victor Kotlyar committed
69
  std::list<cta::log::Param> params;
70
  params.push_back(cta::log::Param("tapeDrive", m_driveConfig.unitName));
Daniele Kruse's avatar
Daniele Kruse committed
71

72
  std::unique_ptr<drive::DriveInterface> drivePtr = createDrive();
Daniele Kruse's avatar
Daniele Kruse committed
73
74
75
  drive::DriveInterface &drive = *drivePtr.get();

  if(drive.hasTapeInPlace()) {
Victor Kotlyar's avatar
Victor Kotlyar committed
76
    m_log(cta::log::INFO, "Probe found tape drive with a tape inside", params);
77
    return false;
78
  } else {
Victor Kotlyar's avatar
Victor Kotlyar committed
79
    m_log(cta::log::INFO, "Probe found tape drive is empty", params);
80
    return true;
Daniele Kruse's avatar
Daniele Kruse committed
81
82
83
84
85
86
  }
}

//------------------------------------------------------------------------------
// createDrive
//------------------------------------------------------------------------------
87
std::unique_ptr<castor::tape::tapeserver::drive::DriveInterface>
88
  castor::tape::tapeserver::daemon::EmptyDriveProbe::createDrive() {
Daniele Kruse's avatar
Daniele Kruse committed
89
  SCSI::DeviceVector dv(m_sysWrapper);
90
  SCSI::DeviceInfo driveInfo = dv.findBySymlink(m_driveConfig.devFilename);
Daniele Kruse's avatar
Daniele Kruse committed
91
92
  
  // Instantiate the drive object
93
  std::unique_ptr<castor::tape::tapeserver::drive::DriveInterface>
Daniele Kruse's avatar
Daniele Kruse committed
94
95
96
    drive(drive::createDrive(driveInfo, m_sysWrapper));

  if(NULL == drive.get()) {
97
    cta::exception::Exception ex;
Daniele Kruse's avatar
Daniele Kruse committed
98
99
100
101
102
103
    ex.getMessage() << "Failed to instantiate drive object";
    throw ex;
  } 
    
  return drive;
}