diff options
Diffstat (limited to 'rbutil/rbutilqt/base/autodetection.cpp')
-rw-r--r-- | rbutil/rbutilqt/base/autodetection.cpp | 231 |
1 files changed, 3 insertions, 228 deletions
diff --git a/rbutil/rbutilqt/base/autodetection.cpp b/rbutil/rbutilqt/base/autodetection.cpp index 2bd61ff5ba..1a78ae54a8 100644 --- a/rbutil/rbutilqt/base/autodetection.cpp +++ b/rbutil/rbutilqt/base/autodetection.cpp | |||
@@ -25,32 +25,6 @@ | |||
25 | #include "../ipodpatcher/ipodpatcher.h" | 25 | #include "../ipodpatcher/ipodpatcher.h" |
26 | #include "../sansapatcher/sansapatcher.h" | 26 | #include "../sansapatcher/sansapatcher.h" |
27 | 27 | ||
28 | #if defined(Q_OS_LINUX) || defined(Q_OS_MACX) | ||
29 | #include <stdio.h> | ||
30 | #endif | ||
31 | #if defined(Q_OS_LINUX) | ||
32 | #include <mntent.h> | ||
33 | #endif | ||
34 | #if defined(Q_OS_MACX) | ||
35 | #include <sys/param.h> | ||
36 | #include <sys/ucred.h> | ||
37 | #include <sys/mount.h> | ||
38 | #endif | ||
39 | #if defined(Q_OS_WIN32) | ||
40 | #if defined(UNICODE) | ||
41 | #define _UNICODE | ||
42 | #endif | ||
43 | #include <stdio.h> | ||
44 | #include <tchar.h> | ||
45 | #include <windows.h> | ||
46 | #include <setupapi.h> | ||
47 | #include <winioctl.h> | ||
48 | #endif | ||
49 | |||
50 | #if defined(Q_OS_OPENBSD) | ||
51 | #include <sys/param.h> | ||
52 | #include <sys/mount.h> | ||
53 | #endif | ||
54 | 28 | ||
55 | #include "system.h" | 29 | #include "system.h" |
56 | #include "utils.h" | 30 | #include "utils.h" |
@@ -69,7 +43,7 @@ bool Autodetection::detect() | |||
69 | detectUsb(); | 43 | detectUsb(); |
70 | 44 | ||
71 | // Try detection via rockbox.info / rbutil.log | 45 | // Try detection via rockbox.info / rbutil.log |
72 | QStringList mounts = mountpoints(); | 46 | QStringList mounts = Utils::mountpoints(); |
73 | 47 | ||
74 | for(int i=0; i< mounts.size();i++) | 48 | for(int i=0; i< mounts.size();i++) |
75 | { | 49 | { |
@@ -183,7 +157,7 @@ bool Autodetection::detect() | |||
183 | #ifdef Q_OS_MACX | 157 | #ifdef Q_OS_MACX |
184 | mp.append("s2"); | 158 | mp.append("s2"); |
185 | #endif | 159 | #endif |
186 | m_mountpoint = resolveMountPoint(mp); | 160 | m_mountpoint = Utils::resolveMountPoint(mp); |
187 | return true; | 161 | return true; |
188 | } | 162 | } |
189 | else { | 163 | else { |
@@ -208,7 +182,7 @@ bool Autodetection::detect() | |||
208 | #ifdef Q_OS_MACX | 182 | #ifdef Q_OS_MACX |
209 | mp.append("s1"); | 183 | mp.append("s1"); |
210 | #endif | 184 | #endif |
211 | m_mountpoint = resolveMountPoint(mp); | 185 | m_mountpoint = Utils::resolveMountPoint(mp); |
212 | return true; | 186 | return true; |
213 | } | 187 | } |
214 | else { | 188 | else { |
@@ -224,205 +198,6 @@ bool Autodetection::detect() | |||
224 | } | 198 | } |
225 | 199 | ||
226 | 200 | ||
227 | QStringList Autodetection::mountpoints() | ||
228 | { | ||
229 | QStringList tempList; | ||
230 | #if defined(Q_OS_WIN32) | ||
231 | QFileInfoList list = QDir::drives(); | ||
232 | for(int i=0; i<list.size();i++) | ||
233 | { | ||
234 | tempList << list.at(i).absolutePath(); | ||
235 | qDebug() << "[Autodetection] Mounted on" << list.at(i).absolutePath(); | ||
236 | } | ||
237 | |||
238 | #elif defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) | ||
239 | int num; | ||
240 | struct statfs *mntinf; | ||
241 | |||
242 | num = getmntinfo(&mntinf, MNT_WAIT); | ||
243 | while(num--) { | ||
244 | tempList << QString(mntinf->f_mntonname); | ||
245 | qDebug() << "[Autodetection] Mounted on" << mntinf->f_mntonname | ||
246 | << "is" << mntinf->f_mntfromname << "type" << mntinf->f_fstypename; | ||
247 | mntinf++; | ||
248 | } | ||
249 | #elif defined(Q_OS_LINUX) | ||
250 | |||
251 | FILE *mn = setmntent("/etc/mtab", "r"); | ||
252 | if(!mn) | ||
253 | return QStringList(""); | ||
254 | |||
255 | struct mntent *ent; | ||
256 | while((ent = getmntent(mn))) { | ||
257 | tempList << QString(ent->mnt_dir); | ||
258 | qDebug() << "[Autodetection] Mounted on" << ent->mnt_dir | ||
259 | << "is" << ent->mnt_fsname << "type" << ent->mnt_type; | ||
260 | } | ||
261 | endmntent(mn); | ||
262 | |||
263 | #else | ||
264 | #error Unknown Platform | ||
265 | #endif | ||
266 | return tempList; | ||
267 | } | ||
268 | |||
269 | |||
270 | /** resolve device name to mount point / drive letter | ||
271 | * @param device device name / disk number | ||
272 | * @return mount point / drive letter | ||
273 | */ | ||
274 | QString Autodetection::resolveMountPoint(QString device) | ||
275 | { | ||
276 | qDebug() << "[Autodetect] resolving mountpoint:" << device; | ||
277 | |||
278 | #if defined(Q_OS_LINUX) | ||
279 | FILE *mn = setmntent("/etc/mtab", "r"); | ||
280 | if(!mn) | ||
281 | return QString(""); | ||
282 | |||
283 | struct mntent *ent; | ||
284 | while((ent = getmntent(mn))) { | ||
285 | // Check for valid filesystem. Allow hfs too, as an Ipod might be a | ||
286 | // MacPod. | ||
287 | if(QString(ent->mnt_fsname) == device) { | ||
288 | QString result; | ||
289 | if(QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive) | ||
290 | || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive)) { | ||
291 | qDebug() << "[Autodetect] resolved mountpoint is:" << ent->mnt_dir; | ||
292 | result = QString(ent->mnt_dir); | ||
293 | } | ||
294 | else { | ||
295 | qDebug() << "[Autodetect] mountpoint is wrong filesystem!"; | ||
296 | } | ||
297 | endmntent(mn); | ||
298 | return result; | ||
299 | } | ||
300 | } | ||
301 | endmntent(mn); | ||
302 | |||
303 | #endif | ||
304 | |||
305 | #if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) | ||
306 | int num; | ||
307 | struct statfs *mntinf; | ||
308 | |||
309 | num = getmntinfo(&mntinf, MNT_WAIT); | ||
310 | while(num--) { | ||
311 | // Check for valid filesystem. Allow hfs too, as an Ipod might be a | ||
312 | // MacPod. | ||
313 | if(QString(mntinf->f_mntfromname) == device) { | ||
314 | if(QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive) | ||
315 | || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive)) { | ||
316 | qDebug() << "[Autodetect] resolved mountpoint is:" << mntinf->f_mntonname; | ||
317 | return QString(mntinf->f_mntonname); | ||
318 | } | ||
319 | else { | ||
320 | qDebug() << "[Autodetect] mountpoint is wrong filesystem!"; | ||
321 | return QString(); | ||
322 | } | ||
323 | } | ||
324 | mntinf++; | ||
325 | } | ||
326 | #endif | ||
327 | |||
328 | #if defined(Q_OS_WIN32) | ||
329 | QString result; | ||
330 | unsigned int driveno = device.replace(QRegExp("^.*([0-9]+)"), "\\1").toInt(); | ||
331 | |||
332 | int letter; | ||
333 | for(letter = 'A'; letter <= 'Z'; letter++) { | ||
334 | if(resolveDevicename(QString(letter)).toUInt() == driveno) { | ||
335 | result = letter; | ||
336 | qDebug() << "[Autodetect] resolved mountpoint is:" << result; | ||
337 | break; | ||
338 | } | ||
339 | } | ||
340 | if(!result.isEmpty()) | ||
341 | return result + ":/"; | ||
342 | #endif | ||
343 | qDebug() << "[Autodetect] resolving mountpoint failed!"; | ||
344 | return QString(""); | ||
345 | } | ||
346 | |||
347 | |||
348 | /** Resolve mountpoint to devicename / disk number | ||
349 | * @param path mountpoint path / drive letter | ||
350 | * @return devicename / disk number | ||
351 | */ | ||
352 | QString Autodetection::resolveDevicename(QString path) | ||
353 | { | ||
354 | qDebug() << "[Autodetect] resolving device name" << path; | ||
355 | #if defined(Q_OS_LINUX) | ||
356 | FILE *mn = setmntent("/etc/mtab", "r"); | ||
357 | if(!mn) | ||
358 | return QString(""); | ||
359 | |||
360 | struct mntent *ent; | ||
361 | while((ent = getmntent(mn))) { | ||
362 | // check for valid filesystem type. | ||
363 | // Linux can handle hfs (and hfsplus), so consider it a valid file | ||
364 | // system. Otherwise resolving the device name would fail, which in | ||
365 | // turn would make it impossible to warn about a MacPod. | ||
366 | if(QString(ent->mnt_dir) == path | ||
367 | && (QString(ent->mnt_type).contains("vfat", Qt::CaseInsensitive) | ||
368 | || QString(ent->mnt_type).contains("hfs", Qt::CaseInsensitive))) { | ||
369 | endmntent(mn); | ||
370 | qDebug() << "[Autodetect] device name is" << ent->mnt_fsname; | ||
371 | return QString(ent->mnt_fsname); | ||
372 | } | ||
373 | } | ||
374 | endmntent(mn); | ||
375 | |||
376 | #endif | ||
377 | |||
378 | #if defined(Q_OS_MACX) || defined(Q_OS_OPENBSD) | ||
379 | int num; | ||
380 | struct statfs *mntinf; | ||
381 | |||
382 | num = getmntinfo(&mntinf, MNT_WAIT); | ||
383 | while(num--) { | ||
384 | // check for valid filesystem type. OS X can handle hfs (hfs+ is | ||
385 | // treated as hfs), BSD should be the same. | ||
386 | if(QString(mntinf->f_mntonname) == path | ||
387 | && (QString(mntinf->f_fstypename).contains("msdos", Qt::CaseInsensitive) | ||
388 | || QString(mntinf->f_fstypename).contains("hfs", Qt::CaseInsensitive))) { | ||
389 | qDebug() << "[Autodetect] device name is" << mntinf->f_mntfromname; | ||
390 | return QString(mntinf->f_mntfromname); | ||
391 | } | ||
392 | mntinf++; | ||
393 | } | ||
394 | #endif | ||
395 | |||
396 | #if defined(Q_OS_WIN32) | ||
397 | DWORD written; | ||
398 | HANDLE h; | ||
399 | TCHAR uncpath[MAX_PATH]; | ||
400 | UCHAR buffer[0x400]; | ||
401 | PVOLUME_DISK_EXTENTS extents = (PVOLUME_DISK_EXTENTS)buffer; | ||
402 | |||
403 | _stprintf(uncpath, _TEXT("\\\\.\\%c:"), path.toAscii().at(0)); | ||
404 | h = CreateFile(uncpath, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, | ||
405 | NULL, OPEN_EXISTING, 0, NULL); | ||
406 | if(h == INVALID_HANDLE_VALUE) { | ||
407 | //qDebug() << "error getting extents for" << uncpath; | ||
408 | return ""; | ||
409 | } | ||
410 | // get the extents | ||
411 | if(DeviceIoControl(h, IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS, | ||
412 | NULL, 0, extents, sizeof(buffer), &written, NULL)) { | ||
413 | if(extents->NumberOfDiskExtents > 1) { | ||
414 | qDebug() << "[Autodetect] resolving device name: volume spans multiple disks!"; | ||
415 | return ""; | ||
416 | } | ||
417 | qDebug() << "[Autodetect] device name is" << extents->Extents[0].DiskNumber; | ||
418 | return QString("%1").arg(extents->Extents[0].DiskNumber); | ||
419 | } | ||
420 | #endif | ||
421 | return QString(""); | ||
422 | |||
423 | } | ||
424 | |||
425 | |||
426 | /** @brief detect devices based on usb pid / vid. | 201 | /** @brief detect devices based on usb pid / vid. |
427 | * @return true upon success, false otherwise. | 202 | * @return true upon success, false otherwise. |
428 | */ | 203 | */ |