]> git.sesse.net Git - vlc/blobdiff - src/misc/update.c
Merge branch '0.9.0-libass' of git://git.videolan.org/vlc
[vlc] / src / misc / update.c
index 5e555e2a30c783da426cccb43b56f50421576d7b..4e83249b840cd902834f3f01c1b886c92994260a 100644 (file)
@@ -53,6 +53,7 @@
 #include <vlc_gcrypt.h>
 
 #include "update.h"
+#include "../libvlc.h"
 
 /*****************************************************************************
  * Misc defines
@@ -1049,6 +1050,9 @@ update_t *__update_New( vlc_object_t *p_this )
     p_update->release.psz_url = NULL;
     p_update->release.psz_desc = NULL;
 
+    p_update->p_download = NULL;
+    p_update->p_check = NULL;
+
     p_update->p_pkey = NULL;
     vlc_gcrypt_init();
 
@@ -1065,6 +1069,18 @@ void update_Delete( update_t *p_update )
 {
     assert( p_update );
 
+    if( p_update->p_check )
+    {
+        assert( !p_update->p_download );
+        vlc_object_kill( p_update->p_check );
+        vlc_thread_join( p_update->p_check );
+    }
+    else if( p_update->p_download )
+    {
+        vlc_object_kill( p_update->p_download );
+        vlc_thread_join( p_update->p_download );
+    }
+
     vlc_mutex_destroy( &p_update->lock );
 
     free( p_update->release.psz_url );
@@ -1336,19 +1352,7 @@ error:
     return false;
 }
 
-
-/**
- * Struct to launch the check in an other thread
- */
-typedef struct
-{
-    VLC_COMMON_MEMBERS
-    update_t *p_update;
-    void (*pf_callback)( void *, bool );
-    void *p_data;
-} update_check_thread_t;
-
-void update_CheckReal( update_check_thread_t *p_uct );
+static void* update_CheckReal( vlc_object_t *p_this );
 
 /**
  * Check for updates
@@ -1362,11 +1366,13 @@ void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void
 {
     assert( p_update );
 
-    update_check_thread_t *p_uct = vlc_object_create( p_update->p_libvlc,
-                                            sizeof( update_check_thread_t ) );
+    update_check_thread_t *p_uct =
+        vlc_custom_create( p_update->p_libvlc, sizeof( *p_uct ),
+                           VLC_OBJECT_GENERIC, "update check" );
     if( !p_uct ) return;
 
     p_uct->p_update = p_update;
+    p_update->p_check = p_uct;
     p_uct->pf_callback = pf_callback;
     p_uct->p_data = p_data;
 
@@ -1374,8 +1380,9 @@ void update_Check( update_t *p_update, void (*pf_callback)( void*, bool ), void
                        VLC_THREAD_PRIORITY_LOW, false );
 }
 
-void update_CheckReal( update_check_thread_t *p_uct )
+void* update_CheckReal( vlc_object_t* p_this )
 {
+    update_check_thread_t *p_uct = (update_check_thread_t *)p_this;
     bool b_ret;
     vlc_mutex_lock( &p_uct->p_update->lock );
 
@@ -1386,7 +1393,10 @@ void update_CheckReal( update_check_thread_t *p_uct )
     if( p_uct->pf_callback )
         (p_uct->pf_callback)( p_uct->p_data, b_ret );
 
+    p_uct->p_update->p_check = NULL;
+
     vlc_object_release( p_uct );
+    return NULL;
 }
 
 /**
@@ -1395,14 +1405,32 @@ void update_CheckReal( update_check_thread_t *p_uct )
  * \param p_update structure
  * \return true if we have to upgrade to the given version to be up to date
  */
+static bool is_strictly_greater( int * a, int * b, int n)
+{
+    if( n <= 0 ) return false;
+    if(a[0] > b[0] ) return true;
+    if(a[0] == b[0] ) return is_strictly_greater( a+1, b+1, n-1 );
+    /* a[0] < b[0] */ return false;
+}
+
 bool update_NeedUpgrade( update_t *p_update )
 {
     assert( p_update );
 
-    return  p_update->release.i_major    < *PACKAGE_VERSION_MAJOR    - '0'  ||
-            p_update->release.i_minor    < *PACKAGE_VERSION_MINOR    - '0'  ||
-            p_update->release.i_revision < *PACKAGE_VERSION_REVISION - '0'  ||
-            p_update->release.extra      < *PACKAGE_VERSION_EXTRA;
+    int current_version[] = {
+        *PACKAGE_VERSION_MAJOR - '0',
+        *PACKAGE_VERSION_MINOR - '0',
+        *PACKAGE_VERSION_REVISION - '0',
+        *PACKAGE_VERSION_EXTRA
+    };
+    int latest_version[] = {
+        p_update->release.i_major,
+        p_update->release.i_minor,
+        p_update->release.i_revision,
+        p_update->release.extra
+    };
+
+    return is_strictly_greater( latest_version, current_version, 4 );
 }
 
 /**
@@ -1427,18 +1455,7 @@ static char *size_str( long int l_size )
     return i_retval == -1 ? NULL : psz_tmp;
 }
 
-
-/**
- * Struct to launch the download in a thread
- */
-typedef struct
-{
-    VLC_COMMON_MEMBERS
-    update_t *p_update;
-    char *psz_destdir;
-} update_download_thread_t;
-
-void update_DownloadReal( update_download_thread_t *p_udt );
+static void* update_DownloadReal( vlc_object_t *p_this );
 
 /**
  * Download the file given in the update_t
@@ -1447,27 +1464,27 @@ void update_DownloadReal( update_download_thread_t *p_udt );
  * \param dir to store the download file
  * \return nothing
  */
-void update_Download( update_t *p_update, char *psz_destdir )
+void update_Download( update_t *p_update, const char *psz_destdir )
 {
     assert( p_update );
 
-    update_download_thread_t *p_udt = vlc_object_create( p_update->p_libvlc,
-                                        sizeof( update_download_thread_t ) );
+    update_download_thread_t *p_udt =
+        vlc_custom_create( p_update->p_libvlc, sizeof( *p_udt ),
+                           VLC_OBJECT_GENERIC, "update download" );
     if( !p_udt )
-    {
-        msg_Err( p_update->p_libvlc, "out of memory" );
         return;
-    }
 
     p_udt->p_update = p_update;
+    p_update->p_download = p_udt;
     p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
 
     vlc_thread_create( p_udt, "download update", update_DownloadReal,
                        VLC_THREAD_PRIORITY_LOW, false );
 }
 
-void update_DownloadReal( update_download_thread_t *p_udt )
+static void* update_DownloadReal( vlc_object_t *p_this )
 {
+    update_download_thread_t *p_udt = (update_download_thread_t *)p_this;
     int i_progress = 0;
     long int l_size;
     long int l_downloaded = 0;
@@ -1528,9 +1545,12 @@ void update_DownloadReal( update_download_thread_t *p_udt )
         free( psz_status );
     }
 
-    while( ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
-                                   !intf_ProgressIsCancelled( p_udt, i_progress ) )
+    vlc_object_lock( p_udt );
+    while( vlc_object_alive( p_udt ) &&
+           ( i_read = stream_Read( p_stream, p_buffer, 1 << 10 ) ) &&
+           !intf_ProgressIsCancelled( p_udt, i_progress ) )
     {
+        vlc_object_unlock( p_udt );
         if( fwrite( p_buffer, i_read, 1, p_file ) < 1 )
         {
             msg_Err( p_udt, "Failed to write into %s", psz_destfile );
@@ -1549,14 +1569,17 @@ void update_DownloadReal( update_download_thread_t *p_udt )
             free( psz_status );
         }
         free( psz_downloaded );
+        vlc_object_lock( p_udt );
     }
 
     /* Finish the progress bar or delete the file if the user had canceled */
     fclose( p_file );
     p_file = NULL;
 
-    if( !intf_ProgressIsCancelled( p_udt, i_progress ) )
+    if( vlc_object_alive( p_udt ) &&
+        !intf_ProgressIsCancelled( p_udt, i_progress ) )
     {
+        vlc_object_unlock( p_udt );
         if( asprintf( &psz_status, "%s\nDone %s (100.0%%)",
             p_update->release.psz_url, psz_size ) != -1 )
         {
@@ -1566,6 +1589,7 @@ void update_DownloadReal( update_download_thread_t *p_udt )
     }
     else
     {
+        vlc_object_unlock( p_udt );
         utf8_unlink( psz_destfile );
         goto end;
     }
@@ -1576,9 +1600,9 @@ void update_DownloadReal( update_download_thread_t *p_udt )
     {
         utf8_unlink( psz_destfile );
 
-        intf_UserFatal( p_udt, true, _("File can not be verified"),
+        intf_UserFatal( p_udt, true, _("File could not be verified"),
             _("It was not possible to download a cryptographic signature for "
-              "downloaded file \"%s\", and so VLC deleted it."),
+              "the downloaded file \"%s\". Thus, it was deleted."),
             psz_destfile );
         msg_Err( p_udt, "Couldn't download signature of downloaded file" );
         goto end;
@@ -1589,9 +1613,9 @@ void update_DownloadReal( update_download_thread_t *p_udt )
         utf8_unlink( psz_destfile );
         msg_Err( p_udt, "Invalid signature issuer" );
         intf_UserFatal( p_udt, true, _("Invalid signature"),
-            _("The cryptographic signature for downloaded file \"%s\" was "
-              "invalid and couldn't be used to securely verify it, and so "
-              "VLC deleted it."),
+            _("The cryptographic signature for the downloaded file \"%s\" was "
+              "invalid and could not be used to securely verify it. Thus, the "
+              "file was deleted."),
             psz_destfile );
         goto end;
     }
@@ -1601,9 +1625,9 @@ void update_DownloadReal( update_download_thread_t *p_udt )
         utf8_unlink( psz_destfile );
         msg_Err( p_udt, "Invalid signature type" );
         intf_UserFatal( p_udt, true, _("Invalid signature"),
-            _("The cryptographic signature for downloaded file \"%s\" was "
-              "invalid and couldn't be used to securely verify it, and so "
-              "VLC deleted it."),
+            _("The cryptographic signature for the downloaded file \"%s\" was "
+              "invalid and could not be used to securely verify it. Thus, the "
+              "file was deleted."),
             psz_destfile );
         goto end;
     }
@@ -1614,8 +1638,8 @@ void update_DownloadReal( update_download_thread_t *p_udt )
         msg_Err( p_udt, "Unable to hash %s", psz_destfile );
         utf8_unlink( psz_destfile );
         intf_UserFatal( p_udt, true, _("File not verifiable"),
-            _("It was not possible to securely verify downloaded file \"%s\", "
-              "and so VLC deleted it."),
+            _("It was not possible to securely verify the downloaded file"
+              " \"%s\". Thus, it was VLC deleted."),
             psz_destfile );
 
         goto end;
@@ -1626,7 +1650,7 @@ void update_DownloadReal( update_download_thread_t *p_udt )
     {
         utf8_unlink( psz_destfile );
         intf_UserFatal( p_udt, true, _("File corrupted"),
-            _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
+            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
              psz_destfile );
         msg_Err( p_udt, "Bad SHA1 hash for %s", psz_destfile );
         free( p_hash );
@@ -1638,7 +1662,7 @@ void update_DownloadReal( update_download_thread_t *p_udt )
     {
         utf8_unlink( psz_destfile );
         intf_UserFatal( p_udt, true, _("File corrupted"),
-            _("Downloaded file \"%s\" was corrupted, and so VLC deleted it."),
+            _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
              psz_destfile );
         msg_Err( p_udt, "BAD SIGNATURE for %s", psz_destfile );
         free( p_hash );
@@ -1658,7 +1682,10 @@ end:
     free( p_buffer );
     free( psz_size );
 
+    p_udt->p_update->p_download = NULL;
+
     vlc_object_release( p_udt );
+    return NULL;
 }
 
 update_release_t *update_GetRelease( update_t *p_update )
@@ -1690,7 +1717,7 @@ bool update_NeedUpgrade( update_t *p_update )
     return false;
 }
 
-void update_Download( update_t *p_update, char *psz_destdir )
+void update_Download( update_t *p_update, const char *psz_destdir )
 {
     (void)p_update; (void)psz_destdir;
 }