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     dbus_bool_t is_disc;
00702     LibHalVolumeDiscType disc_type;
00703     dbus_bool_t disc_has_audio;
00704     dbus_bool_t disc_has_data;
00705     dbus_bool_t disc_is_appendable;
00706     dbus_bool_t disc_is_blank;
00707     dbus_bool_t disc_is_rewritable;
00708 
00709     unsigned int block_size;
00710     unsigned int num_blocks;
00711 
00712     char *desired_mount_point;
00713     char *mount_filesystem;
00714     dbus_bool_t should_mount;
00715 
00716     dbus_bool_t ignore_volume;
00717 
00718 
00719     char mount_options[MOUNT_OPTIONS_SIZE];
00720 
00721     dbus_uint64_t volume_size;
00722 };
00723 
00724 const char *
00725 libhal_drive_get_dedicated_icon_drive (LibHalDrive *drive)
00726 {
00727     return drive->dedicated_icon_drive;
00728 }
00729 
00730 const char *
00731 libhal_drive_get_dedicated_icon_volume (LibHalDrive *drive)
00732 {
00733     return drive->dedicated_icon_volume;
00734 }
00735 
00740 void
00741 libhal_drive_free (LibHalDrive *drive)
00742 {
00743     if (drive == NULL )
00744         return;
00745 
00746     free (drive->udi);
00747     libhal_free_string (drive->device_file);
00748     libhal_free_string (drive->vendor);
00749     libhal_free_string (drive->model);
00750     libhal_free_string (drive->type_textual);
00751     libhal_free_string (drive->physical_device);
00752     libhal_free_string (drive->dedicated_icon_drive);
00753     libhal_free_string (drive->dedicated_icon_volume);
00754     libhal_free_string (drive->serial);
00755     libhal_free_string (drive->firmware_version);
00756     libhal_free_string (drive->desired_mount_point);
00757     libhal_free_string (drive->mount_filesystem);
00758     libhal_free_string_array (drive->capabilities);
00759 
00760     free (drive);
00761 }
00762 
00763 
00768 void
00769 libhal_volume_free (LibHalVolume *vol)
00770 {
00771     if (vol == NULL )
00772         return;
00773 
00774     free (vol->udi);
00775     libhal_free_string (vol->device_file);
00776     libhal_free_string (vol->volume_label);
00777     libhal_free_string (vol->fstype);
00778     libhal_free_string (vol->mount_point);
00779     libhal_free_string (vol->fsversion);
00780     libhal_free_string (vol->uuid);
00781     libhal_free_string (vol->desired_mount_point);
00782     libhal_free_string (vol->mount_filesystem);
00783     libhal_free_string (vol->storage_device);
00784     
00785     free (vol);
00786 }
00787 
00788 
00789 static char **
00790 my_strvdup (char **strv)
00791 {
00792     unsigned int num_elems;
00793     unsigned int i;
00794     char **res;
00795 
00796     for (num_elems = 0; strv[num_elems] != NULL; num_elems++)
00797         ;
00798 
00799     res = calloc (num_elems + 1, sizeof (char*));
00800     if (res == NULL)
00801         goto out;
00802 
00803     for (i = 0; i < num_elems; i++)
00804         res[i] = strdup (strv[i]);
00805     res[i] = NULL;
00806 
00807 out:
00808     return res;
00809 }
00810 
00811 /* ok, hey, so this is a bit ugly */
00812 
00813 #define LIBHAL_PROP_EXTRACT_BEGIN if (FALSE)
00814 #define LIBHAL_PROP_EXTRACT_END ;
00815 #define LIBHAL_PROP_EXTRACT_INT(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_INT32) _where_ = libhal_psi_get_int (&it)
00816 #define LIBHAL_PROP_EXTRACT_UINT64(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_UINT64) _where_ = libhal_psi_get_uint64 (&it)
00817 #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
00818 #define LIBHAL_PROP_EXTRACT_BOOL(_property_, _where_) else if (strcmp (key, _property_) == 0 && type == LIBHAL_PROPERTY_TYPE_BOOLEAN) _where_ = libhal_psi_get_bool (&it)
00819 #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
00820 #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))
00821 
00830 LibHalDrive *
00831 libhal_drive_from_udi (LibHalContext *hal_ctx, const char *udi)
00832 {   
00833     char *bus_textual;
00834     LibHalDrive *drive;
00835     LibHalPropertySet *properties;
00836     LibHalPropertySetIterator it;
00837     DBusError error;
00838     unsigned int i;
00839 
00840     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
00841 
00842     drive = NULL;
00843     properties = NULL;
00844     bus_textual = NULL;
00845 
00846     dbus_error_init (&error);
00847     if (!libhal_device_query_capability (hal_ctx, udi, "storage", &error))
00848         goto error;
00849 
00850     drive = malloc (sizeof (LibHalDrive));
00851     if (drive == NULL)
00852         goto error;
00853     memset (drive, 0x00, sizeof (LibHalDrive));
00854 
00855     drive->hal_ctx = hal_ctx;
00856 
00857     drive->udi = strdup (udi);
00858     if (drive->udi == NULL)
00859         goto error;
00860 
00861     properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
00862     if (properties == NULL)
00863         goto error;
00864 
00865     /* we can count on hal to give us all these properties */
00866     for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
00867         int type;
00868         char *key;
00869         
00870         type = libhal_psi_get_type (&it);
00871         key = libhal_psi_get_key (&it);
00872 
00873         LIBHAL_PROP_EXTRACT_BEGIN;
00874 
00875         LIBHAL_PROP_EXTRACT_INT    ("block.minor",               drive->device_minor);
00876         LIBHAL_PROP_EXTRACT_INT    ("block.major",               drive->device_major);
00877         LIBHAL_PROP_EXTRACT_STRING ("block.device",              drive->device_file);
00878         LIBHAL_PROP_EXTRACT_STRING ("storage.bus",               bus_textual);
00879         LIBHAL_PROP_EXTRACT_STRING ("storage.vendor",            drive->vendor);
00880         LIBHAL_PROP_EXTRACT_STRING ("storage.model",             drive->model);
00881         LIBHAL_PROP_EXTRACT_STRING ("storage.drive_type",        drive->type_textual);
00882 
00883 
00884         LIBHAL_PROP_EXTRACT_STRING ("storage.icon.drive",        drive->dedicated_icon_drive);
00885         LIBHAL_PROP_EXTRACT_STRING ("storage.icon.volume",       drive->dedicated_icon_volume);
00886 
00887         LIBHAL_PROP_EXTRACT_BOOL   ("storage.hotpluggable",      drive->is_hotpluggable);
00888         LIBHAL_PROP_EXTRACT_BOOL   ("storage.removable",         drive->is_removable);
00889         LIBHAL_PROP_EXTRACT_BOOL   ("storage.requires_eject",    drive->requires_eject);
00890 
00891         LIBHAL_PROP_EXTRACT_STRING ("storage.physical_device",   drive->physical_device);
00892         LIBHAL_PROP_EXTRACT_STRING ("storage.firmware_version",  drive->firmware_version);
00893         LIBHAL_PROP_EXTRACT_STRING ("storage.serial",            drive->serial);
00894 
00895         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.cdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_CDR);
00896         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.cdrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_CDRW);
00897         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvd", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDROM);
00898         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR);
00899         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW);
00900         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrwdl", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRWDL);
00901         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdplusrdl", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL);
00902         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdr", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDR);
00903         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdrw", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDRW);
00904         LIBHAL_PROP_EXTRACT_BOOL_BITFIELD ("storage.cdrom.dvdram", drive->cdrom_caps, LIBHAL_DRIVE_CDROM_CAPS_DVDRAM);
00905 
00906         LIBHAL_PROP_EXTRACT_BOOL   ("storage.policy.should_mount",        drive->should_mount);
00907         LIBHAL_PROP_EXTRACT_STRING ("storage.policy.desired_mount_point", drive->desired_mount_point);
00908         LIBHAL_PROP_EXTRACT_STRING ("storage.policy.mount_filesystem",    drive->mount_filesystem);
00909 
00910         LIBHAL_PROP_EXTRACT_BOOL   ("storage.no_partitions_hint",        drive->no_partitions_hint);
00911 
00912         LIBHAL_PROP_EXTRACT_STRLIST ("info.capabilities",                drive->capabilities);
00913 
00914         LIBHAL_PROP_EXTRACT_END;
00915     }
00916 
00917     if (drive->type_textual != NULL) {
00918         if (strcmp (drive->type_textual, "cdrom") == 0) {
00919             drive->cdrom_caps |= LIBHAL_DRIVE_CDROM_CAPS_CDROM;
00920             drive->type = LIBHAL_DRIVE_TYPE_CDROM;
00921         } else if (strcmp (drive->type_textual, "floppy") == 0) {
00922             drive->type = LIBHAL_DRIVE_TYPE_FLOPPY;
00923         } else if (strcmp (drive->type_textual, "disk") == 0) {
00924             if (drive->is_removable)
00925                 drive->type = LIBHAL_DRIVE_TYPE_REMOVABLE_DISK;
00926             else
00927                 drive->type = LIBHAL_DRIVE_TYPE_DISK;               
00928         } else if (strcmp (drive->type_textual, "tape") == 0) {
00929             drive->type = LIBHAL_DRIVE_TYPE_TAPE;
00930         } else if (strcmp (drive->type_textual, "compact_flash") == 0) {
00931             drive->type = LIBHAL_DRIVE_TYPE_COMPACT_FLASH;
00932         } else if (strcmp (drive->type_textual, "memory_stick") == 0) {
00933             drive->type = LIBHAL_DRIVE_TYPE_MEMORY_STICK;
00934         } else if (strcmp (drive->type_textual, "smart_media") == 0) {
00935             drive->type = LIBHAL_DRIVE_TYPE_SMART_MEDIA;
00936         } else if (strcmp (drive->type_textual, "sd_mmc") == 0) {
00937             drive->type = LIBHAL_DRIVE_TYPE_SD_MMC;
00938         } else if (strcmp (drive->type_textual, "zip") == 0) {
00939             drive->type = LIBHAL_DRIVE_TYPE_ZIP;
00940         } else if (strcmp (drive->type_textual, "jaz") == 0) {
00941             drive->type = LIBHAL_DRIVE_TYPE_JAZ;
00942         } else if (strcmp (drive->type_textual, "flashkey") == 0) {
00943             drive->type = LIBHAL_DRIVE_TYPE_FLASHKEY;
00944         } else {
00945                 drive->type = LIBHAL_DRIVE_TYPE_DISK; 
00946         }
00947 
00948     }
00949 
00950     if (drive->capabilities != NULL) {
00951         for (i = 0; drive->capabilities[i] != NULL; i++) {
00952             if (strcmp (drive->capabilities[i], "portable_audio_player") == 0) {
00953                 drive->type = LIBHAL_DRIVE_TYPE_PORTABLE_AUDIO_PLAYER;
00954                 break;
00955             } else if (strcmp (drive->capabilities[i], "camera") == 0) {
00956                 drive->type = LIBHAL_DRIVE_TYPE_CAMERA;
00957                 break;
00958             }
00959         }
00960     }
00961 
00962     if (bus_textual != NULL) {
00963         if (strcmp (bus_textual, "usb") == 0) {
00964             drive->bus = LIBHAL_DRIVE_BUS_USB;
00965         } else if (strcmp (bus_textual, "ieee1394") == 0) {
00966             drive->bus = LIBHAL_DRIVE_BUS_IEEE1394;
00967         } else if (strcmp (bus_textual, "ide") == 0) {
00968             drive->bus = LIBHAL_DRIVE_BUS_IDE;
00969         } else if (strcmp (bus_textual, "scsi") == 0) {
00970             drive->bus = LIBHAL_DRIVE_BUS_SCSI;
00971         } else if (strcmp (bus_textual, "ccw") == 0) {
00972             drive->bus = LIBHAL_DRIVE_BUS_CCW;
00973         }
00974     }
00975 
00976     libhal_free_string (bus_textual);
00977     libhal_free_property_set (properties);
00978 
00979     return drive;
00980 
00981 error:
00982     LIBHAL_FREE_DBUS_ERROR(&error);
00983     libhal_free_string (bus_textual);
00984     libhal_free_property_set (properties);
00985     libhal_drive_free (drive);
00986     return NULL;
00987 }
00988 
00989 const char *
00990 libhal_volume_get_storage_device_udi (LibHalVolume *volume)
00991 {
00992     return volume->storage_device;
00993 }
00994 
00995 const char *libhal_drive_get_physical_device_udi (LibHalDrive *drive)
00996 {
00997     return drive->physical_device;
00998 }
00999 
01000 dbus_bool_t
01001 libhal_drive_requires_eject (LibHalDrive *drive)
01002 {
01003     return drive->requires_eject;
01004 }
01005 
01014 LibHalVolume *
01015 libhal_volume_from_udi (LibHalContext *hal_ctx, const char *udi)
01016 {
01017     char *disc_type_textual;
01018     char *vol_fsusage_textual;
01019     LibHalVolume *vol;
01020     LibHalPropertySet *properties;
01021     LibHalPropertySetIterator it;
01022     DBusError error;
01023 
01024     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01025 
01026     vol = NULL;
01027     properties = NULL;
01028     disc_type_textual = NULL;
01029     vol_fsusage_textual = NULL;
01030 
01031     dbus_error_init (&error);
01032     if (!libhal_device_query_capability (hal_ctx, udi, "volume", &error))
01033         goto error;
01034 
01035     vol = malloc (sizeof (LibHalVolume));
01036     if (vol == NULL)
01037         goto error;
01038     memset (vol, 0x00, sizeof (LibHalVolume));
01039 
01040     vol->udi = strdup (udi);
01041 
01042     properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
01043     if (properties == NULL)
01044         goto error;
01045 
01046     /* we can count on hal to give us all these properties */
01047     for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
01048         int type;
01049         char *key;
01050         
01051         type = libhal_psi_get_type (&it);
01052         key = libhal_psi_get_key (&it);
01053 
01054         LIBHAL_PROP_EXTRACT_BEGIN;
01055 
01056         LIBHAL_PROP_EXTRACT_INT    ("volume.partition.msdos_part_table_type", vol->msdos_part_table_type);
01057 
01058         LIBHAL_PROP_EXTRACT_INT    ("block.minor",               vol->device_minor);
01059         LIBHAL_PROP_EXTRACT_INT    ("block.major",               vol->device_major);
01060         LIBHAL_PROP_EXTRACT_STRING ("block.device",              vol->device_file);
01061 
01062         LIBHAL_PROP_EXTRACT_STRING ("block.storage_device",      vol->storage_device);
01063 
01064         LIBHAL_PROP_EXTRACT_INT    ("volume.block_size",         vol->block_size);
01065         LIBHAL_PROP_EXTRACT_INT    ("volume.num_blocks",         vol->num_blocks);
01066         LIBHAL_PROP_EXTRACT_UINT64 ("volume.size",       vol->volume_size); 
01067         LIBHAL_PROP_EXTRACT_STRING ("volume.label",              vol->volume_label);
01068         LIBHAL_PROP_EXTRACT_STRING ("volume.mount_point",        vol->mount_point);
01069         LIBHAL_PROP_EXTRACT_STRING ("volume.fstype",             vol->fstype);
01070         LIBHAL_PROP_EXTRACT_BOOL   ("volume.is_mounted",         vol->is_mounted);
01071         LIBHAL_PROP_EXTRACT_STRING ("volume.fsusage",            vol_fsusage_textual);
01072 
01073         LIBHAL_PROP_EXTRACT_BOOL   ("volume.ignore",             vol->ignore_volume);
01074 
01075         LIBHAL_PROP_EXTRACT_BOOL   ("volume.is_disc",            vol->is_disc);
01076         LIBHAL_PROP_EXTRACT_STRING ("volume.disc.type",          disc_type_textual);
01077         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.has_audio",     vol->disc_has_audio);
01078         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.has_data",      vol->disc_has_data);
01079         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.is_appendable", vol->disc_is_appendable);
01080         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.is_blank",      vol->disc_is_blank);
01081         LIBHAL_PROP_EXTRACT_BOOL   ("volume.disc.is_rewritable", vol->disc_is_rewritable);
01082 
01083         LIBHAL_PROP_EXTRACT_BOOL   ("volume.policy.should_mount",        vol->should_mount);
01084         LIBHAL_PROP_EXTRACT_STRING ("volume.policy.desired_mount_point", vol->desired_mount_point);
01085         LIBHAL_PROP_EXTRACT_STRING ("volume.policy.mount_filesystem",    vol->mount_filesystem);
01086 
01087         LIBHAL_PROP_EXTRACT_END;
01088     }
01089 
01090     if (disc_type_textual != NULL) {
01091         if (strcmp (disc_type_textual, "cd_rom") == 0) {
01092             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDROM;
01093         } else if (strcmp (disc_type_textual, "cd_r") == 0) {
01094             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDR;
01095         } else if (strcmp (disc_type_textual, "cd_rw") == 0) {
01096             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_CDRW;
01097         } else if (strcmp (disc_type_textual, "dvd_rom") == 0) {
01098             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDROM;
01099         } else if (strcmp (disc_type_textual, "dvd_ram") == 0) {
01100             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDRAM;
01101         } else if (strcmp (disc_type_textual, "dvd_r") == 0) {
01102             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDR;
01103         } else if (strcmp (disc_type_textual, "dvd_rw") == 0) {
01104             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDRW;
01105         } else if (strcmp (disc_type_textual, "dvd_plus_r") == 0) {
01106             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR;
01107         } else if (strcmp (disc_type_textual, "dvd_plus_rw") == 0) {
01108             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSRW;
01109         } else if (strcmp (disc_type_textual, "dvd_plus_r_dl") == 0) {
01110             vol->disc_type = LIBHAL_VOLUME_DISC_TYPE_DVDPLUSR_DL;
01111         }
01112     }
01113 
01114     vol->fsusage = LIBHAL_VOLUME_USAGE_UNKNOWN;
01115     if (vol_fsusage_textual != NULL) {
01116         if (strcmp (vol_fsusage_textual, "filesystem") == 0) {
01117             vol->fsusage = LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM;
01118         } else if (strcmp (vol_fsusage_textual, "partitiontable") == 0) {
01119             vol->fsusage = LIBHAL_VOLUME_USAGE_PARTITION_TABLE;
01120         } else if (strcmp (vol_fsusage_textual, "raid") == 0) {
01121             vol->fsusage = LIBHAL_VOLUME_USAGE_RAID_MEMBER;
01122         } else if (strcmp (vol_fsusage_textual, "crypto") == 0) {
01123             vol->fsusage = LIBHAL_VOLUME_USAGE_CRYPTO;
01124         } else {
01125             vol->fsusage = LIBHAL_VOLUME_USAGE_UNKNOWN;
01126         } 
01127     }
01128 
01129     libhal_free_string (vol_fsusage_textual);
01130     libhal_free_string (disc_type_textual);
01131     libhal_free_property_set (properties);
01132     return vol;
01133 error:
01134     LIBHAL_FREE_DBUS_ERROR(&error);
01135     libhal_free_string (vol_fsusage_textual);
01136     libhal_free_string (disc_type_textual);
01137     libhal_free_property_set (properties);
01138     libhal_volume_free (vol);
01139     return NULL;
01140 }
01141 
01142 
01151 int
01152 libhal_volume_get_msdos_part_table_type (LibHalVolume *volume)
01153 {
01154     return volume->msdos_part_table_type;
01155 }
01156 
01157 /***********************************************************************/
01158 
01166 LibHalDrive *
01167 libhal_drive_from_device_file (LibHalContext *hal_ctx, const char *device_file)
01168 {
01169     int i;
01170     char **hal_udis;
01171     int num_hal_udis;
01172     LibHalDrive *result;
01173     char *found_udi;
01174     DBusError error;
01175 
01176     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01177 
01178     result = NULL;
01179     found_udi = NULL;
01180 
01181     dbus_error_init (&error);
01182     if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "block.device", 
01183                                  device_file, &num_hal_udis, &error)) == NULL)
01184         goto out;
01185 
01186     for (i = 0; i < num_hal_udis; i++) {
01187         char *udi;
01188         char *storage_udi;
01189         DBusError err1;
01190         DBusError err2;
01191         udi = hal_udis[i];
01192 
01193         dbus_error_init (&err1);
01194         dbus_error_init (&err2);
01195         if (libhal_device_query_capability (hal_ctx, udi, "volume", &err1)) {
01196 
01197             storage_udi = libhal_device_get_property_string (hal_ctx, udi, "block.storage_device", &err1);
01198             if (storage_udi == NULL)
01199                 continue;
01200             found_udi = strdup (storage_udi);
01201             libhal_free_string (storage_udi);
01202             break;
01203         } else if (libhal_device_query_capability (hal_ctx, udi, "storage", &err2)) {
01204             found_udi = strdup (udi);
01205         }
01206         LIBHAL_FREE_DBUS_ERROR(&err1);
01207         LIBHAL_FREE_DBUS_ERROR(&err2);
01208     }
01209 
01210     libhal_free_string_array (hal_udis);
01211 
01212     if (found_udi != NULL)
01213         result = libhal_drive_from_udi (hal_ctx, found_udi);
01214 
01215     free (found_udi);
01216 out:
01217     LIBHAL_FREE_DBUS_ERROR(&error);
01218     return result;
01219 }
01220 
01221 
01228 LibHalVolume *
01229 libhal_volume_from_device_file (LibHalContext *hal_ctx, const char *device_file)
01230 {
01231     int i;
01232     char **hal_udis;
01233     int num_hal_udis;
01234     LibHalVolume *result;
01235     char *found_udi;
01236     DBusError error;
01237 
01238     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01239 
01240     result = NULL;
01241     found_udi = NULL;
01242 
01243     dbus_error_init (&error);
01244     if ((hal_udis = libhal_manager_find_device_string_match (hal_ctx, "block.device", 
01245                                  device_file, &num_hal_udis, &error)) == NULL)
01246         goto out;
01247 
01248     for (i = 0; i < num_hal_udis; i++) {
01249         char *udi;
01250         udi = hal_udis[i];
01251         if (libhal_device_query_capability (hal_ctx, udi, "volume", &error)) {
01252             found_udi = strdup (udi);
01253             break;
01254         }
01255     }
01256 
01257     libhal_free_string_array (hal_udis);
01258 
01259     if (found_udi != NULL)
01260         result = libhal_volume_from_udi (hal_ctx, found_udi);
01261 
01262     free (found_udi);
01263 out:
01264     LIBHAL_FREE_DBUS_ERROR(&error);
01265     return result;
01266 }
01267 
01268 dbus_uint64_t
01269 libhal_volume_get_size (LibHalVolume *volume)
01270 {
01271     dbus_uint64_t computed_size;
01272 
01273     computed_size = ((dbus_uint64_t)volume->block_size) * ((dbus_uint64_t)volume->num_blocks);
01274 
01275     if ((computed_size > volume->volume_size) && (volume->volume_size > 0))
01276         return volume->volume_size;
01277     else
01278         return computed_size;
01279 }
01280 
01281 
01282 dbus_bool_t
01283 libhal_drive_is_hotpluggable (LibHalDrive *drive)
01284 {
01285     return drive->is_hotpluggable;
01286 }
01287 
01288 dbus_bool_t
01289 libhal_drive_uses_removable_media (LibHalDrive *drive)
01290 {
01291     return drive->is_removable;
01292 }
01293 
01294 LibHalDriveType
01295 libhal_drive_get_type (LibHalDrive *drive)
01296 {
01297     return drive->type;
01298 }
01299 
01300 LibHalDriveBus
01301 libhal_drive_get_bus (LibHalDrive *drive)
01302 {
01303     return drive->bus;
01304 }
01305 
01306 LibHalDriveCdromCaps
01307 libhal_drive_get_cdrom_caps (LibHalDrive *drive)
01308 {
01309     return drive->cdrom_caps;
01310 }
01311 
01312 unsigned int
01313 libhal_drive_get_device_major (LibHalDrive *drive)
01314 {
01315     return drive->device_major;
01316 }
01317 
01318 unsigned int
01319 libhal_drive_get_device_minor (LibHalDrive *drive)
01320 {
01321     return drive->device_minor;
01322 }
01323 
01324 const char *
01325 libhal_drive_get_type_textual (LibHalDrive *drive)
01326 {
01327     return drive->type_textual;
01328 }
01329 
01330 const char *
01331 libhal_drive_get_device_file (LibHalDrive *drive)
01332 {
01333     return drive->device_file;
01334 }
01335 
01336 const char *
01337 libhal_drive_get_udi (LibHalDrive *drive)
01338 {
01339     return drive->udi;
01340 }
01341 
01342 const char *
01343 libhal_drive_get_serial (LibHalDrive *drive)
01344 {
01345     return drive->serial;
01346 }
01347 
01348 const char *
01349 libhal_drive_get_firmware_version (LibHalDrive *drive)
01350 {
01351     return drive->firmware_version;
01352 }
01353 
01354 const char *
01355 libhal_drive_get_model (LibHalDrive *drive)
01356 {
01357     return drive->model;
01358 }
01359 
01360 const char *
01361 libhal_drive_get_vendor (LibHalDrive *drive)
01362 {
01363     return drive->vendor;
01364 }
01365 
01366 /*****************************************************************************/
01367 
01368 const char *
01369 libhal_volume_get_udi (LibHalVolume *volume)
01370 {
01371     return volume->udi;
01372 }
01373 
01374 const char *
01375 libhal_volume_get_device_file (LibHalVolume *volume)
01376 {
01377     return volume->device_file;
01378 }
01379 
01380 unsigned int libhal_volume_get_device_major (LibHalVolume *volume)
01381 {
01382     return volume->device_major;
01383 }
01384 
01385 unsigned int libhal_volume_get_device_minor (LibHalVolume *volume)
01386 {
01387     return volume->device_minor;
01388 }
01389 
01390 const char *
01391 libhal_volume_get_fstype (LibHalVolume *volume)
01392 {
01393     return volume->fstype;
01394 }
01395 
01396 const char *
01397 libhal_volume_get_fsversion (LibHalVolume *volume)
01398 {
01399     return volume->fsversion;
01400 }
01401 
01402 LibHalVolumeUsage 
01403 libhal_volume_get_fsusage (LibHalVolume *volume)
01404 {
01405     return volume->fsusage;
01406 }
01407 
01408 dbus_bool_t 
01409 libhal_volume_is_mounted (LibHalVolume *volume)
01410 {
01411     return volume->is_mounted;
01412 }
01413 
01414 dbus_bool_t 
01415 libhal_volume_is_partition (LibHalVolume *volume)
01416 {
01417     return volume->is_partition;
01418 }
01419 
01420 dbus_bool_t
01421 libhal_volume_is_disc (LibHalVolume *volume)
01422 {
01423     return volume->is_disc;
01424 }
01425 
01426 unsigned int
01427 libhal_volume_get_partition_number (LibHalVolume *volume)
01428 {
01429     return volume->partition_number;
01430 }
01431 
01432 const char *
01433 libhal_volume_get_label (LibHalVolume *volume)
01434 {
01435     return volume->volume_label;
01436 }
01437 
01438 const char *
01439 libhal_volume_get_mount_point (LibHalVolume *volume)
01440 {
01441     return volume->mount_point;
01442 }
01443 
01444 const char *
01445 libhal_volume_get_uuid (LibHalVolume *volume)
01446 {
01447     return volume->uuid;
01448 }
01449 
01450 dbus_bool_t
01451 libhal_volume_disc_has_audio (LibHalVolume *volume)
01452 {
01453     return volume->disc_has_audio;
01454 }
01455 
01456 dbus_bool_t
01457 libhal_volume_disc_has_data (LibHalVolume *volume)
01458 {
01459     return volume->disc_has_data;
01460 }
01461 
01462 dbus_bool_t
01463 libhal_volume_disc_is_blank (LibHalVolume *volume)
01464 {
01465     return volume->disc_is_blank;
01466 }
01467 
01468 dbus_bool_t
01469 libhal_volume_disc_is_rewritable (LibHalVolume *volume)
01470 {
01471     return volume->disc_is_rewritable;
01472 }
01473 
01474 dbus_bool_t
01475 libhal_volume_disc_is_appendable (LibHalVolume *volume)
01476 {
01477     return volume->disc_is_appendable;
01478 }
01479 
01480 LibHalVolumeDiscType
01481 libhal_volume_get_disc_type (LibHalVolume *volume)
01482 {
01483     return volume->disc_type;
01484 }
01485 
01486 dbus_bool_t
01487 libhal_volume_should_ignore (LibHalVolume     *volume)
01488 {
01489     return volume->ignore_volume;
01490 }
01491 
01492 char ** 
01493 libhal_drive_find_all_volumes (LibHalContext *hal_ctx, LibHalDrive *drive, int *num_volumes)
01494 {
01495     int i;
01496     char **udis;
01497     int num_udis;
01498     const char *drive_udi;
01499     char **result;
01500     DBusError error;
01501 
01502     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01503 
01504     udis = NULL;
01505     result = NULL;
01506     *num_volumes = 0;
01507 
01508     drive_udi = libhal_drive_get_udi (drive);
01509     if (drive_udi == NULL)
01510         goto out;
01511 
01512     /* get initial list... */
01513     dbus_error_init (&error);
01514     if ((udis = libhal_manager_find_device_string_match (hal_ctx, "block.storage_device", 
01515                                  drive_udi, &num_udis, &error)) == NULL)
01516         goto out;
01517 
01518     result = malloc (sizeof (char *) * num_udis);
01519     if (result == NULL)
01520         goto out;
01521 
01522     /* ...and filter out the single UDI that is the drive itself */
01523     for (i = 0; i < num_udis; i++) {
01524         if (strcmp (udis[i], drive_udi) == 0)
01525             continue;
01526         result[*num_volumes] = strdup (udis[i]);
01527         *num_volumes = (*num_volumes) + 1;
01528     }
01529     /* set last element (above removed UDI) to NULL for libhal_free_string_array()*/
01530     result[*num_volumes] = NULL;
01531 
01532 out:
01533     LIBHAL_FREE_DBUS_ERROR(&error);
01534     libhal_free_string_array (udis);
01535     return result;
01536 }
01537 
01538 /*************************************************************************/
01539 
01540 char *
01541 libhal_drive_policy_default_get_mount_root (LibHalContext *hal_ctx)
01542 {
01543     char *result;
01544     DBusError error;
01545 
01546     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01547 
01548     dbus_error_init (&error);
01549     result = libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01550                             "storage.policy.default.mount_root", &error);
01551     LIBHAL_FREE_DBUS_ERROR(&error);
01552 
01553     return result;
01554 }
01555 
01556 dbus_bool_t
01557 libhal_drive_policy_default_use_managed_keyword (LibHalContext *hal_ctx)
01558 {
01559     dbus_bool_t result;
01560     DBusError error;
01561 
01562     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, FALSE);
01563 
01564     dbus_error_init (&error);
01565     result = libhal_device_get_property_bool (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01566                           "storage.policy.default.use_managed_keyword", &error);
01567     LIBHAL_FREE_DBUS_ERROR(&error);
01568 
01569     return result;
01570 }
01571 
01572 char *
01573 libhal_drive_policy_default_get_managed_keyword_primary (LibHalContext *hal_ctx)
01574 {
01575     char *result;
01576     DBusError error;
01577 
01578     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01579 
01580     dbus_error_init (&error);
01581     result = libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01582                             "storage.policy.default.managed_keyword.primary", &error);
01583     LIBHAL_FREE_DBUS_ERROR(&error);
01584 
01585     return result;
01586 }
01587 
01588 char *
01589 libhal_drive_policy_default_get_managed_keyword_secondary (LibHalContext *hal_ctx)
01590 {
01591     char *result;
01592     DBusError error;
01593 
01594     LIBHAL_CHECK_LIBHALCONTEXT(hal_ctx, NULL);
01595 
01596     dbus_error_init (&error);
01597     result = libhal_device_get_property_string (hal_ctx, "/org/freedesktop/Hal/devices/computer",
01598                             "storage.policy.default.managed_keyword.secondary", &error);
01599     LIBHAL_FREE_DBUS_ERROR(&error);
01600 
01601     return result;
01602 }
01603 
01604 /*************************************************************************/
01605 
01606 dbus_bool_t
01607 libhal_drive_policy_is_mountable (LibHalDrive *drive, LibHalStoragePolicy *policy)
01608 {
01609     printf ("should_mount=%d, no_partitions_hint=%d\n", drive->should_mount, drive->no_partitions_hint);
01610 
01611     return drive->should_mount && drive->no_partitions_hint;
01612 }
01613 
01614 const char *
01615 libhal_drive_policy_get_desired_mount_point (LibHalDrive *drive, LibHalStoragePolicy *policy)
01616 {
01617     return drive->desired_mount_point;
01618 }
01619 
01620 /* safely strcat() at most the remaining space in 'dst' */
01621 #define strcat_len(dst, src, dstmaxlen) do {    \
01622     dst[dstmaxlen - 1] = '\0'; \
01623     strncat (dst, src, dstmaxlen - strlen (dst) - 1); \
01624 } while(0)
01625 
01626 
01627 static void
01628 mopts_collect (LibHalContext *hal_ctx, const char *namespace, int namespace_len, 
01629            const char *udi, char *options_string, size_t options_max_len, dbus_bool_t only_collect_imply_opts)
01630 {
01631     LibHalPropertySet *properties;
01632     LibHalPropertySetIterator it;
01633     DBusError error;
01634 
01635     if(hal_ctx == 0) {
01636         fprintf (stderr,"%s %d : LibHalContext *ctx is NULL\n",__FILE__, __LINE__);
01637         return;
01638     }
01639 
01640     dbus_error_init (&error);
01641 
01642     /* first collect from root computer device */
01643     properties = libhal_device_get_all_properties (hal_ctx, udi, &error);
01644     if (properties == NULL)
01645         goto error;
01646 
01647     for (libhal_psi_init (&it, properties); libhal_psi_has_more (&it); libhal_psi_next (&it)) {
01648         int type;
01649         char *key;
01650         
01651         type = libhal_psi_get_type (&it);
01652         key = libhal_psi_get_key (&it);
01653         if (libhal_psi_get_type (&it) == LIBHAL_PROPERTY_TYPE_BOOLEAN &&
01654             strncmp (key, namespace, namespace_len - 1) == 0) {
01655             const char *option = key + namespace_len - 1;
01656             char *location;
01657             dbus_bool_t is_imply_opt;
01658 
01659             is_imply_opt = FALSE;
01660             if (strcmp (option, "user") == 0 ||
01661                 strcmp (option, "users") == 0 ||
01662                 strcmp (option, "defaults") == 0 ||
01663                 strcmp (option, "pamconsole") == 0)
01664                 is_imply_opt = TRUE;
01665 
01666             
01667             if (only_collect_imply_opts) {
01668                 if (!is_imply_opt)
01669                     continue;
01670             } else {
01671                 if (is_imply_opt)
01672                     continue;
01673             }
01674 
01675             if (libhal_psi_get_bool (&it)) {
01676                 /* see if option is already there */
01677                 location = strstr (options_string, option);
01678                 if (location == NULL) {
01679                     if (strlen (options_string) > 0)
01680                         strcat_len (options_string, ",", options_max_len);
01681                     strcat_len (options_string, option, options_max_len);
01682                 }
01683             } else {
01684                 /* remove option if already there */
01685                 location = strstr (options_string, option);
01686                 if (location != NULL) {
01687                     char *end;
01688 
01689                     end = strchr (location, ',');
01690                     if (end == NULL) {
01691                         location[0] = '\0';
01692                     } else {
01693                         strcpy (location, end + 1); /* skip the extra comma */
01694                     }
01695                 }
01696 
01697             }
01698         }
01699     }
01700 error:
01701     LIBHAL_FREE_DBUS_ERROR(&error);
01702     libhal_free_property_set (properties);
01703 }
01704 
01705 
01706 const char *
01707 libhal_drive_policy_get_mount_options (LibHalDrive *drive, LibHalStoragePolicy *policy)
01708 {
01709     const char *result;
01710     char stor_mount_option_default_begin[] = "storage.policy.default.mount_option.";
01711     char stor_mount_option_begin[] = "storage.policy.mount_option.";
01712 
01713     result = NULL;
01714     drive->mount_options[0] = '\0';
01715 
01716     /* collect options != ('pamconsole', 'user', 'users', 'defaults' options that imply other options)  */
01717     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01718                "/org/freedesktop/Hal/devices/computer", drive->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01719     mopts_collect (drive->hal_ctx, stor_mount_option_begin, sizeof (stor_mount_option_begin),
01720                drive->udi, drive->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01721     /* ensure ('pamconsole', 'user', 'users', 'defaults' options that imply other options), are first */
01722     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01723                "/org/freedesktop/Hal/devices/computer", drive->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01724     mopts_collect (drive->hal_ctx, stor_mount_option_begin, sizeof (stor_mount_option_begin),
01725                drive->udi, drive->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01726 
01727     result = drive->mount_options;
01728 
01729     return result;
01730 }
01731 
01732 const char *
01733 libhal_drive_policy_get_mount_fs (LibHalDrive *drive, LibHalStoragePolicy *policy)
01734 {
01735     return drive->mount_filesystem;
01736 }
01737 
01738 
01739 dbus_bool_t
01740 libhal_volume_policy_is_mountable (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01741 {
01742     return drive->should_mount && volume->should_mount;
01743 }
01744 
01745 const char *libhal_volume_policy_get_desired_mount_point (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01746 {
01747     return volume->desired_mount_point;
01748 }
01749 
01750 const char *libhal_volume_policy_get_mount_options (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01751 {
01752     const char *result;
01753     char stor_mount_option_default_begin[] = "storage.policy.default.mount_option.";
01754     char vol_mount_option_begin[] = "volume.policy.mount_option.";
01755 
01756     result = NULL;
01757     volume->mount_options[0] = '\0';
01758 
01759     /* ensure ('pamconsole', 'user', 'users', 'defaults' options that imply other options), are first */
01760     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01761                "/org/freedesktop/Hal/devices/computer", volume->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01762     mopts_collect (drive->hal_ctx, vol_mount_option_begin, sizeof (vol_mount_option_begin),
01763                volume->udi, volume->mount_options, MOUNT_OPTIONS_SIZE, TRUE);
01764     /* collect options != ('pamconsole', 'user', 'users', 'defaults' options that imply other options)  */
01765     mopts_collect (drive->hal_ctx, stor_mount_option_default_begin, sizeof (stor_mount_option_default_begin),
01766                "/org/freedesktop/Hal/devices/computer", volume->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01767     mopts_collect (drive->hal_ctx, vol_mount_option_begin, sizeof (vol_mount_option_begin),
01768                volume->udi, volume->mount_options, MOUNT_OPTIONS_SIZE, FALSE);
01769 
01770     result = volume->mount_options;
01771 
01772     return result;
01773 }
01774 
01775 const char *libhal_volume_policy_get_mount_fs (LibHalDrive *drive, LibHalVolume *volume, LibHalStoragePolicy *policy)
01776 {
01777     return volume->mount_filesystem;
01778 }
01779 
01780 dbus_bool_t       
01781 libhal_drive_no_partitions_hint (LibHalDrive *drive)
01782 {
01783     return drive->no_partitions_hint;
01784 }
01785 

Generated on Fri Nov 23 17:14:31 2007 for HAL by  doxygen 1.4.6