libhal-storage.c

00001 /***************************************************************************
00002  * CVSID: $Id: libhal-storage.c,v 1.27 2006/01/15 16:54:05 david Exp $
00003  *
00004  * libhal-storage.c : HAL convenience library for storage devices and volumes
00005  *
00006  * Copyright (C) 2004 Red Hat, Inc. David Zeuthen <davidz@redhat.com>
00007  * Copyright (C) 2005 Danny Kukawka, <danny.kukawka@web.de>
00008  *
00009  * Licensed under the Academic Free License version 2.1
00010  *
00011  * This program is free software; you can redistribute it and/or modify
00012  * it under the terms of the GNU General Public License as published by
00013  * the Free Software Foundation; either version 2 of the License, or
00014  * (at your option) any later version.
00015  *
00016  * This program is distributed in the hope that it will be useful,
00017  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  * GNU General Public License for more details.
00020  *
00021  * You should have received a copy of the GNU General Public License
00022  * along with this program; if not, write to the Free Software
00023  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024  *
00025  **************************************************************************/
00026 
00027 #ifdef HAVE_CONFIG_H
00028 #  include <config.h>
00029 #endif
00030 
00031 #include <stdio.h>
00032 #include <stdlib.h>
00033 #include <string.h>
00034 #include <dbus/dbus.h>
00035 
00036 #include "../libhal/libhal.h"
00037 #include "libhal-storage.h"
00038 
00039 
00040 #ifdef ENABLE_NLS
00041 # include <libintl.h>
00042 # define _(String) dgettext (GETTEXT_PACKAGE, String)
00043 # ifdef gettext_noop
00044 #   define N_(String) gettext_noop (String)
00045 # else
00046 #   define N_(String) (String)
00047 # endif
00048 #else
00049 /* Stubs that do something close enough.  */
00050 # define textdomain(String) (String)
00051 # define gettext(String) (String)
00052 # define dgettext(Domain,Message) (Message)
00053 # define dcgettext(Domain,Message,Type) (Message)
00054 # define bindtextdomain(Domain,Directory) (Domain)
00055 # define _(String) (String)
00056 # define N_(String) (String)
00057 #endif
00058 
00070 typedef struct IconMappingEntry_s {
00071     LibHalStoragePolicyIcon icon;
00072     char *path;
00073     struct IconMappingEntry_s *next;
00074 } IconMappingEntry;
00075 
00076 struct LibHalStoragePolicy_s {
00077     IconMappingEntry *icon_mappings;
00078 };
00079 
00080 LibHalStoragePolicy *
00081 libhal_storage_policy_new ()
00082 {
00083     LibHalStoragePolicy *p;
00084 
00085     p = malloc (sizeof (LibHalStoragePolicy));
00086     if (p == NULL)
00087         goto out;
00088 
00089     p->icon_mappings = NULL;
00090 out:
00091     return p;
00092 }
00093 
00094 void
00095 libhal_storage_policy_free (LibHalStoragePolicy *policy)
00096 {
00097     IconMappingEntry *i;
00098     IconMappingEntry *j;
00099 
00100     /* free all icon mappings */
00101     for (i = policy->icon_mappings; i != NULL; i = j) {
00102         j = i->next;
00103         free (i->path);
00104         free (i);
00105     }
00106 
00107     free (policy);
00108 }
00109 
00110 void
00111 libhal_storage_policy_set_icon_path (LibHalStoragePolicy *policy, LibHalStoragePolicyIcon icon, const char *path)
00112 {
00113     IconMappingEntry *i;
00114 
00115     /* see if it already exist */
00116     for (i = policy->icon_mappings; i != NULL; i = i->next) {
00117         if (i->icon == icon) {
00118             free (i->path);
00119             i->path = strdup (path);
00120             goto out;
00121         }
00122     }
00123 
00124     i = malloc (sizeof (IconMappingEntry));
00125     if (i == NULL)
00126         goto out;
00127     i->icon = icon;
00128     i->path = strdup (path);
00129     i->next = policy->icon_mappings;
00130     policy->icon_mappings = i;
00131 
00132 out:
00133     return;
00134 }
00135 
00136 void
00137 libhal_storage_policy_set_icon_mapping (LibHalStoragePolicy *policy, LibHalStoragePolicyIconPair *pairs)
00138 {
00139     LibHalStoragePolicyIconPair *i;
00140 
00141     for (i = pairs; i->icon != 0x00; i++) {
00142         libhal_storage_policy_set_icon_path (policy, i->icon, i->icon_path);
00143     }
00144 }
00145 
00146 const char *
00147 libhal_storage_policy_lookup_icon (LibHalStoragePolicy *policy, LibHalStoragePolicyIcon icon)
00148 {
00149     IconMappingEntry *i;
00150     const char *path;
00151 
00152     path = NULL;
00153     for (i = policy->icon_mappings; i != NULL; i = i->next) {
00154         if (i->icon == icon) {
00155             path = i->path;
00156             goto out;
00157         }
00158     }
00159 out:
00160     return path;
00161 }
00162 
00163 
00164 #define MAX_STRING_SZ 256
00165 
00166 char *
00167 libhal_volume_policy_compute_size_as_string (LibHalVolume *volume)
00168 {
00169     dbus_uint64_t size;
00170     char *result;
00171     char* sizes_str[] = {"K", "M", "G", "T", NULL};
00172     dbus_uint64_t cur = 1000L;
00173     dbus_uint64_t base = 10L;
00174     dbus_uint64_t step = 10L*10L*10L;
00175     int cur_str = 0;
00176     char buf[MAX_STRING_SZ];
00177 
00178     result = NULL;
00179 
00180     size = libhal_volume_get_size (volume);
00181 
00182     do {
00183         if (sizes_str[cur_str+1] == NULL || size < cur*step) {
00184             /* found the unit, display a comma number if result is a single digit */
00185             if (size < cur*base) {
00186                 snprintf (buf, MAX_STRING_SZ, "%.01f%s", 
00187                       ((double)size)/((double)cur), sizes_str[cur_str]);
00188                 result = strdup (buf);
00189             } else {
00190                 snprintf (buf, MAX_STRING_SZ, "%lld%s", size / cur, sizes_str[cur_str]);
00191                 result = strdup (buf);
00192                 }
00193             goto out;
00194         }
00195 
00196         cur *= step;
00197         cur_str++;
00198     } while (1);
00199 
00200 out:
00201     return result;
00202 }
00203 
00204 static void
00205 fixup_string (char *s)
00206 {
00207     /* TODO: first strip leading and trailing whitespace */
00208     /*g_strstrip (s);*/
00209 
00210     /* TODO: could do nice things on all-upper case strings */
00211 }
00212 
00213 /* volume may be NULL (e.g. if drive supports removable media) */
00214 char *
00215 libhal_drive_policy_compute_display_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
00216 {
00217     char *name;
00218     char *size_str;
00219     char *vendormodel_str;
00220     const char *model;
00221     const char *vendor;
00222     LibHalDriveType drive_type;
00223     dbus_bool_t drive_is_hotpluggable;
00224     dbus_bool_t drive_is_removable;
00225     LibHalDriveCdromCaps drive_cdrom_caps;
00226     char buf[MAX_STRING_SZ];
00227 
00228     model = libhal_drive_get_model (drive);
00229     vendor = libhal_drive_get_vendor (drive);
00230     drive_type = libhal_drive_get_type (drive);
00231     drive_is_hotpluggable = libhal_drive_is_hotpluggable (drive);
00232     drive_is_removable = libhal_drive_uses_removable_media (drive);
00233     drive_cdrom_caps = libhal_drive_get_cdrom_caps (drive);
00234 
00235     if (volume != NULL)
00236         size_str = libhal_volume_policy_compute_size_as_string (volume);
00237     else
00238         size_str = NULL;
00239 
00240     if (vendor == NULL || strlen (vendor) == 0) {
00241         if (model == NULL || strlen (model) == 0)
00242             vendormodel_str = strdup ("");
00243         else
00244             vendormodel_str = strdup (model);
00245     } else {
00246         if (model == NULL || strlen (model) == 0)
00247             vendormodel_str = strdup (vendor);
00248         else {
00249             snprintf (buf, MAX_STRING_SZ, "%s %s", vendor, model);
00250             vendormodel_str = strdup (buf);
00251         }
00252     }
00253 
00254     fixup_string (vendormodel_str);
00255 
00256     if (drive_type==LIBHAL_DRIVE_TYPE_CDROM) {
00257 
00258         /* Optical drive handling */
00259         char *first;
00260         char *second;
00261 
00262 
00263         first = "CD-ROM";
00264         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_CDR)
00265             first = "CD-R";
00266         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_CDRW)
00267             first = "CD-RW";
00268 
00269         second = "";
00270         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDROM)
00271             second = "/DVD-ROM";
00272         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR)
00273             second = "/DVD+R";
00274         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW)
00275             second = "/DVD+RW";
00276         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDR)
00277             second = "/DVD-R";
00278         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRW)
00279             second = "/DVD-RW";
00280         if (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRAM)
00281             second = "/DVD-RAM";
00282         if ((drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDR) &&
00283             (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR)) {
00284             if(drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL)
00285                 second = "/DVD±R DL";
00286             else
00287                 second = "/DVD±R";
00288         }
00289         if ((drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRW) &&
00290             (drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW)) {
00291                         if(drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL || 
00292                drive_cdrom_caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRWDL)
00293                                 second = "/DVD±RW DL";
00294                         else
00295                                 second = "/DVD±RW";
00296                 }
00297 
00298 
00299         if (drive_is_hotpluggable) {
00300             snprintf (buf, MAX_STRING_SZ, _("External %s%s Drive"), first, second);
00301             name = strdup (buf);
00302         } else {
00303             snprintf (buf, MAX_STRING_SZ, _("%s%s Drive"), first, second);
00304             name = strdup (buf);
00305         }
00306             
00307     } else if (drive_type==LIBHAL_DRIVE_TYPE_FLOPPY) {
00308 
00309         /* Floppy Drive handling */
00310 
00311         if (drive_is_hotpluggable)
00312             name = strdup (_("External Floppy Drive"));
00313         else
00314             name = strdup (_("Floppy Drive"));
00315     } else if (drive_type==LIBHAL_DRIVE_TYPE_DISK && !drive_is_removable) {
00316 
00317         /* Harddisks */
00318 
00319         if (size_str != NULL) {
00320             if (drive_is_hotpluggable) {
00321                 snprintf (buf, MAX_STRING_SZ, _("%s External Hard Drive"), size_str);
00322                 name = strdup (buf);
00323             } else {
00324                 snprintf (buf, MAX_STRING_SZ, _("%s Hard Drive"), size_str);
00325                 name = strdup (buf);
00326             }
00327         } else {
00328             if (drive_is_hotpluggable)
00329                 name = strdup (_("External Hard Drive"));
00330             else
00331                 name = strdup (_("Hard Drive"));
00332         }
00333     } else {
00334 
00335         /* The rest - includes drives with removable Media */
00336 
00337         if (strlen (vendormodel_str) > 0)
00338             name = strdup (vendormodel_str);
00339         else
00340             name = strdup (_("Drive"));
00341     }
00342 
00343     free (vendormodel_str);
00344     free (size_str);
00345 
00346     return name;
00347 }
00348 
00349 char *
00350 libhal_volume_policy_compute_display_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
00351 {
00352     char *name;
00353     char *size_str;
00354     const char *volume_label;
00355     const char *model;
00356     const char *vendor;
00357     LibHalDriveType drive_type;
00358     dbus_bool_t drive_is_hotpluggable;
00359     dbus_bool_t drive_is_removable;
00360     LibHalDriveCdromCaps drive_cdrom_caps;
00361     char buf[MAX_STRING_SZ];
00362 
00363     volume_label = libhal_volume_get_label (volume);
00364     model = libhal_drive_get_model (drive);
00365     vendor = libhal_drive_get_vendor (drive);
00366     drive_type = libhal_drive_get_type (drive);
00367     drive_is_hotpluggable = libhal_drive_is_hotpluggable (drive);
00368     drive_is_removable = libhal_drive_uses_removable_media (drive);
00369     drive_cdrom_caps = libhal_drive_get_cdrom_caps (drive);
00370 
00371     size_str = libhal_volume_policy_compute_size_as_string (volume);
00372 
00373     /* If the volume label is available use that 
00374      *
00375      * TODO: If label is a fully-qualified UNIX path don't use that
00376      */
00377     if (volume_label != NULL) {
00378         name = strdup (volume_label);
00379         goto out;
00380     }
00381 
00382     /* Handle media in optical drives */
00383     if (drive_type==LIBHAL_DRIVE_TYPE_CDROM) {
00384         switch (libhal_volume_get_disc_type (volume)) {
00385 
00386         default:
00387             /* explict fallthrough */
00388         case LIBHAL_VOLUME_DISC_TYPE_CDROM:
00389             name = strdup (_("CD-ROM "));
00390             break;
00391             
00392         case LIBHAL_VOLUME_DISC_TYPE_CDR:
00393             if (libhal_volume_disc_is_blank (volume))
00394                 name = strdup (_("Blank CD-R"));
00395             else
00396                 name = strdup (_("CD-R"));
00397             break;
00398             
00399         case LIBHAL_VOLUME_DISC_TYPE_CDRW:
00400             if (libhal_volume_disc_is_blank (volume))
00401                 name = strdup (_("Blank CD-RW"));
00402             else
00403                 name = strdup (_("CD-RW"));
00404             break;
00405             
00406         case LIBHAL_VOLUME_DISC_TYPE_DVDROM:
00407             name = strdup (_("DVD-ROM"));
00408             break;
00409             
00410         case LIBHAL_VOLUME_DISC_TYPE_DVDRAM:
00411             if (libhal_volume_disc_is_blank (volume))
00412                 name = strdup (_("Blank DVD-RAM"));
00413             else
00414                 name = strdup (_("DVD-RAM"));
00415             break;
00416             
00417         case LIBHAL_VOLUME_DISC_TYPE_DVDR:
00418             if (libhal_volume_disc_is_blank (volume))
00419                 name = strdup (_("Blank DVD-R"));
00420             else
00421                 name = strdup (_("DVD-R"));
00422             break;
00423             
00424         case LIBHAL_VOLUME_DISC_TYPE_DVDRW:
00425             if (libhal_volume_disc_is_blank (volume))
00426                 name = strdup (_("Blank DVD-RW"));
00427             else
00428                 name = strdup (_("DVD-RW"));
00429             break;
00430 
00431         case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR:
00432             if (libhal_volume_disc_is_blank (volume))
00433                 name = strdup (_("Blank DVD+R"));
00434             else
00435                 name = strdup (_("DVD+R"));
00436             break;
00437             
00438         case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSRW:
00439             if (libhal_volume_disc_is_blank (volume))
00440                 name = strdup (_("Blank DVD+RW"));
00441             else
00442                 name = strdup (_("DVD+RW"));
00443             break;
00444         
00445         case LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR_DL:
00446             if (libhal_volume_disc_is_blank (volume))
00447                 name = strdup (_("Blank DVD+R Dual-Layer"));
00448             else
00449                 name = strdup (_("DVD+R Dual-Layer"));
00450             break;
00451         }
00452         
00453         /* Special case for pure audio disc */
00454         if (libhal_volume_disc_has_audio (volume) && !libhal_volume_disc_has_data (volume)) {
00455             free (name);
00456             name = strdup (_("Audio CD"));
00457         }
00458 
00459         goto out;
00460     }
00461 
00462     /* Fallback: size of media */
00463     if (drive_is_removable) {
00464         snprintf (buf, MAX_STRING_SZ, _("%s Removable Media"), size_str);
00465         name = strdup (buf);
00466     } else {
00467         snprintf (buf, MAX_STRING_SZ, _("%s Media"), size_str);
00468         name = strdup (buf);
00469     }
00470 
00471     /* Fallback: Use drive name */
00472     /*name = libhal_drive_policy_compute_display_name (drive, volume);*/
00473 
00474 out:
00475     free (size_str);
00476     return name;
00477 }
00478 
00479 char *
00480 libhal_drive_policy_compute_icon_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
00481 {
00482     const char *name;
00483     LibHalDriveBus bus;
00484     LibHalDriveType drive_type;
00485 
00486     bus        = libhal_drive_get_bus (drive);
00487     drive_type = libhal_drive_get_type (drive);
00488 
00489     /* by design, the enums are laid out so we can do easy computations */
00490 
00491     switch (drive_type) {
00492     case LIBHAL_DRIVE_TYPE_REMOVABLE_DISK:
00493     case LIBHAL_DRIVE_TYPE_DISK:
00494     case LIBHAL_DRIVE_TYPE_CDROM:
00495     case LIBHAL_DRIVE_TYPE_FLOPPY:
00496         name = libhal_storage_policy_lookup_icon (policy, 0x10000 + drive_type*0x100 + bus);
00497         break;
00498 
00499     default:
00500         name = libhal_storage_policy_lookup_icon (policy, 0x10000 + drive_type*0x100);
00501     }
00502 
00503     if (name != NULL)
00504         return strdup (name);
00505     else
00506         return NULL;
00507 }
00508 
00509 char *
00510 libhal_volume_policy_compute_icon_name (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
00511 {
00512     const char *name;
00513     LibHalDriveBus bus;
00514     LibHalDriveType drive_type;
00515     LibHalVolumeDiscType disc_type;
00516 
00517     /* by design, the enums are laid out so we can do easy computations */
00518 
00519     if (libhal_volume_is_disc (volume)) {
00520         disc_type = libhal_volume_get_disc_type (volume);
00521         name = libhal_storage_policy_lookup_icon (policy, 0x30000 + disc_type);
00522         goto out;
00523     }
00524 
00525     if (drive == NULL) {
00526         name = libhal_storage_policy_lookup_icon (policy, LIBHAL_STORAGE_ICON_VOLUME_REMOVABLE_DISK);
00527         goto out;
00528     }
00529 
00530     bus        = libhal_drive_get_bus (drive);
00531     drive_type = libhal_drive_get_type (drive);
00532 
00533     switch (drive_type) {
00534     case LIBHAL_DRIVE_TYPE_REMOVABLE_DISK:
00535     case LIBHAL_DRIVE_TYPE_DISK:
00536     case LIBHAL_DRIVE_TYPE_CDROM:
00537     case LIBHAL_DRIVE_TYPE_FLOPPY:
00538         name = libhal_storage_policy_lookup_icon (policy, 0x20000 + drive_type*0x100 + bus);
00539         break;
00540 
00541     default:
00542         name = libhal_storage_policy_lookup_icon (policy, 0x20000 + drive_type*0x100);
00543     }
00544 out:
00545     if (name != NULL)
00546         return strdup (name);
00547     else
00548         return NULL;
00549 }
00550 
00567 dbus_bool_t
00568 libhal_volume_policy_should_be_visible (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy, 
00569                      const char *target_mount_point)
00570 {
00571     unsigned int i;
00572     dbus_bool_t is_visible;
00573     const char *label;
00574     const char *mount_point;
00575     const char *fstype;
00576     const char *fhs23_toplevel_mount_points[] = {
00577         "/",
00578         "/bin",
00579         "/boot",
00580         "/dev",
00581         "/etc",
00582         "/home",
00583         "/lib",
00584         "/lib64",
00585         "/media",
00586         "/mnt",
00587         "/opt",
00588         "/root",
00589         "/sbin",
00590         "/srv",
00591         "/tmp",
00592         "/usr",
00593         "/var",
00594         "/proc",
00595         "/sbin",
00596         NULL
00597     };
00598 
00599     is_visible = FALSE;
00600 
00601     /* skip if hal says it's not used as a filesystem */
00602     if (libhal_volume_get_fsusage (volume) != LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM)
00603         goto out;
00604 
00605     label = libhal_volume_get_label (volume);
00606     mount_point = libhal_volume_get_mount_point (volume);
00607     fstype = libhal_volume_get_fstype (volume);
00608 
00609     /* use target mount point if we're not mounted yet */
00610     if (mount_point == NULL)
00611         mount_point = target_mount_point;
00612 
00613     /* bail out if we don't know the filesystem */
00614     if (fstype == NULL)
00615         goto out;
00616 
00617     /* blacklist fhs2.3 top level mount points */
00618     if (mount_point != NULL) {
00619         for (i = 0; fhs23_toplevel_mount_points[i] != NULL; i++) {
00620             if (strcmp (mount_point, fhs23_toplevel_mount_points[i]) == 0)
00621                 goto out;
00622         }
00623     }
00624 
00625     /* blacklist partitions with name 'bootstrap' of type HFS (Apple uses that) */
00626     if (label != NULL && strcmp (label, "bootstrap") == 0 && strcmp (fstype, "hfs") == 0)
00627         goto out;
00628 
00629     /* only the real lucky mount points will make it this far :-) */
00630     is_visible = TRUE;
00631 
00632 out:
00633     return is_visible;
00634 }
00635 
00636 /*************************************************************************/
00637 
00638 #define MOUNT_OPTIONS_SIZE 256
00639 
00640 struct LibHalDrive_s {
00641     char *udi;
00642 
00643     int device_major;
00644     int device_minor;
00645     char *device_file;
00646 
00647     LibHalDriveBus bus;
00648     char *vendor;             /* may be "", is never NULL */
00649     char *model;              /* may be "", is never NULL */
00650     dbus_bool_t is_hotpluggable;
00651     dbus_bool_t is_removable;
00652     dbus_bool_t requires_eject;
00653 
00654     LibHalDriveType type;
00655     char *type_textual;
00656 
00657     char *physical_device;  /* UDI of physical device, e.g. the 
00658                  * IDE, USB, IEEE1394 device */
00659 
00660     char *dedicated_icon_drive;
00661     char *dedicated_icon_volume;
00662 
00663     char *serial;
00664     char *firmware_version;
00665     LibHalDriveCdromCaps cdrom_caps;
00666 
00667     char *desired_mount_point;
00668     char *mount_filesystem;
00669     dbus_bool_t should_mount;
00670 
00671     dbus_bool_t no_partitions_hint;
00672 
00673     LibHalContext *hal_ctx;
00674 
00675     char **capabilities;
00676 
00677     char mount_options[MOUNT_OPTIONS_SIZE];
00678 };
00679 
00680 struct LibHalVolume_s {
00681     char *udi;
00682 
00683     int device_major;
00684     int device_minor;
00685     char *device_file;
00686     char *volume_label; /* may be NULL, is never "" */
00687     dbus_bool_t is_mounted;
00688     char *mount_point;  /* NULL iff !is_mounted */
00689     char *fstype;       /* NULL iff !is_mounted or unknown */
00690     char *fsversion;
00691     char *uuid;
00692     char *storage_device;
00693 
00694     LibHalVolumeUsage fsusage;
00695 
00696     dbus_bool_t is_partition;
00697     unsigned int partition_number;
00698 
00699     int msdos_part_table_type;
00700     
00701 
00702     dbus_bool_t is_disc;
00703     LibHalVolumeDiscType disc_type;
00704     dbus_bool_t disc_has_audio;
00705     dbus_bool_t disc_has_data;
00706     dbus_bool_t disc_is_appendable;
00707     dbus_bool_t disc_is_blank;
00708     dbus_bool_t disc_is_rewritable;
00709 
00710     unsigned int block_size;
00711     unsigned int num_blocks;
00712 
00713     char *desired_mount_point;
00714     char *mount_filesystem;
00715     dbus_bool_t should_mount;
00716 
00717     dbus_bool_t ignore_volume;
00718 
00719 
00720     char mount_options[MOUNT_OPTIONS_SIZE];
00721 };
00722 
00723 const char *
00724 libhal_drive_get_dedicated_icon_drive (LibHalDrive *drive)
00725 {
00726     return drive->dedicated_icon_drive;
00727 }
00728 
00729 const char *
00730 libhal_drive_get_dedicated_icon_volume (LibHalDrive *drive)
00731 {
00732     return drive->dedicated_icon_volume;
00733 }
00734 
00739 void
00740 libhal_drive_free (LibHalDrive *drive)
00741 {
00742     if (drive == NULL )
00743         return;
00744 
00745     free (drive->udi);
00746     libhal_free_string (drive->device_file);
00747     libhal_free_string (drive->vendor);
00748     libhal_free_string (drive->model);
00749     libhal_free_string (drive->type_textual);
00750     libhal_free_string (drive->physical_device);
00751     libhal_free_string (drive->serial);
00752     libhal_free_string (drive->firmware_version);
00753     libhal_free_string (drive->desired_mount_point);
00754     libhal_free_string (drive->mount_filesystem);
00755     libhal_free_string_array (drive->capabilities);
00756 }
00757 
00758 
00763 void
00764 libhal_volume_free (LibHalVolume *vol)
00765 {
00766     if (vol == NULL )
00767         return;
00768 
00769     free (vol->udi);
00770     libhal_free_string (vol->device_file);
00771     libhal_free_string (vol->volume_label);
00772     libhal_free_string (vol->fstype);
00773     libhal_free_string (vol->mount_point);
00774     libhal_free_string (vol->fsversion);
00775     libhal_free_string (vol->uuid);
00776     libhal_free_string (vol->desired_mount_point);
00777     libhal_free_string (vol->mount_filesystem);
00778 }
00779 
00780 
00781 static char **
00782 my_strvdup (char **strv)
00783 {
00784     unsigned int num_elems;
00785     unsigned int i;
00786     char **res;
00787 
00788     for (num_elems = 0; strv[num_elems] != NULL; num_elems++)
00789         ;
00790 
00791     res = calloc (num_elems + 1, sizeof (char*));
00792     if (res == NULL)
00793         goto out;
00794 
00795     for (i = 0; i < num_elems; i++)
00796         res[i] = strdup (strv[i]);
00797     res[i] = NULL;
00798 
00799 out:
00800     return res;
00801 }
00802 
00803 /* ok, hey, so this is a bit ugly */
00804 
00805 #define LIBHAL_PROP_EXTRACT_BEGIN if (FALSE)
00806 #define LIBHAL_PROP_EXTRACT_END ;
00807 #define LIBHAL_PROP_EXTRACT_INT(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_INT32) _where_ = libhal_psi_get_int (&it)
00808 #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
00809 #define LIBHAL_PROP_EXTRACT_BOOL(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_BOOLEAN) _where_ = libhal_psi_get_bool (&it)
00810 #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
00811 #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))
00812 
00821 LibHalDrive *
00822 libhal_drive_from_udi (LibHalContext *hal_ctx, const char *udi)
00823 {   
00824     char *bus_textual;
00825     LibHalDrive *drive;
00826     LibHalPropertySet *properties;
00827     LibHalPropertySetIterator it;
00828     DBusError error;
00829     unsigned int i;
00830 
00831     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
00832 
00833     drive = NULL;
00834     properties = NULL;
00835     bus_textual = NULL;
00836 
00837     dbus_error_init (&error);
00838     if (!libhal_device_query_capability (hal_ctx, udi, "storage", &error))
00839         goto error;
00840 
00841     drive = malloc (sizeof (LibHalDrive));
00842     if (drive == NULL)
00843         goto error;
00844     memset (drive, 0x00, sizeof (LibHalDrive));
00845 
00846     drive->hal_ctx = hal_ctx;
00847 
00848     drive->udi = strdup (udi);
00849     if (drive->udi == NULL)
00850         goto error;
00851 
00852     properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
00853     if (properties == NULL)
00854         goto error;
00855 
00856     /* we can count on hal to give us all these properties */
00857     for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
00858         int type;
00859         char *key;
00860         
00861         type = libhal_psi_get_type (&it);
00862         key = libhal_psi_get_key (&it);
00863 
00864         LIBHAL_PROP_EXTRACT_BEGIN;
00865 
00866         LIBHAL_PROP_EXTRACT_INT    ("block.minor",               drive->device_minor);
00867         LIBHAL_PROP_EXTRACT_INT    ("block.major",               drive->device_major);
00868         LIBHAL_PROP_EXTRACT_STRING ("block.device",              drive->device_file);
00869         LIBHAL_PROP_EXTRACT_STRING ("storage.bus",               bus_textual);
00870         LIBHAL_PROP_EXTRACT_STRING ("storage.vendor",            drive->vendor);
00871         LIBHAL_PROP_EXTRACT_STRING ("storage.model",             drive->model);
00872         LIBHAL_PROP_EXTRACT_STRING ("storage.drive_type",        drive->type_textual);
00873 
00874 
00875         LIBHAL_PROP_EXTRACT_STRING ("storage.icon.drive",        drive->dedicated_icon_drive);
00876         LIBHAL_PROP_EXTRACT_STRING ("storage.icon.volume",       drive->dedicated_icon_volume);
00877 
00878         LIBHAL_PROP_EXTRACT_BOOL   ("storage.hotpluggable",      drive->is_hotpluggable);
00879         LIBHAL_PROP_EXTRACT_BOOL   ("storage.removable",         drive->is_removable);
00880         LIBHAL_PROP_EXTRACT_BOOL   ("storage.requires_eject",    drive->requires_eject);
00881 
00882         LIBHAL_PROP_EXTRACT_STRING ("storage.physical_device",   drive->physical_device);
00883         LIBHAL_PROP_EXTRACT_STRING ("storage.firmware_version",  drive->firmware_version);
00884         LIBHAL_PROP_EXTRACT_STRING ("storage.serial",            drive->serial);
00885 
00886         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.cdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_CDR);
00887         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.cdrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_CDRW);
00888         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvd", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDROM);
00889         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR);
00890         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW);
00891         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrwdl", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRWDL);
00892         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrdl", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL);
00893         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDR);
00894         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDRW);
00895         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdram", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDRAM);
00896 
00897         LIBHAL_PROP_EXTRACT_BOOL   ("storage.policy.should_mount",        drive->should_mount);
00898         LIBHAL_PROP_EXTRACT_STRING ("storage.policy.desired_mount_point", drive->desired_mount_point);
00899         LIBHAL_PROP_EXTRACT_STRING ("storage.policy.mount_filesystem",    drive->mount_filesystem);
00900 
00901         LIBHAL_PROP_EXTRACT_BOOL   ("storage.no_partitions_hint",        drive->no_partitions_hint);
00902 
00903         LIBHAL_PROP_EXTRACT_STRLIST ("info.capabilities",                drive->capabilities);
00904 
00905         LIBHAL_PROP_EXTRACT_END;
00906     }
00907 
00908     if (drive->type_textual != NULL) {
00909         if (strcmp (drive->type_textual, "cdrom") == 0) {
00910             drive->cdrom_caps |= LIBHAL_DRIVE_CDROM_CAPS_CDROM;
00911             drive->type = LIBHAL_DRIVE_TYPE_CDROM;
00912         } else if (strcmp (drive->type_textual, "floppy") == 0) {
00913             drive->type = LIBHAL_DRIVE_TYPE_FLOPPY;
00914         } else if (strcmp (drive->type_textual, "disk") == 0) {
00915             if (drive->is_removable)
00916                 drive->type = LIBHAL_DRIVE_TYPE_REMOVABLE_DISK;
00917             else
00918                 drive->type = LIBHAL_DRIVE_TYPE_DISK;               
00919         } else if (strcmp (drive->type_textual, "tape") == 0) {
00920             drive->type = LIBHAL_DRIVE_TYPE_TAPE;
00921         } else if (strcmp (drive->type_textual, "compact_flash") == 0) {
00922             drive->type = LIBHAL_DRIVE_TYPE_COMPACT_FLASH;
00923         } else if (strcmp (drive->type_textual, "memory_stick") == 0) {
00924             drive->type = LIBHAL_DRIVE_TYPE_MEMORY_STICK;
00925         } else if (strcmp (drive->type_textual, "smart_media") == 0) {
00926             drive->type = LIBHAL_DRIVE_TYPE_SMART_MEDIA;
00927         } else if (strcmp (drive->type_textual, "sd_mmc") == 0) {
00928             drive->type = LIBHAL_DRIVE_TYPE_SD_MMC;
00929         } else if (strcmp (drive->type_textual, "zip") == 0) {
00930             drive->type = LIBHAL_DRIVE_TYPE_ZIP;
00931         } else if (strcmp (drive->type_textual, "jaz") == 0) {
00932             drive->type = LIBHAL_DRIVE_TYPE_JAZ;
00933         } else if (strcmp (drive->type_textual, "flashkey") == 0) {
00934             drive->type = LIBHAL_DRIVE_TYPE_FLASHKEY;
00935         } else {
00936                 drive->type = LIBHAL_DRIVE_TYPE_DISK; 
00937         }
00938 
00939     }
00940 
00941     if (drive->capabilities != NULL) {
00942         for (i = 0; drive->capabilities[i] != NULL; i++) {
00943             if (strcmp (drive->capabilities[i], "portable_audio_player") == 0) {
00944                 drive->type = LIBHAL_DRIVE_TYPE_PORTABLE_AUDIO_PLAYER;
00945                 break;
00946             } else if (strcmp (drive->capabilities[i], "camera") == 0) {
00947                 drive->type = LIBHAL_DRIVE_TYPE_CAMERA;
00948                 break;
00949             }
00950         }
00951     }
00952 
00953     if (bus_textual != NULL) {
00954         if (strcmp (bus_textual, "usb") == 0) {
00955             drive->bus = LIBHAL_DRIVE_BUS_USB;
00956         } else if (strcmp (bus_textual, "ieee1394") == 0) {
00957             drive->bus = LIBHAL_DRIVE_BUS_IEEE1394;
00958         } else if (strcmp (bus_textual, "ide") == 0) {
00959             drive->bus = LIBHAL_DRIVE_BUS_IDE;
00960         } else if (strcmp (bus_textual, "scsi") == 0) {
00961             drive->bus = LIBHAL_DRIVE_BUS_SCSI;
00962         } else if (strcmp (bus_textual, "ccw") == 0) {
00963             drive->bus = LIBHAL_DRIVE_BUS_CCW;
00964         }
00965     }
00966 
00967     libhal_free_string (bus_textual);
00968     libhal_free_property_set (properties);
00969 
00970     return drive;
00971 
00972 error:
00973     libhal_free_string (bus_textual);
00974     libhal_free_property_set (properties);
00975     libhal_drive_free (drive);
00976     return NULL;
00977 }
00978 
00979 const char *
00980 libhal_volume_get_storage_device_udi (LibHalVolume *volume)
00981 {
00982     return volume->storage_device;
00983 }
00984 
00985 const char *libhal_drive_get_physical_device_udi (LibHalDrive *drive)
00986 {
00987     return drive->physical_device;
00988 }
00989 
00990 dbus_bool_t
00991 libhal_drive_requires_eject (LibHalDrive *drive)
00992 {
00993     return drive->requires_eject;
00994 }
00995 
01004 LibHalVolume *
01005 libhal_volume_from_udi (LibHalContext *hal_ctx, const char *udi)
01006 {
01007     char *disc_type_textual;
01008     char *vol_fsusage_textual;
01009     LibHalVolume *vol;
01010     LibHalPropertySet *properties;
01011     LibHalPropertySetIterator it;
01012     DBusError error;
01013 
01014     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01015 
01016     vol = NULL;
01017     properties = NULL;
01018     disc_type_textual = NULL;
01019     vol_fsusage_textual = NULL;
01020 
01021     dbus_error_init (&error);
01022     if (!libhal_device_query_capability (hal_ctx, udi, "volume", &error))
01023         goto error;
01024 
01025     vol = malloc (sizeof (LibHalVolume));
01026     if (vol == NULL)
01027         goto error;
01028     memset (vol, 0x00, sizeof (LibHalVolume));
01029 
01030     vol->udi = strdup (udi);
01031 
01032     properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
01033     if (properties == NULL)
01034         goto error;
01035 
01036     /* we can count on hal to give us all these properties */
01037     for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
01038         int type;
01039         char *key;
01040         
01041         type = libhal_psi_get_type (&it);
01042         key = libhal_psi_get_key (&it);
01043 
01044         LIBHAL_PROP_EXTRACT_BEGIN;
01045 
01046         LIBHAL_PROP_EXTRACT_INT    ("volume.partition.msdos_part_table_type", vol->msdos_part_table_type);
01047 
01048         LIBHAL_PROP_EXTRACT_INT    ("block.minor",               vol->device_minor);
01049         LIBHAL_PROP_EXTRACT_INT    ("block.major",               vol->device_major);
01050         LIBHAL_PROP_EXTRACT_STRING ("block.device",              vol->device_file);
01051 
01052         LIBHAL_PROP_EXTRACT_STRING ("block.storage_device",      vol->storage_device);
01053 
01054         LIBHAL_PROP_EXTRACT_INT    ("volume.block_size",         vol->block_size);
01055         LIBHAL_PROP_EXTRACT_INT    ("volume.num_blocks",         vol->num_blocks);
01056         LIBHAL_PROP_EXTRACT_STRING ("volume.label",              vol->volume_label);
01057         LIBHAL_PROP_EXTRACT_STRING ("volume.mount_point",        vol->mount_point);
01058         LIBHAL_PROP_EXTRACT_STRING ("volume.fstype",             vol->fstype);
01059         LIBHAL_PROP_EXTRACT_BOOL   ("volume.is_mounted",         vol->is_mounted);
01060         LIBHAL_PROP_EXTRACT_STRING ("volume.fsusage",            vol_fsusage_textual);
01061 
01062         LIBHAL_PROP_EXTRACT_BOOL   ("volume.ignore",             vol->ignore_volume);
01063 
01064         LIBHAL_PROP_EXTRACT_BOOL   ("volume.is_disc",            vol->is_disc);
01065         LIBHAL_PROP_EXTRACT_STRING ("volume.disc.type",          disc_type_textual);
01066         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.has_audio",     vol->disc_has_audio);
01067         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.has_data",      vol->disc_has_data);
01068         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.is_appendable", vol->disc_is_appendable);
01069         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.is_blank",      vol->disc_is_blank);
01070         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.is_rewritable", vol->disc_is_rewritable);
01071 
01072         LIBHAL_PROP_EXTRACT_BOOL   ("volume.policy.should_mount",        vol->should_mount);
01073         LIBHAL_PROP_EXTRACT_STRING ("volume.policy.desired_mount_point", vol->desired_mount_point);
01074         LIBHAL_PROP_EXTRACT_STRING ("volume.policy.mount_filesystem",    vol->mount_filesystem);
01075 
01076         LIBHAL_PROP_EXTRACT_END;
01077     }
01078 
01079     if (disc_type_textual != NULL) {
01080         if (strcmp (disc_type_textual, "cd_rom") == 0) {
01081             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDROM;
01082         } else if (strcmp (disc_type_textual, "cd_r") == 0) {
01083             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDR;
01084         } else if (strcmp (disc_type_textual, "cd_rw") == 0) {
01085             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDRW;
01086         } else if (strcmp (disc_type_textual, "dvd_rom") == 0) {
01087             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDROM;
01088         } else if (strcmp (disc_type_textual, "dvd_ram") == 0) {
01089             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDRAM;
01090         } else if (strcmp (disc_type_textual, "dvd_r") == 0) {
01091             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDR;
01092         } else if (strcmp (disc_type_textual, "dvd_rw") == 0) {
01093             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDRW;
01094         } else if (strcmp (disc_type_textual, "dvd_plus_r") == 0) {
01095             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR;
01096         } else if (strcmp (disc_type_textual, "dvd_plus_rw") == 0) {
01097             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSRW;
01098         } else if (strcmp (disc_type_textual, "dvd_plus_r_dl") == 0) {
01099             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR_DL;
01100         }
01101     }
01102 
01103     vol->fsusage = LIBHAL_VOLUME_USAGE_UNKNOWN;
01104     if (vol_fsusage_textual != NULL) {
01105         if (strcmp (vol_fsusage_textual, "filesystem") == 0) {
01106             vol->fsusage = LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM;
01107         } else if (strcmp (vol_fsusage_textual, "partitiontable") == 0) {
01108             vol->fsusage = LIBHAL_VOLUME_USAGE_PARTITION_TABLE;
01109         } else if (strcmp (vol_fsusage_textual, "raid") == 0) {
01110             vol->fsusage = LIBHAL_VOLUME_USAGE_RAID_MEMBER;
01111         } else if (strcmp (vol_fsusage_textual, "crypto") == 0) {
01112             vol->fsusage = LIBHAL_VOLUME_USAGE_CRYPTO;
01113         } else {
01114             vol->fsusage = LIBHAL_VOLUME_USAGE_UNKNOWN;
01115         } 
01116     }
01117 
01118     libhal_free_string (vol_fsusage_textual);
01119     libhal_free_string (disc_type_textual);
01120     libhal_free_property_set (properties);
01121     return vol;
01122 error:
01123     libhal_free_string (vol_fsusage_textual);
01124     libhal_free_string (disc_type_textual);
01125     libhal_free_property_set (properties);
01126     libhal_volume_free (vol);
01127     return NULL;
01128 }
01129 
01130 
01139 int
01140 libhal_volume_get_msdos_part_table_type (LibHalVolume *volume)
01141 {
01142     return volume->msdos_part_table_type;
01143 }
01144 
01145 /***********************************************************************/
01146 
01154 LibHalDrive *
01155 libhal_drive_from_device_file (LibHalContext *hal_ctx, const char *device_file)
01156 {
01157     int i;
01158     char **hal_udis;
01159     int num_hal_udis;
01160     LibHalDrive *result;
01161     char *found_udi;
01162     DBusError error;
01163 
01164     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01165 
01166     result = NULL;
01167     found_udi = NULL;
01168 
01169     dbus_error_init (&error);
01170     if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "block.device", 
01171                                  device_file, &num_hal_udis, &error)) == NULL)
01172         goto out;
01173 
01174     for (i = 0; i < num_hal_udis; i++) {
01175         char *udi;
01176         char *storage_udi;
01177         DBusError err1;
01178         DBusError err2;
01179         udi = hal_udis[i];
01180 
01181         dbus_error_init (&err1);
01182         dbus_error_init (&err2);
01183         if (libhal_device_query_capability (hal_ctx, udi, "volume", &err1)) {
01184 
01185             storage_udi = libhal_device_get_property_string (hal_ctx, udi, "block.storage_device", &err1);
01186             if (storage_udi == NULL)
01187                 continue;
01188             found_udi = strdup (storage_udi);
01189             libhal_free_string (storage_udi);
01190             break;
01191         } else if (libhal_device_query_capability (hal_ctx, udi, "storage", &err2)) {
01192             found_udi = strdup (udi);
01193         }
01194     }
01195 
01196     libhal_free_string_array (hal_udis);
01197 
01198     if (found_udi != NULL)
01199         result = libhal_drive_from_udi (hal_ctx, found_udi);
01200 
01201     free (found_udi);
01202 out:
01203     return result;
01204 }
01205 
01206 
01213 LibHalVolume *
01214 libhal_volume_from_device_file (LibHalContext *hal_ctx, const char *device_file)
01215 {
01216     int i;
01217     char **hal_udis;
01218     int num_hal_udis;
01219     LibHalVolume *result;
01220     char *found_udi;
01221     DBusError error;
01222 
01223     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01224 
01225     result = NULL;
01226     found_udi = NULL;
01227 
01228     dbus_error_init (&error);
01229     if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "block.device", 
01230                                  device_file, &num_hal_udis, &error)) == NULL)
01231         goto out;
01232 
01233     for (i = 0; i < num_hal_udis; i++) {
01234         char *udi;
01235         udi = hal_udis[i];
01236         if (libhal_device_query_capability (hal_ctx, udi, "volume", &error)) {
01237             found_udi = strdup (udi);
01238             break;
01239         }
01240     }
01241 
01242     libhal_free_string_array (hal_udis);
01243 
01244     if (found_udi != NULL)
01245         result = libhal_volume_from_udi (hal_ctx, found_udi);
01246 
01247     free (found_udi);
01248 out:
01249     return result;
01250 }
01251 
01252 dbus_uint64_t
01253 libhal_volume_get_size (LibHalVolume *volume)
01254 {
01255     return ((dbus_uint64_t)volume->block_size) * ((dbus_uint64_t)volume->num_blocks);
01256 }
01257 
01258 
01259 dbus_bool_t
01260 libhal_drive_is_hotpluggable (LibHalDrive *drive)
01261 {
01262     return drive->is_hotpluggable;
01263 }
01264 
01265 dbus_bool_t
01266 libhal_drive_uses_removable_media (LibHalDrive *drive)
01267 {
01268     return drive->is_removable;
01269 }
01270 
01271 LibHalDriveType
01272 libhal_drive_get_type (LibHalDrive *drive)
01273 {
01274     return drive->type;
01275 }
01276 
01277 LibHalDriveBus
01278 libhal_drive_get_bus (LibHalDrive *drive)
01279 {
01280     return drive->bus;
01281 }
01282 
01283 LibHalDriveCdromCaps
01284 libhal_drive_get_cdrom_caps (LibHalDrive *drive)
01285 {
01286     return drive->cdrom_caps;
01287 }
01288 
01289 unsigned int
01290 libhal_drive_get_device_major (LibHalDrive *drive)
01291 {
01292     return drive->device_major;
01293 }
01294 
01295 unsigned int
01296 libhal_drive_get_device_minor (LibHalDrive *drive)
01297 {
01298     return drive->device_minor;
01299 }
01300 
01301 const char *
01302 libhal_drive_get_type_textual (LibHalDrive *drive)
01303 {
01304     return drive->type_textual;
01305 }
01306 
01307 const char *
01308 libhal_drive_get_device_file (LibHalDrive *drive)
01309 {
01310     return drive->device_file;
01311 }
01312 
01313 const char *
01314 libhal_drive_get_udi (LibHalDrive *drive)
01315 {
01316     return drive->udi;
01317 }
01318 
01319 const char *
01320 libhal_drive_get_serial (LibHalDrive *drive)
01321 {
01322     return drive->serial;
01323 }
01324 
01325 const char *
01326 libhal_drive_get_firmware_version (LibHalDrive *drive)
01327 {
01328     return drive->firmware_version;
01329 }
01330 
01331 const char *
01332 libhal_drive_get_model (LibHalDrive *drive)
01333 {
01334     return drive->model;
01335 }
01336 
01337 const char *
01338 libhal_drive_get_vendor (LibHalDrive *drive)
01339 {
01340     return drive->vendor;
01341 }
01342 
01343 /*****************************************************************************/
01344 
01345 const char *
01346 libhal_volume_get_udi (LibHalVolume *volume)
01347 {
01348     return volume->udi;
01349 }
01350 
01351 const char *
01352 libhal_volume_get_device_file (LibHalVolume *volume)
01353 {
01354     return volume->device_file;
01355 }
01356 
01357 unsigned int libhal_volume_get_device_major (LibHalVolume *volume)
01358 {
01359     return volume->device_major;
01360 }
01361 
01362 unsigned int libhal_volume_get_device_minor (LibHalVolume *volume)
01363 {
01364     return volume->device_minor;
01365 }
01366 
01367 const char *
01368 libhal_volume_get_fstype (LibHalVolume *volume)
01369 {
01370     return volume->fstype;
01371 }
01372 
01373 const char *
01374 libhal_volume_get_fsversion (LibHalVolume *volume)
01375 {
01376     return volume->fsversion;
01377 }
01378 
01379 LibHalVolumeUsage 
01380 libhal_volume_get_fsusage (LibHalVolume *volume)
01381 {
01382     return volume->fsusage;
01383 }
01384 
01385 dbus_bool_t 
01386 libhal_volume_is_mounted (LibHalVolume *volume)
01387 {
01388     return volume->is_mounted;
01389 }
01390 
01391 dbus_bool_t 
01392 libhal_volume_is_partition (LibHalVolume *volume)
01393 {
01394     return volume->is_partition;
01395 }
01396 
01397 dbus_bool_t
01398 libhal_volume_is_disc (LibHalVolume *volume)
01399 {
01400     return volume->is_disc;
01401 }
01402 
01403 unsigned int
01404 libhal_volume_get_partition_number (LibHalVolume *volume)
01405 {
01406     return volume->partition_number;
01407 }
01408 
01409 const char *
01410 libhal_volume_get_label (LibHalVolume *volume)
01411 {
01412     return volume->volume_label;
01413 }
01414 
01415 const char *
01416 libhal_volume_get_mount_point (LibHalVolume *volume)
01417 {
01418     return volume->mount_point;
01419 }
01420 
01421 const char *
01422 libhal_volume_get_uuid (LibHalVolume *volume)
01423 {
01424     return volume->uuid;
01425 }
01426 
01427 dbus_bool_t
01428 libhal_volume_disc_has_audio (LibHalVolume *volume)
01429 {
01430     return volume->disc_has_audio;
01431 }
01432 
01433 dbus_bool_t
01434 libhal_volume_disc_has_data (LibHalVolume *volume)
01435 {
01436     return volume->disc_has_data;
01437 }
01438 
01439 dbus_bool_t
01440 libhal_volume_disc_is_blank (LibHalVolume *volume)
01441 {
01442     return volume->disc_is_blank;
01443 }
01444 
01445 dbus_bool_t
01446 libhal_volume_disc_is_rewritable (LibHalVolume *volume)
01447 {
01448     return volume->disc_is_rewritable;
01449 }
01450 
01451 dbus_bool_t
01452 libhal_volume_disc_is_appendable (LibHalVolume *volume)
01453 {
01454     return volume->disc_is_appendable;
01455 }
01456 
01457 LibHalVolumeDiscType
01458 libhal_volume_get_disc_type (LibHalVolume *volume)
01459 {
01460     return volume->disc_type;
01461 }
01462 
01463 dbus_bool_t
01464 libhal_volume_should_ignore (LibHalVolume     *volume)
01465 {
01466     return volume->ignore_volume;
01467 }
01468 
01469 char ** 
01470 libhal_drive_find_all_volumes (LibHalContext *hal_ctx, LibHalDrive *drive, int *num_volumes)
01471 {
01472     int i;
01473     char **udis;
01474     int num_udis;
01475     const char *drive_udi;
01476     char **result;
01477     DBusError error;
01478 
01479     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01480 
01481     udis = NULL;
01482     result = NULL;
01483     *num_volumes = 0;
01484 
01485     drive_udi = libhal_drive_get_udi (drive);
01486     if (drive_udi == NULL)
01487         goto out;
01488 
01489     /* get initial list... */
01490     dbus_error_init (&error);
01491     if ((udis = libhal_manager_find_device_string_match (hal_ctx, "block.storage_device", 
01492                                  drive_udi, &num_udis, &error)) == NULL)
01493         goto out;
01494 
01495     result = malloc (sizeof (char *) * num_udis);
01496     if (result == NULL)
01497         goto out;
01498 
01499     /* ...and filter out the single UDI that is the drive itself */
01500     for (i = 0; i < num_udis; i++) {
01501         if (strcmp (udis[i], drive_udi) == 0)
01502             continue;
01503         result[*num_volumes] = strdup (udis[i]);
01504         *num_volumes = (*num_volumes) + 1;
01505     }
01506     /* set last element (above removed UDI) to NULL for libhal_free_string_array()*/
01507     result[*num_volumes] = NULL;
01508 
01509 out:
01510     libhal_free_string_array (udis);
01511     return result;
01512 }
01513 
01514 /*************************************************************************/
01515 
01516 char *
01517 libhal_drive_policy_default_get_mount_root (LibHalContext *hal_ctx)
01518 {
01519     DBusError error;
01520 
01521     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01522 
01523     dbus_error_init (&error);
01524     return libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01525                           "storage.policy.default.mount_root", &error);
01526 }
01527 
01528 dbus_bool_t
01529 libhal_drive_policy_default_use_managed_keyword (LibHalContext *hal_ctx)
01530 {
01531     DBusError error;
01532 
01533     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, FALSE);
01534 
01535     dbus_error_init (&error);
01536     return libhal_device_get_property_bool (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01537                         "storage.policy.default.use_managed_keyword", &error);
01538 }
01539 
01540 char *
01541 libhal_drive_policy_default_get_managed_keyword_primary (LibHalContext *hal_ctx)
01542 {
01543     DBusError error;
01544 
01545     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01546 
01547     dbus_error_init (&error);
01548     return libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01549                           "storage.policy.default.managed_keyword.primary", &error);
01550 }
01551 
01552 char *
01553 libhal_drive_policy_default_get_managed_keyword_secondary (LibHalContext *hal_ctx)
01554 {
01555     DBusError error;
01556 
01557     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01558 
01559     dbus_error_init (&error);
01560     return libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01561                           "storage.policy.default.managed_keyword.secondary", &error);
01562 }
01563 
01564 /*************************************************************************/
01565 
01566 dbus_bool_t
01567 libhal_drive_policy_is_mountable (LibHalDrive *drive, LibHalStoragePolicy *policy)
01568 {
01569     printf ("should_mount=%d, no_partitions_hint=%d\n", drive->should_mount, drive->no_partitions_hint);
01570 
01571     return drive->should_mount && drive->no_partitions_hint;
01572 }
01573 
01574 const char *
01575 libhal_drive_policy_get_desired_mount_point (LibHalDrive *drive, LibHalStoragePolicy *policy)
01576 {
01577     return drive->desired_mount_point;
01578 }
01579 
01580 /* safely strcat() at most the remaining space in 'dst' */
01581 #define strcat_len(dst, src, dstmaxlen) do {    \
01582     dst[dstmaxlen - 1] = '\0'; \
01583     strncat (dst, src, dstmaxlen - strlen (dst) - 1); \
01584 } while(0)
01585 
01586 
01587 static void
01588 mopts_collect (LibHalContext *hal_ctx, const char *namespace, int namespace_len, 
01589            const char *udi, char *options_string, size_t options_max_len, dbus_bool_t only_collect_imply_opts)
01590 {
01591     LibHalPropertySet *properties;
01592     LibHalPropertySetIterator it;
01593     DBusError error;
01594 
01595     if(hal_ctx == 0) {
01596         fprintf (stderr,"%s %d : LibHalContext *ctx is NULL\n",__FILE__, __LINE__);
01597         return;
01598     }
01599 
01600     dbus_error_init (&error);
01601 
01602     /* first collect from root computer device */
01603     properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
01604     if (properties == NULL)
01605         goto error;
01606     for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
01607         int type;
01608         char *key;
01609         
01610         type = libhal_psi_get_type (&it);
01611         key = libhal_psi_get_key (&it);
01612         if (libhal_psi_get_type (&it) == LIBHAL_PROPERTY_TYPE_BOOLEAN &&
01613             strncmp (key, namespace, namespace_len - 1) == 0) {
01614             const char *option = key + namespace_len - 1;
01615             char *location;
01616             dbus_bool_t is_imply_opt;
01617 
01618             is_imply_opt = FALSE;
01619             if (strcmp (option, "user") == 0 ||
01620                 strcmp (option, "users") == 0 ||
01621                 strcmp (option, "defaults") == 0 ||
01622                 strcmp (option, "pamconsole") == 0)
01623                 is_imply_opt = TRUE;
01624 
01625             
01626             if (only_collect_imply_opts) {
01627                 if (!is_imply_opt)
01628                     continue;
01629             } else {
01630                 if (is_imply_opt)
01631                     continue;
01632             }
01633 
01634             if (libhal_psi_get_bool (&it)) {
01635                 /* see if option is already there */
01636                 location = strstr (options_string, option);
01637                 if (location == NULL) {
01638                     if (strlen (options_string) > 0)
01639                         strcat_len (options_string, ",", options_max_len);
01640                     strcat_len (options_string, option, options_max_len);
01641                 }
01642             } else {
01643                 /* remove option if already there */
01644                 location = strstr (options_string, option);
01645                 if (location != NULL) {
01646                     char *end;
01647 
01648                     end = strchr (location, ',');
01649                     if (end == NULL) {
01650                         location[0] = '\0';
01651                     } else {
01652                         strcpy (location, end + 1); /* skip the extra comma */
01653                     }
01654                 }
01655 
01656             }
01657         }
01658     }
01659 error:
01660     libhal_free_property_set (properties);
01661 }
01662 
01663 
01664 const char *
01665 libhal_drive_policy_get_mount_options (LibHalDrive *drive, LibHalStoragePolicy *policy)
01666 {
01667     const char *result;
01668     char stor_mount_option_default_begin[] = "storage.policy.default.mount_option.";
01669     char stor_mount_option_begin[] = "storage.policy.mount_option.";
01670 
01671     result = NULL;
01672     drive->mount_options[0] = '\0';
01673 
01674     /* collect options != ('pamconsole', 'user', 'users', 'defaults' options that imply other options)  */
01675     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01676                "/org/freedesktop/Hal/devices/computer", drive->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01677     mopts_collect (drive->hal_ctx, stor_mount_option_begin, sizeof (stor_mount_option_begin),
01678                drive->udi, drive->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01679     /* ensure ('pamconsole', 'user', 'users', 'defaults' options that imply other options), are first */
01680     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01681                "/org/freedesktop/Hal/devices/computer", drive->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01682     mopts_collect (drive->hal_ctx, stor_mount_option_begin, sizeof (stor_mount_option_begin),
01683                drive->udi, drive->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01684 
01685     result = drive->mount_options;
01686 
01687     return result;
01688 }
01689 
01690 const char *
01691 libhal_drive_policy_get_mount_fs (LibHalDrive *drive, LibHalStoragePolicy *policy)
01692 {
01693     return drive->mount_filesystem;
01694 }
01695 
01696 
01697 dbus_bool_t
01698 libhal_volume_policy_is_mountable (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01699 {
01700     return drive->should_mount && volume->should_mount;
01701 }
01702 
01703 const char *libhal_volume_policy_get_desired_mount_point (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01704 {
01705     return volume->desired_mount_point;
01706 }
01707 
01708 const char *libhal_volume_policy_get_mount_options (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01709 {
01710     const char *result;
01711     char stor_mount_option_default_begin[] = "storage.policy.default.mount_option.";
01712     char vol_mount_option_begin[] = "volume.policy.mount_option.";
01713 
01714     result = NULL;
01715     volume->mount_options[0] = '\0';
01716 
01717     /* ensure ('pamconsole', 'user', 'users', 'defaults' options that imply other options), are first */
01718     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01719                "/org/freedesktop/Hal/devices/computer", volume->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01720     mopts_collect (drive->hal_ctx, vol_mount_option_begin, sizeof (vol_mount_option_begin),
01721                volume->udi, volume->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01722     /* collect options != ('pamconsole', 'user', 'users', 'defaults' options that imply other options)  */
01723     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01724                "/org/freedesktop/Hal/devices/computer", volume->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01725     mopts_collect (drive->hal_ctx, vol_mount_option_begin, sizeof (vol_mount_option_begin),
01726                volume->udi, volume->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01727 
01728     result = volume->mount_options;
01729 
01730     return result;
01731 }
01732 
01733 const char *libhal_volume_policy_get_mount_fs (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01734 {
01735     return volume->mount_filesystem;
01736 }
01737 
01738 dbus_bool_t       
01739 libhal_drive_no_partitions_hint (LibHalDrive *drive)
01740 {
01741     return drive->no_partitions_hint;
01742 }
01743 

Generated on Tue May 2 08:52:17 2006 for HAL by  doxygen 1.4.6