Remove most traces of glib.
[platform/upstream/libzypp.git] / zypp / target / hal / HalContext.cc
1 /*---------------------------------------------------------------------\
2 |                          ____ _   __ __ ___                          |
3 |                         |__  / \ / / . \ . \                         |
4 |                           / / \ V /|  _/  _/                         |
5 |                          / /__ | | | | | |                           |
6 |                         /_____||_| |_| |_|                           |
7 |                                                                      |
8 \---------------------------------------------------------------------*/
9 /** \file zypp/target/hal/HalContext.cc
10  *
11  *  \brief Hardware abstaction layer library wrapper implementation.
12  */
13 #ifndef FAKE_HAL // disables zypp's HAL dependency
14
15 #include <zypp/target/hal/HalContext.h>
16 #include <zypp/thread/Mutex.h>
17 #include <zypp/thread/MutexLock.h>
18 #include <zypp/base/NonCopyable.h>
19 #include <zypp/base/Logger.h>
20 #include <zypp/base/String.h>
21 #include <zypp/base/Gettext.h>
22
23 #include <hal/libhal.h>
24 #include <hal/libhal-storage.h>
25
26 #include <iostream>
27
28 using namespace std;
29
30 //////////////////////////////////////////////////////////////////////
31 namespace zypp
32 { ////////////////////////////////////////////////////////////////////
33   ////////////////////////////////////////////////////////////////////
34   namespace target
35   { //////////////////////////////////////////////////////////////////
36     //////////////////////////////////////////////////////////////////
37     namespace hal
38     { ////////////////////////////////////////////////////////////////
39
40       using zypp::thread::Mutex;
41       using zypp::thread::MutexLock;
42
43       ////////////////////////////////////////////////////////////////
44       namespace // anonymous
45       { //////////////////////////////////////////////////////////////
46
47
48         //////////////////////////////////////////////////////////////
49         // STATIC
50         /**
51          ** hmm... currently a global one..
52         */
53         static Mutex g_Mutex;
54
55
56         //////////////////////////////////////////////////////////////
57         /**
58          * Internal hal (dbus ) error helper class.
59          */
60         class HalError
61         {
62         public:
63           DBusError error;
64
65           HalError()  { dbus_error_init(&error); }
66           ~HalError() { dbus_error_free(&error); }
67
68           inline bool         isSet() const
69           {
70             return dbus_error_is_set(&error);
71           }
72
73           inline HalException halException(const std::string &msg = std::string()) const
74           {
75             if( isSet() && error.name != NULL && error.message != NULL) {
76               return HalException(error.name, error.message);
77             }
78             else if( !msg.empty()) {
79               return HalException(msg);
80             }
81             else {
82               return HalException();
83             }
84           }
85         };
86
87
88         // -----------------------------------------------------------
89         inline void
90         VERIFY_CONTEXT(const zypp::RW_pointer<HalContext_Impl> &h)
91         {
92           if( !h)
93           {
94             ZYPP_THROW(HalException(_("HalContext not connected")));
95           }
96         }
97
98         // -----------------------------------------------------------
99         inline void
100         VERIFY_DRIVE(const zypp::RW_pointer<HalDrive_Impl> &d)
101         {
102           if( !d)
103           {
104             ZYPP_THROW(HalException(_("HalDrive not initialized")));
105           }
106         }
107
108         // -----------------------------------------------------------
109         inline void
110         VERIFY_VOLUME(const zypp::RW_pointer<HalVolume_Impl> &v)
111         {
112           if( !v)
113           {
114             ZYPP_THROW(HalException(_("HalVolume not initialized")));
115           }
116         }
117
118         //////////////////////////////////////////////////////////////
119       } // anonymous
120       ////////////////////////////////////////////////////////////////
121
122       ////////////////////////////////////////////////////////////////
123       std::ostream &
124       HalException::dumpOn( std::ostream & str ) const
125       {
126         if(!e_name.empty() && !e_msg.empty())
127           return str << msg() << ": " << e_msg << " (" << e_name << ")";
128         else if(!e_msg.empty())
129           return str << msg() << ": " << e_msg;
130         else
131           return str << msg();
132       }
133
134       ////////////////////////////////////////////////////////////////
135       class HalContext_Impl
136       {
137       public:
138         HalContext_Impl();
139         ~HalContext_Impl();
140
141         DBusConnection *conn;
142         LibHalContext  *hctx;
143         bool            pcon; // private connection
144       };
145
146
147       ////////////////////////////////////////////////////////////////
148       class HalDrive_Impl
149       {
150       public:
151         zypp::RW_pointer<HalContext_Impl>  hal;
152         LibHalDrive                       *drv;
153
154         HalDrive_Impl()
155           : hal(), drv(NULL)
156         {
157         }
158
159         HalDrive_Impl(const zypp::RW_pointer<HalContext_Impl> &r,
160                       LibHalDrive *d)
161           : hal(r), drv(d)
162         {
163         }
164
165         ~HalDrive_Impl()
166         {
167           if( drv)
168             libhal_drive_free(drv);
169         }
170       };
171
172
173       ////////////////////////////////////////////////////////////////
174       class HalVolume_Impl
175       {
176       public:
177         LibHalVolume *vol;
178
179         HalVolume_Impl(LibHalVolume *v=NULL)
180           : vol(v)
181         {
182         }
183
184         ~HalVolume_Impl()
185         {
186           if( vol)
187             libhal_volume_free(vol);
188         }
189       };
190
191
192       ////////////////////////////////////////////////////////////////
193       HalContext_Impl::HalContext_Impl()
194         : conn(NULL)
195         , hctx(NULL)
196         , pcon(false) // we allways use shared connections at the moment
197       {
198         HalError err;
199
200         if( pcon)
201           conn = dbus_bus_get_private(DBUS_BUS_SYSTEM, &err.error);
202         else
203           conn = dbus_bus_get(DBUS_BUS_SYSTEM, &err.error);
204         if( !conn) {
205           ZYPP_THROW(err.halException(
206             _("Unable to create dbus connection")
207           ));
208         }
209
210         hctx = libhal_ctx_new();
211         if( !hctx)
212         {
213           if( pcon)
214               dbus_connection_close(conn);
215           dbus_connection_unref(conn);
216           conn = NULL;
217
218           ZYPP_THROW(HalException(
219             _("libhal_ctx_new: Can't create libhal context")
220           ));
221         }
222
223         if( !libhal_ctx_set_dbus_connection(hctx, conn))
224         {
225           libhal_ctx_free(hctx);
226           hctx = NULL;
227
228           if( pcon)
229             dbus_connection_close(conn);
230           dbus_connection_unref(conn);
231           conn = NULL;
232
233           ZYPP_THROW(HalException(
234             _("libhal_set_dbus_connection: Can't set dbus connection")
235           ));
236         }
237
238         if( !libhal_ctx_init(hctx, &err.error))
239         {
240           libhal_ctx_free(hctx);
241           hctx = NULL;
242
243           if( pcon)
244             dbus_connection_close(conn);
245           dbus_connection_unref(conn);
246           conn = NULL;
247
248           ZYPP_THROW(err.halException(
249             _("Unable to initalize HAL context -- hald not running?")
250           ));
251         }
252       }
253
254       // -------------------------------------------------------------
255       HalContext_Impl::~HalContext_Impl()
256       {
257         if( hctx)
258         {
259           HalError err;
260           libhal_ctx_shutdown(hctx, &err.error);
261           libhal_ctx_free( hctx);
262         }
263         if( conn)
264         {
265           if( pcon)
266             dbus_connection_close(conn);
267           dbus_connection_unref(conn);
268         }
269       }
270
271
272       ////////////////////////////////////////////////////////////////
273       HalContext::HalContext(bool autoconnect)
274         : h_impl( NULL)
275       {
276         MutexLock lock(g_Mutex);
277
278         if( autoconnect)
279           h_impl.reset( new HalContext_Impl());
280       }
281
282       // -------------------------------------------------------------
283       HalContext::HalContext(const HalContext &context)
284         : h_impl( NULL)
285       {
286         MutexLock lock(g_Mutex);
287
288         zypp::RW_pointer<HalContext_Impl>(context.h_impl).swap(h_impl);
289       }
290
291       // -------------------------------------------------------------
292       HalContext::~HalContext()
293       {
294         MutexLock  lock(g_Mutex);
295
296         h_impl.reset();
297       }
298
299       // --------------------------------------------------------------
300       HalContext &
301       HalContext::operator=(const HalContext &context)
302       {
303         MutexLock  lock(g_Mutex);
304
305         if( this == &context)
306           return *this;
307
308         zypp::RW_pointer<HalContext_Impl>(context.h_impl).swap(h_impl);
309         return *this;
310       }
311
312       // --------------------------------------------------------------
313       HalContext::operator HalContext::bool_type() const
314       {
315         MutexLock  lock(g_Mutex);
316
317         return h_impl;
318       }
319
320       // --------------------------------------------------------------
321       void
322       HalContext::connect()
323       {
324         MutexLock lock(g_Mutex);
325
326         if( !h_impl)
327           h_impl.reset( new HalContext_Impl());
328       }
329
330       // --------------------------------------------------------------
331       std::vector<std::string>
332       HalContext::getAllDevices() const
333       {
334         MutexLock  lock(g_Mutex);
335         VERIFY_CONTEXT(h_impl);
336
337         HalError   err;
338         char     **names;
339         int        count = 0;
340
341         names = libhal_get_all_devices( h_impl->hctx, &count, &err.error);
342         if( !names)
343         {
344           ZYPP_THROW(err.halException());
345         }
346
347         std::vector<std::string> ret(names, names + count);
348         libhal_free_string_array(names);
349         return ret;
350       }
351
352       // --------------------------------------------------------------
353       HalDrive
354       HalContext::getDriveFromUDI(const std::string &udi) const
355       {
356         MutexLock  lock(g_Mutex);
357         VERIFY_CONTEXT(h_impl);
358
359         LibHalDrive *drv = libhal_drive_from_udi(h_impl->hctx, udi.c_str());
360         if( drv != NULL)
361           return HalDrive(new HalDrive_Impl( h_impl, drv));
362         else
363           return HalDrive();
364       }
365
366       // --------------------------------------------------------------
367       HalVolume
368       HalContext::getVolumeFromUDI(const std::string &udi) const
369       {
370         MutexLock  lock(g_Mutex);
371         VERIFY_CONTEXT(h_impl);
372
373         LibHalVolume *vol = libhal_volume_from_udi(h_impl->hctx, udi.c_str());
374         if( vol)
375           return HalVolume( new HalVolume_Impl(vol));
376         else
377           return HalVolume();
378       }
379
380       // --------------------------------------------------------------
381       HalVolume
382       HalContext::getVolumeFromDeviceFile(const std::string &device_file) const
383       {
384         MutexLock  lock(g_Mutex);
385         VERIFY_CONTEXT(h_impl);
386
387         LibHalVolume *vol = libhal_volume_from_device_file(h_impl->hctx,
388                                                            device_file.c_str());
389         if( vol)
390           return HalVolume( new HalVolume_Impl(vol));
391         else
392           return HalVolume();
393       }
394
395       // --------------------------------------------------------------
396       std::vector<std::string>
397       HalContext::findDevicesByCapability(const std::string &capability) const
398       {
399         MutexLock  lock(g_Mutex);
400         VERIFY_CONTEXT(h_impl);
401
402         HalError   err;
403         char     **names;
404         int        count = 0;
405
406         names = libhal_find_device_by_capability(h_impl->hctx,
407                                                  capability.c_str(),
408                                                  &count, &err.error);
409         if( !names)
410         {
411           ZYPP_THROW(err.halException());
412         }
413
414         std::vector<std::string> ret(names, names + count);
415         libhal_free_string_array(names);
416         return ret;
417       }
418
419       // --------------------------------------------------------------
420       bool
421       HalContext::getDevicePropertyBool  (const std::string &udi,
422                                           const std::string &key) const
423       {
424         MutexLock  lock(g_Mutex);
425         VERIFY_CONTEXT(h_impl);
426
427         HalError      err;
428         dbus_bool_t   ret;
429
430         ret = libhal_device_get_property_bool  (h_impl->hctx,
431                                                 udi.c_str(),
432                                                 key.c_str(),
433                                                 &err.error);
434         if( err.isSet())
435         {
436           ZYPP_THROW(err.halException());
437         }
438         return ret;
439       }
440
441       // --------------------------------------------------------------
442       int32_t
443       HalContext::getDevicePropertyInt32 (const std::string &udi,
444                                           const std::string &key) const
445       {
446         MutexLock  lock(g_Mutex);
447         VERIFY_CONTEXT(h_impl);
448
449         HalError      err;
450         dbus_int32_t  ret;
451
452         ret = libhal_device_get_property_int   (h_impl->hctx,
453                                                 udi.c_str(),
454                                                 key.c_str(),
455                                                 &err.error);
456         if( err.isSet())
457         {
458           ZYPP_THROW(err.halException());
459         }
460         return ret;
461       }
462
463       // --------------------------------------------------------------
464       uint64_t
465       HalContext::getDevicePropertyUInt64(const std::string &udi,
466                                           const std::string &key) const
467       {
468         MutexLock  lock(g_Mutex);
469         VERIFY_CONTEXT(h_impl);
470
471         HalError      err;
472         dbus_uint64_t ret;
473
474         ret = libhal_device_get_property_uint64(h_impl->hctx,
475                                                 udi.c_str(),
476                                                 key.c_str(),
477                                                 &err.error);
478         if( err.isSet())
479         {
480           ZYPP_THROW(err.halException());
481         }
482         return ret;
483       }
484
485       // --------------------------------------------------------------
486       double
487       HalContext::getDevicePropertyDouble(const std::string &udi,
488                                           const std::string &key) const
489       {
490         MutexLock  lock(g_Mutex);
491         VERIFY_CONTEXT(h_impl);
492
493         HalError      err;
494         double        ret;
495
496         ret = libhal_device_get_property_bool  (h_impl->hctx,
497                                                 udi.c_str(),
498                                                 key.c_str(),
499                                                 &err.error);
500         if( err.isSet())
501         {
502           ZYPP_THROW(err.halException());
503         }
504         return ret;
505       }
506
507
508       // --------------------------------------------------------------
509       std::string
510       HalContext::getDevicePropertyString(const std::string &udi,
511                                           const std::string &key) const
512       {
513         MutexLock  lock(g_Mutex);
514         VERIFY_CONTEXT(h_impl);
515
516         HalError      err;
517         std::string   ret;
518         char         *ptr;
519
520         ptr = libhal_device_get_property_string(h_impl->hctx,
521                                                 udi.c_str(),
522                                                 key.c_str(),
523                                                 &err.error);
524         if( err.isSet())
525         {
526           ZYPP_THROW(err.halException());
527         }
528         if( ptr != NULL)
529         {
530           ret = ptr;
531           free(ptr);
532         }
533         return ret;
534       }
535
536       // --------------------------------------------------------------
537       void
538       HalContext::setDevicePropertyBool  (const std::string &udi,
539                                           const std::string &key,
540                                           bool               value)
541       {
542         MutexLock  lock(g_Mutex);
543         VERIFY_CONTEXT(h_impl);
544
545         HalError      err;
546         dbus_bool_t   ret;
547
548         ret = libhal_device_set_property_bool  (h_impl->hctx,
549                                                 udi.c_str(),
550                                                 key.c_str(),
551                                                 value ? 1 : 0,
552                                                 &err.error);
553         if( !ret)
554         {
555           ZYPP_THROW(err.halException());
556         }
557       }
558
559       // --------------------------------------------------------------
560       void
561       HalContext::setDevicePropertyInt32 (const std::string &udi,
562                                           const std::string &key,
563                                           int32_t            value)
564       {
565         MutexLock  lock(g_Mutex);
566         VERIFY_CONTEXT(h_impl);
567
568         HalError      err;
569         dbus_bool_t   ret;
570
571         ret = libhal_device_set_property_int   (h_impl->hctx,
572                                                 udi.c_str(),
573                                                 key.c_str(),
574                                                 value,
575                                                 &err.error);
576         if( !ret)
577         {
578           ZYPP_THROW(err.halException());
579         }
580       }
581
582       // --------------------------------------------------------------
583       void
584       HalContext::setDevicePropertyUInt64(const std::string &udi,
585                                           const std::string &key,
586                                           uint64_t           value)
587       {
588         MutexLock  lock(g_Mutex);
589         VERIFY_CONTEXT(h_impl);
590
591         HalError      err;
592         dbus_bool_t   ret;
593
594         ret = libhal_device_set_property_uint64(h_impl->hctx,
595                                                 udi.c_str(),
596                                                 key.c_str(),
597                                                 value,
598                                                 &err.error);
599         if( !ret)
600         {
601           ZYPP_THROW(err.halException());
602         }
603       }
604
605       // --------------------------------------------------------------
606       void
607       HalContext::setDevicePropertyDouble(const std::string &udi,
608                                           const std::string &key,
609                                           double             value)
610       {
611         MutexLock  lock(g_Mutex);
612         VERIFY_CONTEXT(h_impl);
613
614         HalError      err;
615         dbus_bool_t   ret;
616
617         ret = libhal_device_set_property_double(h_impl->hctx,
618                                                 udi.c_str(),
619                                                 key.c_str(),
620                                                 value,
621                                                 &err.error);
622         if( !ret)
623         {
624           ZYPP_THROW(err.halException());
625         }
626       }
627
628       // --------------------------------------------------------------
629       void
630       HalContext::setDevicePropertyString(const std::string &udi,
631                                           const std::string &key,
632                                           const std::string &value)
633       {
634         MutexLock  lock(g_Mutex);
635         VERIFY_CONTEXT(h_impl);
636
637         HalError      err;
638         dbus_bool_t   ret;
639
640         ret = libhal_device_set_property_string(h_impl->hctx,
641                                                 udi.c_str(),
642                                                 key.c_str(),
643                                                 value.c_str(),
644                                                 &err.error);
645         if( !ret)
646         {
647           ZYPP_THROW(err.halException());
648         }
649       }
650
651       // --------------------------------------------------------------
652       void
653       HalContext::removeDeviceProperty(const std::string &udi,
654                                        const std::string &key)
655       {
656         MutexLock  lock(g_Mutex);
657         VERIFY_CONTEXT(h_impl);
658
659         HalError      err;
660         dbus_bool_t   ret;
661
662         ret = libhal_device_remove_property(h_impl->hctx,
663                                             udi.c_str(),
664                                             key.c_str(),
665                                             &err.error);
666         if( !ret)
667         {
668           ZYPP_THROW(err.halException());
669         }
670       }
671
672       ////////////////////////////////////////////////////////////////
673       HalDrive::HalDrive()
674         : d_impl( NULL)
675       {
676       }
677
678       // --------------------------------------------------------------
679       HalDrive::HalDrive(HalDrive_Impl *impl)
680         : d_impl( NULL)
681       {
682         MutexLock  lock(g_Mutex);
683
684         d_impl.reset(impl);
685       }
686
687       // --------------------------------------------------------------
688       HalDrive::HalDrive(const HalDrive &drive)
689         : d_impl( NULL)
690       {
691         MutexLock  lock(g_Mutex);
692
693         zypp::RW_pointer<HalDrive_Impl>(drive.d_impl).swap(d_impl);
694       }
695
696       // --------------------------------------------------------------
697       HalDrive::~HalDrive()
698       {
699         MutexLock  lock(g_Mutex);
700
701         d_impl.reset();
702       }
703
704       // --------------------------------------------------------------
705       HalDrive &
706       HalDrive::operator=(const HalDrive &drive)
707       {
708         MutexLock  lock(g_Mutex);
709
710         if( this == &drive)
711           return *this;
712
713         zypp::RW_pointer<HalDrive_Impl>(drive.d_impl).swap(d_impl);
714         return *this;
715       }
716
717       // --------------------------------------------------------------
718       HalDrive::operator HalDrive::bool_type() const
719       {
720         MutexLock  lock(g_Mutex);
721
722         return d_impl;
723       }
724
725       // --------------------------------------------------------------
726       std::string
727       HalDrive::getUDI() const
728       {
729         MutexLock  lock(g_Mutex);
730         VERIFY_DRIVE(d_impl);
731
732         const char *ptr = libhal_drive_get_udi(d_impl->drv);
733         return std::string(ptr ? ptr : "");
734       }
735
736       // --------------------------------------------------------------
737       std::string
738       HalDrive::getTypeName() const
739       {
740         MutexLock  lock(g_Mutex);
741         VERIFY_DRIVE(d_impl);
742
743         const char *ptr = libhal_drive_get_type_textual(d_impl->drv);
744         return std::string(ptr ? ptr : "");
745       }
746
747       // --------------------------------------------------------------
748       std::string
749       HalDrive::getDeviceFile() const
750       {
751         MutexLock  lock(g_Mutex);
752         VERIFY_DRIVE(d_impl);
753
754         return std::string(libhal_drive_get_device_file(d_impl->drv));
755       }
756
757       // --------------------------------------------------------------
758       unsigned int
759       HalDrive::getDeviceMajor() const
760       {
761         MutexLock  lock(g_Mutex);
762         VERIFY_DRIVE(d_impl);
763
764         return libhal_drive_get_device_major(d_impl->drv);
765       }
766
767       // --------------------------------------------------------------
768       unsigned int
769       HalDrive::getDeviceMinor() const
770       {
771         MutexLock  lock(g_Mutex);
772         VERIFY_DRIVE(d_impl);
773
774         return libhal_drive_get_device_minor(d_impl->drv);
775       }
776
777       // --------------------------------------------------------------
778       bool
779       HalDrive::usesRemovableMedia() const
780       {
781         MutexLock  lock(g_Mutex);
782         VERIFY_DRIVE(d_impl);
783
784         return libhal_drive_uses_removable_media(d_impl->drv);
785       }
786
787       // --------------------------------------------------------------
788       std::vector<std::string>
789       HalDrive::getCdromCapabilityNames() const
790       {
791         MutexLock  lock(g_Mutex);
792         VERIFY_DRIVE(d_impl);
793
794         std::vector<std::string> ret;
795         LibHalDriveCdromCaps     caps;
796
797         /*
798         ** FIXME: there is no textual variant :-(
799         **        using property key names...
800         */
801         caps = libhal_drive_get_cdrom_caps(d_impl->drv);
802
803         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDROM)
804           ret.push_back("cdrom");
805         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDR)
806           ret.push_back("cdr");
807         if(caps & LIBHAL_DRIVE_CDROM_CAPS_CDRW)
808           ret.push_back("cdrw");
809         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRAM)
810           ret.push_back("dvdram");
811         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDROM)
812           ret.push_back("dvd");
813         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDR)
814           ret.push_back("dvdr");
815         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDRW)
816           ret.push_back("dvdrw");
817         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSR)
818           ret.push_back("dvdplusr");
819         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRW)
820           ret.push_back("dvdplusrw");
821         if(caps & LIBHAL_DRIVE_CDROM_CAPS_DVDPLUSRDL)
822           ret.push_back("dvdplusrdl");
823
824         return ret;
825
826 #if 0
827         if( libhal_drive_get_type(d_impl->drv) != LIBHAL_DRIVE_TYPE_CDROM)
828           ZYPP_THROW(HalException(_("Not a CDROM drive")));
829
830         /*
831         ** FIXME: we use property keys matching
832         **          "storage.cdrom.cd*"
833         **          "storage.cdrom.dvd*"
834         ** but this may print other bool keys,
835         ** that are not CDROM caps.
836         */
837         LibHalPropertySet         *props;
838         HalError                   err;
839
840         props = libhal_device_get_all_properties(d_impl->hal->hctx,
841                                                  getUDI().c_str(),
842                                                  &err.error);
843         if( !props)
844           ZYPP_THROW(err.halException());
845
846         std::vector<std::string>   ret(1, getTypeName());
847         std::string                key;
848         std::string                dvd("storage.cdrom.dvd");
849         std::string                cd ("storage.cdrom.cd");
850
851         LibHalPropertySetIterator  it;
852         for(libhal_psi_init(&it, props);
853             libhal_psi_has_more(&it);
854             libhal_psi_next(&it))
855         {
856           if( libhal_psi_get_type(&it) == LIBHAL_PROPERTY_TYPE_BOOLEAN &&
857               libhal_psi_get_bool(&it))
858           {
859             key = libhal_psi_get_key(&it);
860             if( key.compare(0, cd.size(), cd) == 0)
861             {
862               ret.push_back(key.substr(sizeof("storage.cdrom.")-1));
863             }
864             else
865             if( key.compare(0, dvd.size(), dvd) == 0)
866             {
867               ret.push_back(key.substr(sizeof("storage.cdrom.")-1));
868             }
869           }
870         }
871         libhal_free_property_set(props);
872
873         return ret;
874 #endif
875       }
876
877       // --------------------------------------------------------------
878       std::vector<std::string>
879       HalDrive::findAllVolumes() const
880       {
881         MutexLock  lock(g_Mutex);
882         VERIFY_DRIVE(d_impl);
883
884         char     **names;
885         int        count = 0;
886
887         names = libhal_drive_find_all_volumes(d_impl->hal->hctx,
888                                               d_impl->drv,
889                                               &count);
890
891         std::vector<std::string> ret;
892         ret.assign(names, names + count);
893         libhal_free_string_array(names);
894         return ret;
895       }
896
897
898       ////////////////////////////////////////////////////////////////
899       HalVolume::HalVolume()
900         : v_impl( NULL)
901       {}
902
903       HalVolume::HalVolume(HalVolume_Impl *impl)
904         : v_impl( NULL)
905       {
906         MutexLock  lock(g_Mutex);
907
908         v_impl.reset(impl);
909       }
910
911       // --------------------------------------------------------------
912       HalVolume::HalVolume(const HalVolume &volume)
913         : v_impl( NULL)
914       {
915         MutexLock  lock(g_Mutex);
916
917         zypp::RW_pointer<HalVolume_Impl>(volume.v_impl).swap(v_impl);
918       }
919
920       // --------------------------------------------------------------
921       HalVolume::~HalVolume()
922       {
923         MutexLock  lock(g_Mutex);
924
925         v_impl.reset();
926       }
927
928       // --------------------------------------------------------------
929       HalVolume &
930       HalVolume::operator=(const HalVolume &volume)
931       {
932         MutexLock  lock(g_Mutex);
933
934         if( this == &volume)
935           return *this;
936
937         zypp::RW_pointer<HalVolume_Impl>(volume.v_impl).swap(v_impl);
938         return *this;
939       }
940
941       // --------------------------------------------------------------
942       HalVolume::operator HalVolume::bool_type() const
943       {
944         MutexLock  lock(g_Mutex);
945
946         return v_impl;
947       }
948
949       // --------------------------------------------------------------
950       std::string
951       HalVolume::getUDI() const
952       {
953         MutexLock  lock(g_Mutex);
954         VERIFY_VOLUME(v_impl);
955
956         const char *ptr = libhal_volume_get_udi(v_impl->vol);
957         return std::string(ptr ? ptr : "");
958       }
959
960       // --------------------------------------------------------------
961       std::string
962       HalVolume::getDeviceFile() const
963       {
964         MutexLock  lock(g_Mutex);
965         VERIFY_VOLUME(v_impl);
966
967         return std::string(libhal_volume_get_device_file(v_impl->vol));
968       }
969
970       // --------------------------------------------------------------
971       unsigned int
972       HalVolume::getDeviceMajor() const
973       {
974         MutexLock  lock(g_Mutex);
975         VERIFY_VOLUME(v_impl);
976
977         return libhal_volume_get_device_major(v_impl->vol);
978       }
979
980       // --------------------------------------------------------------
981       unsigned int
982       HalVolume::getDeviceMinor() const
983       {
984         MutexLock  lock(g_Mutex);
985         VERIFY_VOLUME(v_impl);
986
987         return libhal_volume_get_device_minor(v_impl->vol);
988       }
989
990       // --------------------------------------------------------------
991       bool
992       HalVolume::isDisc() const
993       {
994         MutexLock  lock(g_Mutex);
995         VERIFY_VOLUME(v_impl);
996
997         return libhal_volume_is_disc(v_impl->vol);
998       }
999
1000       // --------------------------------------------------------------
1001       bool
1002       HalVolume::isPartition() const
1003       {
1004         MutexLock  lock(g_Mutex);
1005         VERIFY_VOLUME(v_impl);
1006
1007         return libhal_volume_is_partition(v_impl->vol);
1008       }
1009
1010       // --------------------------------------------------------------
1011       bool
1012       HalVolume::isMounted() const
1013       {
1014         MutexLock  lock(g_Mutex);
1015         VERIFY_VOLUME(v_impl);
1016
1017         return libhal_volume_is_mounted(v_impl->vol);
1018       }
1019
1020       // --------------------------------------------------------------
1021       std::string
1022       HalVolume::getFSType() const
1023       {
1024         MutexLock  lock(g_Mutex);
1025         VERIFY_VOLUME(v_impl);
1026
1027         return std::string( libhal_volume_get_fstype(v_impl->vol));
1028       }
1029
1030       // --------------------------------------------------------------
1031       std::string
1032       HalVolume::getFSUsage() const
1033       {
1034         MutexLock  lock(g_Mutex);
1035         VERIFY_VOLUME(v_impl);
1036
1037         LibHalVolumeUsage usage( libhal_volume_get_fsusage(v_impl->vol));
1038         std::string       ret;
1039         switch( usage)
1040         {
1041           case  LIBHAL_VOLUME_USAGE_MOUNTABLE_FILESYSTEM:
1042             ret = "filesystem";
1043           break;
1044           case LIBHAL_VOLUME_USAGE_PARTITION_TABLE:
1045             ret = "partitiontable";
1046           break;
1047           case LIBHAL_VOLUME_USAGE_RAID_MEMBER:
1048             return "raid";
1049           break;
1050           case LIBHAL_VOLUME_USAGE_CRYPTO:
1051             ret = "crypto";
1052           break;
1053           case LIBHAL_VOLUME_USAGE_UNKNOWN:
1054           default:
1055           break;
1056         }
1057         return ret;
1058       }
1059
1060       // --------------------------------------------------------------
1061       std::string
1062       HalVolume::getMountPoint() const
1063       {
1064         VERIFY_VOLUME(v_impl);
1065
1066         return std::string( libhal_volume_get_mount_point(v_impl->vol));
1067       }
1068
1069
1070       ////////////////////////////////////////////////////////////////
1071     } // namespace hal
1072     //////////////////////////////////////////////////////////////////
1073     //////////////////////////////////////////////////////////////////
1074   } // namespace target
1075   ////////////////////////////////////////////////////////////////////
1076   ////////////////////////////////////////////////////////////////////
1077 } // namespace zypp
1078 //////////////////////////////////////////////////////////////////////
1079 #else // FAKE_HAL
1080 #include <zypp/target/hal/HalContext.h>
1081 #include <zypp/target/hal/HalException.h>
1082 namespace zypp
1083 { ////////////////////////////////////////////////////////////////////
1084   ////////////////////////////////////////////////////////////////////
1085   namespace target
1086   { //////////////////////////////////////////////////////////////////
1087     //////////////////////////////////////////////////////////////////
1088     namespace hal
1089     { ////////////////////////////////////////////////////////////////
1090
1091       std::ostream &
1092       HalException::dumpOn( std::ostream & str ) const
1093       { return str; }
1094
1095       // --------------------------------------------------------------
1096       class HalContext_Impl
1097       {};
1098       class HalDrive_Impl
1099       {};
1100       class HalVolume_Impl
1101       {};
1102
1103       // --------------------------------------------------------------
1104       HalContext::HalContext(bool)
1105       {}
1106       HalContext::~HalContext()
1107       {}
1108       HalContext &
1109       HalContext::operator=(const HalContext &)
1110       { return *this; }
1111       HalContext::operator HalContext::bool_type() const
1112       { return 0; }
1113       void
1114       HalContext::connect()
1115       {}
1116       std::vector<std::string>
1117       HalContext::getAllDevices() const
1118       { return std::vector<std::string>(); }
1119       HalDrive
1120       HalContext::getDriveFromUDI(const std::string &) const
1121       { return HalDrive(); }
1122       HalVolume
1123       HalContext::getVolumeFromUDI(const std::string &) const
1124       { return HalVolume(); }
1125       HalVolume
1126       HalContext::getVolumeFromDeviceFile(const std::string &) const
1127       { return HalVolume(); }
1128       std::vector<std::string>
1129       HalContext::findDevicesByCapability(const std::string &) const
1130       { return std::vector<std::string>(); }
1131       bool
1132       HalContext::getDevicePropertyBool(const std::string &, const std::string &) const
1133       { return false; }
1134       void
1135       HalContext::setDevicePropertyBool  (const std::string &, const std::string &, bool value)
1136       {}
1137       void
1138       HalContext::removeDeviceProperty(const std::string &, const std::string &)
1139       {}
1140       std::string
1141       HalContext::getDevicePropertyString(const std::string &, const std::string &) const
1142       { return ""; }
1143       // --------------------------------------------------------------
1144       HalDrive::HalDrive()
1145       {}
1146       HalDrive::~HalDrive()
1147       {}
1148       HalDrive &
1149       HalDrive::operator=(const HalDrive &)
1150       { return *this; }
1151       HalDrive::operator HalDrive::bool_type() const
1152       { return 0; }
1153       std::string
1154       HalDrive::getUDI() const
1155       { return std::string(); }
1156       std::string
1157       HalDrive::getTypeName() const
1158       { return std::string(); }
1159       std::string
1160       HalDrive::getDeviceFile() const
1161       { return std::string(); }
1162       unsigned int
1163       HalDrive::getDeviceMinor() const
1164       { return 0; }
1165       unsigned int
1166       HalDrive::getDeviceMajor() const
1167       { return 0; }
1168       bool
1169       HalDrive::usesRemovableMedia() const
1170       { return false; }
1171       std::vector<std::string>
1172       HalDrive::getCdromCapabilityNames() const
1173       { return std::vector<std::string>(); }
1174       std::vector<std::string>
1175       HalDrive::findAllVolumes() const
1176       { return std::vector<std::string>(); }
1177
1178       // --------------------------------------------------------------
1179       HalVolume::HalVolume()
1180       {}
1181       HalVolume::~HalVolume()
1182       {}
1183       HalVolume &
1184       HalVolume::operator=(const HalVolume &)
1185       { return *this; }
1186       HalVolume::operator HalVolume::bool_type() const
1187       { return 0; }
1188       std::string
1189       HalVolume::getUDI() const
1190       { return std::string(); }
1191       std::string
1192       HalVolume::getDeviceFile() const
1193       { return std::string(); }
1194       unsigned int
1195       HalVolume::getDeviceMinor() const
1196       { return 0; }
1197       unsigned int
1198       HalVolume::getDeviceMajor() const
1199       { return 0; }
1200       bool
1201       HalVolume::isDisc() const
1202       { return false; }
1203       bool
1204       HalVolume::isPartition() const
1205       { return false; }
1206       bool
1207       HalVolume::isMounted() const
1208       { return false; }
1209       std::string
1210       HalVolume::getFSType() const
1211       { return std::string(); }
1212       std::string
1213       HalVolume::getFSUsage() const
1214       { return std::string(); }
1215       std::string
1216       HalVolume::getMountPoint() const
1217       { return std::string(); }
1218
1219       ////////////////////////////////////////////////////////////////
1220     } // namespace hal
1221     //////////////////////////////////////////////////////////////////
1222     //////////////////////////////////////////////////////////////////
1223   } // namespace target
1224   ////////////////////////////////////////////////////////////////////
1225   ////////////////////////////////////////////////////////////////////
1226 } // namespace zypp
1227 //////////////////////////////////////////////////////////////////////
1228 #endif // FAKE_HAL
1229
1230 /*
1231 ** vim: set ts=2 sts=2 sw=2 ai et:
1232 */