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