Hardware authentication for Linux using ordinary USB Flash Drives.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2137 lines
60 KiB

13 years ago
  1. /***************************************************************************
  2. * CVSID: $Id$
  3. *
  4. * libhal-storage.c : HAL convenience library for storage devices and volumes
  5. *
  6. * Copyright (C) 2004 Red Hat, Inc.
  7. *
  8. * Author: David Zeuthen <davidz@redhat.com>
  9. *
  10. * Licensed under the Academic Free License version 2.1
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 2 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  25. *
  26. **************************************************************************/
  27. #ifdef HAVE_CONFIG_H
  28. # include <config.h>
  29. #endif
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. #include <dbus/dbus.h>
  34. #include <libhal.h>
  35. #include "libhal-storage.h"
  36. #ifdef ENABLE_NLS
  37. # include <libintl.h>
  38. # define _(String) dgettext (GETTEXT_PACKAGE, String)
  39. # ifdef gettext_noop
  40. # define N_(String) gettext_noop (String)
  41. # else
  42. # define N_(String) (String)
  43. # endif
  44. #else
  45. /* Stubs that do something close enough. */
  46. # define textdomain(String) (String)
  47. # define gettext(String) (String)
  48. # define dgettext(Domain,Message) (Message)
  49. # define dcgettext(Domain,Message,Type) (Message)
  50. # define bindtextdomain(Domain,Directory) (Domain)
  51. # define _(String) (String)
  52. # define N_(String) (String)
  53. #endif
  54. typedef struct IconMappingEntry_s {
  55. LibHalStoragePolicyIcon icon;
  56. char *path;
  57. struct IconMappingEntry_s *next;
  58. } IconMappingEntry;
  59. struct LibHalStoragePolicy_s {
  60. IconMappingEntry *icon_mappings;
  61. };
  62. LibHalStoragePolicy *
  63. libhal_storage_policy_new (void)
  64. {
  65. LibHalStoragePolicy *p;
  66. p = malloc (sizeof (LibHalStoragePolicy));
  67. if (p == NULL)
  68. goto out;
  69. p->icon_mappings = NULL;
  70. out:
  71. return p;
  72. }
  73. void
  74. libhal_storage_policy_free (LibHalStoragePolicy *policy)
  75. {
  76. IconMappingEntry *i;
  77. IconMappingEntry *j;
  78. /* free all icon mappings */
  79. for (i = policy->icon_mappings; i != NULL; i = j) {
  80. j = i->next;
  81. free (i->path);
  82. free (i);
  83. }
  84. free (policy);
  85. }
  86. void
  87. libhal_storage_policy_set_icon_path (LibHalStoragePolicy *policy, LibHalStoragePolicyIcon icon, const char *path)
  88. {
  89. IconMappingEntry *i;
  90. /* see if it already exist */
  91. for (i = policy->icon_mappings; i != NULL; i = i->next) {
  92. if (i->icon == icon) {
  93. free (i->path);
  94. i->path = strdup (path);
  95. goto out;
  96. }
  97. }
  98. i = malloc (sizeof (IconMappingEntry));
  99. if (i == NULL)
  100. goto out;
  101. i->icon = icon;
  102. i->path = strdup (path);
  103. i->next = policy->icon_mappings;
  104. policy->icon_mappings = i;
  105. out:
  106. return;
  107. }
  108. void
  109. libhal_storage_policy_set_icon_mapping (LibHalStoragePolicy *policy, LibHalStoragePolicyIconPair *pairs)
  110. {
  111. LibHalStoragePolicyIconPair *i;
  112. for (i = pairs; i->icon != 0x00; i++) {
  113. libhal_storage_policy_set_icon_path (policy, i->icon, i->icon_path);
  114. }
  115. }
  116. const char *
  117. libhal_storage_policy_lookup_icon (LibHalStoragePolicy *policy, LibHalStoragePolicyIcon icon)
  118. {
  119. IconMappingEntry *i;
  120. const char *path;
  121. path = NULL;
  122. for (i = policy->icon_mappings; i != NULL; i = i->next) {
  123. if (i->icon == icon) {
  124. path = i->path;
  125. goto out;
  126. }
  127. }
  128. out:
  129. return path;
  130. }
  131. #define MAX_STRING_SZ 256
  132. char *
  133. libhal_volume_policy_compute_size_as_string (LibHalVolume *volume)
  134. {
  135. dbus_uint64_t size;
  136. char *result;
  137. char* sizes_str[] = {"K", "M", "G", "T", NULL};
  138. dbus_uint64_t cur = 1000L;
  139. dbus_uint64_t base = 10L;
  140. dbus_uint64_t step = 10L*10L*10L;
  141. int cur_str = 0;
  142. char buf[MAX_STRING_SZ];
  143. result = NULL;
  144. size = libhal_volume_get_size (volume);
  145. do {
  146. if (sizes_str[cur_str+1] == NULL || size < cur*step) {
  147. /* found the unit, display a comma number if result is a single digit */
  148. if (size < cur*base) {
  149. snprintf (buf, MAX_STRING_SZ, "%.01f%s",
  150. ((double)size)/((double)cur), sizes_str[cur_str]);
  151. result = strdup (buf);
  152. } else {
  153. snprintf (buf, MAX_STRING_SZ, "%llu%s", (long long unsigned int) size / cur, sizes_str[cur_str]);
  154. result = strdup (buf);
  155. }
  156. goto out;
  157. }
  158. cur *= step;
  159. cur_str++;
  160. } while (1);
  161. out:
  162. return result;
  163. }
  164. /* volume may be NULL (e.g. if drive supports removable media) */
  165. char *
  166. libhal_drive_policy_compute_display_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  167. {
  168. char *name;
  169. char *size_str;
  170. char *vendormodel_str;
  171. const char *model;
  172. const char *vendor;
  173. LibHalDriveType drive_type;
  174. dbus_bool_t drive_is_hotpluggable;
  175. dbus_bool_t drive_is_removable;
  176. LibHalDriveCdromCaps drive_cdrom_caps;
  177. char buf[MAX_STRING_SZ];
  178. model = libhal_drive_get_model (drive);
  179. vendor = libhal_drive_get_vendor (drive);
  180. drive_type = libhal_drive_get_type (drive);
  181. drive_is_hotpluggable = libhal_drive_is_hotpluggable (drive);
  182. drive_is_removable = libhal_drive_uses_removable_media (drive);
  183. drive_cdrom_caps = libhal_drive_get_cdrom_caps (drive);
  184. if (volume != NULL)
  185. size_str = libhal_volume_policy_compute_size_as_string (volume);
  186. else
  187. size_str = NULL;
  188. if (vendor == NULL || strlen (vendor) == 0) {
  189. if (model == NULL || strlen (model) == 0)
  190. vendormodel_str = strdup ("");
  191. else
  192. vendormodel_str = strdup (model);
  193. } else {
  194. if (model == NULL || strlen (model) == 0)
  195. vendormodel_str = strdup (vendor);
  196. else {
  197. snprintf (buf, MAX_STRING_SZ, "%s %s", vendor, model);
  198. vendormodel_str = strdup (buf);
  199. }
  200. }
  201. if (drive_type==LIBHAL_DRIVE_TYPE_CDROM) {
  202. /* Optical drive handling */
  203. char *first;
  204. char *second;
  205. first = "CD-ROM";
  206. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_CDR)
  207. first = "CD-R";
  208. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_CDRW)
  209. first = "CD-RW";
  210. second = "";
  211. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDROM)
  212. second = "/DVD-ROM";
  213. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR)
  214. second = "/DVD+R";
  215. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW)
  216. second = "/DVD+RW";
  217. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDR)
  218. second = "/DVD-R";
  219. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRW)
  220. second = "/DVD-RW";
  221. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRAM)
  222. second = "/DVD-RAM";
  223. if ((drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDR) &&
  224. (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR)) {
  225. if(drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL)
  226. second = "/DVD±R DL";
  227. else
  228. second = "/DVD±R";
  229. }
  230. if ((drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRW) &&
  231. (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW)) {
  232. if(drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL ||
  233. drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRWDL)
  234. second = "/DVD±RW DL";
  235. else
  236. second = "/DVD±RW";
  237. }
  238. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_BDROM)
  239. second = "/BD-ROM";
  240. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_BDR)
  241. second = "/BD-R";
  242. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_BDRE)
  243. second = "/BD-RE";
  244. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_HDDVDROM)
  245. second = "/HD DVD-ROM";
  246. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_HDDVDR)
  247. second = "/HD DVD-R";
  248. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_HDDVDRW)
  249. second = "/HD DVD-RW";
  250. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_MRW)
  251. second = "/MRW";
  252. if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_MRWW)
  253. second = "/MRW-W";
  254. if (drive_is_hotpluggable) {
  255. snprintf (buf, MAX_STRING_SZ, _("External %s%s Drive"), first, second);
  256. name = strdup (buf);
  257. } else {
  258. snprintf (buf, MAX_STRING_SZ, _("%s%s Drive"), first, second);
  259. name = strdup (buf);
  260. }
  261. } else if (drive_type==LIBHAL_DRIVE_TYPE_MO) {
  262. if (drive_is_hotpluggable)
  263. name = strdup (_("External Magneto Optical Drive"));
  264. else
  265. name = strdup (_("Magneto Optical Drive"));
  266. } else if (drive_type==LIBHAL_DRIVE_TYPE_FLOPPY) {
  267. /* Floppy Drive handling */
  268. if (drive_is_hotpluggable)
  269. name = strdup (_("External Floppy Drive"));
  270. else
  271. name = strdup (_("Floppy Drive"));
  272. } else if (drive_type==LIBHAL_DRIVE_TYPE_DISK && !drive_is_removable) {
  273. /* Harddisks */
  274. if (size_str != NULL) {
  275. if (drive_is_hotpluggable) {
  276. snprintf (buf, MAX_STRING_SZ, _("%s External Hard Drive"), size_str);
  277. name = strdup (buf);
  278. } else {
  279. snprintf (buf, MAX_STRING_SZ, _("%s Hard Drive"), size_str);
  280. name = strdup (buf);
  281. }
  282. } else {
  283. if (drive_is_hotpluggable)
  284. name = strdup (_("External Hard Drive"));
  285. else
  286. name = strdup (_("Hard Drive"));
  287. }
  288. } else {
  289. /* The rest - includes drives with removable Media */
  290. if (strlen (vendormodel_str) > 0)
  291. name = strdup (vendormodel_str);
  292. else
  293. name = strdup (_("Drive"));
  294. }
  295. free (vendormodel_str);
  296. free (size_str);
  297. return name;
  298. }
  299. char *
  300. libhal_volume_policy_compute_display_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  301. {
  302. char *name;
  303. char *size_str;
  304. const char *volume_label;
  305. LibHalDriveType drive_type;
  306. dbus_bool_t drive_is_removable;
  307. char buf[MAX_STRING_SZ];
  308. name = NULL;
  309. volume_label = libhal_volume_get_label (volume);
  310. drive_type = libhal_drive_get_type (drive);
  311. drive_is_removable = libhal_drive_uses_removable_media (drive);
  312. size_str = libhal_volume_policy_compute_size_as_string (volume);
  313. /* If the volume label is available use that
  314. *
  315. * TODO: If label is a fully-qualified UNIX path don't use that
  316. */
  317. if (volume_label != NULL) {
  318. name = strdup (volume_label);
  319. goto out;
  320. }
  321. /* Handle media in optical drives */
  322. if (drive_type==LIBHAL_DRIVE_TYPE_CDROM) {
  323. switch (libhal_volume_get_disc_type (volume)) {
  324. default:
  325. /* explict fallthrough */
  326. case LIBHAL_VOLUME_DISC_TYPE_CDROM:
  327. name = strdup (_("CD-ROM "));
  328. break;
  329. case LIBHAL_VOLUME_DISC_TYPE_CDR:
  330. if (libhal_volume_disc_is_blank (volume))
  331. name = strdup (_("Blank CD-R"));
  332. else
  333. name = strdup (_("CD-R"));
  334. break;
  335. case LIBHAL_VOLUME_DISC_TYPE_CDRW:
  336. if (libhal_volume_disc_is_blank (volume))
  337. name = strdup (_("Blank CD-RW"));
  338. else
  339. name = strdup (_("CD-RW"));
  340. break;
  341. case LIBHAL_VOLUME_DISC_TYPE_DVDROM:
  342. name = strdup (_("DVD-ROM"));
  343. break;
  344. case LIBHAL_VOLUME_DISC_TYPE_DVDRAM:
  345. if (libhal_volume_disc_is_blank (volume))
  346. name = strdup (_("Blank DVD-RAM"));
  347. else
  348. name = strdup (_("DVD-RAM"));
  349. break;
  350. case LIBHAL_VOLUME_DISC_TYPE_DVDR:
  351. if (libhal_volume_disc_is_blank (volume))
  352. name = strdup (_("Blank DVD-R"));
  353. else
  354. name = strdup (_("DVD-R"));
  355. break;
  356. case LIBHAL_VOLUME_DISC_TYPE_DVDR_DL:
  357. if (libhal_volume_disc_is_blank (volume))
  358. name = strdup (_("Blank DVD-R Dual-Layer"));
  359. else
  360. name = strdup (_("DVD-R Dual-Layer"));
  361. break;
  362. case LIBHAL_VOLUME_DISC_TYPE_DVDRW:
  363. if (libhal_volume_disc_is_blank (volume))
  364. name = strdup (_("Blank DVD-RW"));
  365. else
  366. name = strdup (_("DVD-RW"));
  367. break;
  368. case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR:
  369. if (libhal_volume_disc_is_blank (volume))
  370. name = strdup (_("Blank DVD+R"));
  371. else
  372. name = strdup (_("DVD+R"));
  373. break;
  374. case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSRW:
  375. if (libhal_volume_disc_is_blank (volume))
  376. name = strdup (_("Blank DVD+RW"));
  377. else
  378. name = strdup (_("DVD+RW"));
  379. break;
  380. case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR_DL:
  381. if (libhal_volume_disc_is_blank (volume))
  382. name = strdup (_("Blank DVD+R Dual-Layer"));
  383. else
  384. name = strdup (_("DVD+R Dual-Layer"));
  385. break;
  386. case LIBHAL_VOLUME_DISC_TYPE_BDROM:
  387. name = strdup (_("BD-ROM"));
  388. break;
  389. case LIBHAL_VOLUME_DISC_TYPE_BDR:
  390. if (libhal_volume_disc_is_blank (volume))
  391. name = strdup (_("Blank BD-R"));
  392. else
  393. name = strdup (_("BD-R"));
  394. break;
  395. case LIBHAL_VOLUME_DISC_TYPE_BDRE:
  396. if (libhal_volume_disc_is_blank (volume))
  397. name = strdup (_("Blank BD-RE"));
  398. else
  399. name = strdup (_("BD-RE"));
  400. break;
  401. case LIBHAL_VOLUME_DISC_TYPE_HDDVDROM:
  402. name = strdup (_("HD DVD-ROM"));
  403. break;
  404. case LIBHAL_VOLUME_DISC_TYPE_HDDVDR:
  405. if (libhal_volume_disc_is_blank (volume))
  406. name = strdup (_("Blank HD DVD-R"));
  407. else
  408. name = strdup (_("HD DVD-R"));
  409. break;
  410. case LIBHAL_VOLUME_DISC_TYPE_HDDVDRW:
  411. if (libhal_volume_disc_is_blank (volume))
  412. name = strdup (_("Blank HD DVD-RW"));
  413. else
  414. name = strdup (_("HD DVD-RW"));
  415. break;
  416. }
  417. /* Special case for pure audio disc */
  418. if (libhal_volume_disc_has_audio (volume) && !libhal_volume_disc_has_data (volume)) {
  419. free (name);
  420. name = strdup (_("Audio CD"));
  421. }
  422. goto out;
  423. } else if (drive_type==LIBHAL_DRIVE_TYPE_MO) {
  424. if (libhal_volume_get_disc_type (volume) == LIBHAL_VOLUME_DISC_TYPE_MO) {
  425. if (libhal_volume_disc_is_blank (volume))
  426. name = strdup (_("Blank Magneto Optical"));
  427. else
  428. name = strdup (_("Magneto Optical"));
  429. }
  430. goto out;
  431. }
  432. /* Fallback: size of media */
  433. if (drive_is_removable) {
  434. snprintf (buf, MAX_STRING_SZ, _("%s Removable Media"), size_str);
  435. name = strdup (buf);
  436. } else {
  437. snprintf (buf, MAX_STRING_SZ, _("%s Media"), size_str);
  438. name = strdup (buf);
  439. }
  440. /* Fallback: Use drive name */
  441. /*name = libhal_drive_policy_compute_display_name (drive, volume);*/
  442. out:
  443. free (size_str);
  444. return name;
  445. }
  446. char *
  447. libhal_drive_policy_compute_icon_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  448. {
  449. const char *name;
  450. LibHalDriveBus bus;
  451. LibHalDriveType drive_type;
  452. bus = libhal_drive_get_bus (drive);
  453. drive_type = libhal_drive_get_type (drive);
  454. /* by design, the enums are laid out so we can do easy computations */
  455. switch (drive_type) {
  456. case LIBHAL_DRIVE_TYPE_REMOVABLE_DISK:
  457. case LIBHAL_DRIVE_TYPE_DISK:
  458. case LIBHAL_DRIVE_TYPE_CDROM:
  459. case LIBHAL_DRIVE_TYPE_MO:
  460. case LIBHAL_DRIVE_TYPE_FLOPPY:
  461. name = libhal_storage_policy_lookup_icon (policy, 0x10000 + drive_type*0x100 + bus);
  462. break;
  463. default:
  464. name = libhal_storage_policy_lookup_icon (policy, 0x10000 + drive_type*0x100);
  465. }
  466. if (name != NULL)
  467. return strdup (name);
  468. else
  469. return NULL;
  470. }
  471. char *
  472. libhal_volume_policy_compute_icon_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  473. {
  474. const char *name;
  475. LibHalDriveBus bus;
  476. LibHalDriveType drive_type;
  477. LibHalVolumeDiscType disc_type;
  478. /* by design, the enums are laid out so we can do easy computations */
  479. if (libhal_volume_is_disc (volume)) {
  480. disc_type = libhal_volume_get_disc_type (volume);
  481. name = libhal_storage_policy_lookup_icon (policy, 0x30000 + disc_type);
  482. goto out;
  483. }
  484. if (drive == NULL) {
  485. name = libhal_storage_policy_lookup_icon (policy, LIBHAL_STORAGE_ICON_VOLUME_REMOVABLE_DISK);
  486. goto out;
  487. }
  488. bus = libhal_drive_get_bus (drive);
  489. drive_type = libhal_drive_get_type (drive);
  490. switch (drive_type) {
  491. case LIBHAL_DRIVE_TYPE_REMOVABLE_DISK:
  492. case LIBHAL_DRIVE_TYPE_DISK:
  493. case LIBHAL_DRIVE_TYPE_CDROM:
  494. case LIBHAL_DRIVE_TYPE_MO:
  495. case LIBHAL_DRIVE_TYPE_FLOPPY:
  496. name = libhal_storage_policy_lookup_icon (policy, 0x20000 + drive_type*0x100 + bus);
  497. break;
  498. default:
  499. name = libhal_storage_policy_lookup_icon (policy, 0x20000 + drive_type*0x100);
  500. }
  501. out:
  502. if (name != NULL)
  503. return strdup (name);
  504. else
  505. return NULL;
  506. }
  507. /**
  508. * libhal_volume_policy_should_be_visible:
  509. * @drive: Drive that the volume is stemming from
  510. * @volume: Volume
  511. * @policy: Policy object
  512. * @target_mount_point: The mount point that the volume is expected to
  513. * be mounted at if not already mounted. This may
  514. * e.g. stem from /etc/fstab. If this is NULL the
  515. * then mount point isn't taking into account when
  516. * evaluating whether the volume should be visible
  517. * Returns: Whether the volume should be shown in a desktop
  518. * environment.
  519. * Policy function to determine if a volume should be visible in a desktop
  520. * environment. This is useful to hide certain system volumes as bootstrap
  521. * partitions, the /usr partition, swap partitions and other volumes that
  522. * a unprivileged desktop user shouldn't know even exists.
  523. */
  524. dbus_bool_t
  525. libhal_volume_policy_should_be_visible (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy,
  526. const char *target_mount_point)
  527. {
  528. unsigned int i;
  529. dbus_bool_t is_visible;
  530. const char *label;
  531. const char *mount_point;
  532. const char *fstype;
  533. const char *fhs23_toplevel_mount_points[] = {
  534. "/",
  535. "/bin",
  536. "/boot",
  537. "/dev",
  538. "/etc",
  539. "/home",
  540. "/lib",
  541. "/lib64",
  542. "/media",
  543. "/mnt",
  544. "/opt",
  545. "/root",
  546. "/sbin",
  547. "/srv",
  548. "/tmp",
  549. "/usr",
  550. "/var",
  551. "/proc",
  552. "/sbin",
  553. NULL
  554. };
  555. is_visible = FALSE;
  556. /* skip if hal says it's not used as a filesystem */
  557. if (libhal_volume_get_fsusage (volume) != LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM)
  558. goto out;
  559. label = libhal_volume_get_label (volume);
  560. mount_point = libhal_volume_get_mount_point (volume);
  561. fstype = libhal_volume_get_fstype (volume);
  562. /* use target mount point if we're not mounted yet */
  563. if (mount_point == NULL)
  564. mount_point = target_mount_point;
  565. /* bail out if we don't know the filesystem */
  566. if (fstype == NULL)
  567. goto out;
  568. /* blacklist fhs2.3 top level mount points */
  569. if (mount_point != NULL) {
  570. for (i = 0; fhs23_toplevel_mount_points[i] != NULL; i++) {
  571. if (strcmp (mount_point, fhs23_toplevel_mount_points[i]) == 0)
  572. goto out;
  573. }
  574. }
  575. /* blacklist partitions with name 'bootstrap' of type HFS (Apple uses that) */
  576. if (label != NULL && strcmp (label, "bootstrap") == 0 && strcmp (fstype, "hfs") == 0)
  577. goto out;
  578. /* only the real lucky mount points will make it this far :-) */
  579. is_visible = TRUE;
  580. out:
  581. return is_visible;
  582. }
  583. /*************************************************************************/
  584. #define MOUNT_OPTIONS_SIZE 256
  585. struct LibHalDrive_s {
  586. char *udi;
  587. int device_major;
  588. int device_minor;
  589. char *device_file;
  590. LibHalDriveBus bus;
  591. char *vendor; /* may be "", is never NULL */
  592. char *model; /* may be "", is never NULL */
  593. dbus_bool_t is_hotpluggable;
  594. dbus_bool_t is_removable;
  595. dbus_bool_t is_media_detected;
  596. dbus_bool_t is_media_detection_automatic;
  597. dbus_bool_t requires_eject;
  598. LibHalDriveType type;
  599. char *type_textual;
  600. char *physical_device; /* UDI of physical device, e.g. the
  601. * IDE, USB, IEEE1394 device */
  602. char *dedicated_icon_drive;
  603. char *dedicated_icon_volume;
  604. char *serial;
  605. char *firmware_version;
  606. LibHalDriveCdromCaps cdrom_caps;
  607. char *desired_mount_point;
  608. char *mount_filesystem;
  609. dbus_bool_t should_mount;
  610. dbus_bool_t no_partitions_hint;
  611. dbus_uint64_t drive_size;
  612. dbus_uint64_t drive_media_size;
  613. char *partition_scheme;
  614. LibHalContext *hal_ctx;
  615. char **capabilities;
  616. char mount_options[MOUNT_OPTIONS_SIZE];
  617. };
  618. struct LibHalVolume_s {
  619. char *udi;
  620. int device_major;
  621. int device_minor;
  622. char *device_file;
  623. char *volume_label; /* may be NULL, is never "" */
  624. dbus_bool_t is_mounted;
  625. dbus_bool_t is_mounted_read_only; /* TRUE iff is_mounted and r/o fs */
  626. char *mount_point; /* NULL iff !is_mounted */
  627. char *fstype; /* NULL iff !is_mounted or unknown */
  628. char *fsversion;
  629. char *uuid;
  630. char *storage_device;
  631. LibHalVolumeUsage fsusage;
  632. dbus_bool_t is_partition;
  633. unsigned int partition_number;
  634. char *partition_scheme;
  635. char *partition_type;
  636. char *partition_label;
  637. char *partition_uuid;
  638. char **partition_flags;
  639. int msdos_part_table_type;
  640. dbus_uint64_t msdos_part_table_start;
  641. dbus_uint64_t msdos_part_table_size;
  642. dbus_bool_t is_disc;
  643. LibHalVolumeDiscType disc_type;
  644. dbus_bool_t disc_has_audio;
  645. dbus_bool_t disc_has_data;
  646. dbus_bool_t disc_is_appendable;
  647. dbus_bool_t disc_is_blank;
  648. dbus_bool_t disc_is_rewritable;
  649. unsigned int block_size;
  650. dbus_uint64_t num_blocks;
  651. char *desired_mount_point;
  652. char *mount_filesystem;
  653. dbus_bool_t should_mount;
  654. dbus_bool_t ignore_volume;
  655. char *crypto_backing_volume;
  656. char mount_options[MOUNT_OPTIONS_SIZE];
  657. dbus_uint64_t volume_size;
  658. dbus_uint64_t disc_capacity;
  659. dbus_uint64_t partition_start_offset;
  660. dbus_uint64_t partition_media_size;
  661. };
  662. const char *
  663. libhal_drive_get_dedicated_icon_drive (LibHalDrive *drive)
  664. {
  665. return drive->dedicated_icon_drive;
  666. }
  667. const char *
  668. libhal_drive_get_dedicated_icon_volume (LibHalDrive *drive)
  669. {
  670. return drive->dedicated_icon_volume;
  671. }
  672. /**
  673. * libhal_drive_free:
  674. * @drive: Object to free
  675. *
  676. * Free all resources used by a LibHalDrive object.
  677. */
  678. void
  679. libhal_drive_free (LibHalDrive *drive)
  680. {
  681. if (drive == NULL )
  682. return;
  683. free (drive->udi);
  684. libhal_free_string (drive->device_file);
  685. libhal_free_string (drive->vendor);
  686. libhal_free_string (drive->model);
  687. libhal_free_string (drive->type_textual);
  688. libhal_free_string (drive->physical_device);
  689. libhal_free_string (drive->dedicated_icon_drive);
  690. libhal_free_string (drive->dedicated_icon_volume);
  691. libhal_free_string (drive->serial);
  692. libhal_free_string (drive->firmware_version);
  693. libhal_free_string (drive->desired_mount_point);
  694. libhal_free_string (drive->mount_filesystem);
  695. libhal_free_string_array (drive->capabilities);
  696. libhal_free_string (drive->partition_scheme);
  697. free (drive);
  698. }
  699. /**
  700. * libhal_volume_free:
  701. * @volume: Object to free
  702. *
  703. * Free all resources used by a LibHalVolume object.
  704. */
  705. void
  706. libhal_volume_free (LibHalVolume *volume)
  707. {
  708. if (volume == NULL )
  709. return;
  710. free (volume->udi);
  711. libhal_free_string (volume->device_file);
  712. libhal_free_string (volume->volume_label);
  713. libhal_free_string (volume->fstype);
  714. libhal_free_string (volume->mount_point);
  715. libhal_free_string (volume->fsversion);
  716. libhal_free_string (volume->uuid);
  717. libhal_free_string (volume->desired_mount_point);
  718. libhal_free_string (volume->mount_filesystem);
  719. libhal_free_string (volume->crypto_backing_volume);
  720. libhal_free_string (volume->storage_device);
  721. libhal_free_string (volume->partition_scheme);
  722. libhal_free_string (volume->partition_type);
  723. libhal_free_string (volume->partition_label);
  724. libhal_free_string (volume->partition_uuid);
  725. libhal_free_string_array (volume->partition_flags);
  726. free (volume);
  727. }
  728. static char **
  729. my_strvdup (char **strv)
  730. {
  731. unsigned int num_elems;
  732. unsigned int i;
  733. char **res;
  734. for (num_elems = 0; strv[num_elems] != NULL; num_elems++)
  735. ;
  736. res = calloc (num_elems + 1, sizeof (char*));
  737. if (res == NULL)
  738. goto out;
  739. for (i = 0; i < num_elems; i++)
  740. res[i] = strdup (strv[i]);
  741. res[i] = NULL;
  742. out:
  743. return res;
  744. }
  745. /* ok, hey, so this is a bit ugly */
  746. #define LIBHAL_PROP_EXTRACT_BEGIN if (FALSE)
  747. #define LIBHAL_PROP_EXTRACT_END ;
  748. #define LIBHAL_PROP_EXTRACT_INT(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_INT32) _where_ = libhal_psi_get_int (&it)
  749. #define LIBHAL_PROP_EXTRACT_UINT64(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_UINT64) _where_ = libhal_psi_get_uint64 (&it)
  750. #define LIBHAL_PROP_EXTRACT_STRING(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_STRING) _where_ = (libhal_psi_get_string (&it) != NULL && strlen (libhal_psi_get_string (&it)) > 0) ? strdup (libhal_psi_get_string (&it)) : NULL
  751. #define LIBHAL_PROP_EXTRACT_BOOL(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_BOOLEAN) _where_ = libhal_psi_get_bool (&it)
  752. #define LIBHAL_PROP_EXTRACT_BOOL_BITFIELD(_property_, _where_, _field_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_BOOLEAN) _where_ |= libhal_psi_get_bool (&it) ? _field_ : 0
  753. #define LIBHAL_PROP_EXTRACT_STRLIST(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_STRLIST) _where_ = my_strvdup (libhal_psi_get_strlist (&it))
  754. /**
  755. * libhal_drive_from_udi:
  756. * @hal_ctx: libhal context
  757. * @udi: HAL UDI
  758. *
  759. * Returns: LibHalDrive object or NULL if UDI is invalid
  760. *
  761. * Given a UDI for a HAL device of capability 'storage', this
  762. * function retrieves all the relevant properties into convenient
  763. * in-process data structures.
  764. */
  765. LibHalDrive *
  766. libhal_drive_from_udi (LibHalContext *hal_ctx, const char *udi)
  767. {
  768. char *bus_textual;
  769. LibHalDrive *drive;
  770. LibHalPropertySet *properties;
  771. LibHalPropertySetIterator it;
  772. DBusError error;
  773. unsigned int i;
  774. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  775. drive = NULL;
  776. properties = NULL;
  777. bus_textual = NULL;
  778. dbus_error_init (&error);
  779. if (!libhal_device_query_capability (hal_ctx, udi, "storage", &error))
  780. goto error;
  781. drive = malloc (sizeof (LibHalDrive));
  782. if (drive == NULL)
  783. goto error;
  784. memset (drive, 0x00, sizeof (LibHalDrive));
  785. drive->hal_ctx = hal_ctx;
  786. drive->udi = strdup (udi);
  787. if (drive->udi == NULL)
  788. goto error;
  789. properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
  790. if (properties == NULL)
  791. goto error;
  792. /* we can count on hal to give us all these properties */
  793. for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
  794. int type;
  795. char *key;
  796. type = libhal_psi_get_type (&it);
  797. key = libhal_psi_get_key (&it);
  798. LIBHAL_PROP_EXTRACT_BEGIN;
  799. LIBHAL_PROP_EXTRACT_INT ("block.minor", drive->device_minor);
  800. LIBHAL_PROP_EXTRACT_INT ("block.major", drive->device_major);
  801. LIBHAL_PROP_EXTRACT_STRING ("block.device", drive->device_file);
  802. LIBHAL_PROP_EXTRACT_STRING ("storage.bus", bus_textual);
  803. LIBHAL_PROP_EXTRACT_STRING ("storage.vendor", drive->vendor);
  804. LIBHAL_PROP_EXTRACT_STRING ("storage.model", drive->model);
  805. LIBHAL_PROP_EXTRACT_STRING ("storage.drive_type", drive->type_textual);
  806. LIBHAL_PROP_EXTRACT_UINT64 ("storage.size", drive->drive_size);
  807. LIBHAL_PROP_EXTRACT_STRING ("storage.icon.drive", drive->dedicated_icon_drive);
  808. LIBHAL_PROP_EXTRACT_STRING ("storage.icon.volume", drive->dedicated_icon_volume);
  809. LIBHAL_PROP_EXTRACT_BOOL ("storage.hotpluggable", drive->is_hotpluggable);
  810. LIBHAL_PROP_EXTRACT_BOOL ("storage.removable", drive->is_removable);
  811. LIBHAL_PROP_EXTRACT_BOOL ("storage.removable.media_available", drive->is_media_detected);
  812. LIBHAL_PROP_EXTRACT_BOOL ("storage.media_check_enabled", drive->is_media_detection_automatic);
  813. LIBHAL_PROP_EXTRACT_UINT64 ("storage.removable.media_size", drive->drive_media_size);
  814. LIBHAL_PROP_EXTRACT_BOOL ("storage.requires_eject", drive->requires_eject);
  815. LIBHAL_PROP_EXTRACT_STRING ("storage.partitioning_scheme", drive->partition_scheme);
  816. LIBHAL_PROP_EXTRACT_STRING ("storage.originating_device", drive->physical_device);
  817. LIBHAL_PROP_EXTRACT_STRING ("storage.firmware_version", drive->firmware_version);
  818. LIBHAL_PROP_EXTRACT_STRING ("storage.serial", drive->serial);
  819. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.cdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_CDR);
  820. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.cdrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_CDRW);
  821. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvd", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDROM);
  822. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR);
  823. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW);
  824. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrwdl", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRWDL);
  825. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrdl", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL);
  826. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDR);
  827. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDRW);
  828. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdram", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDRAM);
  829. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.bd", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_BDROM);
  830. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.bdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_BDR);
  831. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.bdre", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_BDRE);
  832. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.hddvd", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_HDDVDROM);
  833. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.hddvdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_HDDVDR);
  834. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.hddvdrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_HDDVDRW);
  835. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.mo", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_MO);
  836. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.mrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_MRW);
  837. LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.mrw_w", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_MRWW);
  838. LIBHAL_PROP_EXTRACT_BOOL ("storage.policy.should_mount", drive->should_mount);
  839. LIBHAL_PROP_EXTRACT_STRING ("storage.policy.desired_mount_point", drive->desired_mount_point);
  840. LIBHAL_PROP_EXTRACT_STRING ("storage.policy.mount_filesystem", drive->mount_filesystem);
  841. LIBHAL_PROP_EXTRACT_BOOL ("storage.no_partitions_hint", drive->no_partitions_hint);
  842. LIBHAL_PROP_EXTRACT_STRLIST ("info.capabilities", drive->capabilities);
  843. LIBHAL_PROP_EXTRACT_END;
  844. }
  845. if (drive->type_textual != NULL) {
  846. if (strcmp (drive->type_textual, "cdrom") == 0) {
  847. drive->cdrom_caps |= LIBHAL_DRIVE_CDROM_CAPS_CDROM;
  848. drive->type = LIBHAL_DRIVE_TYPE_CDROM;
  849. } else if (strcmp (drive->type_textual, "optical") == 0) {
  850. drive->type = LIBHAL_DRIVE_TYPE_MO;
  851. } else if (strcmp (drive->type_textual, "floppy") == 0) {
  852. drive->type = LIBHAL_DRIVE_TYPE_FLOPPY;
  853. } else if (strcmp (drive->type_textual, "disk") == 0) {
  854. if (drive->is_removable)
  855. drive->type = LIBHAL_DRIVE_TYPE_REMOVABLE_DISK;
  856. else
  857. drive->type = LIBHAL_DRIVE_TYPE_DISK;
  858. } else if (strcmp (drive->type_textual, "tape") == 0) {
  859. drive->type = LIBHAL_DRIVE_TYPE_TAPE;
  860. } else if (strcmp (drive->type_textual, "compact_flash") == 0) {
  861. drive->type = LIBHAL_DRIVE_TYPE_COMPACT_FLASH;
  862. } else if (strcmp (drive->type_textual, "memory_stick") == 0) {
  863. drive->type = LIBHAL_DRIVE_TYPE_MEMORY_STICK;
  864. } else if (strcmp (drive->type_textual, "smart_media") == 0) {
  865. drive->type = LIBHAL_DRIVE_TYPE_SMART_MEDIA;
  866. } else if (strcmp (drive->type_textual, "sd_mmc") == 0) {
  867. drive->type = LIBHAL_DRIVE_TYPE_SD_MMC;
  868. } else if (strcmp (drive->type_textual, "zip") == 0) {
  869. drive->type = LIBHAL_DRIVE_TYPE_ZIP;
  870. } else if (strcmp (drive->type_textual, "jaz") == 0) {
  871. drive->type = LIBHAL_DRIVE_TYPE_JAZ;
  872. } else if (strcmp (drive->type_textual, "flashkey") == 0) {
  873. drive->type = LIBHAL_DRIVE_TYPE_FLASHKEY;
  874. } else {
  875. drive->type = LIBHAL_DRIVE_TYPE_DISK;
  876. }
  877. }
  878. if (drive->capabilities != NULL) {
  879. for (i = 0; drive->capabilities[i] != NULL; i++) {
  880. if (strcmp (drive->capabilities[i], "portable_audio_player") == 0) {
  881. drive->type = LIBHAL_DRIVE_TYPE_PORTABLE_AUDIO_PLAYER;
  882. break;
  883. } else if (strcmp (drive->capabilities[i], "camera") == 0) {
  884. drive->type = LIBHAL_DRIVE_TYPE_CAMERA;
  885. break;
  886. }
  887. }
  888. }
  889. if (bus_textual != NULL) {
  890. if (strcmp (bus_textual, "usb") == 0) {
  891. drive->bus = LIBHAL_DRIVE_BUS_USB;
  892. } else if (strcmp (bus_textual, "ieee1394") == 0) {
  893. drive->bus = LIBHAL_DRIVE_BUS_IEEE1394;
  894. } else if (strcmp (bus_textual, "ide") == 0) {
  895. drive->bus = LIBHAL_DRIVE_BUS_IDE;
  896. } else if (strcmp (bus_textual, "scsi") == 0) {
  897. drive->bus = LIBHAL_DRIVE_BUS_SCSI;
  898. } else if (strcmp (bus_textual, "ccw") == 0) {
  899. drive->bus = LIBHAL_DRIVE_BUS_CCW;
  900. }
  901. }
  902. libhal_free_string (bus_textual);
  903. libhal_free_property_set (properties);
  904. return drive;
  905. error:
  906. LIBHAL_FREE_DBUS_ERROR(&error);
  907. libhal_free_string (bus_textual);
  908. libhal_free_property_set (properties);
  909. libhal_drive_free (drive);
  910. return NULL;
  911. }
  912. const char *
  913. libhal_volume_get_storage_device_udi (LibHalVolume *volume)
  914. {
  915. return volume->storage_device;
  916. }
  917. const char *libhal_drive_get_physical_device_udi (LibHalDrive *drive)
  918. {
  919. return drive->physical_device;
  920. }
  921. dbus_bool_t
  922. libhal_drive_requires_eject (LibHalDrive *drive)
  923. {
  924. return drive->requires_eject;
  925. }
  926. /**
  927. * libhal_volume_from_udi:
  928. * @hal_ctx: libhal context
  929. * @udi: HAL UDI
  930. *
  931. * Returns: LibHalVolume object or NULL if UDI is invalid
  932. *
  933. * Given a UDI for a LIBHAL device of capability 'volume', this
  934. * function retrieves all the relevant properties into convenient
  935. * in-process data structures.
  936. */
  937. LibHalVolume *
  938. libhal_volume_from_udi (LibHalContext *hal_ctx, const char *udi)
  939. {
  940. char *disc_type_textual;
  941. char *vol_fsusage_textual;
  942. LibHalVolume *vol;
  943. LibHalPropertySet *properties;
  944. LibHalPropertySetIterator it;
  945. DBusError error;
  946. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  947. vol = NULL;
  948. properties = NULL;
  949. disc_type_textual = NULL;
  950. vol_fsusage_textual = NULL;
  951. dbus_error_init (&error);
  952. if (!libhal_device_query_capability (hal_ctx, udi, "volume", &error))
  953. goto error;
  954. vol = malloc (sizeof (LibHalVolume));
  955. if (vol == NULL)
  956. goto error;
  957. memset (vol, 0x00, sizeof (LibHalVolume));
  958. vol->udi = strdup (udi);
  959. properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
  960. if (properties == NULL)
  961. goto error;
  962. /* we can count on hal to give us all these properties */
  963. for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
  964. int type;
  965. char *key;
  966. type = libhal_psi_get_type (&it);
  967. key = libhal_psi_get_key (&it);
  968. LIBHAL_PROP_EXTRACT_BEGIN;
  969. LIBHAL_PROP_EXTRACT_BOOL ("volume.is_partition", vol->is_partition);
  970. LIBHAL_PROP_EXTRACT_INT ("volume.partition.number", vol->partition_number);
  971. LIBHAL_PROP_EXTRACT_STRING ("volume.partition.scheme", vol->partition_scheme);
  972. LIBHAL_PROP_EXTRACT_STRING ("volume.partition.type", vol->partition_type);
  973. LIBHAL_PROP_EXTRACT_STRING ("volume.partition.label", vol->partition_label);
  974. LIBHAL_PROP_EXTRACT_STRING ("volume.partition.uuid", vol->partition_uuid);
  975. LIBHAL_PROP_EXTRACT_STRLIST ("volume.partition.flags", vol->partition_flags);
  976. LIBHAL_PROP_EXTRACT_UINT64 ("volume.partition.start", vol->partition_start_offset);
  977. LIBHAL_PROP_EXTRACT_UINT64 ("volume.partition.media_size", vol->partition_media_size);
  978. LIBHAL_PROP_EXTRACT_INT ("volume.partition.msdos_part_table_type", vol->msdos_part_table_type);
  979. LIBHAL_PROP_EXTRACT_UINT64 ("volume.partition.msdos_part_table_start", vol->msdos_part_table_start);
  980. LIBHAL_PROP_EXTRACT_UINT64 ("volume.partition.msdos_part_table_size", vol->msdos_part_table_size);
  981. LIBHAL_PROP_EXTRACT_INT ("block.minor", vol->device_minor);
  982. LIBHAL_PROP_EXTRACT_INT ("block.major", vol->device_major);
  983. LIBHAL_PROP_EXTRACT_STRING ("block.device", vol->device_file);
  984. LIBHAL_PROP_EXTRACT_STRING ("block.storage_device", vol->storage_device);
  985. LIBHAL_PROP_EXTRACT_STRING ("volume.crypto_luks.clear.backing_volume", vol->crypto_backing_volume);
  986. LIBHAL_PROP_EXTRACT_INT ("volume.block_size", vol->block_size);
  987. LIBHAL_PROP_EXTRACT_UINT64 ("volume.num_blocks", vol->num_blocks);
  988. LIBHAL_PROP_EXTRACT_UINT64 ("volume.size", vol->volume_size);
  989. LIBHAL_PROP_EXTRACT_STRING ("volume.label", vol->volume_label);
  990. LIBHAL_PROP_EXTRACT_STRING ("volume.mount_point", vol->mount_point);
  991. LIBHAL_PROP_EXTRACT_STRING ("volume.fstype", vol->fstype);
  992. LIBHAL_PROP_EXTRACT_STRING ("volume.fsversion", vol->fsversion);
  993. LIBHAL_PROP_EXTRACT_BOOL ("volume.is_mounted", vol->is_mounted);
  994. LIBHAL_PROP_EXTRACT_BOOL ("volume.is_mounted_read_only", vol->is_mounted_read_only);
  995. LIBHAL_PROP_EXTRACT_STRING ("volume.fsusage", vol_fsusage_textual);
  996. LIBHAL_PROP_EXTRACT_STRING ("volume.uuid", vol->uuid);
  997. LIBHAL_PROP_EXTRACT_BOOL ("volume.ignore", vol->ignore_volume);
  998. LIBHAL_PROP_EXTRACT_BOOL ("volume.is_disc", vol->is_disc);
  999. LIBHAL_PROP_EXTRACT_STRING ("volume.disc.type", disc_type_textual);
  1000. LIBHAL_PROP_EXTRACT_BOOL ("volume.disc.has_audio", vol->disc_has_audio);
  1001. LIBHAL_PROP_EXTRACT_BOOL ("volume.disc.has_data", vol->disc_has_data);
  1002. LIBHAL_PROP_EXTRACT_BOOL ("volume.disc.is_appendable", vol->disc_is_appendable);
  1003. LIBHAL_PROP_EXTRACT_BOOL ("volume.disc.is_blank", vol->disc_is_blank);
  1004. LIBHAL_PROP_EXTRACT_BOOL ("volume.disc.is_rewritable", vol->disc_is_rewritable);
  1005. LIBHAL_PROP_EXTRACT_UINT64 ("volume.disc.capacity", vol->disc_capacity);
  1006. LIBHAL_PROP_EXTRACT_BOOL ("volume.policy.should_mount", vol->should_mount);
  1007. LIBHAL_PROP_EXTRACT_STRING ("volume.policy.desired_mount_point", vol->desired_mount_point);
  1008. LIBHAL_PROP_EXTRACT_STRING ("volume.policy.mount_filesystem", vol->mount_filesystem);
  1009. LIBHAL_PROP_EXTRACT_END;
  1010. }
  1011. if (disc_type_textual != NULL) {
  1012. if (strcmp (disc_type_textual, "cd_rom") == 0) {
  1013. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDROM;
  1014. } else if (strcmp (disc_type_textual, "cd_r") == 0) {
  1015. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDR;
  1016. } else if (strcmp (disc_type_textual, "cd_rw") == 0) {
  1017. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDRW;
  1018. } else if (strcmp (disc_type_textual, "dvd_rom") == 0) {
  1019. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDROM;
  1020. } else if (strcmp (disc_type_textual, "dvd_ram") == 0) {
  1021. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDRAM;
  1022. } else if (strcmp (disc_type_textual, "dvd_r") == 0) {
  1023. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDR;
  1024. } else if (strcmp (disc_type_textual, "dvd_rw") == 0) {
  1025. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDRW;
  1026. } else if (strcmp (disc_type_textual, "dvd_r_dl") == 0) {
  1027. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDR_DL;
  1028. } else if (strcmp (disc_type_textual, "dvd_plus_r") == 0) {
  1029. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR;
  1030. } else if (strcmp (disc_type_textual, "dvd_plus_rw") == 0) {
  1031. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSRW;
  1032. } else if (strcmp (disc_type_textual, "dvd_plus_r_dl") == 0) {
  1033. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR_DL;
  1034. } else if (strcmp (disc_type_textual, "bd_rom") == 0) {
  1035. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_BDROM;
  1036. } else if (strcmp (disc_type_textual, "bd_r") == 0) {
  1037. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_BDR;
  1038. } else if (strcmp (disc_type_textual, "bd_re") == 0) {
  1039. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_BDRE;
  1040. } else if (strcmp (disc_type_textual, "hddvd_rom") == 0) {
  1041. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_HDDVDROM;
  1042. } else if (strcmp (disc_type_textual, "hddvd_r") == 0) {
  1043. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_HDDVDR;
  1044. } else if (strcmp (disc_type_textual, "hddvd_rw") == 0) {
  1045. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_HDDVDRW;
  1046. } else if (strcmp (disc_type_textual, "mo") == 0) {
  1047. vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_MO;
  1048. }
  1049. }
  1050. vol->fsusage = LIBHAL_VOLUME_USAGE_UNKNOWN;
  1051. if (vol_fsusage_textual != NULL) {
  1052. if (strcmp (vol_fsusage_textual, "filesystem") == 0) {
  1053. vol->fsusage = LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM;
  1054. } else if (strcmp (vol_fsusage_textual, "partitiontable") == 0) {
  1055. vol->fsusage = LIBHAL_VOLUME_USAGE_PARTITION_TABLE;
  1056. } else if (strcmp (vol_fsusage_textual, "raid") == 0) {
  1057. vol->fsusage = LIBHAL_VOLUME_USAGE_RAID_MEMBER;
  1058. } else if (strcmp (vol_fsusage_textual, "crypto") == 0) {
  1059. vol->fsusage = LIBHAL_VOLUME_USAGE_CRYPTO;
  1060. } else if (strcmp (vol_fsusage_textual, "other") == 0) {
  1061. vol->fsusage = LIBHAL_VOLUME_USAGE_OTHER;
  1062. } else {
  1063. vol->fsusage = LIBHAL_VOLUME_USAGE_UNKNOWN;
  1064. }
  1065. }
  1066. libhal_free_string (vol_fsusage_textual);
  1067. libhal_free_string (disc_type_textual);
  1068. libhal_free_property_set (properties);
  1069. return vol;
  1070. error:
  1071. if (dbus_error_is_set (&error)) {
  1072. dbus_error_free (&error);
  1073. }
  1074. libhal_free_string (vol_fsusage_textual);
  1075. libhal_free_string (disc_type_textual);
  1076. libhal_free_property_set (properties);
  1077. libhal_volume_free (vol);
  1078. return NULL;
  1079. }
  1080. /**
  1081. * libhal_volume_get_msdos_part_table_type:
  1082. * @volume: Volume object
  1083. *
  1084. * Returns: The partition type or -1 if volume is not
  1085. * a partition or the media the volume stems from
  1086. * isn't partition with a MS DOS style table
  1087. *
  1088. * If the volume is on a drive with a MSDOS style partition table, return
  1089. * the partition table id.
  1090. */
  1091. int
  1092. libhal_volume_get_msdos_part_table_type (LibHalVolume *volume)
  1093. {
  1094. return volume->msdos_part_table_type;
  1095. }
  1096. /**
  1097. * libhal_volume_get_msdos_part_table_start:
  1098. * @volume: Volume object
  1099. *
  1100. * Returns: The partition start offset or -1 if volume isnt
  1101. * a partition or the media the volume stems from
  1102. * isn't partition with a MS DOS style table
  1103. *
  1104. * If the volume is on a drive with a MSDOS style partition table, return
  1105. * the partition start offset according to the partition table.
  1106. */
  1107. dbus_uint64_t
  1108. libhal_volume_get_msdos_part_table_start (LibHalVolume *volume)
  1109. {
  1110. return volume->msdos_part_table_start;
  1111. }
  1112. /**
  1113. * libhal_volume_get_msdos_part_table_size:
  1114. * @volume: Volume object
  1115. *
  1116. * Returns: The partition size or -1 if volume is not
  1117. * a partition or the media the volume stems from
  1118. * isn't partition with a MS DOS style table
  1119. *
  1120. * If the volume is on a drive with a MSDOS style partition table, return
  1121. * the partition size according to the partition table.
  1122. */
  1123. dbus_uint64_t
  1124. libhal_volume_get_msdos_part_table_size (LibHalVolume *volume)
  1125. {
  1126. return volume->msdos_part_table_size;
  1127. }
  1128. /***********************************************************************/
  1129. /**
  1130. * libhal_drive_from_device_file:
  1131. * @hal_ctx: libhal context to use
  1132. * @device_file: Name of special device file, e.g. '/dev/hdc'
  1133. *
  1134. * Returns: LibHalDrive object or NULL if it doesn't exist
  1135. *
  1136. * Get the drive object that either is (when given e.g. /dev/sdb) or contains
  1137. * (when given e.g. /dev/sdb1) the given device file.
  1138. */
  1139. LibHalDrive *
  1140. libhal_drive_from_device_file (LibHalContext *hal_ctx, const char *device_file)
  1141. {
  1142. int i;
  1143. char **hal_udis;
  1144. int num_hal_udis;
  1145. LibHalDrive *result;
  1146. char *found_udi;
  1147. DBusError error;
  1148. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  1149. result = NULL;
  1150. found_udi = NULL;
  1151. dbus_error_init (&error);
  1152. if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "block.device",
  1153. device_file, &num_hal_udis, &error)) == NULL) {
  1154. LIBHAL_FREE_DBUS_ERROR(&error);
  1155. goto out;
  1156. }
  1157. for (i = 0; i < num_hal_udis; i++) {
  1158. char *udi;
  1159. char *storage_udi;
  1160. DBusError err1;
  1161. DBusError err2;
  1162. udi = hal_udis[i];
  1163. dbus_error_init (&err1);
  1164. dbus_error_init (&err2);
  1165. if (libhal_device_query_capability (hal_ctx, udi, "volume", &err1)) {
  1166. storage_udi = libhal_device_get_property_string (hal_ctx, udi, "block.storage_device", &err1);
  1167. if (storage_udi == NULL)
  1168. continue;
  1169. found_udi = strdup (storage_udi);
  1170. libhal_free_string (storage_udi);
  1171. break;
  1172. } else if (libhal_device_query_capability (hal_ctx, udi, "storage", &err2)) {
  1173. found_udi = strdup (udi);
  1174. }
  1175. LIBHAL_FREE_DBUS_ERROR(&err1);
  1176. LIBHAL_FREE_DBUS_ERROR(&err2);
  1177. }
  1178. libhal_free_string_array (hal_udis);
  1179. if (found_udi != NULL)
  1180. result = libhal_drive_from_udi (hal_ctx, found_udi);
  1181. free (found_udi);
  1182. out:
  1183. return result;
  1184. }
  1185. /**
  1186. * libhal_volume_from_mount_point:
  1187. * @hal_ctx: libhal context to use
  1188. * @mount_point: Name of mount point without terminting slash, e.g. '/media/disk'
  1189. *
  1190. * Returns: LibHalVolume object or NULL if it doesn't exist
  1191. *
  1192. * Get the volume object for a given mount point
  1193. */
  1194. LibHalVolume *
  1195. libhal_volume_from_mount_point (LibHalContext *hal_ctx,
  1196. const char *mount_point)
  1197. {
  1198. int i;
  1199. char **hal_udis;
  1200. int num_hal_udis;
  1201. LibHalVolume *result;
  1202. char *found_udi;
  1203. DBusError error;
  1204. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  1205. result = NULL;
  1206. found_udi = NULL;
  1207. dbus_error_init (&error);
  1208. if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "volume.mount_point",
  1209. mount_point, &num_hal_udis, &error)) == NULL)
  1210. goto out;
  1211. for (i = 0; i < num_hal_udis; i++) {
  1212. char *udi;
  1213. udi = hal_udis[i];
  1214. if (libhal_device_query_capability (hal_ctx, udi, "volume", &error)) {
  1215. found_udi = strdup (udi);
  1216. break;
  1217. }
  1218. }
  1219. libhal_free_string_array (hal_udis);
  1220. if (found_udi != NULL)
  1221. result = libhal_volume_from_udi (hal_ctx, found_udi);
  1222. free (found_udi);
  1223. out:
  1224. LIBHAL_FREE_DBUS_ERROR(&error);
  1225. return result;
  1226. }
  1227. /**
  1228. * libhal_volume_from_device_file:
  1229. * @hal_ctx: libhal context to use
  1230. * @device_file: Name of special device file, e.g. '/dev/hda5'
  1231. *
  1232. * Returns: LibHalVolume object or NULL if it doesn't exist
  1233. *
  1234. * Get the volume object for a given device file.
  1235. */
  1236. LibHalVolume *
  1237. libhal_volume_from_device_file (LibHalContext *hal_ctx, const char *device_file)
  1238. {
  1239. int i;
  1240. char **hal_udis;
  1241. int num_hal_udis;
  1242. LibHalVolume *result;
  1243. char *found_udi;
  1244. DBusError error;
  1245. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  1246. result = NULL;
  1247. found_udi = NULL;
  1248. dbus_error_init (&error);
  1249. if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "block.device",
  1250. device_file, &num_hal_udis, &error)) == NULL)
  1251. goto out;
  1252. for (i = 0; i < num_hal_udis; i++) {
  1253. char *udi;
  1254. udi = hal_udis[i];
  1255. if (libhal_device_query_capability (hal_ctx, udi, "volume", &error)) {
  1256. found_udi = strdup (udi);
  1257. break;
  1258. }
  1259. }
  1260. libhal_free_string_array (hal_udis);
  1261. if (found_udi != NULL)
  1262. result = libhal_volume_from_udi (hal_ctx, found_udi);
  1263. free (found_udi);
  1264. out:
  1265. LIBHAL_FREE_DBUS_ERROR(&error);
  1266. return result;
  1267. }
  1268. dbus_uint64_t
  1269. libhal_volume_get_size (LibHalVolume *volume)
  1270. {
  1271. if (volume->volume_size > 0)
  1272. return volume->volume_size;
  1273. else
  1274. return ((dbus_uint64_t)volume->block_size) * volume->num_blocks;
  1275. }
  1276. dbus_uint64_t
  1277. libhal_volume_get_disc_capacity (LibHalVolume *volume)
  1278. {
  1279. return volume->disc_capacity;
  1280. }
  1281. dbus_bool_t
  1282. libhal_drive_is_hotpluggable (LibHalDrive *drive)
  1283. {
  1284. return drive->is_hotpluggable;
  1285. }
  1286. dbus_bool_t
  1287. libhal_drive_uses_removable_media (LibHalDrive *drive)
  1288. {
  1289. return drive->is_removable;
  1290. }
  1291. dbus_bool_t
  1292. libhal_drive_is_media_detected (LibHalDrive *drive)
  1293. {
  1294. return drive->is_media_detected;
  1295. }
  1296. dbus_bool_t
  1297. libhal_drive_is_media_detection_automatic (LibHalDrive *drive)
  1298. {
  1299. return drive->is_media_detection_automatic;
  1300. }
  1301. dbus_uint64_t
  1302. libhal_drive_get_size (LibHalDrive *drive)
  1303. {
  1304. return drive->drive_size;
  1305. }
  1306. dbus_uint64_t
  1307. libhal_drive_get_media_size (LibHalDrive *drive)
  1308. {
  1309. return drive->drive_media_size;
  1310. }
  1311. const char *
  1312. libhal_drive_get_partition_scheme (LibHalDrive *drive)
  1313. {
  1314. return drive->partition_scheme;
  1315. }
  1316. LibHalDriveType
  1317. libhal_drive_get_type (LibHalDrive *drive)
  1318. {
  1319. return drive->type;
  1320. }
  1321. LibHalDriveBus
  1322. libhal_drive_get_bus (LibHalDrive *drive)
  1323. {
  1324. return drive->bus;
  1325. }
  1326. LibHalDriveCdromCaps
  1327. libhal_drive_get_cdrom_caps (LibHalDrive *drive)
  1328. {
  1329. return drive->cdrom_caps;
  1330. }
  1331. unsigned int
  1332. libhal_drive_get_device_major (LibHalDrive *drive)
  1333. {
  1334. return drive->device_major;
  1335. }
  1336. unsigned int
  1337. libhal_drive_get_device_minor (LibHalDrive *drive)
  1338. {
  1339. return drive->device_minor;
  1340. }
  1341. const char *
  1342. libhal_drive_get_type_textual (LibHalDrive *drive)
  1343. {
  1344. return drive->type_textual;
  1345. }
  1346. const char *
  1347. libhal_drive_get_device_file (LibHalDrive *drive)
  1348. {
  1349. return drive->device_file;
  1350. }
  1351. const char *
  1352. libhal_drive_get_udi (LibHalDrive *drive)
  1353. {
  1354. return drive->udi;
  1355. }
  1356. const char *
  1357. libhal_drive_get_serial (LibHalDrive *drive)
  1358. {
  1359. return drive->serial;
  1360. }
  1361. const char *
  1362. libhal_drive_get_firmware_version (LibHalDrive *drive)
  1363. {
  1364. return drive->firmware_version;
  1365. }
  1366. const char *
  1367. libhal_drive_get_model (LibHalDrive *drive)
  1368. {
  1369. return drive->model;
  1370. }
  1371. const char *
  1372. libhal_drive_get_vendor (LibHalDrive *drive)
  1373. {
  1374. return drive->vendor;
  1375. }
  1376. /*****************************************************************************/
  1377. const char *
  1378. libhal_volume_get_udi (LibHalVolume *volume)
  1379. {
  1380. return volume->udi;
  1381. }
  1382. const char *
  1383. libhal_volume_get_device_file (LibHalVolume *volume)
  1384. {
  1385. return volume->device_file;
  1386. }
  1387. unsigned int libhal_volume_get_device_major (LibHalVolume *volume)
  1388. {
  1389. return volume->device_major;
  1390. }
  1391. unsigned int libhal_volume_get_device_minor (LibHalVolume *volume)
  1392. {
  1393. return volume->device_minor;
  1394. }
  1395. const char *
  1396. libhal_volume_get_fstype (LibHalVolume *volume)
  1397. {
  1398. return volume->fstype;
  1399. }
  1400. const char *
  1401. libhal_volume_get_fsversion (LibHalVolume *volume)
  1402. {
  1403. return volume->fsversion;
  1404. }
  1405. LibHalVolumeUsage
  1406. libhal_volume_get_fsusage (LibHalVolume *volume)
  1407. {
  1408. return volume->fsusage;
  1409. }
  1410. dbus_bool_t
  1411. libhal_volume_is_mounted (LibHalVolume *volume)
  1412. {
  1413. return volume->is_mounted;
  1414. }
  1415. dbus_bool_t
  1416. libhal_volume_is_mounted_read_only (LibHalVolume *volume)
  1417. {
  1418. return volume->is_mounted_read_only;
  1419. }
  1420. dbus_bool_t
  1421. libhal_volume_is_partition (LibHalVolume *volume)
  1422. {
  1423. return volume->is_partition;
  1424. }
  1425. dbus_bool_t
  1426. libhal_volume_is_disc (LibHalVolume *volume)
  1427. {
  1428. return volume->is_disc;
  1429. }
  1430. unsigned int
  1431. libhal_volume_get_partition_number (LibHalVolume *volume)
  1432. {
  1433. return volume->partition_number;
  1434. }
  1435. const char *
  1436. libhal_volume_get_partition_scheme (LibHalVolume *volume)
  1437. {
  1438. return volume->partition_scheme;
  1439. }
  1440. const char *
  1441. libhal_volume_get_partition_type (LibHalVolume *volume)
  1442. {
  1443. return volume->partition_type;
  1444. }
  1445. const char *
  1446. libhal_volume_get_partition_label (LibHalVolume *volume)
  1447. {
  1448. return volume->partition_label;
  1449. }
  1450. const char *
  1451. libhal_volume_get_partition_uuid (LibHalVolume *volume)
  1452. {
  1453. return volume->partition_uuid;
  1454. }
  1455. const char **
  1456. libhal_volume_get_partition_flags (LibHalVolume *volume)
  1457. {
  1458. return (const char **) volume->partition_flags;
  1459. }
  1460. dbus_uint64_t
  1461. libhal_volume_get_partition_start_offset (LibHalVolume *volume)
  1462. {
  1463. return volume->partition_start_offset;
  1464. }
  1465. dbus_uint64_t
  1466. libhal_volume_get_partition_media_size (LibHalVolume *volume)
  1467. {
  1468. return volume->partition_media_size;
  1469. }
  1470. const char *
  1471. libhal_volume_get_label (LibHalVolume *volume)
  1472. {
  1473. return volume->volume_label;
  1474. }
  1475. const char *
  1476. libhal_volume_get_mount_point (LibHalVolume *volume)
  1477. {
  1478. return volume->mount_point;
  1479. }
  1480. const char *
  1481. libhal_volume_get_uuid (LibHalVolume *volume)
  1482. {
  1483. return volume->uuid;
  1484. }
  1485. dbus_bool_t
  1486. libhal_volume_disc_has_audio (LibHalVolume *volume)
  1487. {
  1488. return volume->disc_has_audio;
  1489. }
  1490. dbus_bool_t
  1491. libhal_volume_disc_has_data (LibHalVolume *volume)
  1492. {
  1493. return volume->disc_has_data;
  1494. }
  1495. dbus_bool_t
  1496. libhal_volume_disc_is_blank (LibHalVolume *volume)
  1497. {
  1498. return volume->disc_is_blank;
  1499. }
  1500. dbus_bool_t
  1501. libhal_volume_disc_is_rewritable (LibHalVolume *volume)
  1502. {
  1503. return volume->disc_is_rewritable;
  1504. }
  1505. dbus_bool_t
  1506. libhal_volume_disc_is_appendable (LibHalVolume *volume)
  1507. {
  1508. return volume->disc_is_appendable;
  1509. }
  1510. LibHalVolumeDiscType
  1511. libhal_volume_get_disc_type (LibHalVolume *volume)
  1512. {
  1513. return volume->disc_type;
  1514. }
  1515. dbus_bool_t
  1516. libhal_volume_should_ignore (LibHalVolume *volume)
  1517. {
  1518. return volume->ignore_volume;
  1519. }
  1520. char **
  1521. libhal_drive_find_all_volumes (LibHalContext *hal_ctx, LibHalDrive *drive, int *num_volumes)
  1522. {
  1523. int i;
  1524. char **udis;
  1525. int num_udis;
  1526. const char *drive_udi;
  1527. char **result;
  1528. DBusError error;
  1529. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  1530. udis = NULL;
  1531. result = NULL;
  1532. *num_volumes = 0;
  1533. drive_udi = libhal_drive_get_udi (drive);
  1534. if (drive_udi == NULL)
  1535. goto out;
  1536. /* get initial list... */
  1537. dbus_error_init (&error);
  1538. if ((udis = libhal_manager_find_device_string_match (hal_ctx, "block.storage_device",
  1539. drive_udi, &num_udis, &error)) == NULL) {
  1540. LIBHAL_FREE_DBUS_ERROR(&error);
  1541. goto out;
  1542. }
  1543. result = malloc (sizeof (char *) * num_udis);
  1544. if (result == NULL)
  1545. goto out;
  1546. /* ...and filter out the single UDI that is the drive itself */
  1547. for (i = 0; i < num_udis; i++) {
  1548. if (strcmp (udis[i], drive_udi) == 0)
  1549. continue;
  1550. result[*num_volumes] = strdup (udis[i]);
  1551. *num_volumes = (*num_volumes) + 1;
  1552. }
  1553. /* set last element (above removed UDI) to NULL for libhal_free_string_array()*/
  1554. result[*num_volumes] = NULL;
  1555. out:
  1556. libhal_free_string_array (udis);
  1557. return result;
  1558. }
  1559. const char *
  1560. libhal_volume_crypto_get_backing_volume_udi (LibHalVolume *volume)
  1561. {
  1562. return volume->crypto_backing_volume;
  1563. }
  1564. char *
  1565. libhal_volume_crypto_get_clear_volume_udi (LibHalContext *hal_ctx, LibHalVolume *volume)
  1566. {
  1567. DBusError error;
  1568. char **clear_devices;
  1569. int num_clear_devices;
  1570. char *result;
  1571. result = NULL;
  1572. LIBHAL_CHECK_LIBHALCONTEXT (hal_ctx, NULL);
  1573. dbus_error_init (&error);
  1574. clear_devices = libhal_manager_find_device_string_match (hal_ctx,
  1575. "volume.crypto_luks.clear.backing_volume",
  1576. volume->udi,
  1577. &num_clear_devices,
  1578. &error);
  1579. if (clear_devices != NULL) {
  1580. if (num_clear_devices >= 1) {
  1581. result = strdup (clear_devices[0]);
  1582. }
  1583. libhal_free_string_array (clear_devices);
  1584. }
  1585. return result;
  1586. }
  1587. /*************************************************************************/
  1588. char *
  1589. libhal_drive_policy_default_get_mount_root (LibHalContext *hal_ctx)
  1590. {
  1591. char *result;
  1592. DBusError error;
  1593. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  1594. dbus_error_init (&error);
  1595. if ((result = libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
  1596. "storage.policy.default.mount_root", &error)) == NULL)
  1597. LIBHAL_FREE_DBUS_ERROR(&error);
  1598. return result;
  1599. }
  1600. dbus_bool_t
  1601. libhal_drive_policy_default_use_managed_keyword (LibHalContext *hal_ctx)
  1602. {
  1603. dbus_bool_t result;
  1604. DBusError error;
  1605. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, FALSE);
  1606. dbus_error_init (&error);
  1607. if ((result = libhal_device_get_property_bool (hal_ctx, "/org/freedesktop/Hal/devices/computer",
  1608. "storage.policy.default.use_managed_keyword", &error)) == FALSE)
  1609. LIBHAL_FREE_DBUS_ERROR(&error);
  1610. return result;
  1611. }
  1612. char *
  1613. libhal_drive_policy_default_get_managed_keyword_primary (LibHalContext *hal_ctx)
  1614. {
  1615. char *result;
  1616. DBusError error;
  1617. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  1618. dbus_error_init (&error);
  1619. if ((result = libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
  1620. "storage.policy.default.managed_keyword.primary", &error)) == NULL)
  1621. LIBHAL_FREE_DBUS_ERROR(&error);
  1622. return result;
  1623. }
  1624. char *
  1625. libhal_drive_policy_default_get_managed_keyword_secondary (LibHalContext *hal_ctx)
  1626. {
  1627. char *result;
  1628. DBusError error;
  1629. LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
  1630. dbus_error_init (&error);
  1631. if ((result = libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
  1632. "storage.policy.default.managed_keyword.secondary", &error)) == NULL)
  1633. LIBHAL_FREE_DBUS_ERROR(&error);
  1634. return result;
  1635. }
  1636. /*************************************************************************/
  1637. dbus_bool_t
  1638. libhal_drive_policy_is_mountable (LibHalDrive *drive, LibHalStoragePolicy *policy)
  1639. {
  1640. printf ("should_mount=%u, no_partitions_hint=%u\n", drive->should_mount, drive->no_partitions_hint);
  1641. return drive->should_mount && drive->no_partitions_hint;
  1642. }
  1643. const char *
  1644. libhal_drive_policy_get_desired_mount_point (LibHalDrive *drive, LibHalStoragePolicy *policy)
  1645. {
  1646. return drive->desired_mount_point;
  1647. }
  1648. /* safely strcat() at most the remaining space in 'dst' */
  1649. #define strcat_len(dst, src, dstmaxlen) do { \
  1650. dst[dstmaxlen - 1] = '\0'; \
  1651. strncat (dst, src, dstmaxlen - strlen (dst) - 1); \
  1652. } while(0)
  1653. static void
  1654. mopts_collect (LibHalContext *hal_ctx, const char *namespace, int namespace_len,
  1655. const char *udi, char *options_string, size_t options_max_len, dbus_bool_t only_collect_imply_opts)
  1656. {
  1657. LibHalPropertySet *properties;
  1658. LibHalPropertySetIterator it;
  1659. DBusError error;
  1660. if(hal_ctx == NULL) {
  1661. fprintf (stderr,"%s %d : LibHalContext *ctx is NULL\n",__FILE__, __LINE__);
  1662. return;
  1663. }
  1664. dbus_error_init (&error);
  1665. /* first collect from root computer device */
  1666. properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
  1667. if (properties == NULL ) {
  1668. LIBHAL_FREE_DBUS_ERROR(&error);
  1669. return;
  1670. }
  1671. for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
  1672. char *key;
  1673. key = libhal_psi_get_key (&it);
  1674. if (libhal_psi_get_type (&it) == LIBHAL_PROPERTY_TYPE_BOOLEAN &&
  1675. strncmp (key, namespace, namespace_len - 1) == 0) {
  1676. const char *option = key + namespace_len - 1;
  1677. char *location;
  1678. dbus_bool_t is_imply_opt;
  1679. is_imply_opt = FALSE;
  1680. if (strcmp (option, "user") == 0 ||
  1681. strcmp (option, "users") == 0 ||
  1682. strcmp (option, "defaults") == 0 ||
  1683. strcmp (option, "pamconsole") == 0)
  1684. is_imply_opt = TRUE;
  1685. if (only_collect_imply_opts) {
  1686. if (!is_imply_opt)
  1687. continue;
  1688. } else {
  1689. if (is_imply_opt)
  1690. continue;
  1691. }
  1692. if (libhal_psi_get_bool (&it)) {
  1693. /* see if option is already there */
  1694. location = strstr (options_string, option);
  1695. if (location == NULL) {
  1696. if (strlen (options_string) > 0)
  1697. strcat_len (options_string, ",", options_max_len);
  1698. strcat_len (options_string, option, options_max_len);
  1699. }
  1700. } else {
  1701. /* remove option if already there */
  1702. location = strstr (options_string, option);
  1703. if (location != NULL) {
  1704. char *end;
  1705. end = strchr (location, ',');
  1706. if (end == NULL) {
  1707. location[0] = '\0';
  1708. } else {
  1709. strcpy (location, end + 1); /* skip the extra comma */
  1710. }
  1711. }
  1712. }
  1713. }
  1714. }
  1715. libhal_free_property_set (properties);
  1716. }
  1717. const char *
  1718. libhal_drive_policy_get_mount_options (LibHalDrive *drive, LibHalStoragePolicy *policy)
  1719. {
  1720. const char *result;
  1721. char stor_mount_option_default_begin[] = "storage.policy.default.mount_option.";
  1722. char stor_mount_option_begin[] = "storage.policy.mount_option.";
  1723. result = NULL;
  1724. drive->mount_options[0] = '\0';
  1725. /* collect options != ('pamconsole', 'user', 'users', 'defaults' options that imply other options) */
  1726. mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
  1727. "/org/freedesktop/Hal/devices/computer", drive->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
  1728. mopts_collect (drive->hal_ctx, stor_mount_option_begin, sizeof (stor_mount_option_begin),
  1729. drive->udi, drive->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
  1730. /* ensure ('pamconsole', 'user', 'users', 'defaults' options that imply other options), are first */
  1731. mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
  1732. "/org/freedesktop/Hal/devices/computer", drive->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
  1733. mopts_collect (drive->hal_ctx, stor_mount_option_begin, sizeof (stor_mount_option_begin),
  1734. drive->udi, drive->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
  1735. result = drive->mount_options;
  1736. return result;
  1737. }
  1738. const char *
  1739. libhal_drive_policy_get_mount_fs (LibHalDrive *drive, LibHalStoragePolicy *policy)
  1740. {
  1741. return drive->mount_filesystem;
  1742. }
  1743. dbus_bool_t
  1744. libhal_volume_policy_is_mountable (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  1745. {
  1746. return drive->should_mount && volume->should_mount;
  1747. }
  1748. const char *libhal_volume_policy_get_desired_mount_point (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  1749. {
  1750. return volume->desired_mount_point;
  1751. }
  1752. const char *libhal_volume_policy_get_mount_options (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  1753. {
  1754. const char *result;
  1755. char stor_mount_option_default_begin[] = "storage.policy.default.mount_option.";
  1756. char vol_mount_option_begin[] = "volume.policy.mount_option.";
  1757. result = NULL;
  1758. volume->mount_options[0] = '\0';
  1759. /* ensure ('pamconsole', 'user', 'users', 'defaults' options that imply other options), are first */
  1760. mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
  1761. "/org/freedesktop/Hal/devices/computer", volume->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
  1762. mopts_collect (drive->hal_ctx, vol_mount_option_begin, sizeof (vol_mount_option_begin),
  1763. volume->udi, volume->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
  1764. /* collect options != ('pamconsole', 'user', 'users', 'defaults' options that imply other options) */
  1765. mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
  1766. "/org/freedesktop/Hal/devices/computer", volume->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
  1767. mopts_collect (drive->hal_ctx, vol_mount_option_begin, sizeof (vol_mount_option_begin),
  1768. volume->udi, volume->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
  1769. result = volume->mount_options;
  1770. return result;
  1771. }
  1772. const char *libhal_volume_policy_get_mount_fs (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
  1773. {
  1774. return volume->mount_filesystem;
  1775. }
  1776. dbus_bool_t
  1777. libhal_drive_no_partitions_hint (LibHalDrive *drive)
  1778. {
  1779. return drive->no_partitions_hint;
  1780. }