Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Title:auto generate mount point for mtp device #257

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 78 additions & 3 deletions fuse/fuse_ll.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,28 @@

#include <fuse_lowlevel.h>

#include <unistd.h>

struct deviceInfo
{
std::string _Manufacturer;
std::string _Model;
std::string _DeviceVersion;
std::string _SerialNumber;
std::string _devicePath;
};

std::string get_atf_path()
{
// int uid = getuid();
// std::string uidstr = std::to_string(uid);
// return "/run/user/" + uidstr + "/aft-mtp/";

std::string home = getenv("HOME");
home += "/aft-mtp/";
return home;
}

namespace
{
using namespace mtp::fuse;
Expand Down Expand Up @@ -73,6 +95,21 @@ namespace
std::map<mtp::StorageId, std::string> _storageToName;
std::map<std::string, mtp::StorageId> _storageFromName;

public:
deviceInfo getDeviceInfo()
{
deviceInfo info;
if (_session)
{
info._Manufacturer = _session->GetDeviceInfo().Manufacturer;
info._Model = _session->GetDeviceInfo().Model;
info._DeviceVersion = _session->GetDeviceInfo().DeviceVersion;
info._SerialNumber = _session->GetDeviceInfo().SerialNumber;
info._devicePath = _device->GetPacketer().GetPipe()->GetInterface()->GetPath();
}
return info;
}

private:
static FuseId ToFuse(mtp::ObjectId id)
{ return FuseId(id.Id + MtpObjectShift); }
Expand Down Expand Up @@ -766,6 +803,8 @@ namespace
int main(int argc, char **argv)
{
bool claimInterface = true;
bool fsname_defined = false;

for(int i = 1; i < argc; ++i)
{
if (strcmp(argv[i], "-C") == 0)
Expand All @@ -774,6 +813,11 @@ int main(int argc, char **argv)
mtp::g_debug = true;
if (i + 1 < argc && strcmp(argv[i], "-o") == 0 && strcmp(argv[i + 1], "debug") == 0)
mtp::g_debug = true;

if(i + 1 < argc && strcmp(argv[i], "-o") == 0 && (strlen(argv[i + 1]) > strlen("fsname=")) && strncmp(argv[i + 1], "fsname=", strlen("fsname=")) == 0)
{
fsname_defined = true;
}
}

try
Expand All @@ -800,15 +844,46 @@ int main(int argc, char **argv)
ops.unlink = &Unlink;
ops.statfs = &StatFS;

struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
//create mount point
deviceInfo info = g_wrapper->getDeviceInfo();

info._Manufacturer.erase(
std::remove_if(info._Manufacturer.begin(), info._Manufacturer.end(),
[](char c) { return !std::isalnum(c); }),
info._Manufacturer.end());

info._SerialNumber.erase(
std::remove_if(info._SerialNumber.begin(), info._SerialNumber.end(),
[](char c) { return !std::isalnum(c); }),
info._SerialNumber.end());

std::string dirPath = get_atf_path() + info._Manufacturer + info._SerialNumber;
std::string cmd = "mkdir -p " + dirPath;
system(cmd.c_str());
const char *real_mount_point = dirPath.c_str();

//-o fsname=xx
char *rargv[100];
int rargc = argc;
std::string rfsnamecmd = "fsname=" + info._Manufacturer + info._SerialNumber;
if(rfsnamecmd.empty()) { rfsnamecmd = "fsname=aft-mtp"; }
for(int i = 0; i < argc; ++i) { rargv[i] = argv[i]; }
if(!fsname_defined)
{
rargc += 2;
rargv[rargc - 2] = "-o";
rargv[rargc - 1] = (char*)rfsnamecmd.c_str();
}

struct fuse_args args = FUSE_ARGS_INIT(rargc, rargv);
struct fuse_chan *ch;
char *mountpoint;
int err = -1;
int multithreaded = 0, foreground = 0;

if (fuse_parse_cmdline(&args, &mountpoint, &multithreaded, &foreground) != -1 &&
mountpoint != NULL &&
(ch = fuse_mount(mountpoint, &args)) != NULL) {
real_mount_point != NULL &&
(ch = fuse_mount(real_mount_point, &args)) != NULL) {
struct fuse_session *se;

se = fuse_lowlevel_new(&args, &ops,
Expand Down
3 changes: 3 additions & 0 deletions mtp/backend/linux/usb/Interface.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ namespace mtp { namespace usb
public:
Interface(int index, const std::string &path);

std::string GetPath() const
{ return _path; }

u8 GetClass() const
{ return _class; }

Expand Down
6 changes: 5 additions & 1 deletion mtp/ptp/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@ namespace mtp
return std::make_shared<Session>(_packeter.GetPipe(), sessionId);
}

PipePacketer Device::GetPacketer()
{
return _packeter;
}

u8 Device::GetInterfaceStringIndex(usb::DeviceDescriptorPtr desc, u8 number)
{
static const u16 DT_INTERFACE = 4;
Expand Down Expand Up @@ -184,5 +189,4 @@ namespace mtp

return nullptr;
}

}
3 changes: 2 additions & 1 deletion mtp/ptp/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,10 @@ namespace mtp

public:
Device(usb::BulkPipePtr pipe);

SessionPtr OpenSession(u32 sessionId, int timeout = Session::DefaultTimeout);

PipePacketer GetPacketer();
static DevicePtr Open(usb::ContextPtr context, usb::DeviceDescriptorPtr desc, bool claimInterface = true, bool resetDevice = false); //fixme: add flags here
static DevicePtr FindFirst(bool claimInterface = true, bool resetDevice = false);
};
Expand Down