Support for mounting by device file when libusb1 is missing

This commit is contained in:
Vladimir Kondratiev 2015-08-22 11:20:39 +03:00
parent 5357ae6422
commit 8a971b0ba7
6 changed files with 53 additions and 41 deletions

View File

@ -161,18 +161,14 @@ SMTPFileSystem::SMTPFileSystemOptions::SMTPFileSystemOptions()
, m_enable_move(false)
, m_list_devices(false)
, m_device_no(1)
#ifdef HAVE_LIBUSB1
, m_device_file(nullptr)
#endif // HAVE_LIBUSB1
, m_mount_point(nullptr)
{
}
SMTPFileSystem::SMTPFileSystemOptions::~SMTPFileSystemOptions()
{
#ifdef HAVE_LIBUSB1
free(static_cast<void*>(m_device_file));
#endif // HAVE_LIBUSB1
free(static_cast<void*>(m_mount_point));
}
@ -184,7 +180,6 @@ int SMTPFileSystem::SMTPFileSystemOptions::opt_proc(void *data, const char *arg,
SMTPFileSystemOptions *options = static_cast<SMTPFileSystemOptions*>(data);
if (key == FUSE_OPT_KEY_NONOPT) {
#ifdef HAVE_LIBUSB1
if (options->m_mount_point && !options->m_device_file) {
options->m_device_file = options->m_mount_point;
options->m_mount_point = nullptr;
@ -192,12 +187,6 @@ int SMTPFileSystem::SMTPFileSystemOptions::opt_proc(void *data, const char *arg,
// Unknown positional argument supplied
return -1;
}
#else
if (options->m_mount_point) {
// Unknown positional argument supplied
return -1;
}
#endif //HAVE_LIBUSB1
fuse_opt_add_opt(&options->m_mount_point, arg);
return 0;
@ -314,13 +303,11 @@ bool SMTPFileSystem::parseOptions(int argc, char **argv)
--m_options.m_device_no;
#ifdef HAVE_LIBUSB1
// device file and -- device are mutually exclusive, fail if both set
if (m_options.m_device_no && m_options.m_device_file) {
m_options.m_good = false;
return false;
}
#endif // HAVE_LIBUSB1
m_options.m_good = true;
return true;
@ -332,10 +319,7 @@ void SMTPFileSystem::printHelp() const
struct fuse_operations tmp_operations;
memset(&tmp_operations, 0, sizeof(tmp_operations));
std::cerr << "usage: " << smtpfs_basename(m_args.argv[0])
#ifdef HAVE_LIBUSB1
<< " <source>"
#endif // HAVE_LIBUSB1
<< " mountpoint [options]\n\n"
<< " <source> mountpoint [options]\n\n"
<< "general options:\n"
<< " -o opt,[opt...] mount options\n"
<< " -h --help print help\n"
@ -387,14 +371,11 @@ bool SMTPFileSystem::exec()
return false;
}
#ifdef HAVE_LIBUSB1
if (m_options.m_device_file) {
// Try to use device file first, if provided
if (!m_device.connect(m_options.m_device_file))
return false;
} else
#endif // HAVE_LIBUSB1
{
} else {
// Connect to MTP device by order number, if no device file supplied
if (!m_device.connect(m_options.m_device_no))
return false;

View File

@ -41,9 +41,7 @@ private:
int m_enable_move;
int m_list_devices;
int m_device_no;
#ifdef HAVE_LIBUSB1
char *m_device_file;
#endif // HAVE_LIBUSB1
char *m_mount_point;
SMTPFileSystemOptions();

View File

@ -79,7 +79,7 @@ bool MTPDevice::connect(LIBMTP_raw_device_t *dev)
return true;
}
bool MTPDevice::connect(int dev_no)
bool MTPDevice::connect_priv(int dev_no, const std::string &dev_file)
{
if (m_device) {
logerr("Already connected.\n");
@ -95,12 +95,6 @@ bool MTPDevice::connect(int dev_no)
&raw_devices, &raw_devices_cnt);
StreamHelper::on();
if (dev_no < 0 || dev_no >= raw_devices_cnt) {
logerr("Can not connect to device no. ", dev_no + 1, ".\n");
free(static_cast<void*>(raw_devices));
return false;
}
if (err != LIBMTP_ERROR_NONE) {
switch(err) {
case LIBMTP_ERROR_NO_DEVICE_ATTACHED:
@ -124,6 +118,31 @@ bool MTPDevice::connect(int dev_no)
return false;
}
#ifndef HAVE_LIBUSB1
if (!dev_file.empty()) {
std::string dev_path(smtpfs_realpath(dev_file));
for (dev_no = 0; dev_no < raw_devices_cnt; ++dev_no) {
std::string usb_devpath = smtpfs_usb_devpath(
raw_devices[dev_no].bus_location, raw_devices[dev_no].devnum);
if (usb_devpath == dev_path)
break;
}
if (dev_no == raw_devices_cnt) {
logerr("Can not open such device '", dev_file, "'.\n");
free(static_cast<void*>(raw_devices));
return false;
}
}
#endif // !HAVE_LIBUSB1
if (dev_no < 0 || dev_no >= raw_devices_cnt) {
logerr("Can not connect to device no. ", dev_no + 1, ".\n");
free(static_cast<void*>(raw_devices));
return false;
}
LIBMTP_raw_device_t *raw_device = &raw_devices[dev_no];
#ifdef HAVE_LIBUSB1
@ -154,6 +173,11 @@ bool MTPDevice::connect(int dev_no)
return true;
}
bool MTPDevice::connect(int dev_no)
{
return connect_priv(dev_no, std::string());
}
#ifdef HAVE_LIBUSB1
bool MTPDevice::connect(const std::string &dev_file)
{
@ -182,6 +206,11 @@ bool MTPDevice::connect(const std::string &dev_file)
return rval;
}
#else
bool MTPDevice::connect(const std::string &dev_file)
{
return connect_priv(-1, dev_file);
}
#endif
void MTPDevice::disconnect()

View File

@ -94,6 +94,7 @@ private:
bool enumStorages();
static Capabilities getCapabilities(const MTPDevice &device);
bool connect_priv(int dev_no, const std::string &dev_file);
private:
LIBMTP_mtpdevice_t *m_device;

View File

@ -78,10 +78,8 @@ void StreamHelper::off()
s_enabled = true;
}
#ifdef HAVE_LIBUSB1
const char smtpfs_path_delimiter = '/';
const std::string smtpfs_devbususb = "/dev/bus/usb/";
#endif // HAVE_LIBUSB1
std::string smtpfs_dirname(const std::string &path)
{
@ -156,6 +154,17 @@ bool smtpfs_remove_dir(const std::string &dirname)
return true;
}
std::string smtpfs_usb_devpath(uint8_t bnum, uint8_t dnum)
{
std::stringstream ss;
ss << smtpfs_devbususb
<< std::setw(3) << std::setfill('0')
<< static_cast<uint16_t>(bnum) << "/"
<< std::setw(3) << std::setfill('0')
<< static_cast<uint16_t>(dnum);
return ss.str();
}
#ifdef HAVE_LIBUSB1
LIBMTP_raw_device_t *smtpfs_raw_device_new_priv(libusb_device *usb_device)
{
@ -205,15 +214,7 @@ LIBMTP_raw_device_t *smtpfs_raw_device_new(const std::string &path)
dev = dev_list[i];
uint8_t bnum = libusb_get_bus_number(dev_list[i]);
uint8_t dnum = libusb_get_device_address(dev_list[i]);
std::stringstream ss;
ss << smtpfs_devbususb
<< std::setw(3) << std::setfill('0')
<< static_cast<uint16_t>(bnum) << "/"
<< std::setw(3) << std::setfill('0')
<< static_cast<uint16_t>(dnum);
if (ss.str() == dev_path)
if (smtpfs_usb_devpath(bnum, dnum) == dev_path)
break;
dev = nullptr;
}

View File

@ -19,6 +19,7 @@
#define SIMPLE_MTPFS_UTIL
#include <config.h>
#include <cstdint>
#include <string>
#ifdef HAVE_LIBUSB1
@ -43,6 +44,7 @@ std::string smtpfs_dirname(const std::string &path);
std::string smtpfs_basename(const std::string &path);
std::string smtpfs_realpath(const std::string &path);
std::string smtpfs_get_tmpdir();
std::string smtpfs_usb_devpath(uint8_t bnum, uint8_t dnum);
bool smtpfs_create_dir(const std::string &dirname);
bool smtpfs_remove_dir(const std::string &dirname);