HalContext.cc

Go to the documentation of this file.
00001 /*---------------------------------------------------------------------\
00002 |                          ____ _   __ __ ___                          |
00003 |                         |__  / \ / / . \ . \                         |
00004 |                           / / \ V /|  _/  _/                         |
00005 |                          / /__ | | | | | |                           |
00006 |                         /_____||_| |_| |_|                           |
00007 |                                                                      |
00008 \---------------------------------------------------------------------*/
00013 #ifndef FAKE_HAL // disables zypp's HAL dependency
00014 
00015 #include <zypp/target/hal/HalContext.h>
00016 #include <zypp/thread/Mutex.h>
00017 #include <zypp/thread/MutexLock.h>
00018 #include <zypp/base/NonCopyable.h>
00019 #include <zypp/base/Logger.h>
00020 #include <zypp/base/String.h>
00021 #include <zypp/base/Gettext.h>
00022 
00023 #include <dbus/dbus-glib-lowlevel.h>
00024 #include <dbus/dbus-glib.h>
00025 #include <hal/libhal.h>
00026 #include <hal/libhal-storage.h>
00027 
00028 #include <iostream>
00029 
00030 using namespace std;
00031 
00033 namespace zypp
00034 { 
00035 
00036   namespace target
00037   { 
00038 
00039     namespace hal
00040     { 
00041 
00042       using zypp::thread::Mutex;
00043       using zypp::thread::MutexLock;
00044 
00046       namespace // anonymous
00047       { 
00048 
00049 
00051         // STATIC
00055         static Mutex g_Mutex;
00056 
00057 
00059 
00062         class HalError
00063         {
00064         public:
00065           DBusError error;
00066 
00067           HalError()  { dbus_error_init(&error); }
00068           ~HalError() { dbus_error_free(&error); }
00069 
00070           inline bool         isSet() const
00071           {
00072             return dbus_error_is_set(&error);
00073           }
00074 
00075           inline HalException halException(const std::string &msg = std::string()) const
00076           {
00077             if( isSet() && error.name != NULL && error.message != NULL) {
00078               return HalException(error.name, error.message);
00079             }
00080             else if( !msg.empty()) {
00081               return HalException(msg);
00082             }
00083             else {
00084               return HalException();
00085             }
00086           }
00087         };
00088 
00089 
00090         // -----------------------------------------------------------
00091         inline void
00092         VERIFY_CONTEXT(const zypp::RW_pointer<HalContext_Impl> &h)
00093         {
00094           if( !h)
00095           {
00096             ZYPP_THROW(HalException(_("HalContext not connected")));
00097           }
00098         }
00099 
00100         // -----------------------------------------------------------
00101         inline void
00102         VERIFY_DRIVE(const zypp::RW_pointer<HalDrive_Impl> &d)
00103         {
00104           if( !d)
00105           {
00106             ZYPP_THROW(HalException(_("HalDrive not initialized")));
00107           }
00108         }
00109 
00110         // -----------------------------------------------------------
00111         inline void
00112         VERIFY_VOLUME(const zypp::RW_pointer<HalVolume_Impl> &v)
00113         {
00114           if( !v)
00115           {
00116             ZYPP_THROW(HalException(_("HalVolume not initialized")));
00117           }
00118         }
00119 
00121       } // anonymous
00123 
00125       std::ostream &
00126       HalException::dumpOn( std::ostream & str ) const
00127       {
00128         if(!e_name.empty() && !e_msg.empty())
00129           return str << msg() << ": " << e_msg << " (" << e_name << ")";
00130         else if(!e_msg.empty())
00131           return str << msg() << ": " << e_msg;
00132         else
00133           return str << msg();
00134       }
00135 
00137       class HalContext_Impl
00138       {
00139       public:
00140         HalContext_Impl(bool monitorable = false);
00141         ~HalContext_Impl();
00142 
00143         DBusConnection *conn;
00144         LibHalContext  *hctx;
00145         bool            pcon; // private connection
00146       };
00147 
00148 
00150       class HalDrive_Impl
00151       {
00152       public:
00153         zypp::RW_pointer<HalContext_Impl>  hal;
00154         LibHalDrive                       *drv;
00155 
00156         HalDrive_Impl()
00157           : hal(), drv(NULL)
00158         {
00159         }
00160 
00161         HalDrive_Impl(const zypp::RW_pointer<HalContext_Impl> &r,
00162                       LibHalDrive *d)
00163           : hal(r), drv(d)
00164         {
00165         }
00166 
00167         ~HalDrive_Impl()
00168         {
00169           if( drv)
00170             libhal_drive_free(drv);
00171         }
00172       };
00173 
00174 
00176       class HalVolume_Impl
00177       {
00178       public:
00179         LibHalVolume *vol;
00180 
00181         HalVolume_Impl(LibHalVolume *v=NULL)
00182           : vol(v)
00183         {
00184         }
00185 
00186         ~HalVolume_Impl()
00187         {
00188           if( vol)
00189             libhal_volume_free(vol);
00190         }
00191       };
00192 
00193 
00195       HalContext_Impl::HalContext_Impl(bool monitorable)
00196         : conn(NULL)
00197         , hctx(NULL)
00198         , pcon(false) // we allways use shared connections at the moment
00199       {
00200         HalError err;
00201 
00202         if( pcon)
00203           conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err.error);
00204         else
00205           conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err.error);
00206         if( !conn) {
00207           ZYPP_THROW(err.halException(
00208             _("Unable to create dbus connection")
00209           ));
00210         }
00211 
00212         if( monitorable)
00213           dbus_connection_setup_with_g_main(conn, NULL);
00214 
00215         hctx = libhal_ctx_new();
00216         if( !hctx)
00217         {
00218           if( pcon)
00219               dbus_connection_close(conn);
00220           dbus_connection_unref(conn);
00221           conn = NULL;
00222 
00223           ZYPP_THROW(HalException(
00224             _("libhal_ctx_new: Can't create libhal context")
00225           ));
00226         }
00227 
00228         if( !libhal_ctx_set_dbus_connection(hctx, conn))
00229         {
00230           libhal_ctx_free(hctx);
00231           hctx = NULL;
00232 
00233           if( pcon)
00234             dbus_connection_close(conn);
00235           dbus_connection_unref(conn);
00236           conn = NULL;
00237 
00238           ZYPP_THROW(HalException(
00239             _("libhal_set_dbus_connection: Can't set dbus connection")
00240           ));
00241         }
00242 
00243         if( !libhal_ctx_init(hctx, &err.error))
00244         {
00245           libhal_ctx_free(hctx);
00246           hctx = NULL;
00247 
00248           if( pcon)
00249             dbus_connection_close(conn);
00250           dbus_connection_unref(conn);
00251           conn = NULL;
00252 
00253           ZYPP_THROW(err.halException(
00254             _("Unable to initalize HAL context -- hald not running?")
00255           ));
00256         }
00257       }
00258 
00259       // -------------------------------------------------------------
00260       HalContext_Impl::~HalContext_Impl()
00261       {
00262         if( hctx)
00263         {
00264           HalError err;
00265           libhal_ctx_shutdown(hctx, &err.error);
00266           libhal_ctx_free( hctx);
00267         }
00268         if( conn)
00269         {
00270           if( pcon)
00271             dbus_connection_close(conn);
00272           dbus_connection_unref(conn);
00273         }
00274       }
00275 
00276 
00278       HalContext::HalContext(bool autoconnect)
00279         : h_impl( NULL)
00280       {
00281         MutexLock lock(g_Mutex);
00282 
00283         if( autoconnect)
00284           h_impl.reset( new HalContext_Impl());
00285       }
00286 
00287       // -------------------------------------------------------------
00288       HalContext::HalContext(const HalContext &context)
00289         : h_impl( NULL)
00290       {
00291         MutexLock lock(g_Mutex);
00292 
00293         zypp::RW_pointer<HalContext_Impl>(context.h_impl).swap(h_impl);
00294       }
00295 
00296       // -------------------------------------------------------------
00297       HalContext::HalContext(bool autoconnect, bool monitorable)
00298         : h_impl( NULL)
00299       {
00300         MutexLock lock(g_Mutex);
00301 
00302         if( autoconnect)
00303           h_impl.reset( new HalContext_Impl(monitorable));
00304       }
00305 
00306       // -------------------------------------------------------------
00307       HalContext::~HalContext()
00308       {
00309         MutexLock  lock(g_Mutex);
00310 
00311         h_impl.reset();
00312       }
00313 
00314       // --------------------------------------------------------------
00315       HalContext &
00316       HalContext::operator=(const HalContext &context)
00317       {
00318         MutexLock  lock(g_Mutex);
00319 
00320         if( this == &context)
00321           return *this;
00322 
00323         zypp::RW_pointer<HalContext_Impl>(context.h_impl).swap(h_impl);
00324         return *this;
00325       }
00326 
00327       // --------------------------------------------------------------
00328       HalContext::operator HalContext::bool_type() const
00329       {
00330         MutexLock  lock(g_Mutex);
00331 
00332         return h_impl;
00333       }
00334 
00335       // --------------------------------------------------------------
00336       void
00337       HalContext::connect()
00338       {
00339         MutexLock lock(g_Mutex);
00340 
00341         if( !h_impl)
00342           h_impl.reset( new HalContext_Impl());
00343       }
00344 
00345       // --------------------------------------------------------------
00346       std::vector<std::string>
00347       HalContext::getAllDevices() const
00348       {
00349         MutexLock  lock(g_Mutex);
00350         VERIFY_CONTEXT(h_impl);
00351 
00352         HalError   err;
00353         char     **names;
00354         int        count = 0;
00355 
00356         names = libhal_get_all_devices( h_impl->hctx, &count, &err.error);
00357         if( !names)
00358         {
00359           ZYPP_THROW(err.halException());
00360         }
00361 
00362         std::vector<std::string> ret(names, names + count);
00363         libhal_free_string_array(names);
00364         return ret;
00365       }
00366 
00367       // --------------------------------------------------------------
00368       HalDrive
00369       HalContext::getDriveFromUDI(const std::string &udi) const
00370       {
00371         MutexLock  lock(g_Mutex);
00372         VERIFY_CONTEXT(h_impl);
00373 
00374         LibHalDrive *drv = libhal_drive_from_udi(h_impl->hctx, udi.c_str());
00375         if( drv != NULL)
00376           return HalDrive(new HalDrive_Impl( h_impl, drv));
00377         else
00378           return HalDrive();
00379       }
00380 
00381       // --------------------------------------------------------------
00382       HalVolume
00383       HalContext::getVolumeFromUDI(const std::string &udi) const
00384       {
00385         MutexLock  lock(g_Mutex);
00386         VERIFY_CONTEXT(h_impl);
00387 
00388         LibHalVolume *vol = libhal_volume_from_udi(h_impl->hctx, udi.c_str());
00389         if( vol)
00390           return HalVolume( new HalVolume_Impl(vol));
00391         else
00392           return HalVolume();
00393       }
00394 
00395       // --------------------------------------------------------------
00396       HalVolume
00397       HalContext::getVolumeFromDeviceFile(const std::string &device_file) const
00398       {
00399         MutexLock  lock(g_Mutex);
00400         VERIFY_CONTEXT(h_impl);
00401 
00402         LibHalVolume *vol = libhal_volume_from_device_file(h_impl->hctx,
00403                                                            device_file.c_str());
00404         if( vol)
00405           return HalVolume( new HalVolume_Impl(vol));
00406         else
00407           return HalVolume();
00408       }
00409 
00410       // --------------------------------------------------------------
00411       std::vector<std::string>
00412       HalContext::findDevicesByCapability(const std::string &capability) const
00413       {
00414         MutexLock  lock(g_Mutex);
00415         VERIFY_CONTEXT(h_impl);
00416 
00417         HalError   err;
00418         char     **names;
00419         int        count = 0;
00420 
00421         names = libhal_find_device_by_capability(h_impl->hctx,
00422                                                  capability.c_str(),
00423                                                  &count, &err.error);
00424         if( !names)
00425         {
00426           ZYPP_THROW(err.halException());
00427         }
00428 
00429         std::vector<std::string> ret(names, names + count);
00430         libhal_free_string_array(names);
00431         return ret;
00432       }
00433 
00434       // --------------------------------------------------------------
00435       bool
00436       HalContext::getDevicePropertyBool  (const std::string &udi,
00437                                           const std::string &key) const
00438       {
00439         MutexLock  lock(g_Mutex);
00440         VERIFY_CONTEXT(h_impl);
00441 
00442         HalError      err;
00443         dbus_bool_t   ret;
00444 
00445         ret = libhal_device_get_property_bool  (h_impl->hctx,
00446                                                 udi.c_str(),
00447                                                 key.c_str(),
00448                                                 &err.error);
00449         if( err.isSet())
00450         {
00451           ZYPP_THROW(err.halException());
00452         }
00453         return ret;
00454       }
00455 
00456       // --------------------------------------------------------------
00457       int32_t
00458       HalContext::getDevicePropertyInt32 (const std::string &udi,
00459                                           const std::string &key) const
00460       {
00461         MutexLock  lock(g_Mutex);
00462         VERIFY_CONTEXT(h_impl);
00463 
00464         HalError      err;
00465         dbus_int32_t  ret;
00466 
00467         ret = libhal_device_get_property_int   (h_impl->hctx,
00468                                                 udi.c_str(),
00469                                                 key.c_str(),
00470                                                 &err.error);
00471         if( err.isSet())
00472         {
00473           ZYPP_THROW(err.halException());
00474         }
00475         return ret;
00476       }
00477 
00478       // --------------------------------------------------------------
00479       uint64_t
00480       HalContext::getDevicePropertyUInt64(const std::string &udi,
00481                                           const std::string &key) const
00482       {
00483         MutexLock  lock(g_Mutex);
00484         VERIFY_CONTEXT(h_impl);
00485 
00486         HalError      err;
00487         dbus_uint64_t ret;
00488 
00489         ret = libhal_device_get_property_uint64(h_impl->hctx,
00490                                                 udi.c_str(),
00491                                                 key.c_str(),
00492                                                 &err.error);
00493         if( err.isSet())
00494         {
00495           ZYPP_THROW(err.halException());
00496         }
00497         return ret;
00498       }
00499 
00500       // --------------------------------------------------------------
00501       double
00502       HalContext::getDevicePropertyDouble(const std::string &udi,
00503                                           const std::string &key) const
00504       {
00505         MutexLock  lock(g_Mutex);
00506         VERIFY_CONTEXT(h_impl);
00507 
00508         HalError      err;
00509         double        ret;
00510 
00511         ret = libhal_device_get_property_bool  (h_impl->hctx,
00512                                                 udi.c_str(),
00513                                                 key.c_str(),
00514                                                 &err.error);
00515         if( err.isSet())
00516         {
00517           ZYPP_THROW(err.halException());
00518         }
00519         return ret;
00520       }
00521 
00522 
00523       // --------------------------------------------------------------
00524       std::string
00525       HalContext::getDevicePropertyString(const std::string &udi,
00526                                           const std::string &key) const
00527       {
00528         MutexLock  lock(g_Mutex);
00529         VERIFY_CONTEXT(h_impl);
00530 
00531         HalError      err;
00532         std::string   ret;
00533         char         *ptr;
00534 
00535         ptr = libhal_device_get_property_string(h_impl->hctx,
00536                                                 udi.c_str(),
00537                                                 key.c_str(),
00538                                                 &err.error);
00539         if( err.isSet())
00540         {
00541           ZYPP_THROW(err.halException());
00542         }
00543         if( ptr != NULL)
00544         {
00545           ret = ptr;
00546           free(ptr);
00547         }
00548         return ret;
00549       }
00550 
00551       // --------------------------------------------------------------
00552       void
00553       HalContext::setDevicePropertyBool  (const std::string &udi,
00554                                           const std::string &key,
00555                                           bool               value)
00556       {
00557         MutexLock  lock(g_Mutex);
00558         VERIFY_CONTEXT(h_impl);
00559 
00560         HalError      err;
00561         dbus_bool_t   ret;
00562 
00563         ret = libhal_device_set_property_bool  (h_impl->hctx,
00564                                                 udi.c_str(),
00565                                                 key.c_str(),
00566                                                 value ? 1 : 0,
00567                                                 &err.error);
00568         if( !ret)
00569         {
00570           ZYPP_THROW(err.halException());
00571         }
00572       }
00573 
00574       // --------------------------------------------------------------
00575       void
00576       HalContext::setDevicePropertyInt32 (const std::string &udi,
00577                                           const std::string &key,
00578                                           int32_t            value)
00579       {
00580         MutexLock  lock(g_Mutex);
00581         VERIFY_CONTEXT(h_impl);
00582 
00583         HalError      err;
00584         dbus_bool_t   ret;
00585 
00586         ret = libhal_device_set_property_int   (h_impl->hctx,
00587                                                 udi.c_str(),
00588                                                 key.c_str(),
00589                                                 value,
00590                                                 &err.error);
00591         if( !ret)
00592         {
00593           ZYPP_THROW(err.halException());
00594         }
00595       }
00596 
00597       // --------------------------------------------------------------
00598       void
00599       HalContext::setDevicePropertyUInt64(const std::string &udi,
00600                                           const std::string &key,
00601                                           uint64_t           value)
00602       {
00603         MutexLock  lock(g_Mutex);
00604         VERIFY_CONTEXT(h_impl);
00605 
00606         HalError      err;
00607         dbus_bool_t   ret;
00608 
00609         ret = libhal_device_set_property_uint64(h_impl->hctx,
00610                                                 udi.c_str(),
00611                                                 key.c_str(),
00612                                                 value,
00613                                                 &err.error);
00614         if( !ret)
00615         {
00616           ZYPP_THROW(err.halException());
00617         }
00618       }
00619 
00620       // --------------------------------------------------------------
00621       void
00622       HalContext::setDevicePropertyDouble(const std::string &udi,
00623                                           const std::string &key,
00624                                           double             value)
00625       {
00626         MutexLock  lock(g_Mutex);
00627         VERIFY_CONTEXT(h_impl);
00628 
00629         HalError      err;
00630         dbus_bool_t   ret;
00631 
00632         ret = libhal_device_set_property_double(h_impl->hctx,
00633                                                 udi.c_str(),
00634                                                 key.c_str(),
00635                                                 value,
00636                                                 &err.error);
00637         if( !ret)
00638         {
00639           ZYPP_THROW(err.halException());
00640         }
00641       }
00642 
00643       // --------------------------------------------------------------
00644       void
00645       HalContext::setDevicePropertyString(const std::string &udi,
00646                                           const std::string &key,
00647                                           const std::string &value)
00648       {
00649         MutexLock  lock(g_Mutex);
00650         VERIFY_CONTEXT(h_impl);
00651 
00652         HalError      err;
00653         dbus_bool_t   ret;
00654 
00655         ret = libhal_device_set_property_string(h_impl->hctx,
00656                                                 udi.c_str(),
00657                                                 key.c_str(),
00658                                                 value.c_str(),
00659                                                 &err.error);
00660         if( !ret)
00661         {
00662           ZYPP_THROW(err.halException());
00663         }
00664       }
00665 
00666       // --------------------------------------------------------------
00667       void
00668       HalContext::removeDeviceProperty(const std::string &udi,
00669                                        const std::string &key)
00670       {
00671         MutexLock  lock(g_Mutex);
00672         VERIFY_CONTEXT(h_impl);
00673 
00674         HalError      err;
00675         dbus_bool_t   ret;
00676 
00677         ret = libhal_device_remove_property(h_impl->hctx,
00678                                             udi.c_str(),
00679                                             key.c_str(),
00680                                             &err.error);
00681         if( !ret)
00682         {
00683           ZYPP_THROW(err.halException());
00684         }
00685       }
00686 
00688       HalDrive::HalDrive()
00689         : d_impl( NULL)
00690       {
00691       }
00692 
00693       // --------------------------------------------------------------
00694       HalDrive::HalDrive(HalDrive_Impl *impl)
00695         : d_impl( NULL)
00696       {
00697         MutexLock  lock(g_Mutex);
00698 
00699         d_impl.reset(impl);
00700       }
00701 
00702       // --------------------------------------------------------------
00703       HalDrive::HalDrive(const HalDrive &drive)
00704         : d_impl( NULL)
00705       {
00706         MutexLock  lock(g_Mutex);
00707 
00708         zypp::RW_pointer<HalDrive_Impl>(drive.d_impl).swap(d_impl);
00709       }
00710 
00711       // --------------------------------------------------------------
00712       HalDrive::~HalDrive()
00713       {
00714         MutexLock  lock(g_Mutex);
00715 
00716         d_impl.reset();
00717       }
00718 
00719       // --------------------------------------------------------------
00720       HalDrive &
00721       HalDrive::operator=(const HalDrive &drive)
00722       {
00723         MutexLock  lock(g_Mutex);
00724 
00725         if( this == &drive)
00726           return *this;
00727 
00728         zypp::RW_pointer<HalDrive_Impl>(drive.d_impl).swap(d_impl);
00729         return *this;
00730       }
00731 
00732       // --------------------------------------------------------------
00733       HalDrive::operator HalDrive::bool_type() const
00734       {
00735         MutexLock  lock(g_Mutex);
00736 
00737         return d_impl;
00738       }
00739 
00740       // --------------------------------------------------------------
00741       std::string
00742       HalDrive::getUDI() const
00743       {
00744         MutexLock  lock(g_Mutex);
00745         VERIFY_DRIVE(d_impl);
00746 
00747         const char *ptr = libhal_drive_get_udi(d_impl->drv);
00748         return std::string(ptr ? ptr : "");
00749       }
00750 
00751       // --------------------------------------------------------------
00752       std::string
00753       HalDrive::getTypeName() const
00754       {
00755         MutexLock  lock(g_Mutex);
00756         VERIFY_DRIVE(d_impl);
00757 
00758         const char *ptr = libhal_drive_get_type_textual(d_impl->drv);
00759         return std::string(ptr ? ptr : "");
00760       }
00761 
00762       // --------------------------------------------------------------
00763       std::string
00764       HalDrive::getDeviceFile() const
00765       {
00766         MutexLock  lock(g_Mutex);
00767         VERIFY_DRIVE(d_impl);
00768 
00769         return std::string(libhal_drive_get_device_file(d_impl->drv));
00770       }
00771 
00772       // --------------------------------------------------------------
00773       unsigned int
00774       HalDrive::getDeviceMajor() const
00775       {
00776         MutexLock  lock(g_Mutex);
00777         VERIFY_DRIVE(d_impl);
00778 
00779         return libhal_drive_get_device_major(d_impl->drv);
00780       }
00781 
00782       // --------------------------------------------------------------
00783       unsigned int
00784       HalDrive::getDeviceMinor() const
00785       {
00786         MutexLock  lock(g_Mutex);
00787         VERIFY_DRIVE(d_impl);
00788 
00789         return libhal_drive_get_device_minor(d_impl->drv);
00790       }
00791 
00792       // --------------------------------------------------------------
00793       bool
00794       HalDrive::usesRemovableMedia() const
00795       {
00796         MutexLock  lock(g_Mutex);
00797         VERIFY_DRIVE(d_impl);
00798 
00799         return libhal_drive_uses_removable_media(d_impl->drv);
00800       }
00801 
00802       // --------------------------------------------------------------
00803       std::vector<std::string>
00804       HalDrive::getCdromCapabilityNames() const
00805       {
00806         MutexLock  lock(g_Mutex);
00807         VERIFY_DRIVE(d_impl);
00808 
00809         std::vector<std::string> ret;
00810         LibHalDriveCdromCaps     caps;
00811 
00812         /*
00813         ** FIXME: there is no textual variant :-(
00814         **        using property key names...
00815         */
00816         caps = libhal_drive_get_cdrom_caps(d_impl->drv);
00817 
00818         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDROM)
00819           ret.push_back("cdrom");
00820         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDR)
00821           ret.push_back("cdr");
00822         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDRW)
00823           ret.push_back("cdrw");
00824         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRAM)
00825           ret.push_back("dvdram");
00826         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDROM)
00827           ret.push_back("dvd");
00828         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDR)
00829           ret.push_back("dvdr");
00830         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRW)
00831           ret.push_back("dvdrw");
00832         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR)
00833           ret.push_back("dvdplusr");
00834         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW)
00835           ret.push_back("dvdplusrw");
00836         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL)
00837           ret.push_back("dvdplusrdl");
00838 
00839         return ret;
00840 
00841 #if 0
00842         if( libhal_drive_get_type(d_impl->drv) != LIBHAL_DRIVE_TYPE_CDROM)
00843           ZYPP_THROW(HalException(_("Not a CDROM drive")));
00844 
00845         /*
00846         ** FIXME: we use property keys matching
00847         **          "storage.cdrom.cd*"
00848         **          "storage.cdrom.dvd*"
00849         ** but this may print other bool keys,
00850         ** that are not CDROM caps.
00851         */
00852         LibHalPropertySet         *props;
00853         HalError                   err;
00854 
00855         props = libhal_device_get_all_properties(d_impl->hal->hctx,
00856                                                  getUDI().c_str(),
00857                                                  &err.error);
00858         if( !props)
00859           ZYPP_THROW(err.halException());
00860 
00861         std::vector<std::string>   ret(1, getTypeName());
00862         std::string                key;
00863         std::string                dvd("storage.cdrom.dvd");
00864         std::string                cd ("storage.cdrom.cd");
00865 
00866         LibHalPropertySetIterator  it;
00867         for(libhal_psi_init(&it, props);
00868             libhal_psi_has_more(&it);
00869             libhal_psi_next(&it))
00870         {
00871           if( libhal_psi_get_type(&it) == LIBHAL_PROPERTY_TYPE_BOOLEAN &&
00872               libhal_psi_get_bool(&it))
00873           {
00874             key = libhal_psi_get_key(&it);
00875             if( key.compare(0, cd.size(), cd) == 0)
00876             {
00877               ret.push_back(key.substr(sizeof("storage.cdrom.")-1));
00878             }
00879             else
00880             if( key.compare(0, dvd.size(), dvd) == 0)
00881             {
00882               ret.push_back(key.substr(sizeof("storage.cdrom.")-1));
00883             }
00884           }
00885         }
00886         libhal_free_property_set(props);
00887 
00888         return ret;
00889 #endif
00890       }
00891 
00892       // --------------------------------------------------------------
00893       std::vector<std::string>
00894       HalDrive::findAllVolumes() const
00895       {
00896         MutexLock  lock(g_Mutex);
00897         VERIFY_DRIVE(d_impl);
00898 
00899         char     **names;
00900         int        count = 0;
00901 
00902         names = libhal_drive_find_all_volumes(d_impl->hal->hctx,
00903                                               d_impl->drv,
00904                                               &count);
00905 
00906         std::vector<std::string> ret;
00907         ret.assign(names, names + count);
00908         libhal_free_string_array(names);
00909         return ret;
00910       }
00911 
00912 
00914       HalVolume::HalVolume()
00915         : v_impl( NULL)
00916       {}
00917 
00918       HalVolume::HalVolume(HalVolume_Impl *impl)
00919         : v_impl( NULL)
00920       {
00921         MutexLock  lock(g_Mutex);
00922 
00923         v_impl.reset(impl);
00924       }
00925 
00926       // --------------------------------------------------------------
00927       HalVolume::HalVolume(const HalVolume &volume)
00928         : v_impl( NULL)
00929       {
00930         MutexLock  lock(g_Mutex);
00931 
00932         zypp::RW_pointer<HalVolume_Impl>(volume.v_impl).swap(v_impl);
00933       }
00934 
00935       // --------------------------------------------------------------
00936       HalVolume::~HalVolume()
00937       {
00938         MutexLock  lock(g_Mutex);
00939 
00940         v_impl.reset();
00941       }
00942 
00943       // --------------------------------------------------------------
00944       HalVolume &
00945       HalVolume::operator=(const HalVolume &volume)
00946       {
00947         MutexLock  lock(g_Mutex);
00948 
00949         if( this == &volume)
00950           return *this;
00951 
00952         zypp::RW_pointer<HalVolume_Impl>(volume.v_impl).swap(v_impl);
00953         return *this;
00954       }
00955 
00956       // --------------------------------------------------------------
00957       HalVolume::operator HalVolume::bool_type() const
00958       {
00959         MutexLock  lock(g_Mutex);
00960 
00961         return v_impl;
00962       }
00963 
00964       // --------------------------------------------------------------
00965       std::string
00966       HalVolume::getUDI() const
00967       {
00968         MutexLock  lock(g_Mutex);
00969         VERIFY_VOLUME(v_impl);
00970 
00971         const char *ptr = libhal_volume_get_udi(v_impl->vol);
00972         return std::string(ptr ? ptr : "");
00973       }
00974 
00975       // --------------------------------------------------------------
00976       std::string
00977       HalVolume::getDeviceFile() const
00978       {
00979         MutexLock  lock(g_Mutex);
00980         VERIFY_VOLUME(v_impl);
00981 
00982         return std::string(libhal_volume_get_device_file(v_impl->vol));
00983       }
00984 
00985       // --------------------------------------------------------------
00986       unsigned int
00987       HalVolume::getDeviceMajor() const
00988       {
00989         MutexLock  lock(g_Mutex);
00990         VERIFY_VOLUME(v_impl);
00991 
00992         return libhal_volume_get_device_major(v_impl->vol);
00993       }
00994 
00995       // --------------------------------------------------------------
00996       unsigned int
00997       HalVolume::getDeviceMinor() const
00998       {
00999         MutexLock  lock(g_Mutex);
01000         VERIFY_VOLUME(v_impl);
01001 
01002         return libhal_volume_get_device_minor(v_impl->vol);
01003       }
01004 
01005       // --------------------------------------------------------------
01006       bool
01007       HalVolume::isDisc() const
01008       {
01009         MutexLock  lock(g_Mutex);
01010         VERIFY_VOLUME(v_impl);
01011 
01012         return libhal_volume_is_disc(v_impl->vol);
01013       }
01014 
01015       // --------------------------------------------------------------
01016       bool
01017       HalVolume::isPartition() const
01018       {
01019         MutexLock  lock(g_Mutex);
01020         VERIFY_VOLUME(v_impl);
01021 
01022         return libhal_volume_is_partition(v_impl->vol);
01023       }
01024 
01025       // --------------------------------------------------------------
01026       bool
01027       HalVolume::isMounted() const
01028       {
01029         MutexLock  lock(g_Mutex);
01030         VERIFY_VOLUME(v_impl);
01031 
01032         return libhal_volume_is_mounted(v_impl->vol);
01033       }
01034 
01035       // --------------------------------------------------------------
01036       std::string
01037       HalVolume::getFSType() const
01038       {
01039         MutexLock  lock(g_Mutex);
01040         VERIFY_VOLUME(v_impl);
01041 
01042         return std::string( libhal_volume_get_fstype(v_impl->vol));
01043       }
01044 
01045       // --------------------------------------------------------------
01046       std::string
01047       HalVolume::getFSUsage() const
01048       {
01049         MutexLock  lock(g_Mutex);
01050         VERIFY_VOLUME(v_impl);
01051 
01052         LibHalVolumeUsage usage( libhal_volume_get_fsusage(v_impl->vol));
01053         std::string       ret;
01054         switch( usage)
01055         {
01056           case  LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM:
01057             ret = "filesystem";
01058           break;
01059           case LIBHAL_VOLUME_USAGE_PARTITION_TABLE:
01060             ret = "partitiontable";
01061           break;
01062           case LIBHAL_VOLUME_USAGE_RAID_MEMBER:
01063             return "raid";
01064           break;
01065           case LIBHAL_VOLUME_USAGE_CRYPTO:
01066             ret = "crypto";
01067           break;
01068           case LIBHAL_VOLUME_USAGE_UNKNOWN:
01069           default:
01070           break;
01071         }
01072         return ret;
01073       }
01074 
01075       // --------------------------------------------------------------
01076       std::string
01077       HalVolume::getMountPoint() const
01078       {
01079         VERIFY_VOLUME(v_impl);
01080 
01081         return std::string( libhal_volume_get_mount_point(v_impl->vol));
01082       }
01083 
01084 
01086     } // namespace hal
01089   } // namespace target
01092 } // namespace zypp
01094 #else // FAKE_HAL
01095 #include <zypp/target/hal/HalContext.h>
01096 #include <zypp/target/hal/HalException.h>
01097 namespace zypp
01098 { 
01099 
01100   namespace target
01101   { 
01102 
01103     namespace hal
01104     { 
01105 
01106       std::ostream &
01107       HalException::dumpOn( std::ostream & str ) const
01108       { return str; }
01109 
01110       // --------------------------------------------------------------
01111       class HalContext_Impl
01112       {};
01113       class HalDrive_Impl
01114       {};
01115       class HalVolume_Impl
01116       {};
01117 
01118       // --------------------------------------------------------------
01119       HalContext::HalContext(bool)
01120       {}
01121       HalContext::~HalContext()
01122       {}
01123       HalContext &
01124       HalContext::operator=(const HalContext &)
01125       { return *this; }
01126       HalContext::operator HalContext::bool_type() const
01127       { return 0; }
01128       void
01129       HalContext::connect()
01130       {}
01131       std::vector<std::string>
01132       HalContext::getAllDevices() const
01133       { return std::vector<std::string>(); }
01134       HalDrive
01135       HalContext::getDriveFromUDI(const std::string &) const
01136       { return HalDrive(); }
01137       HalVolume
01138       HalContext::getVolumeFromUDI(const std::string &) const
01139       { return HalVolume(); }
01140       HalVolume
01141       HalContext::getVolumeFromDeviceFile(const std::string &) const
01142       { return HalVolume(); }
01143       std::vector<std::string>
01144       HalContext::findDevicesByCapability(const std::string &) const
01145       { return std::vector<std::string>(); }
01146       bool
01147       HalContext::getDevicePropertyBool(const std::string &, const std::string &) const
01148       { return false; }
01149       void
01150       HalContext::setDevicePropertyBool  (const std::string &, const std::string &, bool value)
01151       {}
01152       void
01153       HalContext::removeDeviceProperty(const std::string &, const std::string &)
01154       {}
01155       std::string
01156       HalContext::getDevicePropertyString(const std::string &, const std::string &) const
01157       { return ""; }
01158       // --------------------------------------------------------------
01159       HalDrive::HalDrive()
01160       {}
01161       HalDrive::~HalDrive()
01162       {}
01163       HalDrive &
01164       HalDrive::operator=(const HalDrive &)
01165       { return *this; }
01166       HalDrive::operator HalDrive::bool_type() const
01167       { return 0; }
01168       std::string
01169       HalDrive::getUDI() const
01170       { return std::string(); }
01171       std::string
01172       HalDrive::getTypeName() const
01173       { return std::string(); }
01174       std::string
01175       HalDrive::getDeviceFile() const
01176       { return std::string(); }
01177       unsigned int
01178       HalDrive::getDeviceMinor() const
01179       { return 0; }
01180       unsigned int
01181       HalDrive::getDeviceMajor() const
01182       { return 0; }
01183       bool
01184       HalDrive::usesRemovableMedia() const
01185       { return false; }
01186       std::vector<std::string>
01187       HalDrive::getCdromCapabilityNames() const
01188       { return std::vector<std::string>(); }
01189       std::vector<std::string>
01190       HalDrive::findAllVolumes() const
01191       { return std::vector<std::string>(); }
01192 
01193       // --------------------------------------------------------------
01194       HalVolume::HalVolume()
01195       {}
01196       HalVolume::~HalVolume()
01197       {}
01198       HalVolume &
01199       HalVolume::operator=(const HalVolume &)
01200       { return *this; }
01201       HalVolume::operator HalVolume::bool_type() const
01202       { return 0; }
01203       std::string
01204       HalVolume::getUDI() const
01205       { return std::string(); }
01206       std::string
01207       HalVolume::getDeviceFile() const
01208       { return std::string(); }
01209       unsigned int
01210       HalVolume::getDeviceMinor() const
01211       { return 0; }
01212       unsigned int
01213       HalVolume::getDeviceMajor() const
01214       { return 0; }
01215       bool
01216       HalVolume::isDisc() const
01217       { return false; }
01218       bool
01219       HalVolume::isPartition() const
01220       { return false; }
01221       bool
01222       HalVolume::isMounted() const
01223       { return false; }
01224       std::string
01225       HalVolume::getFSType() const
01226       { return std::string(); }
01227       std::string
01228       HalVolume::getFSUsage() const
01229       { return std::string(); }
01230       std::string
01231       HalVolume::getMountPoint() const
01232       { return std::string(); }
01233 
01235     } // namespace hal
01238   } // namespace target
01241 } // namespace zypp
01243 #endif // FAKE_HAL
01244 
01245 /*
01246 ** vim: set ts=2 sts=2 sw=2 ai et:
01247 */

Generated on Tue Sep 25 19:23:09 2007 for libzypp by  doxygen 1.5.3