From 4405a0fff74faaed363bc3ea4c4997293ac9d4a3 Mon Sep 17 00:00:00 2001 From: Nikias Bassen Date: Thu, 29 Jul 2021 02:45:10 +0200 Subject: common: Return proper error codes from userpref_read_pair_record --- common/userpref.c | 33 +++++++++++++++++++-------------- common/userpref.h | 9 +++++---- 2 files changed, 24 insertions(+), 18 deletions(-) (limited to 'common') diff --git a/common/userpref.c b/common/userpref.c index 0c6050f..bf7e1bd 100644 --- a/common/userpref.c +++ b/common/userpref.c @@ -309,8 +309,10 @@ userpref_error_t userpref_save_pair_record(const char *udid, uint32_t device_id, * @param udid The device UDID as given by the device * @param pair_record The pair record to read * - * @return 1 on success and 0 if no device record is given or if it has already - * been saved previously. + * @return USERPREF_E_SUCCESS on success, + * USERPREF_E_NOENT if no pairing record was found, + * USERPREF_E_READ_ERROR if retrieving the pairing record from usbmuxd failed, + * or USERPREF_E_INVALID_CONF otherwise. */ userpref_error_t userpref_read_pair_record(const char *udid, plist_t *pair_record) { @@ -318,24 +320,27 @@ userpref_error_t userpref_read_pair_record(const char *udid, plist_t *pair_recor uint32_t record_size = 0; int res = usbmuxd_read_pair_record(udid, &record_data, &record_size); - if (res < 0) { - if (record_data) - free(record_data); - - return USERPREF_E_INVALID_CONF; + free(record_data); + switch (-res) { + case ENOENT: + return USERPREF_E_NOENT; + case ETIMEDOUT: + return USERPREF_E_READ_ERROR; + default: + return USERPREF_E_INVALID_CONF; + } } *pair_record = NULL; - if (memcmp(record_data, "bplist00", 8) == 0) { - plist_from_bin(record_data, record_size, pair_record); - } else { - plist_from_xml(record_data, record_size, pair_record); - } - + plist_from_memory(record_data, record_size, pair_record); free(record_data); - return res == 0 ? USERPREF_E_SUCCESS: USERPREF_E_UNKNOWN_ERROR; + if (!*pair_record) { + debug_info("Failed to parse pairing record"); + return USERPREF_E_INVALID_CONF; + } + return USERPREF_E_SUCCESS; } /** diff --git a/common/userpref.h b/common/userpref.h index 072721a..75bb8b7 100644 --- a/common/userpref.h +++ b/common/userpref.h @@ -54,10 +54,11 @@ typedef gnutls_datum_t key_data_t; typedef enum { USERPREF_E_SUCCESS = 0, USERPREF_E_INVALID_ARG = -1, - USERPREF_E_INVALID_CONF = -2, - USERPREF_E_SSL_ERROR = -3, - USERPREF_E_READ_ERROR = -4, - USERPREF_E_WRITE_ERROR = -5, + USERPREF_E_NOENT = -2, + USERPREF_E_INVALID_CONF = -3, + USERPREF_E_SSL_ERROR = -4, + USERPREF_E_READ_ERROR = -5, + USERPREF_E_WRITE_ERROR = -6, USERPREF_E_UNKNOWN_ERROR = -256 } userpref_error_t; -- cgit v1.1-32-gdbae