]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/tools/ScsiDeviceList.h
Restore win32 dir from Branch-5.2 and update it
[bacula/bacula] / bacula / src / win32 / tools / ScsiDeviceList.h
1 /*
2  * ScsiDeviceList.cpp - Class which provides information on installed devices.
3  *
4  * Author: Robert Nelson, August, 2006 <robertn@the-nelsons.org>
5  *
6  * Version $Id$
7  *
8  * This file was contributed to the Bacula project by Robert Nelson.
9  *
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 
15  * or object form.
16  *
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>.
20  */
21 /*
22    Bacula® - The Network Backup Solution
23
24    Copyright (C) 2006-2006 Free Software Foundation Europe e.V.
25
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 three of the GNU Affero General Public
30    License as published by the Free Software Foundation and included
31    in the file LICENSE.
32
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.
37
38    You should have received a copy of the GNU Affero 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
41    02110-1301, USA.
42
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.
47 */
48
49 #pragma once
50 #include "vector"
51
52 class CScsiDeviceListEntry
53 {
54    friend class CScsiDeviceList;
55
56    static LPCTSTR    c_DeviceTypes[];
57    static const int  c_MaxDevicePathLength = 16;      // :255:255:255:255
58
59 public:
60    enum DeviceType { Unknown, CDRom, Changer, Disk, Tape };
61
62    CScsiDeviceListEntry(void);
63    CScsiDeviceListEntry(const CScsiDeviceListEntry &other);
64    ~CScsiDeviceListEntry(void);
65
66    inline CScsiDeviceListEntry &operator =(const CScsiDeviceListEntry &other);
67
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();
73
74 private:
75    DeviceType  m_eDeviceType;
76    LPTSTR      m_lpszIdentifier;
77    LPTSTR      m_lpszDeviceName;
78    DWORD       m_dwDeviceId;
79    TCHAR       m_szDevicePath[c_MaxDevicePathLength + 1];
80 };
81
82 CScsiDeviceListEntry &
83 CScsiDeviceListEntry::operator =(const CScsiDeviceListEntry &other)
84 {
85    m_eDeviceType = other.m_eDeviceType;
86
87    if (m_lpszIdentifier != NULL)
88    {
89       free(m_lpszIdentifier);
90    }
91    m_lpszIdentifier = other.m_lpszIdentifier != NULL ? _tcsdup(other.m_lpszIdentifier) : NULL;
92
93    if (m_lpszDeviceName != NULL)
94    {
95       free(m_lpszDeviceName);
96    }
97    m_lpszDeviceName = other.m_lpszDeviceName != NULL ? _tcsdup(other.m_lpszDeviceName) : NULL;
98
99    m_dwDeviceId = other.m_dwDeviceId;
100    _tcscpy(m_szDevicePath, other.m_szDevicePath);
101
102    return *this;
103 }
104
105 LPCTSTR
106 CScsiDeviceListEntry::GetDevicePath()
107 {
108    if (m_szDevicePath[0] == _T('\0'))
109    {
110       _sntprintf( m_szDevicePath, c_MaxDevicePathLength, 
111                   _T("%d:%d:%d:%d"), 
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');
117    }
118
119    return m_szDevicePath;
120 }
121
122 class CScsiDeviceList :
123    public std::vector<CScsiDeviceListEntry>
124 {
125    static TCHAR         c_ScsiPath[];
126    static LPCTSTR       c_lpszFormatList[];
127
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;
131
132 // Logical Unit Id 255
133    static const int     c_MaxSubkeyLength = 19;
134
135 // Identifier = 28, DeviceName = 10+, Type = 23
136    static const int     c_MaxValueLength = 30;
137
138 // Adapter \\ Bus \\ Target \\ LUN
139    static const int     c_MaxKeyDepth = 4;
140
141 public:
142    inline   CScsiDeviceList(void);
143    inline   ~CScsiDeviceList(void);
144
145    bool     Populate();
146    void     PrintLastError(LPTSTR lpszName = NULL);
147
148 protected:
149    bool     ProcessKey(HKEY hKey, int iLevel, DWORD dwDeviceId);
150    bool     ProcessValues(HKEY hKey, DWORD dwDeviceId);
151
152 private:
153    TCHAR    m_szLastOperation[80 + 1]; // Max length "Enumerating subkeys of "
154    TCHAR    m_szLastKey[c_MaxKeyPathLength + 1];
155    DWORD    m_dwLastKeyLength;
156    LONG     m_lLastError;
157 };
158
159 CScsiDeviceList::CScsiDeviceList(void)
160 {
161    m_szLastOperation[0] = _T('\0');
162    m_szLastKey[0] = _T('\0');
163    m_dwLastKeyLength = 0;
164    m_lLastError = 0;
165 }
166
167 CScsiDeviceList::~CScsiDeviceList(void)
168 {
169 }