-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmtp2fs.cpp
More file actions
182 lines (155 loc) · 3.72 KB
/
mtp2fs.cpp
File metadata and controls
182 lines (155 loc) · 3.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <Windows.h>
#include <PortableDeviceApi.h>
#include <print>
#include <iostream>
#include <wrl/client.h>
#include <unordered_map>
#include <codecvt>
#include <algorithm>
#include "Com.hpp"
#include "Funcs.hpp"
#include "DeviceManager.hpp"
#include "MediaDevice.hpp"
#include "Filesystem.hpp"
#pragma comment(lib, "PortableDeviceGUIDs.lib")
using namespace Microsoft::WRL;
using namespace mtp2fs;
using namespace mtp2fs::fs;
enum class ManageResult
{
Error,
NoDevices,
OK
};
auto main(int numArgs, char **ppArgs) -> int
{
COM c_;
std::unordered_map<std::string, int> argsDict =
{
{"--help",1},
{"-h", 1},
{"/h", 1},
{"--mount", 2},
{"-m", 2},
{"/m", 2},
{"--list", 3},
{"-l", 3},
{"/l", 3}
};
bool showList = false;
std::string requiredDevice;
std::string requiredDrive;
for(int i = 1; i < numArgs; ++i)
{
switch(argsDict[ppArgs[i]])
{
case 1:
std::println("mtp2fs - Mounts a MTP device to a file system.");
std::println("Usage: mtp2fs [arguments]");
std::println("Arguments:");
std::println("--help, -h, /h : Shows this help message.");
std::println("--mount <name> <drive letter> (-m, /h): Mounts a MTP device to a specified drive.");
std::println("--list, -l, /l : Lists MTP devices.");
return 0;
case 2:
if (!(numArgs > i + 1))
{
std::println("--mount requires a device name.");
return -3;
}
if (!(numArgs > i + 2))
{
std::println("--mount requires a device letter");
return -4;
}
requiredDevice = ppArgs[i + 1];
requiredDrive = ppArgs[i + 2];
i += 2;
break;
case 3:
showList = true;
break;
default:
std::println("No arguments specified! Use --help .");
return 0;
}
}
DeviceManager manager;
auto deviceList = manager.EnumerateDevices();
int ret = 0;
//Enumerate devices:
auto devices = deviceList.and_then([showList](std::vector<Device> devices) -> decltype(deviceList)
{
if(showList)
{
std::println("Connected MTP devices:");
for(auto& device : devices)
{
if(device.ID == L"")
{
std::println("Private MTP devices:");
}
else
{
std::println("\t {}:{}", ws2s(device.Name), ws2s(device.Desc));
}
}
}
return std::expected<std::vector<Device>, DeviceManager::DeviceEnumerationFailure>(devices);
}
).or_else([&ret](DeviceManager::DeviceEnumerationFailure fail) -> decltype(deviceList)
{
switch(fail)
{
case DeviceManager::DeviceEnumerationFailure::PortableDeviceManager_Creation_Fail:
std::println("IPortableDeviceManager creation failed!");
ret = -1;
break;
case DeviceManager::DeviceEnumerationFailure::OK_NoDevices:
std::println("No MTP devices detected.");
ret = -2;
break;
default:
std::println("Unknown error.");
ret = -500;
break;
}
return std::unexpected<decltype(fail)>(fail);
}
);
if (showList) return 0;
if (!devices.has_value()) return ret;
//Select device:
struct DeviceEntry
{
std::string Name;
std::wstring ID;
int Index;
};
std::vector<DeviceEntry> dst;
for (auto& device : devices.value())
{
auto name = ws2s(device.Name);
dst.emplace_back( name, device.ID, EditDistance(requiredDevice, name));
}
std::sort(dst.begin(), dst.end(), [](const DeviceEntry& a, const DeviceEntry& b) -> bool
{
return a.Index < b.Index;
}
);
std::println("Selecting device {}", dst[0].Name);
//Open device.
MediaDevice device(dst[0].ID);
if (!device.IsValid()) return -3 + device.Error();
//Create filesystem
if(requiredDrive.length() == 1)
std::println("Creating filesystem at drive {}:\\\\", requiredDrive);
else
std::println("Creating filesystem at path {}", requiredDrive);
Filesystem fs(requiredDrive);
if (fs.IsValid())
Sleep(10000);
else
return -5;
return ret;
}