gchar *gis_http_fetch(GisHttp *http, const gchar *uri, const char *local,
GisCacheType mode, GisChunkCallback callback, gpointer user_data)
{
- g_debug("GisHttp: fetch - %.20s... >> %s/%s mode=%d",
+ g_debug("GisHttp: fetch - %s... >> %s/%s mode=%d",
uri, http->prefix, local, mode);
gchar *path = g_build_filename(g_get_user_cache_dir(), PACKAGE,
/* Do the cache if necessasairy */
if (!(mode == GIS_ONCE && g_file_test(path, G_FILE_TEST_EXISTS)) &&
mode != GIS_LOCAL) {
- g_debug("GisHttp: do_cache - Caching file %s", local);
+ g_debug("GisHttp: fetch - Caching file %s", local);
/* Open the file for writting */
- FILE *fp = fopen_p(path, "a");
+ gchar *part = path;
+ if (!g_file_test(path, G_FILE_TEST_EXISTS))
+ part = g_strdup_printf("%s.part", path);
+ FILE *fp = fopen_p(part, "a");
/* Make temp data */
struct _CacheInfo info = {
soup_message_headers_set_range(message->request_headers, ftell(fp), -1);
soup_session_send_message(http->soup, message);
+ /* Close file */
+ fclose(fp);
+ if (path != part && SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
+ g_rename(part, path);
+ g_free(part);
+ }
+
/* Finished */
if (message->status_code == 416) {
/* Range unsatisfiable, file already complete */
- } else if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
+ } else if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
g_warning("GisHttp: done_cb - error copying file, status=%d\n"
"\tsrc=%s\n"
"\tdst=%s",
message->status_code, uri, path);
+ return NULL;
+ }
}
/* TODO: free everything.. */