| 1 |
/* |
| 2 |
* afc.c |
| 3 |
* Contains functions for the built-in AFC client. |
| 4 |
* |
| 5 |
* Copyright (c) 2014 Martin Szulecki All Rights Reserved. |
| 6 |
* Copyright (c) 2009-2014 Nikias Bassen. All Rights Reserved. |
| 7 |
* Copyright (c) 2008 Zach C. All Rights Reserved. |
| 8 |
* |
| 9 |
* This library is free software; you can redistribute it and/or |
| 10 |
* modify it under the terms of the GNU Lesser General Public |
| 11 |
* License as published by the Free Software Foundation; either |
| 12 |
* version 2.1 of the License, or (at your option) any later version. |
| 13 |
* |
| 14 |
* This library is distributed in the hope that it will be useful, |
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 |
* Lesser General Public License for more details. |
| 18 |
* |
| 19 |
* You should have received a copy of the GNU Lesser General Public |
| 20 |
* License along with this library; if not, write to the Free Software |
| 21 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 22 |
*/ |
| 23 |
|
| 24 |
#ifdef HAVE_CONFIG_H |
| 25 |
#include <config.h> |
| 26 |
#endif |
| 27 |
#include <stdio.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <string.h> |
| 30 |
|
| 31 |
#ifndef _MSC_VER |
| 32 |
#include <unistd.h> |
| 33 |
#endif |
| 34 |
|
| 35 |
#include "idevice.h" |
| 36 |
#include "afc.h" |
| 37 |
#include "common/debug.h" |
| 38 |
#include "endianness.h" |
| 39 |
|
| 40 |
/** |
| 41 |
* Locks an AFC client, done for thread safety stuff |
| 42 |
* |
| 43 |
* @param client The AFC client connection to lock |
| 44 |
*/ |
| 45 |
static void afc_lock(afc_client_t client) |
| 46 |
{ |
| 47 |
debug_info("Locked"); |
| 48 |
mutex_lock(&client->mutex); |
| 49 |
} |
| 50 |
|
| 51 |
/** |
| 52 |
* Unlocks an AFC client, done for thread safety stuff. |
| 53 |
* |
| 54 |
* @param client The AFC |
| 55 |
*/ |
| 56 |
static void afc_unlock(afc_client_t client) |
| 57 |
{ |
| 58 |
debug_info("Unlocked"); |
| 59 |
mutex_unlock(&client->mutex); |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* Makes a connection to the AFC service on the device using the given |
| 64 |
* connection. |
| 65 |
* |
| 66 |
* @param service_client A connected service client |
| 67 |
* @param client Pointer that will be set to a newly allocated afc_client_t |
| 68 |
* upon successful return. |
| 69 |
* |
| 70 |
* @return AFC_E_SUCCESS on success, AFC_E_INVALID_ARG if connection is |
| 71 |
* invalid, or AFC_E_NO_MEM if there is a memory allocation problem. |
| 72 |
*/ |
| 73 |
|
| 74 |
afc_error_t afc_client_new_with_service_client(service_client_t service_client, afc_client_t *client) |
| 75 |
{ |
| 76 |
if (!service_client) |
| 77 |
return AFC_E_INVALID_ARG; |
| 78 |
|
| 79 |
afc_client_t client_loc = (afc_client_t) malloc(sizeof(struct afc_client_private)); |
| 80 |
client_loc->parent = service_client; |
| 81 |
client_loc->free_parent = 0; |
| 82 |
|
| 83 |
/* allocate a packet */ |
| 84 |
client_loc->packet_extra = 1024; |
| 85 |
client_loc->afc_packet = (AFCPacket *) malloc(sizeof(AFCPacket) + client_loc->packet_extra); |
| 86 |
if (!client_loc->afc_packet) { |
| 87 |
free(client_loc); |
| 88 |
return AFC_E_NO_MEM; |
| 89 |
} |
| 90 |
client_loc->afc_packet->packet_num = 0; |
| 91 |
client_loc->afc_packet->entire_length = 0; |
| 92 |
client_loc->afc_packet->this_length = 0; |
| 93 |
memcpy(client_loc->afc_packet->magic, AFC_MAGIC, AFC_MAGIC_LEN); |
| 94 |
mutex_init(&client_loc->mutex); |
| 95 |
|
| 96 |
*client = client_loc; |
| 97 |
return AFC_E_SUCCESS; |
| 98 |
} |
| 99 |
|
| 100 |
afc_error_t afc_client_new(idevice_t device, lockdownd_service_descriptor_t service, afc_client_t * client) |
| 101 |
{ |
| 102 |
if (!device || !service || service->port == 0) |
| 103 |
return AFC_E_INVALID_ARG; |
| 104 |
|
| 105 |
service_client_t parent = NULL; |
| 106 |
if (service_client_new(device, service, &parent) != SERVICE_E_SUCCESS) { |
| 107 |
return AFC_E_MUX_ERROR; |
| 108 |
} |
| 109 |
|
| 110 |
afc_error_t err = afc_client_new_with_service_client(parent, client); |
| 111 |
if (err != AFC_E_SUCCESS) { |
| 112 |
service_client_free(parent); |
| 113 |
} else { |
| 114 |
(*client)->free_parent = 1; |
| 115 |
} |
| 116 |
return err; |
| 117 |
} |
| 118 |
|
| 119 |
afc_error_t afc_client_start_service(idevice_t device, afc_client_t * client, const char* label) |
| 120 |
{ |
| 121 |
int32_t err = AFC_E_UNKNOWN_ERROR; |
| 122 |
service_client_factory_start_service(device, AFC_SERVICE_NAME, (void**)client, label, SERVICE_CONSTRUCTOR(afc_client_new), &err); |
| 123 |
return err; |
| 124 |
} |
| 125 |
|
| 126 |
afc_error_t afc_client_free(afc_client_t client) |
| 127 |
{ |
| 128 |
if (!client || !client->afc_packet) |
| 129 |
return AFC_E_INVALID_ARG; |
| 130 |
|
| 131 |
if (client->free_parent && client->parent) { |
| 132 |
service_client_free(client->parent); |
| 133 |
client->parent = NULL; |
| 134 |
} |
| 135 |
free(client->afc_packet); |
| 136 |
mutex_destroy(&client->mutex); |
| 137 |
free(client); |
| 138 |
return AFC_E_SUCCESS; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Dispatches an AFC packet over a client. |
| 143 |
* |
| 144 |
* @param client The client to send data through. |
| 145 |
* @param operation The operation to perform. |
| 146 |
* @param data The data to send together with the header. |
| 147 |
* @param data_length The length of the data to send with the header. |
| 148 |
* @param payload The data to send after the header has been sent. |
| 149 |
* @param payload_length The length of data to send after the header. |
| 150 |
* @param bytes_sent The total number of bytes actually sent. |
| 151 |
* |
| 152 |
* @return AFC_E_SUCCESS on success or an AFC_E_* error value. |
| 153 |
*/ |
| 154 |
static afc_error_t afc_dispatch_packet(afc_client_t client, uint64_t operation, uint32_t data_length, const char* payload, uint32_t payload_length, uint32_t *bytes_sent) |
| 155 |
{ |
| 156 |
uint32_t sent = 0; |
| 157 |
|
| 158 |
if (!client || !client->parent || !client->afc_packet) |
| 159 |
return AFC_E_INVALID_ARG; |
| 160 |
|
| 161 |
*bytes_sent = 0; |
| 162 |
|
| 163 |
if (!payload || !payload_length) |
| 164 |
payload_length = 0; |
| 165 |
|
| 166 |
client->afc_packet->packet_num++; |
| 167 |
client->afc_packet->operation = operation; |
| 168 |
client->afc_packet->entire_length = sizeof(AFCPacket) + data_length + payload_length; |
| 169 |
client->afc_packet->this_length = sizeof(AFCPacket) + data_length; |
| 170 |
|
| 171 |
debug_info("packet length = %i", client->afc_packet->this_length); |
| 172 |
|
| 173 |
/* send AFC packet header and data */ |
| 174 |
AFCPacket_to_LE(client->afc_packet); |
| 175 |
debug_buffer((char*)client->afc_packet, sizeof(AFCPacket) + data_length); |
| 176 |
sent = 0; |
| 177 |
service_send(client->parent, (void*)client->afc_packet, sizeof(AFCPacket) + data_length, &sent); |
| 178 |
AFCPacket_from_LE(client->afc_packet); |
| 179 |
*bytes_sent += sent; |
| 180 |
if (sent < sizeof(AFCPacket) + data_length) { |
| 181 |
return AFC_E_SUCCESS; |
| 182 |
} |
| 183 |
|
| 184 |
sent = 0; |
| 185 |
if (payload_length > 0) { |
| 186 |
if (payload_length > 256) { |
| 187 |
debug_info("packet payload follows (256/%u)", payload_length); |
| 188 |
debug_buffer(payload, 256); |
| 189 |
} else { |
| 190 |
debug_info("packet payload follows"); |
| 191 |
debug_buffer(payload, payload_length); |
| 192 |
} |
| 193 |
service_send(client->parent, payload, payload_length, &sent); |
| 194 |
} |
| 195 |
*bytes_sent += sent; |
| 196 |
if (sent < payload_length) { |
| 197 |
return AFC_E_SUCCESS; |
| 198 |
} |
| 199 |
|
| 200 |
return AFC_E_SUCCESS; |
| 201 |
} |
| 202 |
|
| 203 |
/** |
| 204 |
* Receives data through an AFC client and sets a variable to the received data. |
| 205 |
* |
| 206 |
* @param client The client to receive data on. |
| 207 |
* @param bytes The char* to point to the newly-received data. |
| 208 |
* @param bytes_recv How much data was received. |
| 209 |
* |
| 210 |
* @return AFC_E_SUCCESS on success or an AFC_E_* error value. |
| 211 |
*/ |
| 212 |
static afc_error_t afc_receive_data(afc_client_t client, char **bytes, uint32_t *bytes_recv) |
| 213 |
{ |
| 214 |
AFCPacket header; |
| 215 |
uint32_t entire_len = 0; |
| 216 |
uint32_t this_len = 0; |
| 217 |
uint32_t current_count = 0; |
| 218 |
uint64_t param1 = -1; |
| 219 |
char *buf = NULL; |
| 220 |
uint32_t recv_len = 0; |
| 221 |
|
| 222 |
if (bytes_recv) { |
| 223 |
*bytes_recv = 0; |
| 224 |
} |
| 225 |
if (bytes) { |
| 226 |
*bytes = NULL; |
| 227 |
} |
| 228 |
|
| 229 |
/* first, read the AFC header */ |
| 230 |
service_receive(client->parent, (char*)&header, sizeof(AFCPacket), &recv_len); |
| 231 |
AFCPacket_from_LE(&header); |
| 232 |
if (recv_len == 0) { |
| 233 |
debug_info("Just didn't get enough."); |
| 234 |
return AFC_E_MUX_ERROR; |
| 235 |
} |
| 236 |
|
| 237 |
if (recv_len < sizeof(AFCPacket)) { |
| 238 |
debug_info("Did not even get the AFCPacket header"); |
| 239 |
return AFC_E_MUX_ERROR; |
| 240 |
} |
| 241 |
|
| 242 |
/* check if it's a valid AFC header */ |
| 243 |
if (strncmp(header.magic, AFC_MAGIC, AFC_MAGIC_LEN) != 0) { |
| 244 |
debug_info("Invalid AFC packet received (magic != " AFC_MAGIC ")!"); |
| 245 |
} |
| 246 |
|
| 247 |
/* check if it has the correct packet number */ |
| 248 |
if (header.packet_num != client->afc_packet->packet_num) { |
| 249 |
/* otherwise print a warning but do not abort */ |
| 250 |
debug_info("ERROR: Unexpected packet number (%lld != %lld) aborting.", header.packet_num, client->afc_packet->packet_num); |
| 251 |
return AFC_E_OP_HEADER_INVALID; |
| 252 |
} |
| 253 |
|
| 254 |
/* then, read the attached packet */ |
| 255 |
if (header.this_length < sizeof(AFCPacket)) { |
| 256 |
debug_info("Invalid AFCPacket header received!"); |
| 257 |
return AFC_E_OP_HEADER_INVALID; |
| 258 |
} |
| 259 |
if ((header.this_length == header.entire_length) |
| 260 |
&& header.entire_length == sizeof(AFCPacket)) { |
| 261 |
debug_info("Empty AFCPacket received!"); |
| 262 |
if (header.operation == AFC_OP_DATA) { |
| 263 |
return AFC_E_SUCCESS; |
| 264 |
} |
| 265 |
return AFC_E_IO_ERROR; |
| 266 |
} |
| 267 |
|
| 268 |
debug_info("received AFC packet, full len=%lld, this len=%lld, operation=0x%llx", header.entire_length, header.this_length, header.operation); |
| 269 |
|
| 270 |
entire_len = (uint32_t)header.entire_length - sizeof(AFCPacket); |
| 271 |
this_len = (uint32_t)header.this_length - sizeof(AFCPacket); |
| 272 |
|
| 273 |
buf = (char*)malloc(entire_len); |
| 274 |
if (this_len > 0) { |
| 275 |
recv_len = 0; |
| 276 |
service_receive(client->parent, buf, this_len, &recv_len); |
| 277 |
if (recv_len <= 0) { |
| 278 |
free(buf); |
| 279 |
debug_info("Did not get packet contents!"); |
| 280 |
return AFC_E_NOT_ENOUGH_DATA; |
| 281 |
} |
| 282 |
if (recv_len < this_len) { |
| 283 |
free(buf); |
| 284 |
debug_info("Could not receive this_len=%d bytes", this_len); |
| 285 |
return AFC_E_NOT_ENOUGH_DATA; |
| 286 |
} |
| 287 |
} |
| 288 |
|
| 289 |
current_count = this_len; |
| 290 |
|
| 291 |
if (entire_len > this_len) { |
| 292 |
while (current_count < entire_len) { |
| 293 |
recv_len = 0; |
| 294 |
service_receive(client->parent, buf+current_count, entire_len - current_count, &recv_len); |
| 295 |
if (recv_len <= 0) { |
| 296 |
debug_info("Error receiving data (recv returned %d)", recv_len); |
| 297 |
break; |
| 298 |
} |
| 299 |
current_count += recv_len; |
| 300 |
} |
| 301 |
if (current_count < entire_len) { |
| 302 |
debug_info("WARNING: could not receive full packet (read %s, size %d)", current_count, entire_len); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
if (current_count >= sizeof(uint64_t)) { |
| 307 |
param1 = le64toh(*(uint64_t*)(buf)); |
| 308 |
} |
| 309 |
|
| 310 |
debug_info("packet data size = %i", current_count); |
| 311 |
if (current_count > 256) { |
| 312 |
debug_info("packet data follows (256/%u)", current_count); |
| 313 |
debug_buffer(buf, 256); |
| 314 |
} else { |
| 315 |
debug_info("packet data follows"); |
| 316 |
debug_buffer(buf, current_count); |
| 317 |
} |
| 318 |
|
| 319 |
/* check operation types */ |
| 320 |
if (header.operation == AFC_OP_STATUS) { |
| 321 |
/* status response */ |
| 322 |
debug_info("got a status response, code=%lld", param1); |
| 323 |
|
| 324 |
if (param1 != AFC_E_SUCCESS) { |
| 325 |
/* error status */ |
| 326 |
/* free buffer */ |
| 327 |
free(buf); |
| 328 |
return (afc_error_t)param1; |
| 329 |
} |
| 330 |
} else if (header.operation == AFC_OP_DATA) { |
| 331 |
/* data response */ |
| 332 |
debug_info("got a data response"); |
| 333 |
} else if (header.operation == AFC_OP_FILE_OPEN_RES) { |
| 334 |
/* file handle response */ |
| 335 |
debug_info("got a file handle response, handle=%lld", param1); |
| 336 |
} else if (header.operation == AFC_OP_FILE_TELL_RES) { |
| 337 |
/* tell response */ |
| 338 |
debug_info("got a tell response, position=%lld", param1); |
| 339 |
} else { |
| 340 |
/* unknown operation code received */ |
| 341 |
free(buf); |
| 342 |
|
| 343 |
debug_info("WARNING: Unknown operation code received 0x%llx param1=%lld", header.operation, param1); |
| 344 |
#ifndef _WIN32 |
| 345 |
fprintf(stderr, "%s: WARNING: Unknown operation code received 0x%llx param1=%lld", __func__, (long long)header.operation, (long long)param1); |
| 346 |
#endif |
| 347 |
|
| 348 |
return AFC_E_OP_NOT_SUPPORTED; |
| 349 |
} |
| 350 |
|
| 351 |
if (bytes) { |
| 352 |
*bytes = buf; |
| 353 |
} else { |
| 354 |
free(buf); |
| 355 |
} |
| 356 |
|
| 357 |
*bytes_recv = current_count; |
| 358 |
return AFC_E_SUCCESS; |
| 359 |
} |
| 360 |
|
| 361 |
/** |
| 362 |
* Returns counts of null characters within a string. |
| 363 |
*/ |
| 364 |
static uint32_t count_nullspaces(const char *string, uint32_t number) |
| 365 |
{ |
| 366 |
uint32_t i = 0, nulls = 0; |
| 367 |
|
| 368 |
for (i = 0; i < number; i++) { |
| 369 |
if (string[i] == '\0') |
| 370 |
nulls++; |
| 371 |
} |
| 372 |
|
| 373 |
return nulls; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Splits a string of tokens by null characters and returns each token in a |
| 378 |
* char array/list. |
| 379 |
* |
| 380 |
* @param tokens The characters to split into a list. |
| 381 |
* @param length The length of the tokens string. |
| 382 |
* |
| 383 |
* @return A char ** list with each token found in the string. The caller is |
| 384 |
* responsible for freeing the memory. |
| 385 |
*/ |
| 386 |
static char **make_strings_list(char *tokens, uint32_t length) |
| 387 |
{ |
| 388 |
uint32_t nulls = 0, i = 0, j = 0; |
| 389 |
char **list = NULL; |
| 390 |
|
| 391 |
if (!tokens || !length) |
| 392 |
return NULL; |
| 393 |
|
| 394 |
nulls = count_nullspaces(tokens, length); |
| 395 |
list = (char **) malloc(sizeof(char *) * (nulls + 1)); |
| 396 |
for (i = 0; i < nulls; i++) { |
| 397 |
list[i] = strdup(tokens + j); |
| 398 |
j += strlen(list[i]) + 1; |
| 399 |
} |
| 400 |
list[i] = NULL; |
| 401 |
|
| 402 |
return list; |
| 403 |
} |
| 404 |
|
| 405 |
static plist_t *make_dictionary(char *tokens, size_t length) |
| 406 |
{ |
| 407 |
size_t j = 0; |
| 408 |
plist_t dict = NULL; |
| 409 |
|
| 410 |
if (!tokens || !length) |
| 411 |
return NULL; |
| 412 |
|
| 413 |
dict = plist_new_dict(); |
| 414 |
|
| 415 |
while (j < length) { |
| 416 |
size_t key_len = strnlen(tokens + j, length - j); |
| 417 |
if (j + key_len >= length) { |
| 418 |
plist_free(dict); |
| 419 |
return NULL; |
| 420 |
} |
| 421 |
char* key = tokens + j; |
| 422 |
j += key_len + 1; |
| 423 |
|
| 424 |
if (j >= length) { |
| 425 |
plist_free(dict); |
| 426 |
return NULL; |
| 427 |
} |
| 428 |
|
| 429 |
size_t val_len = strnlen(tokens + j, length - j); |
| 430 |
if (j + val_len >= length) { |
| 431 |
plist_free(dict); |
| 432 |
return NULL; |
| 433 |
} |
| 434 |
char* val = tokens + j; |
| 435 |
j += val_len + 1; |
| 436 |
|
| 437 |
char* endp = NULL; |
| 438 |
unsigned long long u64val = strtoull(val, &endp, 10); |
| 439 |
if (endp && *endp == '\0') { |
| 440 |
plist_dict_set_item(dict, key, plist_new_uint(u64val)); |
| 441 |
} else { |
| 442 |
plist_dict_set_item(dict, key, plist_new_string(val)); |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
return dict; |
| 447 |
} |
| 448 |
|
| 449 |
static int _afc_check_packet_buffer(afc_client_t client, uint32_t data_len) |
| 450 |
{ |
| 451 |
if (data_len > client->packet_extra) { |
| 452 |
client->packet_extra = (data_len & ~8) + 8; |
| 453 |
AFCPacket* newpkt = (AFCPacket*)realloc(client->afc_packet, sizeof(AFCPacket) + client->packet_extra); |
| 454 |
if (!newpkt) { |
| 455 |
return -1; |
| 456 |
} |
| 457 |
client->afc_packet = newpkt; |
| 458 |
} |
| 459 |
return 0; |
| 460 |
} |
| 461 |
|
| 462 |
#define AFC_PACKET_DATA_PTR ((char*)client->afc_packet + sizeof(AFCPacket)) |
| 463 |
|
| 464 |
afc_error_t afc_read_directory(afc_client_t client, const char *path, char ***directory_information) |
| 465 |
{ |
| 466 |
uint32_t bytes = 0; |
| 467 |
char *data = NULL, **list_loc = NULL; |
| 468 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 469 |
|
| 470 |
if (!client || !path || !directory_information || (directory_information && *directory_information)) |
| 471 |
return AFC_E_INVALID_ARG; |
| 472 |
|
| 473 |
afc_lock(client); |
| 474 |
|
| 475 |
uint32_t data_len = (uint32_t)strlen(path)+1; |
| 476 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 477 |
afc_unlock(client); |
| 478 |
debug_info("Failed to realloc packet buffer"); |
| 479 |
return AFC_E_NO_MEM; |
| 480 |
} |
| 481 |
|
| 482 |
/* Send the command */ |
| 483 |
memcpy(AFC_PACKET_DATA_PTR, path, data_len); |
| 484 |
ret = afc_dispatch_packet(client, AFC_OP_READ_DIR, data_len, NULL, 0, &bytes); |
| 485 |
if (ret != AFC_E_SUCCESS) { |
| 486 |
afc_unlock(client); |
| 487 |
return AFC_E_NOT_ENOUGH_DATA; |
| 488 |
} |
| 489 |
/* Receive the data */ |
| 490 |
ret = afc_receive_data(client, &data, &bytes); |
| 491 |
if (ret != AFC_E_SUCCESS) { |
| 492 |
if (data) |
| 493 |
free(data); |
| 494 |
afc_unlock(client); |
| 495 |
return ret; |
| 496 |
} |
| 497 |
/* Parse the data */ |
| 498 |
list_loc = make_strings_list(data, bytes); |
| 499 |
if (data) |
| 500 |
free(data); |
| 501 |
|
| 502 |
afc_unlock(client); |
| 503 |
*directory_information = list_loc; |
| 504 |
|
| 505 |
return ret; |
| 506 |
} |
| 507 |
|
| 508 |
afc_error_t afc_get_device_info(afc_client_t client, char ***device_information) |
| 509 |
{ |
| 510 |
uint32_t bytes = 0; |
| 511 |
char *data = NULL, **list = NULL; |
| 512 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 513 |
|
| 514 |
if (!client || !device_information) |
| 515 |
return AFC_E_INVALID_ARG; |
| 516 |
|
| 517 |
afc_lock(client); |
| 518 |
|
| 519 |
/* Send the command */ |
| 520 |
ret = afc_dispatch_packet(client, AFC_OP_GET_DEVINFO, 0, NULL, 0, &bytes); |
| 521 |
if (ret != AFC_E_SUCCESS) { |
| 522 |
afc_unlock(client); |
| 523 |
return AFC_E_NOT_ENOUGH_DATA; |
| 524 |
} |
| 525 |
/* Receive the data */ |
| 526 |
ret = afc_receive_data(client, &data, &bytes); |
| 527 |
if (ret != AFC_E_SUCCESS) { |
| 528 |
if (data) |
| 529 |
free(data); |
| 530 |
afc_unlock(client); |
| 531 |
return ret; |
| 532 |
} |
| 533 |
/* Parse the data */ |
| 534 |
list = make_strings_list(data, bytes); |
| 535 |
if (data) |
| 536 |
free(data); |
| 537 |
|
| 538 |
afc_unlock(client); |
| 539 |
|
| 540 |
*device_information = list; |
| 541 |
|
| 542 |
return ret; |
| 543 |
} |
| 544 |
|
| 545 |
afc_error_t afc_get_device_info_plist(afc_client_t client, plist_t *device_information) |
| 546 |
{ |
| 547 |
uint32_t bytes = 0; |
| 548 |
char *data = NULL; |
| 549 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 550 |
|
| 551 |
if (!client || !device_information) |
| 552 |
return AFC_E_INVALID_ARG; |
| 553 |
|
| 554 |
afc_lock(client); |
| 555 |
|
| 556 |
/* Send the command */ |
| 557 |
ret = afc_dispatch_packet(client, AFC_OP_GET_DEVINFO, 0, NULL, 0, &bytes); |
| 558 |
if (ret != AFC_E_SUCCESS) { |
| 559 |
afc_unlock(client); |
| 560 |
return AFC_E_NOT_ENOUGH_DATA; |
| 561 |
} |
| 562 |
/* Receive the data */ |
| 563 |
ret = afc_receive_data(client, &data, &bytes); |
| 564 |
if (ret != AFC_E_SUCCESS) { |
| 565 |
if (data) |
| 566 |
free(data); |
| 567 |
afc_unlock(client); |
| 568 |
return ret; |
| 569 |
} |
| 570 |
/* Parse the data */ |
| 571 |
*device_information = make_dictionary(data, bytes); |
| 572 |
free(data); |
| 573 |
|
| 574 |
afc_unlock(client); |
| 575 |
|
| 576 |
return ret; |
| 577 |
} |
| 578 |
|
| 579 |
afc_error_t afc_get_device_info_key(afc_client_t client, const char *key, char **value) |
| 580 |
{ |
| 581 |
afc_error_t ret = AFC_E_INTERNAL_ERROR; |
| 582 |
char **kvps, **ptr; |
| 583 |
|
| 584 |
*value = NULL; |
| 585 |
if (key == NULL) |
| 586 |
return AFC_E_INVALID_ARG; |
| 587 |
|
| 588 |
ret = afc_get_device_info(client, &kvps); |
| 589 |
if (ret != AFC_E_SUCCESS) |
| 590 |
return ret; |
| 591 |
|
| 592 |
for (ptr = kvps; *ptr; ptr++) { |
| 593 |
if (!strcmp(*ptr, key)) { |
| 594 |
*value = strdup(*(ptr+1)); |
| 595 |
break; |
| 596 |
} |
| 597 |
} |
| 598 |
for (ptr = kvps; *ptr; ptr++) { |
| 599 |
free(*ptr); |
| 600 |
} |
| 601 |
free(kvps); |
| 602 |
|
| 603 |
return ret; |
| 604 |
} |
| 605 |
|
| 606 |
afc_error_t afc_remove_path(afc_client_t client, const char *path) |
| 607 |
{ |
| 608 |
uint32_t bytes = 0; |
| 609 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 610 |
|
| 611 |
if (!client || !path || !client->afc_packet || !client->parent) |
| 612 |
return AFC_E_INVALID_ARG; |
| 613 |
|
| 614 |
afc_lock(client); |
| 615 |
|
| 616 |
uint32_t data_len = (uint32_t)strlen(path)+1; |
| 617 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 618 |
afc_unlock(client); |
| 619 |
debug_info("Failed to realloc packet buffer"); |
| 620 |
return AFC_E_NO_MEM; |
| 621 |
} |
| 622 |
|
| 623 |
/* Send command */ |
| 624 |
memcpy(AFC_PACKET_DATA_PTR, path, data_len); |
| 625 |
ret = afc_dispatch_packet(client, AFC_OP_REMOVE_PATH, data_len, NULL, 0, &bytes); |
| 626 |
if (ret != AFC_E_SUCCESS) { |
| 627 |
afc_unlock(client); |
| 628 |
return AFC_E_NOT_ENOUGH_DATA; |
| 629 |
} |
| 630 |
/* Receive response */ |
| 631 |
ret = afc_receive_data(client, NULL, &bytes); |
| 632 |
|
| 633 |
/* special case; unknown error actually means directory not empty */ |
| 634 |
if (ret == AFC_E_UNKNOWN_ERROR) |
| 635 |
ret = AFC_E_DIR_NOT_EMPTY; |
| 636 |
|
| 637 |
afc_unlock(client); |
| 638 |
|
| 639 |
return ret; |
| 640 |
} |
| 641 |
|
| 642 |
afc_error_t afc_rename_path(afc_client_t client, const char *from, const char *to) |
| 643 |
{ |
| 644 |
if (!client || !from || !to || !client->afc_packet || !client->parent) |
| 645 |
return AFC_E_INVALID_ARG; |
| 646 |
|
| 647 |
uint32_t bytes = 0; |
| 648 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 649 |
|
| 650 |
size_t from_len = strlen(from); |
| 651 |
size_t to_len = strlen(to); |
| 652 |
|
| 653 |
afc_lock(client); |
| 654 |
|
| 655 |
uint32_t data_len = (uint32_t)(from_len+1 + to_len+1); |
| 656 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 657 |
afc_unlock(client); |
| 658 |
debug_info("Failed to realloc packet buffer"); |
| 659 |
return AFC_E_NO_MEM; |
| 660 |
} |
| 661 |
|
| 662 |
/* Send command */ |
| 663 |
memcpy(AFC_PACKET_DATA_PTR, from, from_len+1); |
| 664 |
memcpy(AFC_PACKET_DATA_PTR + from_len+1, to, to_len+1); |
| 665 |
ret = afc_dispatch_packet(client, AFC_OP_RENAME_PATH, data_len, NULL, 0, &bytes); |
| 666 |
if (ret != AFC_E_SUCCESS) { |
| 667 |
afc_unlock(client); |
| 668 |
return AFC_E_NOT_ENOUGH_DATA; |
| 669 |
} |
| 670 |
/* Receive response */ |
| 671 |
ret = afc_receive_data(client, NULL, &bytes); |
| 672 |
|
| 673 |
afc_unlock(client); |
| 674 |
|
| 675 |
return ret; |
| 676 |
} |
| 677 |
|
| 678 |
afc_error_t afc_make_directory(afc_client_t client, const char *path) |
| 679 |
{ |
| 680 |
uint32_t bytes = 0; |
| 681 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 682 |
|
| 683 |
if (!client) |
| 684 |
return AFC_E_INVALID_ARG; |
| 685 |
|
| 686 |
afc_lock(client); |
| 687 |
|
| 688 |
uint32_t data_len = (uint32_t)strlen(path)+1; |
| 689 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 690 |
afc_unlock(client); |
| 691 |
debug_info("Failed to realloc packet buffer"); |
| 692 |
return AFC_E_NO_MEM; |
| 693 |
} |
| 694 |
|
| 695 |
/* Send command */ |
| 696 |
memcpy(AFC_PACKET_DATA_PTR, path, data_len); |
| 697 |
ret = afc_dispatch_packet(client, AFC_OP_MAKE_DIR, data_len, NULL, 0, &bytes); |
| 698 |
if (ret != AFC_E_SUCCESS) { |
| 699 |
afc_unlock(client); |
| 700 |
return AFC_E_NOT_ENOUGH_DATA; |
| 701 |
} |
| 702 |
/* Receive response */ |
| 703 |
ret = afc_receive_data(client, NULL, &bytes); |
| 704 |
|
| 705 |
afc_unlock(client); |
| 706 |
|
| 707 |
return ret; |
| 708 |
} |
| 709 |
|
| 710 |
afc_error_t afc_get_file_info(afc_client_t client, const char *path, char ***file_information) |
| 711 |
{ |
| 712 |
char *received = NULL; |
| 713 |
uint32_t bytes = 0; |
| 714 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 715 |
|
| 716 |
if (!client || !path || !file_information) |
| 717 |
return AFC_E_INVALID_ARG; |
| 718 |
|
| 719 |
afc_lock(client); |
| 720 |
|
| 721 |
uint32_t data_len = (uint32_t)strlen(path)+1; |
| 722 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 723 |
afc_unlock(client); |
| 724 |
debug_info("Failed to realloc packet buffer"); |
| 725 |
return AFC_E_NO_MEM; |
| 726 |
} |
| 727 |
|
| 728 |
/* Send command */ |
| 729 |
memcpy(AFC_PACKET_DATA_PTR, path, data_len); |
| 730 |
ret = afc_dispatch_packet(client, AFC_OP_GET_FILE_INFO, data_len, NULL, 0, &bytes); |
| 731 |
if (ret != AFC_E_SUCCESS) { |
| 732 |
afc_unlock(client); |
| 733 |
return AFC_E_NOT_ENOUGH_DATA; |
| 734 |
} |
| 735 |
|
| 736 |
/* Receive data */ |
| 737 |
ret = afc_receive_data(client, &received, &bytes); |
| 738 |
if (received) { |
| 739 |
*file_information = make_strings_list(received, bytes); |
| 740 |
free(received); |
| 741 |
} |
| 742 |
|
| 743 |
afc_unlock(client); |
| 744 |
|
| 745 |
return ret; |
| 746 |
} |
| 747 |
|
| 748 |
afc_error_t afc_get_file_info_plist(afc_client_t client, const char *path, plist_t *file_information) |
| 749 |
{ |
| 750 |
char *received = NULL; |
| 751 |
uint32_t bytes = 0; |
| 752 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 753 |
|
| 754 |
if (!client || !path || !file_information) |
| 755 |
return AFC_E_INVALID_ARG; |
| 756 |
|
| 757 |
afc_lock(client); |
| 758 |
|
| 759 |
uint32_t data_len = (uint32_t)strlen(path)+1; |
| 760 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 761 |
afc_unlock(client); |
| 762 |
debug_info("Failed to realloc packet buffer"); |
| 763 |
return AFC_E_NO_MEM; |
| 764 |
} |
| 765 |
|
| 766 |
/* Send command */ |
| 767 |
memcpy(AFC_PACKET_DATA_PTR, path, data_len); |
| 768 |
ret = afc_dispatch_packet(client, AFC_OP_GET_FILE_INFO, data_len, NULL, 0, &bytes); |
| 769 |
if (ret != AFC_E_SUCCESS) { |
| 770 |
afc_unlock(client); |
| 771 |
return AFC_E_NOT_ENOUGH_DATA; |
| 772 |
} |
| 773 |
|
| 774 |
/* Receive data */ |
| 775 |
ret = afc_receive_data(client, &received, &bytes); |
| 776 |
if (received) { |
| 777 |
*file_information = make_dictionary(received, bytes); |
| 778 |
free(received); |
| 779 |
} |
| 780 |
|
| 781 |
afc_unlock(client); |
| 782 |
|
| 783 |
return ret; |
| 784 |
} |
| 785 |
|
| 786 |
afc_error_t afc_file_open(afc_client_t client, const char *filename, afc_file_mode_t file_mode, uint64_t *handle) |
| 787 |
{ |
| 788 |
if (!client || !client->parent || !client->afc_packet) |
| 789 |
return AFC_E_INVALID_ARG; |
| 790 |
|
| 791 |
//uint64_t file_mode_loc = htole64(file_mode); |
| 792 |
uint32_t bytes = 0; |
| 793 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 794 |
|
| 795 |
/* set handle to 0 so in case an error occurs, the handle is invalid */ |
| 796 |
*handle = 0; |
| 797 |
|
| 798 |
afc_lock(client); |
| 799 |
|
| 800 |
uint32_t data_len = (uint32_t)(strlen(filename)+1 + 8); |
| 801 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 802 |
afc_unlock(client); |
| 803 |
debug_info("Failed to realloc packet buffer"); |
| 804 |
return AFC_E_NO_MEM; |
| 805 |
} |
| 806 |
|
| 807 |
/* Send command */ |
| 808 |
//memcpy(AFC_PACKET_DATA_PTR, &file_mode_loc, 8); |
| 809 |
*(uint64_t*)(AFC_PACKET_DATA_PTR) = htole64(file_mode); |
| 810 |
memcpy(AFC_PACKET_DATA_PTR + 8, filename, data_len-8); |
| 811 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_OPEN, data_len, NULL, 0, &bytes); |
| 812 |
if (ret != AFC_E_SUCCESS) { |
| 813 |
debug_info("Didn't receive a response to the command"); |
| 814 |
afc_unlock(client); |
| 815 |
return AFC_E_NOT_ENOUGH_DATA; |
| 816 |
} |
| 817 |
/* Receive the data */ |
| 818 |
char* data = NULL; |
| 819 |
ret = afc_receive_data(client, &data, &bytes); |
| 820 |
if ((ret == AFC_E_SUCCESS) && (bytes > 0) && data) { |
| 821 |
afc_unlock(client); |
| 822 |
|
| 823 |
/* Get the file handle */ |
| 824 |
memcpy(handle, data, sizeof(uint64_t)); |
| 825 |
free(data); |
| 826 |
return ret; |
| 827 |
} |
| 828 |
/* in case memory was allocated but no data received or an error occurred */ |
| 829 |
free(data); |
| 830 |
|
| 831 |
debug_info("Didn't get any further data"); |
| 832 |
|
| 833 |
afc_unlock(client); |
| 834 |
|
| 835 |
return ret; |
| 836 |
} |
| 837 |
|
| 838 |
afc_error_t afc_file_read(afc_client_t client, uint64_t handle, char *data, uint32_t length, uint32_t *bytes_read) |
| 839 |
{ |
| 840 |
char *input = NULL; |
| 841 |
uint32_t current_count = 0, bytes_loc = 0; |
| 842 |
struct readinfo { |
| 843 |
uint64_t handle; |
| 844 |
uint64_t size; |
| 845 |
}; |
| 846 |
afc_error_t ret = AFC_E_SUCCESS; |
| 847 |
|
| 848 |
if (!client || !client->afc_packet || !client->parent || handle == 0) |
| 849 |
return AFC_E_INVALID_ARG; |
| 850 |
debug_info("called for length %i", length); |
| 851 |
|
| 852 |
//uint32_t data_len = 8 + 8; |
| 853 |
|
| 854 |
afc_lock(client); |
| 855 |
|
| 856 |
/* Send the read command */ |
| 857 |
struct readinfo* readinfo = (struct readinfo*)(AFC_PACKET_DATA_PTR); |
| 858 |
readinfo->handle = handle; |
| 859 |
readinfo->size = htole64(length); |
| 860 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_READ, sizeof(struct readinfo), NULL, 0, &bytes_loc); |
| 861 |
if (ret != AFC_E_SUCCESS) { |
| 862 |
afc_unlock(client); |
| 863 |
return AFC_E_NOT_ENOUGH_DATA; |
| 864 |
} |
| 865 |
/* Receive the data */ |
| 866 |
ret = afc_receive_data(client, &input, &bytes_loc); |
| 867 |
debug_info("afc_receive_data returned error: %d", ret); |
| 868 |
debug_info("bytes returned: %i", bytes_loc); |
| 869 |
if (ret != AFC_E_SUCCESS) { |
| 870 |
afc_unlock(client); |
| 871 |
return ret; |
| 872 |
} |
| 873 |
if (bytes_loc == 0) { |
| 874 |
if (input) |
| 875 |
free(input); |
| 876 |
afc_unlock(client); |
| 877 |
*bytes_read = current_count; |
| 878 |
/* FIXME: check that's actually a success */ |
| 879 |
return ret; |
| 880 |
} |
| 881 |
if (input) { |
| 882 |
debug_info("%d", bytes_loc); |
| 883 |
memcpy(data + current_count, input, (bytes_loc > length) ? length : bytes_loc); |
| 884 |
free(input); |
| 885 |
input = NULL; |
| 886 |
current_count += (bytes_loc > length) ? length : bytes_loc; |
| 887 |
} |
| 888 |
|
| 889 |
afc_unlock(client); |
| 890 |
*bytes_read = current_count; |
| 891 |
return ret; |
| 892 |
} |
| 893 |
|
| 894 |
afc_error_t afc_file_write(afc_client_t client, uint64_t handle, const char *data, uint32_t length, uint32_t *bytes_written) |
| 895 |
{ |
| 896 |
uint32_t current_count = 0; |
| 897 |
uint32_t bytes_loc = 0; |
| 898 |
afc_error_t ret = AFC_E_SUCCESS; |
| 899 |
|
| 900 |
if (!client || !client->afc_packet || !client->parent || !bytes_written || (handle == 0)) |
| 901 |
return AFC_E_INVALID_ARG; |
| 902 |
|
| 903 |
uint32_t data_len = 8; |
| 904 |
|
| 905 |
afc_lock(client); |
| 906 |
|
| 907 |
debug_info("Write length: %i", length); |
| 908 |
|
| 909 |
*(uint64_t*)(AFC_PACKET_DATA_PTR) = handle; |
| 910 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_WRITE, data_len, data, length, &bytes_loc); |
| 911 |
|
| 912 |
current_count += bytes_loc - (sizeof(AFCPacket) + 8); |
| 913 |
|
| 914 |
if (ret != AFC_E_SUCCESS) { |
| 915 |
afc_unlock(client); |
| 916 |
*bytes_written = current_count; |
| 917 |
return AFC_E_SUCCESS; |
| 918 |
} |
| 919 |
|
| 920 |
ret = afc_receive_data(client, NULL, &bytes_loc); |
| 921 |
afc_unlock(client); |
| 922 |
if (ret != AFC_E_SUCCESS) { |
| 923 |
debug_info("Failed to receive reply (%d)", ret); |
| 924 |
} |
| 925 |
*bytes_written = current_count; |
| 926 |
return ret; |
| 927 |
} |
| 928 |
|
| 929 |
afc_error_t afc_file_close(afc_client_t client, uint64_t handle) |
| 930 |
{ |
| 931 |
uint32_t bytes = 0; |
| 932 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 933 |
|
| 934 |
if (!client || (handle == 0)) |
| 935 |
return AFC_E_INVALID_ARG; |
| 936 |
|
| 937 |
uint32_t data_len = 8; |
| 938 |
|
| 939 |
afc_lock(client); |
| 940 |
|
| 941 |
debug_info("File handle %i", handle); |
| 942 |
|
| 943 |
/* Send command */ |
| 944 |
*(uint64_t*)(AFC_PACKET_DATA_PTR) = handle; |
| 945 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_CLOSE, data_len, NULL, 0, &bytes); |
| 946 |
|
| 947 |
if (ret != AFC_E_SUCCESS) { |
| 948 |
afc_unlock(client); |
| 949 |
return AFC_E_UNKNOWN_ERROR; |
| 950 |
} |
| 951 |
|
| 952 |
/* Receive the response */ |
| 953 |
ret = afc_receive_data(client, NULL, &bytes); |
| 954 |
|
| 955 |
afc_unlock(client); |
| 956 |
|
| 957 |
return ret; |
| 958 |
} |
| 959 |
|
| 960 |
afc_error_t afc_file_lock(afc_client_t client, uint64_t handle, afc_lock_op_t operation) |
| 961 |
{ |
| 962 |
uint32_t bytes = 0; |
| 963 |
struct lockinfo { |
| 964 |
uint64_t handle; |
| 965 |
uint64_t op; |
| 966 |
}; |
| 967 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 968 |
|
| 969 |
if (!client || (handle == 0)) |
| 970 |
return AFC_E_INVALID_ARG; |
| 971 |
|
| 972 |
afc_lock(client); |
| 973 |
|
| 974 |
debug_info("file handle %i", handle); |
| 975 |
|
| 976 |
/* Send command */ |
| 977 |
struct lockinfo* lockinfo = (struct lockinfo*)(AFC_PACKET_DATA_PTR); |
| 978 |
lockinfo->handle = handle; |
| 979 |
lockinfo->op = htole64(operation); |
| 980 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_LOCK, sizeof(struct lockinfo), NULL, 0, &bytes); |
| 981 |
if (ret != AFC_E_SUCCESS) { |
| 982 |
afc_unlock(client); |
| 983 |
debug_info("could not send lock command"); |
| 984 |
return AFC_E_UNKNOWN_ERROR; |
| 985 |
} |
| 986 |
/* Receive the response */ |
| 987 |
ret = afc_receive_data(client, NULL, &bytes); |
| 988 |
|
| 989 |
afc_unlock(client); |
| 990 |
|
| 991 |
return ret; |
| 992 |
} |
| 993 |
|
| 994 |
afc_error_t afc_file_seek(afc_client_t client, uint64_t handle, int64_t offset, int whence) |
| 995 |
{ |
| 996 |
uint32_t bytes = 0; |
| 997 |
struct seekinfo { |
| 998 |
uint64_t handle; |
| 999 |
uint64_t whence; |
| 1000 |
int64_t offset; |
| 1001 |
}; |
| 1002 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1003 |
|
| 1004 |
if (!client || (handle == 0)) |
| 1005 |
return AFC_E_INVALID_ARG; |
| 1006 |
|
| 1007 |
afc_lock(client); |
| 1008 |
|
| 1009 |
/* Send the command */ |
| 1010 |
struct seekinfo* seekinfo = (struct seekinfo*)(AFC_PACKET_DATA_PTR); |
| 1011 |
seekinfo->handle = handle; |
| 1012 |
seekinfo->whence = htole64(whence); |
| 1013 |
seekinfo->offset = (int64_t)htole64(offset); |
| 1014 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_SEEK, sizeof(struct seekinfo), NULL, 0, &bytes); |
| 1015 |
|
| 1016 |
if (ret != AFC_E_SUCCESS) { |
| 1017 |
afc_unlock(client); |
| 1018 |
return AFC_E_NOT_ENOUGH_DATA; |
| 1019 |
} |
| 1020 |
/* Receive response */ |
| 1021 |
ret = afc_receive_data(client, NULL, &bytes); |
| 1022 |
|
| 1023 |
afc_unlock(client); |
| 1024 |
|
| 1025 |
return ret; |
| 1026 |
} |
| 1027 |
|
| 1028 |
afc_error_t afc_file_tell(afc_client_t client, uint64_t handle, uint64_t *position) |
| 1029 |
{ |
| 1030 |
char *buffer = NULL; |
| 1031 |
uint32_t bytes = 0; |
| 1032 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1033 |
|
| 1034 |
if (!client || (handle == 0)) |
| 1035 |
return AFC_E_INVALID_ARG; |
| 1036 |
|
| 1037 |
uint32_t data_len = 8; |
| 1038 |
|
| 1039 |
afc_lock(client); |
| 1040 |
|
| 1041 |
/* Send the command */ |
| 1042 |
*(uint64_t*)(AFC_PACKET_DATA_PTR) = handle; |
| 1043 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_TELL, data_len, NULL, 0, &bytes); |
| 1044 |
if (ret != AFC_E_SUCCESS) { |
| 1045 |
afc_unlock(client); |
| 1046 |
return AFC_E_NOT_ENOUGH_DATA; |
| 1047 |
} |
| 1048 |
|
| 1049 |
/* Receive the data */ |
| 1050 |
ret = afc_receive_data(client, &buffer, &bytes); |
| 1051 |
if (bytes > 0 && buffer) { |
| 1052 |
/* Get the position */ |
| 1053 |
memcpy(position, buffer, sizeof(uint64_t)); |
| 1054 |
*position = le64toh(*position); |
| 1055 |
} |
| 1056 |
free(buffer); |
| 1057 |
|
| 1058 |
afc_unlock(client); |
| 1059 |
|
| 1060 |
return ret; |
| 1061 |
} |
| 1062 |
|
| 1063 |
afc_error_t afc_file_truncate(afc_client_t client, uint64_t handle, uint64_t newsize) |
| 1064 |
{ |
| 1065 |
uint32_t bytes = 0; |
| 1066 |
struct truncinfo { |
| 1067 |
uint64_t handle; |
| 1068 |
uint64_t newsize; |
| 1069 |
}; |
| 1070 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1071 |
|
| 1072 |
if (!client || (handle == 0)) |
| 1073 |
return AFC_E_INVALID_ARG; |
| 1074 |
|
| 1075 |
afc_lock(client); |
| 1076 |
|
| 1077 |
/* Send command */ |
| 1078 |
struct truncinfo* truncinfo = (struct truncinfo*)(AFC_PACKET_DATA_PTR); |
| 1079 |
truncinfo->handle = handle; |
| 1080 |
truncinfo->newsize = htole64(newsize); |
| 1081 |
ret = afc_dispatch_packet(client, AFC_OP_FILE_SET_SIZE, sizeof(struct truncinfo), NULL, 0, &bytes); |
| 1082 |
|
| 1083 |
if (ret != AFC_E_SUCCESS) { |
| 1084 |
afc_unlock(client); |
| 1085 |
return AFC_E_NOT_ENOUGH_DATA; |
| 1086 |
} |
| 1087 |
/* Receive response */ |
| 1088 |
ret = afc_receive_data(client, NULL, &bytes); |
| 1089 |
|
| 1090 |
afc_unlock(client); |
| 1091 |
|
| 1092 |
return ret; |
| 1093 |
} |
| 1094 |
|
| 1095 |
afc_error_t afc_truncate(afc_client_t client, const char *path, uint64_t newsize) |
| 1096 |
{ |
| 1097 |
if (!client || !path || !client->afc_packet || !client->parent) |
| 1098 |
return AFC_E_INVALID_ARG; |
| 1099 |
|
| 1100 |
uint32_t bytes = 0; |
| 1101 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1102 |
|
| 1103 |
afc_lock(client); |
| 1104 |
|
| 1105 |
uint32_t data_len = 8 + (uint32_t)(strlen(path)+1); |
| 1106 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 1107 |
afc_unlock(client); |
| 1108 |
debug_info("Failed to realloc packet buffer"); |
| 1109 |
return AFC_E_NO_MEM; |
| 1110 |
} |
| 1111 |
|
| 1112 |
/* Send command */ |
| 1113 |
*(uint64_t*)(AFC_PACKET_DATA_PTR) = htole64(newsize); |
| 1114 |
memcpy(AFC_PACKET_DATA_PTR + 8, path, data_len-8); |
| 1115 |
ret = afc_dispatch_packet(client, AFC_OP_TRUNCATE, data_len, NULL, 0, &bytes); |
| 1116 |
if (ret != AFC_E_SUCCESS) { |
| 1117 |
afc_unlock(client); |
| 1118 |
return AFC_E_NOT_ENOUGH_DATA; |
| 1119 |
} |
| 1120 |
/* Receive response */ |
| 1121 |
ret = afc_receive_data(client, NULL, &bytes); |
| 1122 |
|
| 1123 |
afc_unlock(client); |
| 1124 |
|
| 1125 |
return ret; |
| 1126 |
} |
| 1127 |
|
| 1128 |
afc_error_t afc_make_link(afc_client_t client, afc_link_type_t linktype, const char *target, const char *linkname) |
| 1129 |
{ |
| 1130 |
if (!client || !target || !linkname || !client->afc_packet || !client->parent) |
| 1131 |
return AFC_E_INVALID_ARG; |
| 1132 |
|
| 1133 |
uint32_t bytes = 0; |
| 1134 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1135 |
|
| 1136 |
size_t target_len = strlen(target); |
| 1137 |
size_t link_len = strlen(linkname); |
| 1138 |
|
| 1139 |
afc_lock(client); |
| 1140 |
|
| 1141 |
uint32_t data_len = 8 + target_len + 1 + link_len + 1; |
| 1142 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 1143 |
afc_unlock(client); |
| 1144 |
debug_info("Failed to realloc packet buffer"); |
| 1145 |
return AFC_E_NO_MEM; |
| 1146 |
} |
| 1147 |
|
| 1148 |
debug_info("link type: %lld", htole64(linktype)); |
| 1149 |
debug_info("target: %s, length:%d", target, target_len); |
| 1150 |
debug_info("linkname: %s, length:%d", linkname, link_len); |
| 1151 |
|
| 1152 |
/* Send command */ |
| 1153 |
*(uint64_t*)(AFC_PACKET_DATA_PTR) = htole64(linktype); |
| 1154 |
memcpy(AFC_PACKET_DATA_PTR + 8, target, target_len + 1); |
| 1155 |
memcpy(AFC_PACKET_DATA_PTR + 8 + target_len + 1, linkname, link_len + 1); |
| 1156 |
ret = afc_dispatch_packet(client, AFC_OP_MAKE_LINK, data_len, NULL, 0, &bytes); |
| 1157 |
if (ret != AFC_E_SUCCESS) { |
| 1158 |
afc_unlock(client); |
| 1159 |
return AFC_E_NOT_ENOUGH_DATA; |
| 1160 |
} |
| 1161 |
/* Receive response */ |
| 1162 |
ret = afc_receive_data(client, NULL, &bytes); |
| 1163 |
|
| 1164 |
afc_unlock(client); |
| 1165 |
|
| 1166 |
return ret; |
| 1167 |
} |
| 1168 |
|
| 1169 |
afc_error_t afc_set_file_time(afc_client_t client, const char *path, uint64_t mtime) |
| 1170 |
{ |
| 1171 |
if (!client || !path || !client->afc_packet || !client->parent) |
| 1172 |
return AFC_E_INVALID_ARG; |
| 1173 |
|
| 1174 |
uint32_t bytes = 0; |
| 1175 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1176 |
|
| 1177 |
afc_lock(client); |
| 1178 |
|
| 1179 |
uint32_t data_len = 8 + strlen(path) + 1; |
| 1180 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 1181 |
afc_unlock(client); |
| 1182 |
debug_info("Failed to realloc packet buffer"); |
| 1183 |
return AFC_E_NO_MEM; |
| 1184 |
} |
| 1185 |
|
| 1186 |
/* Send command */ |
| 1187 |
*(uint64_t*)(AFC_PACKET_DATA_PTR) = htole64(mtime); |
| 1188 |
memcpy(AFC_PACKET_DATA_PTR + 8, path, data_len-8); |
| 1189 |
ret = afc_dispatch_packet(client, AFC_OP_SET_FILE_MOD_TIME, data_len, NULL, 0, &bytes); |
| 1190 |
if (ret != AFC_E_SUCCESS) { |
| 1191 |
afc_unlock(client); |
| 1192 |
return AFC_E_NOT_ENOUGH_DATA; |
| 1193 |
} |
| 1194 |
/* Receive response */ |
| 1195 |
ret = afc_receive_data(client, NULL, &bytes); |
| 1196 |
|
| 1197 |
afc_unlock(client); |
| 1198 |
|
| 1199 |
return ret; |
| 1200 |
} |
| 1201 |
|
| 1202 |
afc_error_t afc_remove_path_and_contents(afc_client_t client, const char *path) |
| 1203 |
{ |
| 1204 |
uint32_t bytes = 0; |
| 1205 |
afc_error_t ret = AFC_E_UNKNOWN_ERROR; |
| 1206 |
|
| 1207 |
if (!client || !path || !client->afc_packet || !client->parent) |
| 1208 |
return AFC_E_INVALID_ARG; |
| 1209 |
|
| 1210 |
afc_lock(client); |
| 1211 |
|
| 1212 |
uint32_t data_len = strlen(path) + 1; |
| 1213 |
if (_afc_check_packet_buffer(client, data_len) < 0) { |
| 1214 |
afc_unlock(client); |
| 1215 |
debug_info("Failed to realloc packet buffer"); |
| 1216 |
return AFC_E_NO_MEM; |
| 1217 |
} |
| 1218 |
|
| 1219 |
/* Send command */ |
| 1220 |
memcpy(AFC_PACKET_DATA_PTR, path, data_len); |
| 1221 |
ret = afc_dispatch_packet(client, AFC_OP_REMOVE_PATH_AND_CONTENTS, data_len, NULL, 0, &bytes); |
| 1222 |
if (ret != AFC_E_SUCCESS) { |
| 1223 |
afc_unlock(client); |
| 1224 |
return AFC_E_NOT_ENOUGH_DATA; |
| 1225 |
} |
| 1226 |
/* Receive response */ |
| 1227 |
ret = afc_receive_data(client, NULL, &bytes); |
| 1228 |
|
| 1229 |
afc_unlock(client); |
| 1230 |
|
| 1231 |
return ret; |
| 1232 |
} |
| 1233 |
|
| 1234 |
afc_error_t afc_dictionary_free(char **dictionary) |
| 1235 |
{ |
| 1236 |
int i = 0; |
| 1237 |
|
| 1238 |
if (!dictionary) |
| 1239 |
return AFC_E_INVALID_ARG; |
| 1240 |
|
| 1241 |
for (i = 0; dictionary[i]; i++) { |
| 1242 |
free(dictionary[i]); |
| 1243 |
} |
| 1244 |
free(dictionary); |
| 1245 |
|
| 1246 |
return AFC_E_SUCCESS; |
| 1247 |
} |
| 1248 |
|
| 1249 |
const char* afc_strerror(afc_error_t err) |
| 1250 |
{ |
| 1251 |
switch (err) { |
| 1252 |
case AFC_E_SUCCESS: |
| 1253 |
return "Success"; |
| 1254 |
case AFC_E_UNKNOWN_ERROR: |
| 1255 |
return "Unknown Error"; |
| 1256 |
case AFC_E_OP_HEADER_INVALID: |
| 1257 |
return "Operation header invalid"; |
| 1258 |
case AFC_E_NO_RESOURCES: |
| 1259 |
return "No resources"; |
| 1260 |
case AFC_E_READ_ERROR: |
| 1261 |
return "Read error"; |
| 1262 |
case AFC_E_WRITE_ERROR: |
| 1263 |
return "Write error"; |
| 1264 |
case AFC_E_UNKNOWN_PACKET_TYPE: |
| 1265 |
return "Unknown packet type"; |
| 1266 |
case AFC_E_INVALID_ARG: |
| 1267 |
return "Invalid argument"; |
| 1268 |
case AFC_E_OBJECT_NOT_FOUND: |
| 1269 |
return "Not found"; |
| 1270 |
case AFC_E_OBJECT_IS_DIR: |
| 1271 |
return "Object is a directory"; |
| 1272 |
case AFC_E_PERM_DENIED: |
| 1273 |
return "Permission denied"; |
| 1274 |
case AFC_E_SERVICE_NOT_CONNECTED: |
| 1275 |
return "Service not connected"; |
| 1276 |
case AFC_E_OP_TIMEOUT: |
| 1277 |
return "Timeout"; |
| 1278 |
case AFC_E_TOO_MUCH_DATA: |
| 1279 |
return "Too much data"; |
| 1280 |
case AFC_E_END_OF_DATA: |
| 1281 |
return "End of data"; |
| 1282 |
case AFC_E_OP_NOT_SUPPORTED: |
| 1283 |
return "Operation not supported"; |
| 1284 |
case AFC_E_OBJECT_EXISTS: |
| 1285 |
return "Object exists"; |
| 1286 |
case AFC_E_OBJECT_BUSY: |
| 1287 |
return "Object busy"; |
| 1288 |
case AFC_E_NO_SPACE_LEFT: |
| 1289 |
return "No space left on device"; |
| 1290 |
case AFC_E_OP_WOULD_BLOCK: |
| 1291 |
return "Operation would block"; |
| 1292 |
case AFC_E_IO_ERROR: |
| 1293 |
return "I/O error"; |
| 1294 |
case AFC_E_OP_INTERRUPTED: |
| 1295 |
return "Operation interrupted"; |
| 1296 |
case AFC_E_OP_IN_PROGRESS: |
| 1297 |
return "Operation on progress"; |
| 1298 |
case AFC_E_INTERNAL_ERROR: |
| 1299 |
return "Internal error"; |
| 1300 |
case AFC_E_MUX_ERROR: |
| 1301 |
return "MUX error"; |
| 1302 |
case AFC_E_NO_MEM: |
| 1303 |
return "Out of memory"; |
| 1304 |
case AFC_E_NOT_ENOUGH_DATA: |
| 1305 |
return "Not enough data"; |
| 1306 |
case AFC_E_DIR_NOT_EMPTY: |
| 1307 |
return "Directory not empty"; |
| 1308 |
case AFC_E_FORCE_SIGNED_TYPE: |
| 1309 |
return "Force signed type"; |
| 1310 |
default: |
| 1311 |
break; |
| 1312 |
} |
| 1313 |
return "Unknown Error"; |
| 1314 |
} |