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