Browse Source

move libudev based code into libapt cdrom.cc class

debian/1.8.y
Michael Vogt 13 years ago
parent
commit
cbc9bed8ae
  1. 81
      apt-pkg/cdrom.cc
  2. 36
      apt-pkg/cdrom.h
  3. 2
      apt-pkg/makefile
  4. 54
      methods/cdrom.cc
  5. 6
      test/makefile
  6. 19
      test/test_udevcdrom.cc

81
apt-pkg/cdrom.cc

@ -16,7 +16,7 @@
#include <unistd.h>
#include <stdio.h>
#include <algorithm>
#include <dlfcn.h>
#include "indexcopy.h"
@ -844,3 +844,82 @@ bool pkgCdrom::Add(pkgCdromStatus *log)
return true;
}
pkgUdevCdromDevices::pkgUdevCdromDevices()
: libudev_handle(NULL)
{
}
bool
pkgUdevCdromDevices::Dlopen()
{
// see if we can get libudev
void *h = ::dlopen("libudev.so.0", RTLD_LAZY);
if(h == NULL)
return false;
// get the pointers to the udev structs
libudev_handle = h;
udev_new = (udev* (*)(void)) dlsym(h, "udev_new");
udev_enumerate_add_match_property = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_property");
udev_enumerate_scan_devices = (int (*)(udev_enumerate*))dlsym(h, "udev_enumerate_scan_devices");
udev_enumerate_get_list_entry = (udev_list_entry* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_list_entry");
udev_device_new_from_syspath = (udev_device* (*)(udev*, const char*))dlsym(h, "udev_device_new_from_syspath");
udev_enumerate_get_udev = (udev* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_udev");
udev_list_entry_get_name = (const char* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_name");
udev_device_get_devnode = (const char* (*)(udev_device*))dlsym(h, "udev_device_get_devnode");
udev_enumerate_new = (udev_enumerate* (*)(udev*))dlsym(h, "udev_enumerate_new");
udev_list_entry_get_next = (udev_list_entry* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_next");
udev_device_get_property_value = (const char* (*)(udev_device *, const char *))dlsym(h, "udev_device_get_property_value");
return true;
}
vector<CdromDevice>
pkgUdevCdromDevices::Scan()
{
vector<CdromDevice> cdrom_devices;
struct udev_enumerate *enumerate;
struct udev_list_entry *l, *devices;
struct udev *udev_ctx;
if(libudev_handle == NULL)
return cdrom_devices;
udev_ctx = udev_new();
enumerate = udev_enumerate_new (udev_ctx);
udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
udev_enumerate_scan_devices (enumerate);
devices = udev_enumerate_get_list_entry (enumerate);
for (l = devices; l != NULL; l = udev_list_entry_get_next (l))
{
CdromDevice cdrom;
struct udev_device *udevice;
udevice = udev_device_new_from_syspath (udev_enumerate_get_udev (enumerate), udev_list_entry_get_name (l));
if (udevice == NULL)
continue;
const char* devnode = udev_device_get_devnode(udevice);
const char* mountpath = udev_device_get_property_value(udevice, "FSTAB_DIR");
// fill in the struct
cdrom.DeviceName = string(devnode);
if (mountpath) {
cdrom.MountPath = mountpath;
cdrom.Mounted = true;
} else {
cdrom.Mounted = false;
cdrom.MountPath = "";
}
cdrom_devices.push_back(cdrom);
}
return cdrom_devices;
}
pkgUdevCdromDevices::~pkgUdevCdromDevices()
{
dlclose(libudev_handle);
}

36
apt-pkg/cdrom.h

@ -67,5 +67,41 @@ class pkgCdrom
};
// class that uses libudev to find cdrom devices dynamically
struct CdromDevice
{
string DeviceName;
bool Mounted;
string MountPath;
};
class pkgUdevCdromDevices
{
protected:
// libudev dlopen stucture
void *libudev_handle;
struct udev* (*udev_new)(void);
int (*udev_enumerate_add_match_property)(struct udev_enumerate *udev_enumerate, const char *property, const char *value);
int (*udev_enumerate_scan_devices)(struct udev_enumerate *udev_enumerate);
struct udev_list_entry* (*udev_enumerate_get_list_entry)(struct udev_enumerate *udev_enumerate);
struct udev_device* (*udev_device_new_from_syspath)(struct udev *udev, const char *syspath);
struct udev* (*udev_enumerate_get_udev)(struct udev_enumerate *udev_enumerate);
const char* (*udev_list_entry_get_name)(struct udev_list_entry *list_entry);
const char* (*udev_device_get_devnode)(struct udev_device *udev_device);
struct udev_enumerate *(*udev_enumerate_new) (struct udev *udev);
struct udev_list_entry *(*udev_list_entry_get_next)(struct udev_list_entry *list_entry);
const char* (*udev_device_get_property_value)(struct udev_device *udev_device, const char *key);
// end libudev dlopen
public:
pkgUdevCdromDevices();
virtual ~pkgUdevCdromDevices();
// try to open
bool Dlopen();
vector<CdromDevice> Scan();
};
#endif

2
apt-pkg/makefile

@ -15,7 +15,7 @@ LIBRARY=apt-pkg
LIBEXT=$(GLIBC_VER)$(LIBSTDCPP_VER)
MAJOR=4.7
MINOR=0
SLIBS=$(PTHREADLIB) $(INTLLIBS) -lutil
SLIBS=$(PTHREADLIB) $(INTLLIBS) -lutil -ldl
APT_DOMAIN:=libapt-pkg$(MAJOR)
# Source code for the contributed non-core things

54
methods/cdrom.cc

@ -25,23 +25,6 @@
using namespace std;
struct udev;
struct udev_list_entry;
// libudev dlopen stucture
struct udev_p {
struct udev* (*udev_new)(void);
int (*udev_enumerate_add_match_property)(struct udev_enumerate *udev_enumerate, const char *property, const char *value);
int (*udev_enumerate_scan_devices)(struct udev_enumerate *udev_enumerate);
struct udev_list_entry *(*udev_enumerate_get_list_entry)(struct udev_enumerate *udev_enumerate);
struct udev_device *(*udev_device_new_from_syspath)(struct udev *udev, const char *syspath);
struct udev *(*udev_enumerate_get_udev)(struct udev_enumerate *udev_enumerate);
const char *(*udev_list_entry_get_name)(struct udev_list_entry *list_entry);
const char *(*udev_device_get_devnode)(struct udev_device *udev_device);
struct udev_enumerate *(*udev_enumerate_new) (struct udev *udev);
struct udev_list_entry *(*udev_list_entry_get_next)(struct udev_list_entry *list_entry);
};
class CDROMMethod : public pkgAcqMethod
{
bool DatabaseLoaded;
@ -49,7 +32,6 @@ class CDROMMethod : public pkgAcqMethod
string CurrentID;
string CDROM;
bool MountedByApt;
vector<string> CdromDevices;
virtual bool Fetch(FetchItem *Itm);
string GetID(string Name);
@ -69,42 +51,6 @@ CDROMMethod::CDROMMethod() : pkgAcqMethod("1.0",SingleInstance | LocalOnly |
DatabaseLoaded(false),
MountedByApt(false)
{
// see if we can get libudev
void *h = dlopen("libudev.so.0", RTLD_LAZY);
if (h) {
// the pointers for the udev struct
struct udev_p p;
p.udev_new = (udev* (*)(void)) dlsym(h, "udev_new");
p.udev_enumerate_add_match_property = (int (*)(udev_enumerate*, const char*, const char*))dlsym(h, "udev_enumerate_add_match_property");
p.udev_enumerate_scan_devices = (int (*)(udev_enumerate*))dlsym(h, "udev_enumerate_scan_devices");
p.udev_enumerate_get_list_entry = (udev_list_entry* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_list_entry");
p.udev_device_new_from_syspath = (udev_device* (*)(udev*, const char*))dlsym(h, "udev_device_new_from_syspath");
p.udev_enumerate_get_udev = (udev* (*)(udev_enumerate*))dlsym(h, "udev_enumerate_get_udev");
p.udev_list_entry_get_name = (const char* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_name");
p.udev_device_get_devnode = (const char* (*)(udev_device*))dlsym(h, "udev_device_get_devnode");
p.udev_enumerate_new = (udev_enumerate* (*)(udev*))dlsym(h, "udev_enumerate_new");
p.udev_list_entry_get_next = (udev_list_entry* (*)(udev_list_entry*))dlsym(h, "udev_list_entry_get_next");
struct udev_enumerate *enumerate;
struct udev_list_entry *l, *devices;
struct udev *udev_ctx;
udev_ctx = p.udev_new();
enumerate = p.udev_enumerate_new (udev_ctx);
p.udev_enumerate_add_match_property(enumerate, "ID_CDROM", "1");
p.udev_enumerate_scan_devices (enumerate);
devices = p.udev_enumerate_get_list_entry (enumerate);
for (l = devices; l != NULL; l = p.udev_list_entry_get_next (l))
{
struct udev_device *udevice;
udevice = p.udev_device_new_from_syspath (p.udev_enumerate_get_udev (enumerate), p.udev_list_entry_get_name (l));
if (udevice == NULL)
continue;
const char* devnode = p.udev_device_get_devnode(udevice);
//std::cerr << devnode << std::endl;
CdromDevices.push_back(string(devnode));
}
}
};

6
test/makefile

@ -68,6 +68,12 @@ SLIBS = -lapt-pkg
SOURCE = hash.cc
include $(PROGRAM_H)
# Program for testing udevcdrom
PROGRAM=test_udevcdrom
SLIBS = -lapt-pkg
SOURCE = test_udevcdrom.cc
include $(PROGRAM_H)
# Program for checking rpm versions
PROGRAM=rpmver
SLIBS = -lapt-pkg -lrpm

19
test/test_udevcdrom.cc

@ -0,0 +1,19 @@
#include <apt-pkg/cdrom.h>
#include <stdio.h>
#include <assert.h>
int main()
{
int i;
pkgUdevCdromDevices c;
assert(c.Dlopen());
vector<CdromDevice> l;
l = c.Scan();
assert(l.size() > 0);
for (i=0;i<l.size();i++)
std::cerr << l[i].DeviceName << " "
<< l[i].Mounted << " "
<< l[i].MountPath << std::endl;
}
Loading…
Cancel
Save