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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
--- src/device/k3bdevicemanager.cpp.orig Wed Jan 21 11:20:10 2004
+++ src/device/k3bdevicemanager.cpp Fri Feb 13 21:03:38 2004
@@ -49,6 +49,12 @@
#include <sys/stat.h>
#include <sys/ioctl.h>
+#ifdef __FreeBSD__
+#include <osreldate.h>
+#include <ktempfile.h>
+#include <sys/param.h>
+#endif
+
#ifdef Q_OS_LINUX
@@ -196,6 +202,22 @@
{
m_foundDevices = 0;
+#ifdef __FreeBSD__
+// What k3b does here is the following: it takes a list of device point, which are good candidates for cd drives
+// and asks their properties. If they are indeed cd drives, they are added to a device list. This does not work
+// on FreeBSD (no corresponding ioctls). Here cdrecord is asked for a list of scsi device, which work as cd
+// drives. The device points of those devices are added to the device list.
+ bsd_scan_devices(false);
+ kdDebug() << "(K3bDeviceManager) analyze found device" << endl;
+ for ( QValueList<BSDDevice>::iterator it = m_devices.begin(); it != m_devices.end(); ++it )
+ {
+ if( addDevice( (*it).m_name ) )
+ {
+ kdDebug() << "(K3bDeviceManager) Device " << (*it).m_name << " added" << endl;
+ m_foundDevices++;
+ }
+ }
+#else
QFile info("/proc/sys/dev/cdrom/info");
QString line,devstring;
info.open(IO_ReadOnly);
@@ -286,6 +308,7 @@
// m_foundDevices++;
// }
+#endif
scanFstab();
return m_foundDevices;
@@ -427,6 +450,7 @@
bool K3bCdDevice::DeviceManager::testForCdrom(const QString& devicename)
{
bool ret = false;
+#ifndef __FreeBSD__
int cdromfd = K3bCdDevice::openDevice( devicename.ascii() );
if (cdromfd < 0) {
kdDebug() << "could not open device " << devicename << " (" << strerror(errno) << ")" << endl;
@@ -467,11 +491,51 @@
}
::close( cdromfd );
+#endif
return ret;
}
K3bDevice* K3bCdDevice::DeviceManager::addDevice( const QString& devicename )
{
+#ifdef __FreeBSD__
+// this piece of code extracts some device parameter, like scsi or ide device. The whole process
+// fails on FreeBSD. Here the device name is simply looked up in a list of devices found by a
+// call of cdrecord --scanbus.
+ K3bDevice* device = 0;
+ if (!m_lastRefresh.isValid())
+ bsd_scan_devices(false);
+ bool found = false;
+ for ( QValueList<BSDDevice>::iterator it = m_devices.begin(); it != m_devices.end(); ++it )
+ {
+ if( (*it).m_name == devicename )
+ {
+ device = new K3bDevice((*it).m_name.latin1());
+ device->m_bus = (*it).m_bus;
+ device->m_target = (*it).m_target;
+ device->m_lun = (*it).m_lun;
+ device->m_passDevice = (*it).m_passDevice;
+ device->m_vendor = (*it).m_vendor;
+ device->m_description = (*it).m_product;
+ device->m_version = (*it).m_revision;
+ found = true;
+ break;
+ }
+ }
+ if (!found)
+ {
+ // resolve all symlinks
+ QString resolved = resolveSymLink( devicename );
+ kdDebug() << "(K3bDeviceManager) " << devicename << " resolved to " << resolved << endl;
+ int bus, target, lun;
+ if( K3bDevice* oldDev = findDevice( bus, target, lun ) )
+ {
+ kdDebug() << "(K3bDeviceManager) dev already found" << endl;
+ oldDev->addDeviceNode( resolved );
+ }
+ return 0;
+ }
+
+#else
K3bDevice* device = 0;
// resolve all symlinks
@@ -503,6 +567,7 @@
device->m_target = target;
device->m_lun = lun;
}
+#endif
if( !device->init() ) {
kdDebug() << "Could not initialize device " << devicename << endl;
@@ -578,9 +643,20 @@
if( K3bDevice* dev = findDevice( resolveSymLink(md) ) )
{
+#ifndef __FreeBSD__
kdDebug() << "(K3bDeviceManager) found device for " << md << ": " << resolveSymLink(md) << endl;
if( dev->mountDevice().isEmpty() ) {
dev->setMountPoint( mountInfo->fs_file );
+#else
+ kdDebug() << "(K3bDeviceManager) found device for " << md << " (" << resolveSymLink(md) << "): " << mountInfo->fs_file << endl;
+// Several mount points for one device might exist. If more than one are found, the one with
+// user permission should have a higher priority.
+ struct stat filestat;
+ if( dev->mountDevice().isEmpty() || (mountInfo->fs_file && !stat(mountInfo->fs_file, &filestat) && filestat.st_uid == geteuid()))
+ {
+ kdDebug() << "(K3bDeviceManager) setting mount point for device " << md << ": " << mountInfo->fs_file << endl;
+ dev->setMountPoint( mountInfo->fs_file );
+#endif
dev->setMountDevice( md );
dev->m_supermount = supermount;
}
@@ -589,6 +665,8 @@
{
// compare bus, id, lun since the same device can for example be
// determined as /dev/srX or /dev/scdX
+#ifndef __FreeBSD__
+// On FreeBSD scsi parameters can not be extracted from a file handle
int bus = -1, id = -1, lun = -1;
if( determineBusIdLun( mountInfo->fs_spec, bus, id, lun ) ) {
if( K3bDevice* dev = findDevice( bus, id, lun ) ) {
@@ -599,6 +677,17 @@
}
}
}
+#else
+// Therefore they are looked up in the device list.
+ kdDebug() << "(K3bDeviceManager) device: " << mountInfo->fs_spec << endl;
+ if( K3bDevice* dev = findDevice( mountInfo->fs_spec ) ) {
+ kdDebug() << " found! " << endl;
+ if( dev->mountDevice().isEmpty() ) {
+ dev->setMountPoint( mountInfo->fs_file );
+ dev->setMountDevice( md );
+ }
+ }
+#endif
}
@@ -673,5 +762,126 @@
return QString::fromLatin1( resolved );
}
+
+#ifdef __FreeBSD__
+// Calls "camcontrol devlist" to get a list of all available cd devices.
+// The sudo flag indicates, that "camcontrol devlist" should be
+// called. This is tried, if bsd_scan_devices(false) fails.
+// The result of the scan ist stored in m_devices. If within 3
+// seconds after the last call to bsd_scan_devices this function
+// is called again, the last result is used (prevents to call
+// camcontrol several times within a short time).
+void K3bCdDevice::DeviceManager::bsd_scan_devices(bool sudo)
+{
+ QDateTime now = QDateTime::currentDateTime();
+ if (m_lastRefresh.isValid() && m_lastRefresh.secsTo(now) < 3)
+ return;
+ m_lastRefresh = now;
+ m_devices.clear();
+ kdDebug() << "(K3bDeviceManager) Scanning for devices: " << sudo << endl;
+ KTempFile tmpfile;
+ tmpfile.setAutoDelete(true);
+
+ // create call
+ QString call = "/bin/sh -c \"";
+ if (sudo)
+ call += "sudo ";
+ call += "camcontrol devlist > " + tmpfile.name() + " 2>&1 \"";
+ kdDebug() << "(K3bDeviceManager) Reading device list : " << call << endl;
+ if (system(call.latin1()))
+ {
+ if (!sudo)
+ {
+ m_lastRefresh = m_lastRefresh.addDays(-1);
+ bsd_scan_devices(true);
+ }
+ return;
+ }
+
+ // read tmp file line by line
+ QFile tmp (tmpfile.name());
+ if ( tmp.open( IO_ReadOnly ) )
+ {
+ QTextStream stream( &tmp );
+ while ( !stream.eof() )
+ {
+ QString str = stream.readLine();
+ if (!str.startsWith("<"))
+ continue;
+ str.remove(0,1);
+ int i = str.find(">");
+ if (i < 0)
+ continue;
+ QStringList details = QStringList::split(" ", str.left(i));
+ i = str.find("at scbus", i);
+ if (i < 0)
+ continue;
+ str.remove(0, i+8);
+ i = str.find(" ");
+ if (i < 0)
+ continue;
+ bool ok;
+ int bus = str.left(i).toInt(&ok);
+ if (!ok)
+ continue;
+ i = str.find("target ", i);
+ if (i < 0)
+ continue;
+ str.remove(0, i+7);
+ i = str.find(" ");
+ if (i < 0)
+ continue;
+ int target = str.left(i).toInt(&ok);
+ if (!ok)
+ continue;
+ i = str.find("lun ", i);
+ if (i < 0)
+ continue;
+ str.remove(0, i+4);
+ i = str.find(" ");
+ if (i < 0)
+ continue;
+ int lun = str.left(i).toInt(&ok);
+ if (!ok)
+ continue;
+ i = str.find("(", i);
+ if (i < 0)
+ continue;
+ str.remove(0, i+1);
+ i = str.find(",");
+ if (i < 0)
+ continue;
+ QString name = str.left(i);
+ str.remove(0, i+1);
+ i = str.find(")");
+ if (i < 0)
+ continue;
+ QString pass = name;
+ if (name.startsWith("pass"))
+ name = str.left(i);
+ else
+ pass = str.left(i);
+ QString vendor, product, revision;
+ if (details.count() > 0)
+ revision = details.last();
+ details.pop_back();
+ if (details.count() > 0)
+ product = details.last();
+ details.pop_back();
+ if (details.count() > 0)
+ vendor = details.join(" ");
+ if (!name.startsWith("cd"))
+ continue;
+ kdDebug() << "(K3bDeviceManager) Found device " << name << ", pass = " << pass << ", bus = " << bus << ", target = " << target << ", lun = " << lun << endl;
+ kdDebug() << "(K3bDeviceManager) vendor: " << vendor << ", product: " << product << ", revision: " << revision << ", target = " << target << ", lun = " << lun << endl;
+#if __FreeBSD_version >= 500100
+ m_devices.push_back(BSDDevice("/dev/" + name, "/dev/" + pass, vendor, product, revision, bus, target, lun));
+#else
+ m_devices.push_back(BSDDevice("/dev/" + name + "c", "/dev/" + pass, vendor, product, revision, bus, target, lun));
+#endif
+ }
+ }
+}
+#endif
#include "k3bdevicemanager.moc"
|