]> git.sesse.net Git - vlc/blobdiff - src/misc/update.c
Use var_Inherit* instead of var_CreateGet*.
[vlc] / src / misc / update.c
index ccb68eb2bfcf4ead9c960085cd0679439673d40c..b27d26017876d4f34e0b364248d56253d4b47a36 100644 (file)
@@ -35,9 +35,6 @@
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
-#ifdef HAVE_SYS_STAT_H
-#   include <sys/stat.h>
-#endif
 
 #include <vlc_common.h>
 #include <vlc_update.h>
 #include <vlc_pgpkey.h>
 #include <vlc_stream.h>
 #include <vlc_strings.h>
-#include <vlc_charset.h>
+#include <vlc_fs.h>
 #include <vlc_dialog.h>
+#include <vlc_interface.h>
 
 #include <gcrypt.h>
 #include <vlc_gcrypt.h>
-
+#ifdef WIN32
+#include <shellapi.h>
+#endif
 #include "update.h"
 #include "../libvlc.h"
 
 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-ce"
 #elif defined( WIN32 )
 #   define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-win-x86"
-#elif defined( __APPLE__ )
-#   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
-#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-ppc"
-#   else
-#       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-mac-x86"
-#   endif
 #elif defined( SYS_BEOS )
 #       define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status-beos-x86"
 #else
 #endif
 
 
-/*****************************************************************************
- * Local Prototypes
- *****************************************************************************/
-static void EmptyRelease( update_t *p_update );
-static bool GetUpdateFile( update_t *p_update );
-static char * size_str( long int l_size );
-
-
-
 /*****************************************************************************
  * Update_t functions
  *****************************************************************************/
 
+#undef update_New
 /**
  * Create a new update VLC struct
  *
  * \param p_this the calling vlc_object
  * \return pointer to new update_t or NULL
  */
-update_t *__update_New( vlc_object_t *p_this )
+update_t *update_New( vlc_object_t *p_this )
 {
     update_t *p_update;
     assert( p_this );
@@ -146,12 +132,12 @@ void update_Delete( update_t *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 );
         vlc_object_release( p_update->p_check );
     }
-    else if( p_update->p_download )
+
+    if( p_update->p_download )
     {
         vlc_object_kill( p_update->p_download );
         vlc_thread_join( p_update->p_download );
@@ -198,6 +184,7 @@ static bool GetUpdateFile( update_t *p_update )
     int i_revision = 0;
     unsigned char extra;
     char *psz_version_line = NULL;
+    char *psz_update_data = NULL;
 
     p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
     if( !p_stream )
@@ -207,15 +194,34 @@ static bool GetUpdateFile( update_t *p_update )
         goto error;
     }
 
-    /* Start reading the status file */
-    if( !( psz_version_line = stream_ReadLine( p_stream ) ) )
+    const int64_t i_read = stream_Size( p_stream );
+    psz_update_data = malloc( i_read + 1 ); /* terminating '\0' */
+    if( !psz_update_data )
+        goto error;
+
+    if( stream_Read( p_stream, psz_update_data, i_read ) != i_read )
     {
-        msg_Err( p_update->p_libvlc, "Update file %s is corrupted : missing version",
-                 UPDATE_VLC_STATUS_URL );
+        msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
+                UPDATE_VLC_STATUS_URL );
         goto error;
     }
+    psz_update_data[i_read] = '\0';
+
+    stream_Delete( p_stream );
+    p_stream = NULL;
 
     /* first line : version number */
+    char *psz_update_data_parser = psz_update_data;
+    size_t i_len = strcspn( psz_update_data, "\r\n" );
+    psz_update_data_parser += i_len;
+    while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
+        psz_update_data_parser++;
+
+    if( !(psz_version_line = malloc( i_len + 1)) )
+        goto error;
+    strncpy( psz_version_line, psz_update_data, i_len );
+    psz_version_line[i_len] = '\0';
+
     p_update->release.extra = 0;
     switch( sscanf( psz_version_line, "%i.%i.%i%c",
                     &i_major, &i_minor, &i_revision, &extra ) )
@@ -233,16 +239,27 @@ static bool GetUpdateFile( update_t *p_update )
     }
 
     /* second line : URL */
-    if( !( p_update->release.psz_url = stream_ReadLine( p_stream ) ) )
+    i_len = strcspn( psz_update_data_parser, "\r\n" );
+    if( i_len == 0 )
     {
-        msg_Err( p_update->p_libvlc, "Update file %s is corrupted : URL missing",
+        msg_Err( p_update->p_libvlc, "Update file %s is corrupted: URL missing",
                  UPDATE_VLC_STATUS_URL );
+
         goto error;
     }
 
+    if( !(p_update->release.psz_url = malloc( i_len + 1)) )
+        goto error;
+    strncpy( p_update->release.psz_url, psz_update_data_parser, i_len );
+    p_update->release.psz_url[i_len] = '\0';
+
+    psz_update_data_parser += i_len;
+    while( *psz_update_data_parser == '\r' || *psz_update_data_parser == '\n' )
+        psz_update_data_parser++;
+
     /* Remaining data : description */
-    int i_read = stream_Size( p_stream ) - stream_Tell( p_stream );
-    if( i_read <= 0 )
+    i_len = strlen( psz_update_data_parser );
+    if( i_len == 0 )
     {
         msg_Err( p_update->p_libvlc,
                 "Update file %s is corrupted: description missing",
@@ -250,20 +267,10 @@ static bool GetUpdateFile( update_t *p_update )
         goto error;
     }
 
-    p_update->release.psz_desc = (char*) malloc( i_read + 1 );
-    if( !p_update->release.psz_desc )
+    if( !(p_update->release.psz_desc = malloc( i_len + 1)) )
         goto error;
-
-    if( stream_Read( p_stream, p_update->release.psz_desc, i_read ) != i_read )
-    {
-        msg_Err( p_update->p_libvlc, "Couldn't download update file %s",
-                UPDATE_VLC_STATUS_URL );
-        goto error;
-    }
-    p_update->release.psz_desc[i_read] = '\0';
-
-    stream_Delete( p_stream );
-    p_stream = NULL;
+    strncpy( p_update->release.psz_desc, psz_update_data_parser, i_len );
+    p_update->release.psz_desc[i_len] = '\0';
 
     /* Now that we know the status is valid, we must download its signature
      * to authenticate it */
@@ -333,16 +340,7 @@ static bool GetUpdateFile( update_t *p_update )
         }
     }
 
-    /* FIXME : read the status file all at once instead of line per line */
-    char *psz_text;
-    if( asprintf( &psz_text, "%s\n%s\n%s", psz_version_line,
-                p_update->release.psz_url, p_update->release.psz_desc ) == -1 )
-    {
-        goto error;
-    }
-    FREENULL( psz_version_line );
-
-    uint8_t *p_hash = hash_sha1_from_text( psz_text, &sign );
+    uint8_t *p_hash = hash_sha1_from_text( psz_update_data, &sign );
     if( !p_hash )
     {
         msg_Warn( p_update->p_libvlc, "Can't compute SHA1 hash for status file" );
@@ -373,6 +371,7 @@ error:
     if( p_stream )
         stream_Delete( p_stream );
     free( psz_version_line );
+    free( psz_update_data );
     return false;
 }
 
@@ -454,7 +453,9 @@ bool update_NeedUpgrade( update_t *p_update )
         *PACKAGE_VERSION_MAJOR - '0',
         *PACKAGE_VERSION_MINOR - '0',
         *PACKAGE_VERSION_REVISION - '0',
-        *PACKAGE_VERSION_EXTRA
+        /* extra string of development versions is "-git", "-rc" ..
+         * so make sure version a.b.c is newer than a.b.c-XXX */
+        (*PACKAGE_VERSION_EXTRA == '-') ? -1 : *PACKAGE_VERSION_EXTRA
     };
     int latest_version[] = {
         p_update->release.i_major,
@@ -477,11 +478,11 @@ static char *size_str( long int l_size )
     char *psz_tmp = NULL;
     int i_retval = 0;
     if( l_size >> 30 )
-        i_retval = asprintf( &psz_tmp, _("%.1f GB"), (float)l_size/(1<<30) );
+        i_retval = asprintf( &psz_tmp, _("%.1f GiB"), (float)l_size/(1<<30) );
     else if( l_size >> 20 )
-        i_retval = asprintf( &psz_tmp, _("%.1f MB"), (float)l_size/(1<<20) );
+        i_retval = asprintf( &psz_tmp, _("%.1f MiB"), (float)l_size/(1<<20) );
     else if( l_size >> 10 )
-        i_retval = asprintf( &psz_tmp, _("%.1f kB"), (float)l_size/(1<<10) );
+        i_retval = asprintf( &psz_tmp, _("%.1f KiB"), (float)l_size/(1<<10) );
     else
         i_retval = asprintf( &psz_tmp, _("%ld B"), l_size );
 
@@ -494,12 +495,10 @@ static void* update_DownloadReal( vlc_object_t *p_this );
  * Download the file given in the update_t
  *
  * \param p_update structure
- * \param destination to store the download file
- *        This can be an existing dir, a (non)existing target fullpath filename or
- *        NULL for the current working dir.
+ * \param dir to store the download file
  * \return nothing
  */
-void update_Download( update_t *p_update, const char *destination )
+void update_Download( update_t *p_update, const char *psz_destdir )
 {
     assert( p_update );
 
@@ -519,7 +518,7 @@ void update_Download( update_t *p_update, const char *destination )
 
     p_udt->p_update = p_update;
     p_update->p_download = p_udt;
-    p_udt->psz_destination = destination ? strdup( destination ) : NULL;
+    p_udt->psz_destdir = psz_destdir ? strdup( psz_destdir ) : NULL;
 
     vlc_thread_create( p_udt, "download update", update_DownloadReal,
                        VLC_THREAD_PRIORITY_LOW );
@@ -532,21 +531,20 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     long int l_size;
     long int l_downloaded = 0;
     float f_progress;
-    char *psz_status = NULL;
+    char *psz_status;
     char *psz_downloaded = NULL;
     char *psz_size = NULL;
     char *psz_destfile = NULL;
     char *psz_tmpdestfile = NULL;
 
     FILE *p_file = NULL;
-    struct stat p_stat;
     stream_t *p_stream = NULL;
     void* p_buffer = NULL;
     int i_read;
     int canc;
 
     update_t *p_update = p_udt->p_update;
-    char *psz_destination = p_udt->psz_destination;
+    char *psz_destdir = p_udt->psz_destdir;
 
     msg_Dbg( p_udt, "Opening Stream '%s'", p_update->release.psz_url );
     canc = vlc_savecancel ();
@@ -571,18 +569,10 @@ static void* update_DownloadReal( vlc_object_t *p_this )
         goto end;
     }
     psz_tmpdestfile++;
+    if( asprintf( &psz_destfile, "%s%s", psz_destdir, psz_tmpdestfile ) == -1 )
+        goto end;
 
-    if( utf8_stat( psz_destination, &p_stat) == 0 && (p_stat.st_mode & S_IFDIR) )
-    {
-        if( asprintf( &psz_destfile, "%s%c%s", psz_destination, DIR_SEP_CHAR, psz_tmpdestfile ) == -1 )
-            goto end;
-    }
-    else if( psz_destination )
-        psz_destfile = strdup( psz_destination );
-    else
-        psz_destfile = strdup( psz_tmpdestfile );
-
-    p_file = utf8_fopen( psz_destfile, "w" );
+    p_file = vlc_fopen( psz_destfile, "w" );
     if( !p_file )
     {
         msg_Err( p_udt, "Failed to open %s for writing", psz_destfile );
@@ -625,9 +615,9 @@ static void* update_DownloadReal( vlc_object_t *p_this )
         psz_downloaded = size_str( l_downloaded );
         f_progress = (float)l_downloaded/(float)l_size;
 
-        if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s %.1f%% done" ),
+        if( asprintf( &psz_status, _( "%s\nDownloading... %s/%s %.1f%% done" ),
                       p_update->release.psz_url, psz_downloaded, psz_size,
-                      f_progress ) != -1 )
+                      f_progress*100 ) != -1 )
         {
             dialog_ProgressSet( p_progress, psz_status, f_progress );
             free( psz_status );
@@ -642,17 +632,12 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( vlc_object_alive( p_udt ) &&
         !dialog_ProgressCancelled( p_progress ) )
     {
-        if( asprintf( &psz_status, _("%s\nDone %s (100.0%%)"),
-            p_update->release.psz_url, psz_size ) != -1 )
-        {
-            dialog_ProgressDestroy( p_progress );
-            p_progress = NULL;
-            free( psz_status );
-        }
+        dialog_ProgressDestroy( p_progress );
+        p_progress = NULL;
     }
     else
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         goto end;
     }
 
@@ -660,7 +645,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( download_signature( VLC_OBJECT( p_udt ), &sign,
             p_update->release.psz_url ) != VLC_SUCCESS )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
 
         dialog_FatalWait( p_udt, _("File could not be verified"),
             _("It was not possible to download a cryptographic signature for "
@@ -672,7 +657,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
 
     if( memcmp( sign.issuer_longid, p_update->p_pkey->longid, 8 ) )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         msg_Err( p_udt, "Invalid signature issuer" );
         dialog_FatalWait( p_udt, _("Invalid signature"),
             _("The cryptographic signature for the downloaded file \"%s\" was "
@@ -684,7 +669,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
 
     if( sign.type != BINARY_SIGNATURE )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         msg_Err( p_udt, "Invalid signature type" );
         dialog_FatalWait( p_udt, _("Invalid signature"),
             _("The cryptographic signature for the downloaded file \"%s\" was "
@@ -698,7 +683,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( !p_hash )
     {
         msg_Err( p_udt, "Unable to hash %s", psz_destfile );
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         dialog_FatalWait( p_udt, _("File not verifiable"),
             _("It was not possible to securely verify the downloaded file"
               " \"%s\". Thus, it was deleted."),
@@ -710,7 +695,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( p_hash[0] != sign.hash_verification[0] ||
         p_hash[1] != sign.hash_verification[1] )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         dialog_FatalWait( p_udt, _("File corrupted"),
             _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
              psz_destfile );
@@ -722,7 +707,7 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     if( verify_signature( sign.r, sign.s, &p_update->p_pkey->key, p_hash )
             != VLC_SUCCESS )
     {
-        utf8_unlink( psz_destfile );
+        vlc_unlink( psz_destfile );
         dialog_FatalWait( p_udt, _("File corrupted"),
             _("Downloaded file \"%s\" was corrupted. Thus, it was deleted."),
              psz_destfile );
@@ -734,6 +719,20 @@ static void* update_DownloadReal( vlc_object_t *p_this )
     msg_Info( p_udt, "%s authenticated", psz_destfile );
     free( p_hash );
 
+#ifdef WIN32
+    int answer = dialog_Question( p_udt, _("Update VLC media player"),
+    _("The new version was successfully downloaded. Do you want to close VLC and install it now?"),
+    _("Install"), _("Cancel"), NULL);
+
+    if(answer == 1)
+    {
+        wchar_t psz_wdestfile[MAX_PATH];
+        MultiByteToWideChar( CP_UTF8, 0, psz_destfile, -1, psz_wdestfile, MAX_PATH );
+        answer = ShellExecuteW( NULL, L"open", psz_wdestfile, NULL, NULL, SW_SHOW);
+        if(answer > 32)
+            libvlc_Quit(p_this->p_libvlc);
+    }
+#endif
 end:
     if( p_progress )
         dialog_ProgressDestroy( p_progress );
@@ -741,13 +740,11 @@ end:
         stream_Delete( p_stream );
     if( p_file )
         fclose( p_file );
-    free( psz_destination );
+    free( psz_destdir );
     free( psz_destfile );
     free( p_buffer );
     free( psz_size );
 
-    p_udt->p_update->p_download = NULL;
-
     vlc_restorecancel( canc );
     return NULL;
 }
@@ -758,7 +755,8 @@ update_release_t *update_GetRelease( update_t *p_update )
 }
 
 #else
-update_t *__update_New( vlc_object_t *p_this )
+#undef update_New
+update_t *update_New( vlc_object_t *p_this )
 {
     (void)p_this;
     return NULL;