2 * ScsiDeviceList.cpp - Class which provides information on installed devices.
4 * Author: Robert Nelson, August, 2006 <robertn@the-nelsons.org>
8 * This file was contributed to the Bacula project by Robert Nelson.
10 * Robert Nelson has been granted a perpetual, worldwide,
11 * non-exclusive, no-charge, royalty-free, irrevocable copyright
12 * license to reproduce, prepare derivative works of, publicly
13 * display, publicly perform, sublicense, and distribute the original
14 * work contributed by Robert Nelson to the Bacula project in source
17 * If you wish to license contributions from Robert Nelson
18 * under an alternate open source license please contact
19 * Robert Nelson <robertn@the-nelsons.org>.
22 Bacula® - The Network Backup Solution
24 Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
26 The main author of Bacula is Kern Sibbald, with contributions from
27 many others, a complete list can be found in the file AUTHORS.
28 This program is Free Software; you can redistribute it and/or
29 modify it under the terms of version two of the GNU General Public
30 License as published by the Free Software Foundation and included
33 This program is distributed in the hope that it will be useful, but
34 WITHOUT ANY WARRANTY; without even the implied warranty of
35 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
36 General Public License for more details.
38 You should have received a copy of the GNU General Public License
39 along with this program; if not, write to the Free Software
40 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
43 Bacula® is a registered trademark of Kern Sibbald.
44 The licensor of Bacula is the Free Software Foundation Europe
45 (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
46 Switzerland, email:ftf@fsfeurope.org.
52 class CScsiDeviceListEntry
54 friend class CScsiDeviceList;
56 static LPCTSTR c_DeviceTypes[];
57 static const int c_MaxDevicePathLength = 16; // :255:255:255:255
60 enum DeviceType { Unknown, CDRom, Changer, Disk, Tape };
62 CScsiDeviceListEntry(void);
63 CScsiDeviceListEntry(const CScsiDeviceListEntry &other);
64 ~CScsiDeviceListEntry(void);
66 inline CScsiDeviceListEntry &operator =(const CScsiDeviceListEntry &other);
68 inline DeviceType GetType() { return m_eDeviceType; }
69 inline LPCTSTR GetTypeName() { return c_DeviceTypes[m_eDeviceType]; }
70 inline LPCTSTR GetIdentifier() { return m_lpszIdentifier != NULL ? m_lpszIdentifier : _T(""); }
71 inline LPCTSTR GetDeviceName() { return m_lpszDeviceName != NULL ? m_lpszDeviceName : _T(""); }
72 inline LPCTSTR GetDevicePath();
75 DeviceType m_eDeviceType;
76 LPTSTR m_lpszIdentifier;
77 LPTSTR m_lpszDeviceName;
79 TCHAR m_szDevicePath[c_MaxDevicePathLength + 1];
82 CScsiDeviceListEntry &
83 CScsiDeviceListEntry::operator =(const CScsiDeviceListEntry &other)
85 m_eDeviceType = other.m_eDeviceType;
87 if (m_lpszIdentifier != NULL)
89 free(m_lpszIdentifier);
91 m_lpszIdentifier = other.m_lpszIdentifier != NULL ? _tcsdup(other.m_lpszIdentifier) : NULL;
93 if (m_lpszDeviceName != NULL)
95 free(m_lpszDeviceName);
97 m_lpszDeviceName = other.m_lpszDeviceName != NULL ? _tcsdup(other.m_lpszDeviceName) : NULL;
99 m_dwDeviceId = other.m_dwDeviceId;
100 _tcscpy(m_szDevicePath, other.m_szDevicePath);
106 CScsiDeviceListEntry::GetDevicePath()
108 if (m_szDevicePath[0] == _T('\0'))
110 _sntprintf( m_szDevicePath, c_MaxDevicePathLength,
112 (m_dwDeviceId >> 24) & 0xFF,
113 (m_dwDeviceId >> 16) & 0xFF,
114 (m_dwDeviceId >> 8) & 0xFF,
115 m_dwDeviceId & 0xFF);
116 m_szDevicePath[c_MaxDevicePathLength] = _T('\0');
119 return m_szDevicePath;
122 class CScsiDeviceList :
123 public std::vector<CScsiDeviceListEntry>
125 static TCHAR c_ScsiPath[];
126 static LPCTSTR c_lpszFormatList[];
128 // \\Scsi\\Scsi Port 255\\Scsi Bus 255\\Target Id 255\\Logical Unit Id 255
129 // 1 4 1 13 1 12 1 13 1 19 = 66
130 static const int c_MaxKeyPathLength = 66;
132 // Logical Unit Id 255
133 static const int c_MaxSubkeyLength = 19;
135 // Identifier = 28, DeviceName = 10+, Type = 23
136 static const int c_MaxValueLength = 30;
138 // Adapter \\ Bus \\ Target \\ LUN
139 static const int c_MaxKeyDepth = 4;
142 inline CScsiDeviceList(void);
143 inline ~CScsiDeviceList(void);
146 void PrintLastError(LPTSTR lpszName = NULL);
149 bool ProcessKey(HKEY hKey, int iLevel, DWORD dwDeviceId);
150 bool ProcessValues(HKEY hKey, DWORD dwDeviceId);
153 TCHAR m_szLastOperation[80 + 1]; // Max length "Enumerating subkeys of "
154 TCHAR m_szLastKey[c_MaxKeyPathLength + 1];
155 DWORD m_dwLastKeyLength;
159 CScsiDeviceList::CScsiDeviceList(void)
161 m_szLastOperation[0] = _T('\0');
162 m_szLastKey[0] = _T('\0');
163 m_dwLastKeyLength = 0;
167 CScsiDeviceList::~CScsiDeviceList(void)