diff options
author | Nikias Bassen | 2021-09-01 15:54:53 +0200 |
---|---|---|
committer | Nikias Bassen | 2021-09-01 15:54:53 +0200 |
commit | 32a8ebedc695dfae21497977bf2f77a3d5b50f91 (patch) | |
tree | 0cfd7f83c4d49ee4a20ecdc249167f6dc084396d | |
parent | c3a16f3e4968f38aa8813ad0c0731bdb5aa745fb (diff) | |
download | usbmuxd-32a8ebedc695dfae21497977bf2f77a3d5b50f91.tar.gz usbmuxd-32a8ebedc695dfae21497977bf2f77a3d5b50f91.tar.bz2 |
Remove common code in favor of new libimobiledevice-glue
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | configure.ac | 4 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/client.c | 43 | ||||
-rw-r--r-- | src/conf.c | 2 | ||||
-rw-r--r-- | src/device.c | 69 | ||||
-rw-r--r-- | src/preflight.c | 15 | ||||
-rw-r--r-- | src/usb.c | 2 | ||||
-rw-r--r-- | src/utils.c | 248 | ||||
-rw-r--r-- | src/utils.h | 43 |
10 files changed, 74 insertions, 357 deletions
@@ -45,6 +45,7 @@ sudo apt-get install \ libplist-dev \ libusbmuxd-dev \ libimobiledevice-dev \ + libimobiledevice-glue-dev \ libusb-1.0-0-dev \ udev ``` @@ -152,4 +153,4 @@ iPadOS, tvOS, watchOS, and macOS are trademarks of Apple Inc. usbmuxd is an independent software application and has not been authorized, sponsored, or otherwise approved by Apple Inc. -README Updated on: 2020-06-13 +README Updated on: 2021-08-30 diff --git a/configure.ac b/configure.ac index 7a70be9..b751cc2 100644 --- a/configure.ac +++ b/configure.ac @@ -18,7 +18,7 @@ LT_INIT PKG_CHECK_MODULES(libusb, libusb-1.0 >= 1.0.9) PKG_CHECK_MODULES(libplist, libplist-2.0 >= 2.2.0) PKG_CHECK_MODULES(libimobiledevice, libimobiledevice-1.0 >= 1.3.0, have_limd=yes, have_limd=no) -AC_CHECK_LIB(pthread, [pthread_create, pthread_mutex_lock], [AC_SUBST(libpthread_LIBS,[-lpthread])], [AC_MSG_ERROR([libpthread is required to build usbmuxd])]) +PKG_CHECK_MODULES(limd_glue, libimobiledevice-glue-1.0 >= 1.0.0) AC_ARG_WITH([preflight], [AS_HELP_STRING([--without-preflight], @@ -106,7 +106,7 @@ AC_TYPE_UINT8_T AC_SEARCH_LIBS([clock_gettime],[rt posix4]) # Checks for library functions. -AC_CHECK_FUNCS([strcasecmp strdup strerror strndup stpcpy malloc realloc]) +AC_CHECK_FUNCS([strcasecmp strdup strerror strndup malloc realloc]) AC_CHECK_FUNCS([ppoll clock_gettime localtime_r]) # Check for operating system diff --git a/src/Makefile.am b/src/Makefile.am index 328b700..8a96e46 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -6,11 +6,13 @@ AM_CFLAGS = \ $(GLOBAL_CFLAGS) \ $(libplist_CFLAGS) \ $(libusb_CFLAGS) \ + $(limd_glue_CFLAGS) \ $(libimobiledevice_CFLAGS) AM_LDFLAGS = \ $(libplist_LIBS) \ $(libusb_LIBS) \ + $(limd_glue_LIBS) \ $(libimobiledevice_LIBS) \ $(libpthread_LIBS) diff --git a/src/client.c b/src/client.c index 7395046..75a526d 100644 --- a/src/client.c +++ b/src/client.c @@ -34,10 +34,11 @@ #include <netinet/tcp.h> #include <sys/un.h> #include <arpa/inet.h> -#include <pthread.h> #include <fcntl.h> #include <plist/plist.h> +#include <libimobiledevice-glue/collection.h> +#include <libimobiledevice-glue/thread.h> #include "log.h" #include "usb.h" @@ -75,7 +76,7 @@ struct mux_client { }; static struct collection client_list; -pthread_mutex_t client_list_mutex; +mutex_t client_list_mutex; static uint32_t client_number = 0; #ifdef SO_PEERCRED @@ -224,10 +225,10 @@ int client_accept(int listenfd) client->events = POLLIN; client->info = NULL; - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); client->number = client_number++; collection_add(&client_list, client); - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); #ifdef SO_PEERCRED if (log_level >= LL_INFO) { @@ -252,7 +253,7 @@ int client_accept(int listenfd) void client_close(struct mux_client *client) { int found = 0; - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); FOREACH(struct mux_client *lc, &client_list) { if (client == lc) { found = 1; @@ -262,7 +263,7 @@ void client_close(struct mux_client *client) if (!found) { // in case we get called again but client was already freed usbmuxd_log(LL_DEBUG, "%s: ignoring for non-existing client %p", __func__, client); - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); return; } #ifdef SO_PEERCRED @@ -293,17 +294,17 @@ void client_close(struct mux_client *client) plist_free(client->info); collection_remove(&client_list, client); - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); free(client); } void client_get_fds(struct fdlist *list) { - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); FOREACH(struct mux_client *client, &client_list) { fdlist_add(list, FD_CLIENT, client->fd, client->events); } ENDFOREACH - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); } static int output_buffer_add_message(struct mux_client *client, uint32_t tag, enum usbmuxd_msgtype msg, void *payload, int payload_length) @@ -442,7 +443,7 @@ static int send_listener_list(struct mux_client *client, uint32_t tag) plist_t dict = plist_new_dict(); plist_t listeners = plist_new_array(); - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); FOREACH(struct mux_client *lc, &client_list) { if (lc->state == CLIENT_LISTEN) { plist_t n = NULL; @@ -489,7 +490,7 @@ static int send_listener_list(struct mux_client *client, uint32_t tag) plist_array_append_item(listeners, l); } } ENDFOREACH - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); plist_dict_set_item(dict, "ListenerList", listeners); res = send_plist(client, tag, dict); @@ -975,14 +976,14 @@ static void input_buffer_process(struct mux_client *client) void client_process(int fd, short events) { struct mux_client *client = NULL; - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); FOREACH(struct mux_client *lc, &client_list) { if(lc->fd == fd) { client = lc; break; } } ENDFOREACH - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); if(!client) { usbmuxd_log(LL_INFO, "client_process: fd %d not found in client list", fd); @@ -1004,45 +1005,45 @@ void client_process(int fd, short events) void client_device_add(struct device_info *dev) { - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); usbmuxd_log(LL_DEBUG, "client_device_add: id %d, location 0x%x, serial %s", dev->id, dev->location, dev->serial); device_set_visible(dev->id); FOREACH(struct mux_client *client, &client_list) { if(client->state == CLIENT_LISTEN) send_device_add(client, dev); } ENDFOREACH - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); } void client_device_remove(int device_id) { - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); uint32_t id = device_id; usbmuxd_log(LL_DEBUG, "client_device_remove: id %d", device_id); FOREACH(struct mux_client *client, &client_list) { if(client->state == CLIENT_LISTEN) send_device_remove(client, id); } ENDFOREACH - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); } void client_device_paired(int device_id) { - pthread_mutex_lock(&client_list_mutex); + mutex_lock(&client_list_mutex); uint32_t id = device_id; usbmuxd_log(LL_DEBUG, "client_device_paired: id %d", device_id); FOREACH(struct mux_client *client, &client_list) { if (client->state == CLIENT_LISTEN) send_device_paired(client, id); } ENDFOREACH - pthread_mutex_unlock(&client_list_mutex); + mutex_unlock(&client_list_mutex); } void client_init(void) { usbmuxd_log(LL_DEBUG, "client_init"); collection_init(&client_list); - pthread_mutex_init(&client_list_mutex, NULL); + mutex_init(&client_list_mutex); } void client_shutdown(void) @@ -1051,6 +1052,6 @@ void client_shutdown(void) FOREACH(struct mux_client *client, &client_list) { client_close(client); } ENDFOREACH - pthread_mutex_destroy(&client_list_mutex); + mutex_destroy(&client_list_mutex); collection_free(&client_list); } @@ -39,6 +39,8 @@ #include <shlobj.h> #endif +#include <libimobiledevice-glue/utils.h> + #include "conf.h" #include "utils.h" #include "log.h" diff --git a/src/device.c b/src/device.c index aac40b1..0928021 100644 --- a/src/device.c +++ b/src/device.c @@ -32,8 +32,11 @@ #include <string.h> #include <stdint.h> #include <inttypes.h> -#include <pthread.h> #include <unistd.h> + +#include <libimobiledevice-glue/collection.h> +#include <libimobiledevice-glue/thread.h> + #include "device.h" #include "client.h" #include "preflight.h" @@ -127,19 +130,19 @@ struct mux_device }; static struct collection device_list; -pthread_mutex_t device_list_mutex; +mutex_t device_list_mutex; static struct mux_device* get_mux_device_for_id(int device_id) { struct mux_device *dev = NULL; - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *cdev, &device_list) { if(cdev->id == device_id) { dev = cdev; break; } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); return dev; } @@ -166,7 +169,7 @@ static int get_next_device_id(void) { while(1) { int ok = 1; - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *dev, &device_list) { if(dev->id == next_device_id) { next_device_id++; @@ -174,7 +177,7 @@ static int get_next_device_id(void) break; } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); if(ok) return next_device_id++; } @@ -464,9 +467,9 @@ static int send_tcp_ack(struct mux_connection *conn) */ void device_client_process(int device_id, struct mux_client *client, short events) { - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); struct mux_connection *conn = get_mux_connection(device_id, client); - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); if(!conn) { usbmuxd_log(LL_WARNING, "Could not find connection for device %d client %p", device_id, client); return; @@ -566,9 +569,9 @@ static void device_version_input(struct mux_device *dev, struct version_header * vh->minor = ntohl(vh->minor); if(vh->major != 2 && vh->major != 1) { usbmuxd_log(LL_ERROR, "Device %d has unknown version %d.%d", dev->id, vh->major, vh->minor); - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); collection_remove(&device_list, dev); - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); free(dev); return; } @@ -726,14 +729,14 @@ static void device_tcp_input(struct mux_device *dev, struct tcphdr *th, unsigned void device_data_input(struct usb_device *usbdev, unsigned char *buffer, uint32_t length) { struct mux_device *dev = NULL; - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *tdev, &device_list) { if(tdev->usbdev == usbdev) { dev = tdev; break; } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); if(!dev) { usbmuxd_log(LL_WARNING, "Cannot find device entry for RX input from USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); return; @@ -850,15 +853,15 @@ int device_add(struct usb_device *usbdev) free(dev); return res; } - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); collection_add(&device_list, dev); - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); return 0; } void device_remove(struct usb_device *usbdev) { - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *dev, &device_list) { if(dev->usbdev == usbdev) { usbmuxd_log(LL_NOTICE, "Removed device %d on location 0x%x", dev->id, usb_get_location(usbdev)); @@ -874,48 +877,48 @@ void device_remove(struct usb_device *usbdev) preflight_device_remove_cb(dev->preflight_cb_data); } collection_remove(&device_list, dev); - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); free(dev->pktbuf); free(dev); return; } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); usbmuxd_log(LL_WARNING, "Cannot find device entry while removing USB device %p on location 0x%x", usbdev, usb_get_location(usbdev)); } void device_set_visible(int device_id) { - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *dev, &device_list) { if(dev->id == device_id) { dev->visible = 1; break; } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); } void device_set_preflight_cb_data(int device_id, void* data) { - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *dev, &device_list) { if(dev->id == device_id) { dev->preflight_cb_data = data; break; } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); } int device_get_count(int include_hidden) { int count = 0; struct collection dev_list = {NULL, 0}; - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); collection_copy(&dev_list, &device_list); - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); FOREACH(struct mux_device *dev, &dev_list) { if((dev->state == MUXDEV_ACTIVE) && (include_hidden || dev->visible)) @@ -930,9 +933,9 @@ int device_get_list(int include_hidden, struct device_info **devices) { int count = 0; struct collection dev_list = {NULL, 0}; - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); collection_copy(&dev_list, &device_list); - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); *devices = malloc(sizeof(struct device_info) * dev_list.capacity); struct device_info *p = *devices; @@ -957,7 +960,7 @@ int device_get_list(int include_hidden, struct device_info **devices) int device_get_timeout(void) { uint64_t oldest = (uint64_t)-1LL; - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *dev, &device_list) { if(dev->state == MUXDEV_ACTIVE) { FOREACH(struct mux_connection *conn, &dev->connections) { @@ -966,7 +969,7 @@ int device_get_timeout(void) } ENDFOREACH } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); uint64_t ct = mstime64(); if((int64_t)oldest == -1LL) return 100000; //meh @@ -978,7 +981,7 @@ int device_get_timeout(void) void device_check_timeouts(void) { uint64_t ct = mstime64(); - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *dev, &device_list) { if(dev->state == MUXDEV_ACTIVE) { FOREACH(struct mux_connection *conn, &dev->connections) { @@ -991,14 +994,14 @@ void device_check_timeouts(void) } ENDFOREACH } } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); + mutex_unlock(&device_list_mutex); } void device_init(void) { usbmuxd_log(LL_DEBUG, "device_init"); collection_init(&device_list); - pthread_mutex_init(&device_list_mutex, NULL); + mutex_init(&device_list_mutex); next_device_id = 1; } @@ -1019,7 +1022,7 @@ void device_kill_connections(void) void device_shutdown(void) { usbmuxd_log(LL_DEBUG, "device_shutdown"); - pthread_mutex_lock(&device_list_mutex); + mutex_lock(&device_list_mutex); FOREACH(struct mux_device *dev, &device_list) { FOREACH(struct mux_connection *conn, &dev->connections) { connection_teardown(conn); @@ -1028,7 +1031,7 @@ void device_shutdown(void) collection_remove(&device_list, dev); free(dev); } ENDFOREACH - pthread_mutex_unlock(&device_list_mutex); - pthread_mutex_destroy(&device_list_mutex); + mutex_unlock(&device_list_mutex); + mutex_destroy(&device_list_mutex); collection_free(&device_list); } diff --git a/src/preflight.c b/src/preflight.c index 6a303c2..5902f5d 100644 --- a/src/preflight.c +++ b/src/preflight.c @@ -26,8 +26,6 @@ #include <unistd.h> #include <errno.h> -#include <pthread.h> - #include <sys/time.h> #ifdef HAVE_LIBIMOBILEDEVICE @@ -36,6 +34,8 @@ #include <libimobiledevice/notification_proxy.h> #endif +#include <libimobiledevice-glue/thread.h> + #include "preflight.h" #include "device.h" #include "client.h" @@ -389,18 +389,15 @@ void preflight_worker_device_add(struct device_info* info) infocopy->serial = strdup(info->serial); } - pthread_t th; - pthread_attr_t attr; - - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - - int perr = pthread_create(&th, &attr, preflight_worker_handle_device_add, infocopy); + THREAD_T th; + int perr = thread_new(&th, preflight_worker_handle_device_add, infocopy); if (perr != 0) { free((char*)infocopy->serial); free(infocopy); usbmuxd_log(LL_ERROR, "ERROR: failed to start preflight worker thread for device %s: %s (%d). Invoking client_device_add() directly but things might not work as expected.", info->serial, strerror(perr), perr); client_device_add(info); + } else { + thread_detach(th); } #else client_device_add(info); @@ -31,6 +31,8 @@ #include <libusb.h> +#include <libimobiledevice-glue/collection.h> + #include "usb.h" #include "log.h" #include "device.h" diff --git a/src/utils.c b/src/utils.c index 206c684..2cc5675 100644 --- a/src/utils.c +++ b/src/utils.c @@ -76,254 +76,6 @@ void fdlist_reset(struct fdlist *list) list->count = 0; } -#define CAPACITY_STEP 8 - -void collection_init(struct collection *col) -{ - col->list = malloc(sizeof(void *) * CAPACITY_STEP); - memset(col->list, 0, sizeof(void *) * CAPACITY_STEP); - col->capacity = CAPACITY_STEP; -} - -void collection_free(struct collection *col) -{ - free(col->list); - col->list = NULL; - col->capacity = 0; -} - -void collection_add(struct collection *col, void *element) -{ - int i; - for(i=0; i<col->capacity; i++) { - if(!col->list[i]) { - col->list[i] = element; - return; - } - } - col->list = realloc(col->list, sizeof(void*) * (col->capacity + CAPACITY_STEP)); - memset(&col->list[col->capacity], 0, sizeof(void *) * CAPACITY_STEP); - col->list[col->capacity] = element; - col->capacity += CAPACITY_STEP; -} - -void collection_remove(struct collection *col, void *element) -{ - int i; - for(i=0; i<col->capacity; i++) { - if(col->list[i] == element) { - col->list[i] = NULL; - return; - } - } - util_error("collection_remove: element %p not present in collection %p (cap %d)", element, col, col->capacity); -} - -int collection_count(struct collection *col) -{ - int i, cnt = 0; - for(i=0; i<col->capacity; i++) { - if(col->list[i]) - cnt++; - } - return cnt; -} - -void collection_copy(struct collection *dest, struct collection *src) -{ - if (!dest || !src) return; - dest->capacity = src->capacity; - dest->list = malloc(sizeof(void*) * src->capacity); - memcpy(dest->list, src->list, sizeof(void*) * src->capacity); -} - -#ifndef HAVE_STPCPY -/** - * Copy characters from one string into another - * - * @note: The strings should not overlap, as the behavior is undefined. - * - * @s1: The source string. - * @s2: The destination string. - * - * @return a pointer to the terminating `\0' character of @s1, - * or NULL if @s1 or @s2 is NULL. - */ -char *stpcpy(char * s1, const char * s2) -{ - if (s1 == NULL || s2 == NULL) - return NULL; - - strcpy(s1, s2); - - return s1 + strlen(s2); -} -#endif - -/** - * Concatenate strings into a newly allocated string - * - * @note: Specify NULL for the last string in the varargs list - * - * @str: The first string in the list - * @...: Subsequent strings. Use NULL for the last item. - * - * @return a newly allocated string, or NULL if @str is NULL. This will also - * return NULL and set errno to ENOMEM if memory is exhausted. - */ -char *string_concat(const char *str, ...) -{ - size_t len; - va_list args; - char *s; - char *result; - char *dest; - - if (!str) - return NULL; - - /* Compute final length */ - - len = strlen(str) + 1; /* plus 1 for the null terminator */ - - va_start(args, str); - s = va_arg(args, char *); - while (s) { - len += strlen(s); - s = va_arg(args, char*); - } - va_end(args); - - /* Concat each string */ - - result = malloc(len); - if (!result) - return NULL; /* errno remains set */ - - dest = result; - - dest = stpcpy(dest, str); - - va_start(args, str); - s = va_arg(args, char *); - while (s) { - dest = stpcpy(dest, s); - s = va_arg(args, char *); - } - va_end(args); - - return result; -} - -int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length) -{ - FILE *f; - uint64_t size; - - *length = 0; - - f = fopen(filename, "rb"); - if (!f) { - return 0; - } - - fseek(f, 0, SEEK_END); - size = ftell(f); - rewind(f); - - if (size == 0) { - fclose(f); - return 0; - } - - *buffer = (char*)malloc(sizeof(char)*(size+1)); - - if (!buffer) { - return 0; - } - - int ret = 1; - if (fread(*buffer, sizeof(char), size, f) != size) { - usbmuxd_log(LL_ERROR, "%s: ERROR: couldn't read %d bytes from %s", __func__, (int)size, filename); - free(*buffer); - ret = 0; - errno = EIO; - } - fclose(f); - - *length = size; - return ret; -} - -int buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length) -{ - FILE *f; - - f = fopen(filename, "wb"); - if (f) { - size_t written = fwrite(buffer, sizeof(char), length, f); - fclose(f); - - if (written == length) { - return 1; - } - else { - // Not all data could be written. - errno = EIO; - return 0; - } - } - else { - // Failed to open the file, let the caller know. - return 0; - } -} - -int plist_read_from_filename(plist_t *plist, const char *filename) -{ - char *buffer = NULL; - uint64_t length; - - if (!filename) - return 0; - - if (!buffer_read_from_filename(filename, &buffer, &length)) { - return 0; - } - - if ((length > 8) && (memcmp(buffer, "bplist00", 8) == 0)) { - plist_from_bin(buffer, length, plist); - } else { - plist_from_xml(buffer, length, plist); - } - - free(buffer); - - return 1; -} - -int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format) -{ - char *buffer = NULL; - uint32_t length; - - if (!plist || !filename) - return 0; - - if (format == PLIST_FORMAT_XML) - plist_to_xml(plist, &buffer, &length); - else if (format == PLIST_FORMAT_BINARY) - plist_to_bin(plist, &buffer, &length); - else - return 0; - - int res = buffer_write_to_filename(filename, buffer, length); - - free(buffer); - - return res; -} - #ifndef HAVE_CLOCK_GETTIME typedef int clockid_t; #define CLOCK_MONOTONIC 1 diff --git a/src/utils.h b/src/utils.h index b5cab3f..ce3b2e0 100644 --- a/src/utils.h +++ b/src/utils.h @@ -43,49 +43,6 @@ void fdlist_add(struct fdlist *list, enum fdowner owner, int fd, short events); void fdlist_free(struct fdlist *list); void fdlist_reset(struct fdlist *list); -struct collection { - void **list; - int capacity; -}; - -void collection_init(struct collection *col); -void collection_add(struct collection *col, void *element); -void collection_remove(struct collection *col, void *element); -int collection_count(struct collection *col); -void collection_free(struct collection *col); -void collection_copy(struct collection *dest, struct collection *src); - -#define MERGE_(a,b) a ## _ ## b -#define LABEL_(a,b) MERGE_(a, b) -#define UNIQUE_VAR(a) LABEL_(a, __LINE__) - -#define FOREACH(var, col) \ - do { \ - int UNIQUE_VAR(_iter); \ - for(UNIQUE_VAR(_iter)=0; UNIQUE_VAR(_iter)<(col)->capacity; UNIQUE_VAR(_iter)++) { \ - if(!(col)->list[UNIQUE_VAR(_iter)]) continue; \ - var = (col)->list[UNIQUE_VAR(_iter)]; - -#define ENDFOREACH \ - } \ - } while(0); - -#ifndef HAVE_STPCPY -char *stpcpy(char * s1, const char * s2); -#endif -char *string_concat(const char *str, ...); - -int buffer_read_from_filename(const char *filename, char **buffer, uint64_t *length); -int buffer_write_to_filename(const char *filename, const char *buffer, uint64_t length); - -enum plist_format_t { - PLIST_FORMAT_XML, - PLIST_FORMAT_BINARY -}; - -int plist_read_from_filename(plist_t *plist, const char *filename); -int plist_write_to_filename(plist_t plist, const char *filename, enum plist_format_t format); - uint64_t mstime64(void); void get_tick_count(struct timeval * tv); |