forked from jkcoxson/idevice
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathafc.cpp
More file actions
244 lines (213 loc) · 6.43 KB
/
Copy pathafc.cpp
File metadata and controls
244 lines (213 loc) · 6.43 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
// Jackson Coxson
#include <cstdlib>
#include <idevice++/afc.hpp>
#include <idevice++/bindings.hpp>
#include <idevice++/ffi.hpp>
#include <idevice++/provider.hpp>
namespace IdeviceFFI {
// -------- AfcClient Factory Methods --------
Result<AfcClient, FfiError> AfcClient::connect(Provider& provider) {
AfcClientHandle* out = nullptr;
FfiError e(::afc_client_connect(provider.raw(), &out));
if (e) {
provider.release();
return Err(e);
}
return Ok(AfcClient::adopt(out));
}
Result<AfcClient, FfiError> AfcClient::connect_rsd(AdapterHandle* adapter, RsdHandshakeHandle* handshake) {
AfcClientHandle* out = nullptr;
FfiError e(::afc_client_connect_rsd(adapter, handshake, &out));
if (e) {
return Err(e);
}
return Ok(AfcClient::adopt(out));
}
Result<AfcClient, FfiError> AfcClient::connect_afc2(Provider& provider) {
AfcClientHandle* out = nullptr;
FfiError e(::afc2_client_connect(provider.raw(), &out));
if (e) {
provider.release();
return Err(e);
}
return Ok(AfcClient::adopt(out));
}
Result<AfcClient, FfiError> AfcClient::from_socket(Idevice&& socket) {
AfcClientHandle* out = nullptr;
FfiError e(::afc_client_new(socket.raw(), &out));
if (e) {
return Err(e);
}
socket.release();
return Ok(AfcClient::adopt(out));
}
// -------- AfcClient Ops --------
Result<std::vector<std::string>, FfiError> AfcClient::list_directory(const std::string& path) {
char** entries_raw = nullptr;
size_t count = 0;
FfiError e(::afc_list_directory(handle_.get(), path.c_str(), &entries_raw, &count));
if (e) {
return Err(e);
}
std::vector<std::string> result;
if (entries_raw) {
result.reserve(count);
for (size_t i = 0; i < count; ++i) {
if (entries_raw[i]) {
result.emplace_back(entries_raw[i]);
::idevice_string_free(entries_raw[i]);
}
}
std::free(entries_raw);
}
return Ok(std::move(result));
}
Result<void, FfiError> AfcClient::make_directory(const std::string& path) {
FfiError e(::afc_make_directory(handle_.get(), path.c_str()));
if (e) {
return Err(e);
}
return Ok();
}
Result<AfcFileInfo, FfiError> AfcClient::get_file_info(const std::string& path) {
AfcFileInfo info{};
FfiError e(::afc_get_file_info(handle_.get(), path.c_str(), &info));
if (e) {
return Err(e);
}
// Copy out the info and free the FFI-allocated strings
AfcFileInfo result = info;
// Note: caller should use afc_file_info_free when done, but since we're
// returning a copy, we need to manage the strings ourselves.
// We leave the raw struct as-is since the strings are owned by the FFI.
return Ok(std::move(result));
}
Result<AfcDeviceInfo, FfiError> AfcClient::get_device_info() {
AfcDeviceInfo info{};
FfiError e(::afc_get_device_info(handle_.get(), &info));
if (e) {
return Err(e);
}
return Ok(std::move(info));
}
Result<void, FfiError> AfcClient::remove_path(const std::string& path) {
FfiError e(::afc_remove_path(handle_.get(), path.c_str()));
if (e) {
return Err(e);
}
return Ok();
}
Result<void, FfiError> AfcClient::remove_path_and_contents(const std::string& path) {
FfiError e(::afc_remove_path_and_contents(handle_.get(), path.c_str()));
if (e) {
return Err(e);
}
return Ok();
}
Result<AfcFile, FfiError> AfcClient::file_open(const std::string& path, AfcFopenMode mode) {
AfcFileHandle* out = nullptr;
FfiError e(::afc_file_open(handle_.get(), path.c_str(), mode, &out));
if (e) {
return Err(e);
}
return Ok(AfcFile::adopt(out));
}
Result<void, FfiError>
AfcClient::make_link(const std::string& target, const std::string& source, AfcLinkType link_type) {
FfiError e(::afc_make_link(handle_.get(), target.c_str(), source.c_str(), link_type));
if (e) {
return Err(e);
}
return Ok();
}
Result<void, FfiError> AfcClient::rename_path(const std::string& source,
const std::string& target) {
FfiError e(::afc_rename_path(handle_.get(), source.c_str(), target.c_str()));
if (e) {
return Err(e);
}
return Ok();
}
// -------- AfcFile --------
AfcFile::~AfcFile() noexcept {
if (handle_) {
::afc_file_close(handle_);
handle_ = nullptr;
}
}
AfcFile::AfcFile(AfcFile&& o) noexcept : handle_(o.handle_) {
o.handle_ = nullptr;
}
AfcFile& AfcFile::operator=(AfcFile&& o) noexcept {
if (this != &o) {
if (handle_) {
::afc_file_close(handle_);
}
handle_ = o.handle_;
o.handle_ = nullptr;
}
return *this;
}
Result<void, FfiError> AfcFile::close() {
if (!handle_) {
return Ok();
}
FfiError e(::afc_file_close(handle_));
handle_ = nullptr;
if (e) {
return Err(e);
}
return Ok();
}
Result<std::vector<uint8_t>, FfiError> AfcFile::read(size_t len) {
uint8_t* data = nullptr;
size_t bytes_read = 0;
FfiError e(::afc_file_read(handle_, &data, len, &bytes_read));
if (e) {
return Err(e);
}
std::vector<uint8_t> result;
if (data && bytes_read > 0) {
result.assign(data, data + bytes_read);
::afc_file_read_data_free(data, bytes_read);
}
return Ok(std::move(result));
}
Result<std::vector<uint8_t>, FfiError> AfcFile::read_all() {
uint8_t* data = nullptr;
size_t length = 0;
FfiError e(::afc_file_read_entire(handle_, &data, &length));
if (e) {
return Err(e);
}
std::vector<uint8_t> result;
if (data && length > 0) {
result.assign(data, data + length);
::afc_file_read_data_free(data, length);
}
return Ok(std::move(result));
}
Result<int64_t, FfiError> AfcFile::seek(int64_t offset, int whence) {
int64_t new_pos = 0;
FfiError e(::afc_file_seek(handle_, offset, whence, &new_pos));
if (e) {
return Err(e);
}
return Ok(new_pos);
}
Result<int64_t, FfiError> AfcFile::tell() {
int64_t pos = 0;
FfiError e(::afc_file_tell(handle_, &pos));
if (e) {
return Err(e);
}
return Ok(pos);
}
Result<void, FfiError> AfcFile::write(const uint8_t* data, size_t length) {
FfiError e(::afc_file_write(handle_, data, length));
if (e) {
return Err(e);
}
return Ok();
}
} // namespace IdeviceFFI