#include <glib/gstdio.h>
#include <glib/gi18n.h>
-#ifdef HAVE_MAGIC_H
-#include <magic.h>
-#endif
+#include "file_magic.h"
#include "compression.h"
-
#include "download.h"
#include "curl_download.h"
*/
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 ) )
g_critical ("%s: file rename failed [%s] to [%s]", __FUNCTION__, bz2_name, name );
}
}
-
- return;
-#endif
}
#define VIKING_ETAG_XATTR "xattr::viking.etag"
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};
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 ) {
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 ) )
{
}
/* 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;