X-Git-Url: https://git.street.me.uk/andy/viking.git/blobdiff_plain/686baff0fa2461a831bd1d0b614478a31c6a0a95..6e23e477012f9103c62754ea347395afe8e7c580:/src/download.c diff --git a/src/download.c b/src/download.c index 45aefbc9..798bdba2 100644 --- a/src/download.c +++ b/src/download.c @@ -40,11 +40,8 @@ #include #include -#ifdef HAVE_MAGIC_H -#include -#endif +#include "file_magic.h" #include "compression.h" - #include "download.h" #include "curl_download.h" @@ -205,48 +202,10 @@ static void uncompress_zip ( gchar *name ) */ void a_try_decompress_file (gchar *name) { -#ifdef HAVE_MAGIC_H -#ifdef MAGIC_VERSION - // Or magic_version() if available - probably need libmagic 5.18 or so - // (can't determine exactly which version the versioning became available) - g_debug ("%s: magic version: %d", __FUNCTION__, MAGIC_VERSION ); -#endif - magic_t myt = magic_open ( MAGIC_CONTINUE|MAGIC_ERROR|MAGIC_MIME ); - gboolean zip = FALSE; - gboolean bzip2 = FALSE; - if ( myt ) { -#ifdef WINDOWS - // We have to 'package' the magic database ourselves :( - // --> %PROGRAM FILES%\Viking\magic.mgc - int ml = magic_load ( myt, ".\\magic.mgc" ); -#else - // Use system default - int ml = magic_load ( myt, NULL ); -#endif - if ( ml == 0 ) { - const char* magic = magic_file (myt, name); - g_debug ("%s: magic output: %s", __FUNCTION__, magic ); - - if ( g_strcmp0 (magic, "application/zip; charset=binary") == 0 ) - zip = TRUE; - - if ( g_strcmp0 (magic, "application/x-bzip2; charset=binary") == 0 ) - bzip2 = TRUE; - } - else { - g_critical ("%s: magic load database failure", __FUNCTION__ ); - } - - magic_close ( myt ); - } - - if ( !(zip || bzip2) ) - return; - - if ( zip ) { + if ( file_magic_check (name, "application/zip", ".zip") ) { uncompress_zip ( name ); } - else if ( bzip2 ) { + else if ( file_magic_check (name, "application/x-bzip2", ".bz2") ) { gchar* bz2_name = uncompress_bzip2 ( name ); if ( bz2_name ) { if ( g_remove ( name ) ) @@ -255,9 +214,6 @@ void a_try_decompress_file (gchar *name) g_critical ("%s: file rename failed [%s] to [%s]", __FUNCTION__, bz2_name, name ); } } - - return; -#endif } #define VIKING_ETAG_XATTR "xattr::viking.etag" @@ -363,7 +319,6 @@ static void set_etag(const char *fn, const char *fntmp, CurlDownloadOptions *cdo static DownloadResult_t download( const char *hostname, const char *uri, const char *fn, DownloadFileOptions *options, gboolean ftp, void *handle) { FILE *f; - int ret; gchar *tmpfilename; gboolean failure = FALSE; CurlDownloadOptions cdo = {0, NULL, NULL}; @@ -379,7 +334,7 @@ static DownloadResult_t download( const char *hostname, const char *uri, const c time_t tile_age = a_preferences_get(VIKING_PREFERENCES_NAMESPACE "download_tile_age")->u; /* Get the modified time of this file */ - struct stat buf; + GStatBuf buf; (void)g_stat ( fn, &buf ); time_t file_time = buf.st_mtime; if ( (time(NULL) - file_time) < tile_age ) { @@ -401,6 +356,12 @@ static DownloadResult_t download( const char *hostname, const char *uri, const c g_free ( dir ); } + // Early test for valid hostname & uri to avoid unnecessary tmp file + if ( !hostname && !uri ) { + g_warning ( "%s: Parameter error - neither hostname nor uri defined", __FUNCTION__ ); + return DOWNLOAD_PARAMETERS_ERROR; + } + tmpfilename = g_strdup_printf("%s.tmp", fn); if (!lock_file ( tmpfilename ) ) { @@ -420,7 +381,7 @@ static DownloadResult_t download( const char *hostname, const char *uri, const c } /* Call the backend function */ - ret = curl_download_get_url ( hostname, uri, f, options, ftp, &cdo, handle ); + CURL_download_t ret = curl_download_get_url ( hostname, uri, f, options, ftp, &cdo, handle ); DownloadResult_t result = DOWNLOAD_SUCCESS;