]> Pileus Git - grits/blobdiff - src/data/gis-http.c
Add horizon test
[grits] / src / data / gis-http.c
index fb742c7e0e8d1d8b5c0d3981226c7e45cc197902..144168d3e9e3d556f8a48a231c4ee2aaad979509 100644 (file)
@@ -117,7 +117,7 @@ static void _chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _info)
 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,
@@ -130,10 +130,13 @@ gchar *gis_http_fetch(GisHttp *http, const gchar *uri, const char *local,
        /* 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 = {
@@ -151,14 +154,23 @@ gchar *gis_http_fetch(GisHttp *http, const gchar *uri, const char *local,
                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.. */