X-Git-Url: http://pileus.org/git/?p=grits;a=blobdiff_plain;f=src%2Fdata.c;h=0b6293ddb6b80bf90b049393967d26afa0579656;hp=ab373114c6b0f3e3529f2208d1d1d8bc53860951;hb=1cf8b4ecd92f49dfbbe0472e02d4e5bc8841fe2f;hpb=93c853165104b5119e17be07e325f9097a5ebdb3 diff --git a/src/data.c b/src/data.c index ab37311..0b6293d 100644 --- a/src/data.c +++ b/src/data.c @@ -16,82 +16,112 @@ */ #include +#include #include -#include +#include #include "data.h" typedef struct { AWeatherCacheDoneCallback callback; - gchar *src; - gchar *dst; - gchar *user_data; + gpointer user_data; + gchar *local; + FILE *fp; } cache_file_end_t; -static void cache_file_cb(GObject *source_object, GAsyncResult *res, gpointer _info) +/* + * Open a file, creating parent directories if needed + */ +static FILE *fopen_p(const gchar *path, const gchar *mode) +{ + gchar *parent = g_path_get_dirname(path); + if (!g_file_test(parent, G_FILE_TEST_EXISTS)) + g_mkdir_with_parents(parent, 0755); + g_free(parent); + return fopen(path, mode); +} + +static void cache_file_cb(SoupSession *session, SoupMessage *message, gpointer _info) { cache_file_end_t *info = _info; - GError *error = NULL; - g_file_copy_finish(G_FILE(source_object), res, &error); - if (error) { - g_message("error copying file ([%s]->[%s]): %s", - info->src, info->dst, error->message); + gchar *uri = soup_uri_to_string(soup_message_get_uri(message), FALSE); + g_debug("data: cache_file_cb"); + + if (message->status_code == 416) { + /* Range unsatisfiable, file already complete */ + info->callback(info->local, FALSE, info->user_data); + } else if (SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) { + gint wrote = fwrite(message->response_body->data, 1, + message->response_body->length, info->fp); + g_debug("data: status=%u wrote=%d/%lld", + message->status_code, + wrote, message->response_body->length); + fclose(info->fp); + info->callback(info->local, TRUE, info->user_data); } else { - info->callback(info->dst, info->user_data); + g_warning("data: cache_file_cb - error copying file, status=%d\n" + "\tsrc=%s\n" + "\tdst=%s", + message->status_code, uri, info->local); } - g_free(info->src); - g_free(info->dst); - g_free(info); + g_free(uri); + g_free(info->local); + g_object_unref(session); } -static goffset g_file_get_size(GFile *file) +static void do_cache(gchar *uri, gchar *local, gboolean truncate, gchar *reason, + AWeatherCacheDoneCallback callback, gpointer user_data) { - GError *error = NULL; - GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_STANDARD_SIZE, 0, NULL, &error); - if (error) - g_warning("unable to get file size: %s", error->message); - return g_file_info_get_size(info); + char *name = g_path_get_basename(uri); + g_debug("data: do_cache - Caching file %s: %s", name, reason); + g_free(name); + + cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t)); + info->callback = callback; + info->user_data = user_data; + info->local = local; + + /* TODO: move this to callback so we don't end up with 0 byte files + * Then change back to check for valid file after download */ + if (truncate) info->fp = fopen_p(local, "w"); + else info->fp = fopen_p(local, "a"); + long bytes = ftell(info->fp); + + SoupSession *session = soup_session_async_new(); + SoupMessage *message = soup_message_new("GET", uri); + if (message == NULL) + g_error("message is null, cannot parse uri"); + if (bytes != 0) + soup_message_headers_set_range(message->request_headers, bytes, -1); + soup_session_queue_message(session, message, cache_file_cb, info); } -/** +/* * Cache a image from Ridge to the local disk * \param path Path to the Ridge file, starting after /ridge/ * \return The local path to the cached image */ -void cache_file(char *base, char *path, AWeatherCacheDoneCallback callback, gpointer user_data) +void cache_file(char *base, char *path, AWeatherCacheType update, + AWeatherCacheDoneCallback callback, gpointer user_data) { - gchar *url = g_strconcat(base, path, NULL); + gchar *uri = g_strconcat(base, path, NULL); gchar *local = g_build_filename(g_get_user_cache_dir(), PACKAGE, path, NULL); - GFile *src = g_file_new_for_uri(url); - GFile *dst = g_file_new_for_path(local); - if (!g_file_test(local, G_FILE_TEST_EXISTS)) - g_message("Caching file: local does not exist - %s", local); - else if (g_file_get_size(src) != g_file_get_size(dst)) - g_message("Caching file: sizes mismatch - %lld != %lld", - g_file_get_size(src), g_file_get_size(dst)); - else { - callback(local, user_data); - g_free(local); - g_free(url); - return; - } + if (update == AWEATHER_REFRESH) + return do_cache(uri, local, TRUE, "cache forced", + callback, user_data); - char *dir = g_path_get_dirname(local); - if (!g_file_test(dir, G_FILE_TEST_IS_DIR)) - g_mkdir_with_parents(dir, 0755); - g_free(dir); - cache_file_end_t *info = g_malloc0(sizeof(cache_file_end_t)); - info->callback = callback; - info->src = url; - info->dst = local; - info->user_data = user_data; - g_file_copy_async(src, dst, - G_FILE_COPY_OVERWRITE, // GFileCopyFlags flags, - 0, // int io_priority, - NULL, // GCancellable *cancellable, - NULL, // GFileProgressCallback progress_callback, - NULL, // gpointer progress_callback_data, - cache_file_cb, // GAsyncReadyCallback callback, - info); // gpointer user_data + if (update == AWEATHER_UPDATE) + return do_cache(uri, local, FALSE, "attempting updating", + callback, user_data); + + if (update == AWEATHER_ONCE && !g_file_test(local, G_FILE_TEST_EXISTS)) + return do_cache(uri, local, TRUE, "local does not exist", + callback, user_data); + + /* No nead to cache, run the callback now and clean up */ + callback(local, FALSE, user_data); + g_free(local); + g_free(uri); + return; }