1 /*****************************************************************************
2 * update.c: VLC update and plugins download
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either release 2 of the License, or
12 * (at your option) any later release.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
25 * --> check release types.
26 * --> make sure that the version comparision method is ok.
31 * This file contains functions related to VLC and plugins update management
34 /*****************************************************************************
36 *****************************************************************************/
37 #include <stdlib.h> /* malloc(), free() */
38 #include <ctype.h> /* tolower() */
42 #include "vlc_update.h"
44 #include "vlc_block.h"
45 #include "vlc_stream.h"
47 #include "vlc_interaction.h"
49 /*****************************************************************************
51 *****************************************************************************/
53 /* All release notes and source packages should match on "*"
54 * Only binary installers are OS specific ( we only provide these
55 * for Win32, Mac OS X, WincCE, beos(?) ) */
56 #if defined( UNDER_CE )
57 # define UPDATE_VLC_OS "*"
58 # define UPDATE_VLC_ARCH "*"
59 #elif defined( WIN32 )
60 # define UPDATE_VLC_OS "windows"
61 # define UPDATE_VLC_ARCH "i386"
62 #elif defined( __APPLE__ )
63 # define UPDATE_VLC_OS "macosx"
64 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
65 # define UPDATE_VLC_ARCH "ppc"
67 # define UPDATE_VLC_ARCH "x86"
69 #elif defined( SYS_BEOS )
70 # define UPDATE_VLC_OS "beos"
71 # define UPDATE_VLC_ARCH "i386"
73 # define UPDATE_VLC_OS "*"
74 # define UPDATE_VLC_ARCH "*"
77 #define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status.xml"
78 #define UPDATE_VLC_MIRRORS_URL "http://update.videolan.org/mirrors.xml"
80 #define FREE( a ) free(a);a=NULL;
81 #define STRDUP( a ) ( a ? strdup( a ) : NULL )
83 /*****************************************************************************
85 *****************************************************************************/
87 void FreeMirrorsList( update_t * );
88 void FreeReleasesList( update_t * );
89 void GetMirrorsList( update_t *, vlc_bool_t );
90 void GetFilesList( update_t *, vlc_bool_t );
92 int CompareReleases( struct update_release_t *, struct update_release_t * );
93 int CompareReleaseToCurrent( struct update_release_t * );
95 unsigned int update_iterator_Reset( update_iterator_t * );
96 unsigned int update_iterator_NextFile( update_iterator_t * );
97 unsigned int update_iterator_PrevFile( update_iterator_t * );
98 unsigned int update_iterator_NextMirror( update_iterator_t * );
99 unsigned int update_iterator_PrevMirror( update_iterator_t * );
101 void update_iterator_GetData( update_iterator_t * );
102 void update_iterator_ClearData( update_iterator_t * );
104 /*****************************************************************************
106 *****************************************************************************/
109 * Create a new update VLC struct
111 * \param p_this the calling vlc_object
112 * \return pointer to new update_t or NULL
114 update_t *__update_New( vlc_object_t *p_this )
118 if( p_this == NULL ) return NULL;
120 p_update = (update_t *)malloc( sizeof( update_t ) );
122 vlc_mutex_init( p_this, &p_update->lock );
124 p_update->p_vlc = p_this->p_vlc;
126 p_update->p_releases = NULL;
127 p_update->i_releases = 0;
128 p_update->b_releases = VLC_FALSE;
130 p_update->p_mirrors = NULL;
131 p_update->i_mirrors = 0;
132 p_update->b_mirrors = VLC_FALSE;
138 * Delete an update_t struct
140 * \param p_update update_t* pointer
143 void update_Delete( update_t *p_update )
145 vlc_mutex_destroy( &p_update->lock );
146 FreeMirrorsList( p_update );
147 FreeReleasesList( p_update );
152 * Empty the mirrors list
153 * *p_update should be locked before using this function
155 * \param p_update pointer to the update struct
158 void FreeMirrorsList( update_t *p_update )
162 for( i = 0; i < p_update->i_mirrors; i++ )
164 free( p_update->p_mirrors[i].psz_name );
165 free( p_update->p_mirrors[i].psz_location );
166 free( p_update->p_mirrors[i].psz_type );
167 free( p_update->p_mirrors[i].psz_base_url );
169 FREE( p_update->p_mirrors );
170 p_update->i_mirrors = 0;
171 p_update->b_mirrors = VLC_FALSE;
175 * Empty the releases list
176 * *p_update should be locked before calling this function
178 * \param p_update pointer to the update struct
181 void FreeReleasesList( update_t *p_update )
185 for( i = 0; i < p_update->i_releases; i++ )
188 struct update_release_t *p_release = (p_update->p_releases + i);
189 for( j = 0; j < p_release->i_files; j++ )
191 free( p_release->p_files[j].psz_md5 );
192 free( p_release->p_files[j].psz_url );
193 free( p_release->p_files[j].psz_description );
195 free( p_release->psz_major );
196 free( p_release->psz_minor );
197 free( p_release->psz_revision );
198 free( p_release->psz_extra );
199 free( p_release->psz_svn_revision );
200 free( p_release->p_files );
202 FREE( p_update->p_releases );
203 p_update->i_releases = 0;
204 p_update->b_releases = VLC_FALSE;
208 * Get the mirrors list XML file and parse it
209 * *p_update has to be unlocked when calling this function
211 * \param p_update pointer to the update struct
212 * \param b_force set to VLC_TRUE if you want to force the mirrors list update
215 void GetMirrorsList( update_t *p_update, vlc_bool_t b_force )
217 stream_t *p_stream = NULL;
220 xml_reader_t *p_xml_reader = NULL;
222 char *psz_eltname = NULL;
223 //char *psz_eltvalue = NULL;
224 char *psz_name = NULL;
225 char *psz_value = NULL;
227 struct update_mirror_t tmp_mirror = {0};
229 vlc_mutex_lock( &p_update->lock );
231 if( p_update->b_mirrors && b_force == VLC_FALSE )
233 vlc_mutex_unlock( &p_update->lock );
237 p_xml = xml_Create( p_update->p_vlc );
240 msg_Err( p_update->p_vlc, "Failed to open XML parser" );
244 p_stream = stream_UrlNew( p_update->p_vlc, UPDATE_VLC_MIRRORS_URL );
247 msg_Err( p_update->p_vlc, "Failed to open %s for reading",
248 UPDATE_VLC_MIRRORS_URL );
252 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
256 msg_Err( p_update->p_vlc, "Failed to open %s for parsing",
257 UPDATE_VLC_MIRRORS_URL );
261 if( p_update->p_mirrors )
263 FreeMirrorsList( p_update );
266 while( xml_ReaderRead( p_xml_reader ) == 1 )
268 switch( xml_ReaderNodeType( p_xml_reader ) )
271 msg_Err( p_update->p_vlc, "Error while parsing %s",
272 UPDATE_VLC_MIRRORS_URL );
275 case XML_READER_STARTELEM:
276 psz_eltname = xml_ReaderName( p_xml_reader );
279 msg_Err( p_update->p_vlc, "Error while parsing %s",
280 UPDATE_VLC_MIRRORS_URL );
284 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
286 psz_name = xml_ReaderName( p_xml_reader );
287 psz_value = xml_ReaderValue( p_xml_reader );
289 if( !psz_name || !psz_value )
291 msg_Err( p_update->p_vlc, "Error while parsing %s",
292 UPDATE_VLC_MIRRORS_URL );
296 if( !strcmp( psz_eltname, "mirror" ) )
298 if( !strcmp( psz_name, "name" ) )
299 tmp_mirror.psz_name = STRDUP( psz_value );
300 else if( !strcmp( psz_name, "location" ) )
301 tmp_mirror.psz_location = STRDUP( psz_value );
303 else if( !strcmp( psz_eltname, "url" ) )
305 if( !strcmp( psz_name, "type" ) )
306 tmp_mirror.psz_type = STRDUP( psz_value );
307 else if( !strcmp( psz_name, "base" ) )
308 tmp_mirror.psz_base_url = STRDUP( psz_value );
313 if( !strcmp( psz_eltname, "url" ) )
315 /* append to mirrors list */
316 p_update->p_mirrors =
317 (struct update_mirror_t *)realloc( p_update->p_mirrors,
318 (++(p_update->i_mirrors))
319 *sizeof( struct update_mirror_t ) );
320 p_update->p_mirrors[ p_update->i_mirrors - 1 ] =
322 tmp_mirror.psz_name = STRDUP( tmp_mirror.psz_name );
323 tmp_mirror.psz_location = STRDUP( tmp_mirror.psz_location );
324 tmp_mirror.psz_type = NULL;
325 tmp_mirror.psz_base_url = NULL;
330 case XML_READER_ENDELEM:
331 psz_eltname = xml_ReaderName( p_xml_reader );
334 msg_Err( p_update->p_vlc, "Error while parsing %s",
335 UPDATE_VLC_MIRRORS_URL );
339 if( !strcmp( psz_eltname, "mirror" ) )
341 FREE( tmp_mirror.psz_name );
342 FREE( tmp_mirror.psz_location );
348 /*case XML_READER_TEXT:
349 psz_eltvalue = xml_ReaderValue( p_xml_reader );
350 FREE( psz_eltvalue );
355 p_update->b_mirrors = VLC_TRUE;
358 vlc_mutex_unlock( &p_update->lock );
361 //free( psz_eltvalue );
365 free( tmp_mirror.psz_name );
366 free( tmp_mirror.psz_location );
367 free( tmp_mirror.psz_type );
368 free( tmp_mirror.psz_base_url );
370 if( p_xml_reader && p_xml )
371 xml_ReaderDelete( p_xml, p_xml_reader );
373 stream_Delete( p_stream );
379 * Get the files list XML file and parse it
380 * *p_update has to be unlocked when calling this function
382 * \param p_update pointer to update struct
383 * \param b_force set to VLC_TRUE if you want to force the files list update
386 void GetFilesList( update_t *p_update, vlc_bool_t b_force )
388 stream_t *p_stream = NULL;
391 xml_reader_t *p_xml_reader = NULL;
393 char *psz_eltname = NULL;
394 char *psz_eltvalue = NULL;
395 char *psz_name = NULL;
396 char *psz_value = NULL;
398 struct update_release_t *p_release = NULL;
399 struct update_release_t tmp_release = {0};
400 struct update_file_t tmp_file = {0};
401 tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
403 vlc_bool_t b_os = VLC_FALSE, b_arch = VLC_FALSE;
405 vlc_mutex_lock( &p_update->lock );
407 if( p_update->b_releases && b_force == VLC_FALSE )
409 vlc_mutex_unlock( &p_update->lock );
413 p_xml = xml_Create( p_update->p_vlc );
416 msg_Err( p_update->p_vlc, "Failed to open XML parser" );
420 p_stream = stream_UrlNew( p_update->p_vlc, UPDATE_VLC_STATUS_URL );
423 msg_Err( p_update->p_vlc, "Failed to open %s for reading",
424 UPDATE_VLC_STATUS_URL );
428 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
432 msg_Err( p_update->p_vlc, "Failed to open %s for parsing",
433 UPDATE_VLC_STATUS_URL );
437 if( p_update->p_releases )
439 FreeReleasesList( p_update );
442 while( xml_ReaderRead( p_xml_reader ) == 1 )
444 switch( xml_ReaderNodeType( p_xml_reader ) )
447 msg_Err( p_update->p_vlc, "Error while parsing %s",
448 UPDATE_VLC_STATUS_URL );
451 case XML_READER_STARTELEM:
452 psz_eltname = xml_ReaderName( p_xml_reader );
455 msg_Err( p_update->p_vlc, "Error while parsing %s",
456 UPDATE_VLC_STATUS_URL );
460 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
462 psz_name = xml_ReaderName( p_xml_reader );
463 psz_value = xml_ReaderValue( p_xml_reader );
465 if( !psz_name || !psz_value )
467 msg_Err( p_update->p_vlc, "Error while parsing %s",
468 UPDATE_VLC_STATUS_URL );
474 if( strcmp( psz_eltname, "version" ) == 0 )
476 if( !strcmp( psz_name, "major" ) )
477 tmp_release.psz_major = STRDUP( psz_value );
478 else if( !strcmp( psz_name, "minor" ) )
479 tmp_release.psz_minor = STRDUP( psz_value );
480 else if( !strcmp( psz_name, "revision" ) )
481 tmp_release.psz_revision = STRDUP( psz_value );
482 else if( !strcmp( psz_name, "extra" ) )
483 tmp_release.psz_extra = STRDUP( psz_value );
484 else if( !strcmp( psz_name, "svn" ) )
485 tmp_release.psz_svn_revision =
487 else if( !strcmp( psz_name, "version" ) )
489 if( !strcmp( psz_value, "unstable" ) )
491 UPDATE_RELEASE_TYPE_UNSTABLE;
492 else if( !strcmp( psz_value, "testing" ) )
494 UPDATE_RELEASE_TYPE_TESTING;
497 UPDATE_RELEASE_TYPE_STABLE;
500 else if( !strcmp( psz_eltname, "file" ) )
502 if( !strcmp( psz_name, "type" ) )
504 if( !strcmp( psz_value, "info" ) )
505 tmp_file.i_type = UPDATE_FILE_TYPE_INFO;
506 else if( !strcmp( psz_value, "source" ) )
507 tmp_file.i_type = UPDATE_FILE_TYPE_SOURCE;
508 else if( !strcmp( psz_value, "binary" ) )
509 tmp_file.i_type = UPDATE_FILE_TYPE_BINARY;
510 else if( !strcmp( psz_value, "plugin" ) )
511 tmp_file.i_type = UPDATE_FILE_TYPE_PLUGIN;
513 tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
515 else if( !strcmp( psz_name, "md5" ) )
516 tmp_file.psz_md5 = STRDUP( psz_value );
517 else if( !strcmp( psz_name, "size" ) )
518 tmp_file.l_size = atol( psz_value );
519 else if( !strcmp( psz_name, "url" ) )
520 tmp_file.psz_url = STRDUP( psz_value );
523 if( !strcmp( psz_name, "name" )
524 && ( !strcmp( psz_value, UPDATE_VLC_OS )
525 || !strcmp( psz_value, "*" ) )
526 && !strcmp( psz_eltname, "os" ) )
530 if( b_os && !strcmp( psz_name, "name" )
531 && ( !strcmp( psz_value, UPDATE_VLC_ARCH )
532 || !strcmp( psz_value, "*" ) )
533 && !strcmp( psz_eltname, "arch" ) )
540 if( ( b_os && b_arch && strcmp( psz_eltname, "arch" ) ) )
542 if( !strcmp( psz_eltname, "version" ) )
545 /* look for a previous occurence of this release */
546 for( i = 0; i < p_update->i_releases; i++ )
548 p_release = p_update->p_releases + i;
549 if( CompareReleases( p_release, &tmp_release )
550 == UPDATE_RELEASE_STATUS_EQUAL )
555 /* if this is the first time that we see this release,
556 * append it to the list of releases */
557 if( i == p_update->i_releases )
559 tmp_release.i_status =
560 CompareReleaseToCurrent( &tmp_release );
561 p_update->p_releases =
562 (struct update_release_t *)realloc( p_update->p_releases,
563 (++(p_update->i_releases))*sizeof( struct update_release_t ) );
564 p_update->p_releases[ p_update->i_releases - 1 ] =
567 p_update->p_releases + p_update->i_releases - 1;
568 tmp_release.psz_major = NULL;
569 tmp_release.psz_minor = NULL;
570 tmp_release.psz_revision = NULL;
571 tmp_release.psz_extra = NULL;
572 tmp_release.psz_svn_revision = NULL;
573 tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
574 tmp_release.i_status = 0;
575 tmp_release.p_files = NULL;
576 tmp_release.i_files = 0;
580 FREE( tmp_release.psz_major );
581 FREE( tmp_release.psz_minor );
582 FREE( tmp_release.psz_revision );
583 FREE( tmp_release.psz_extra );
584 FREE( tmp_release.psz_svn_revision );
585 tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
586 FREE( tmp_release.p_files );
587 tmp_release.i_files = 0;
590 else if( !strcmp( psz_eltname, "file" ) )
592 /* append file to p_release's file list */
593 if( p_release == NULL )
598 (struct update_file_t *)realloc( p_release->p_files,
599 (++(p_release->i_files))*sizeof( struct update_file_t ) );
600 p_release->p_files[ p_release->i_files - 1 ] = tmp_file;
601 tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
602 tmp_file.psz_md5 = NULL;
604 tmp_file.psz_url = NULL;
605 tmp_file.psz_description = NULL;
611 case XML_READER_ENDELEM:
612 psz_eltname = xml_ReaderName( p_xml_reader );
615 msg_Err( p_update->p_vlc, "Error while parsing %s",
616 UPDATE_VLC_STATUS_URL );
620 if( !strcmp( psz_eltname, "os" ) )
622 else if( !strcmp( psz_eltname, "arch" ) )
627 case XML_READER_TEXT:
628 psz_eltvalue = xml_ReaderValue( p_xml_reader );
629 if( p_release && p_release->i_files )
630 p_release->p_files[ p_release->i_files - 1 ]
631 .psz_description = STRDUP( psz_eltvalue );
632 FREE( psz_eltvalue );
637 p_update->b_releases = VLC_TRUE;
640 vlc_mutex_unlock( &p_update->lock );
643 free( psz_eltvalue );
647 free( tmp_release.psz_major );
648 free( tmp_release.psz_minor );
649 free( tmp_release.psz_revision );
650 free( tmp_release.psz_extra );
651 free( tmp_release.psz_svn_revision );
653 free( tmp_file.psz_md5 );
654 free( tmp_file.psz_url );
655 free( tmp_file.psz_description );
657 if( p_xml_reader && p_xml )
658 xml_ReaderDelete( p_xml, p_xml_reader );
660 stream_Delete( p_stream );
668 * \param p_update pointer to update struct
669 * \param b_force set to VLC_TRUE if you want to force the update
672 void update_Check( update_t *p_update, vlc_bool_t b_force )
674 if( p_update == NULL ) return;
675 GetMirrorsList( p_update, b_force );
676 GetFilesList( p_update, b_force );
680 * Compare two release numbers
681 * The comparision algorith basically performs an alphabetical order (strcmp)
682 * comparision of each of the version number elements until it finds two
683 * different ones. This is the tricky function.
685 * \param p1 first release
686 * \param p2 second release
687 * \return like strcmp
689 int CompareReleases( struct update_release_t *p1, struct update_release_t *p2 )
692 if( ( d = strcmp( p1->psz_major, p2->psz_major ) ) ) ;
693 else if( ( d = strcmp( p1->psz_minor, p2->psz_minor ) ) ) ;
694 else if( ( d = strcmp( p1->psz_revision, p2->psz_revision ) ) ) ;
697 d = strcmp( p1->psz_extra, p2->psz_extra );
701 * not num < NULL < num
702 * -test and -svn releases are thus always considered older than
703 * -'' or -0 releases, which is the best i could come up with */
706 strtol( p1->psz_extra, &psz_end1, 10 );
707 strtol( p2->psz_extra, &psz_end2, 10 );
708 if( psz_end2 == p2->psz_extra
709 && ( psz_end1 != p1->psz_extra || *p1->psz_extra == '\0' ) )
714 return UPDATE_RELEASE_STATUS_OLDER;
716 return UPDATE_RELEASE_STATUS_EQUAL;
718 return UPDATE_RELEASE_STATUS_NEWER;
722 * Compare a given release's version number to the current VLC's one
725 * \return >0 if newer, 0 if equal and <0 if older
727 int CompareReleaseToCurrent( struct update_release_t *p )
729 struct update_release_t c = {0};
731 c.psz_major = STRDUP( PACKAGE_VERSION_MAJOR );
732 c.psz_minor = STRDUP( PACKAGE_VERSION_MINOR );
733 c.psz_revision = STRDUP( PACKAGE_VERSION_REVISION );
734 c.psz_extra = STRDUP( PACKAGE_VERSION_EXTRA );
735 r = CompareReleases( p, &c );
738 free( c.psz_revision );
743 /*****************************************************************************
744 * Updatei_iterator_t functions
745 *****************************************************************************/
748 * Create a new update iterator structure. This structure can then be used to
749 * describe a position and move through the update and mirror trees/lists.
750 * This will use an existing update struct or create a new one if none is
753 * \param p_u the calling update_t
754 * \return a pointer to an update iterator
756 update_iterator_t *update_iterator_New( update_t *p_u )
758 update_iterator_t *p_uit = NULL;
763 p_uit = (update_iterator_t *)malloc( sizeof( update_iterator_t ) );
764 if( p_uit == NULL ) return NULL;
772 p_uit->i_t = UPDATE_FILE_TYPE_ALL;
773 p_uit->i_rs = UPDATE_RELEASE_STATUS_ALL;
774 p_uit->i_rt = UPDATE_RELEASE_TYPE_STABLE;
776 p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
777 p_uit->file.psz_md5 = NULL;
778 p_uit->file.psz_url = NULL;
779 p_uit->file.l_size = 0;
780 p_uit->file.psz_description = NULL;
782 p_uit->release.psz_version = NULL;
783 p_uit->release.psz_svn_revision = NULL;
784 p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
785 p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;
787 p_uit->mirror.psz_name = NULL;
788 p_uit->mirror.psz_location = NULL;
789 p_uit->mirror.psz_type = NULL;
795 * Delete an update iterator structure (duh!)
797 * \param p_uit pointer to an update iterator
800 void update_iterator_Delete( update_iterator_t *p_uit )
803 update_iterator_ClearData( p_uit );
808 * Reset an update_iterator_t structure
810 * \param p_uit pointer to an update iterator
811 * \return UPDATE_FAIL upon error, UPDATE_SUCCESS otherwise
813 unsigned int update_iterator_Reset( update_iterator_t *p_uit )
815 if( !p_uit ) return UPDATE_FAIL;
821 update_iterator_ClearData( p_uit );
822 return UPDATE_SUCCESS;
826 * Finds the next file in the update tree that matches status and type
827 * requirements set in the update_iterator
829 * \param p_uit update iterator
830 * \return UPDATE_FAIL if we can't find the next file, UPDATE_SUCCESS|UPDATE_FILE if we stay in the same release, UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE if we change the release index
832 unsigned int update_iterator_NextFile( update_iterator_t *p_uit )
836 if( !p_uit ) return UPDATE_FAIL;
840 /* if the update iterator was already in a "no match" state, start over */
841 if( p_uit->i_r == -1 ) p_uit->i_r = 0;
842 //if( p_uit->i_f == -1 ) p_uit->i_f = 0;
844 vlc_mutex_lock( &p_uit->p_u->lock );
846 for( r = p_uit->i_r; r < p_uit->p_u->i_releases; r++ )
848 if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
849 for( f = ( r == p_uit->i_r ? p_uit->i_f + 1 : 0 );
850 f < p_uit->p_u->p_releases[r].i_files; f++ )
852 if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
854 goto done;/* "double break" */
862 r = p_uit->p_u->i_releases;
864 if( old_r == p_uit->i_r )
866 update_iterator_GetData( p_uit );
867 vlc_mutex_unlock( &p_uit->p_u->lock );
868 return UPDATE_SUCCESS|UPDATE_FILE;
870 else if( p_uit->i_r == r )
874 update_iterator_GetData( p_uit );
875 vlc_mutex_unlock( &p_uit->p_u->lock );
880 update_iterator_GetData( p_uit );
881 vlc_mutex_unlock( &p_uit->p_u->lock );
882 return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
887 * Finds the previous file in the update tree that matches status and type
888 * requirements set in the update_iterator
890 * \param p_uit update iterator
891 * \return UPDATE_FAIL if we can't find the previous file, UPDATE_SUCCESS|UPDATE_FILE if we stay in the same release, UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE if we change the release index
894 unsigned int update_iterator_PrevFile( update_iterator_t *p_uit )
898 if( !p_uit ) return UPDATE_FAIL;
902 /* if the update iterator was already in a "no match" state, start over
903 * (begin at the end of the list) */
904 if( p_uit->i_r == -1 ) p_uit->i_r = p_uit->p_u->i_releases - 1;
905 p_uit->i_f = p_uit->p_u->p_releases[p_uit->i_r].i_files + 1;
907 vlc_mutex_lock( &p_uit->p_u->lock );
909 for( r = p_uit->i_r; r >= 0; r-- )
911 if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
912 for( f =( r==p_uit->i_r ? p_uit->i_f - 1 : p_uit->p_u->p_releases[r].i_files );
915 if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
917 goto done;/* "double break" */
925 r = p_uit->p_u->i_releases;
927 if( old_r == p_uit->i_r )
929 update_iterator_GetData( p_uit );
930 vlc_mutex_unlock( &p_uit->p_u->lock );
931 return UPDATE_SUCCESS|UPDATE_FILE;
933 else if( p_uit->i_r == -1 )
937 update_iterator_GetData( p_uit );
938 vlc_mutex_unlock( &p_uit->p_u->lock );
943 update_iterator_GetData( p_uit );
944 vlc_mutex_unlock( &p_uit->p_u->lock );
945 return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
950 * Finds the next mirror in the update tree
952 * \param update iterator
953 * \return UPDATE_FAIL if we can't find the next mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
955 unsigned int update_iterator_NextMirror( update_iterator_t *p_uit )
957 if( !p_uit ) return UPDATE_FAIL;
958 vlc_mutex_lock( &p_uit->p_u->lock );
960 if( p_uit->i_m >= p_uit->p_u->i_mirrors ) p_uit->i_m = -1;
961 update_iterator_GetData( p_uit );
962 vlc_mutex_unlock( &p_uit->p_u->lock );
963 return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
967 * Finds the previous mirror in the update tree
969 * \param update iterator
970 * \return UPDATE_FAIL if we can't find a previous mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
972 unsigned int update_iterator_PrevMirror( update_iterator_t *p_uit )
974 if( !p_uit ) return UPDATE_FAIL;
975 vlc_mutex_lock( &p_uit->p_u->lock );
977 update_iterator_GetData( p_uit );
978 vlc_mutex_unlock( &p_uit->p_u->lock );
979 return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
983 * Change the update iterator's position in the file and mirrors tree
984 * If position is negative, don't change it
986 * \param i_m position in mirrors list
987 * \param i_r position in releases list
988 * \param i_f position in release's files list
989 * \return UPDATE_FAIL when changing position fails or position wasn't changed, a combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE otherwise
991 unsigned int update_iterator_ChooseMirrorAndFile( update_iterator_t *p_uit,
992 int i_m, int i_r, int i_f )
994 unsigned int i_val = 0;
996 if( !p_uit ) return 0;
997 vlc_mutex_lock( &p_uit->p_u->lock );
1001 if( i_m < p_uit->p_u->i_mirrors )
1003 if( i_m != p_uit->i_m )
1004 i_val |= UPDATE_MIRROR;
1012 if( i_r < p_uit->p_u->i_releases )
1014 if( i_r != p_uit->i_r )
1015 i_val |= UPDATE_FILE;
1023 if( i_r >= 0 && i_r < p_uit->p_u->i_releases
1024 && i_f < p_uit->p_u->p_releases[p_uit->i_r].i_files )
1026 if( i_f != p_uit->i_f )
1027 i_val |= UPDATE_FILE;
1033 update_iterator_GetData( p_uit );
1034 vlc_mutex_unlock( &p_uit->p_u->lock );
1036 if( ( i_m < 0 || p_uit->i_m >= 0 )
1037 && ( i_r < 0 || p_uit->i_r >= 0 )
1038 && ( i_f < 0 || p_uit->i_f >= 0 ) )
1040 /* Everything worked */
1041 return UPDATE_SUCCESS|i_val;
1045 /* Something failed */
1051 * Fills the iterator data (file, release and mirror structs)
1052 * The update struct should be locked before calling this function.
1054 * \param p_uit update iterator
1057 void update_iterator_GetData( update_iterator_t *p_uit )
1059 struct update_release_t *p_r = NULL;
1060 struct update_file_t *p_f = NULL;
1061 struct update_mirror_t *p_m = NULL;
1063 update_iterator_ClearData( p_uit );
1065 if( p_uit->i_m >= 0 )
1067 p_m = p_uit->p_u->p_mirrors + p_uit->i_m;
1068 p_uit->mirror.psz_name = STRDUP( p_m->psz_name );
1069 p_uit->mirror.psz_location = STRDUP( p_m->psz_location );
1070 p_uit->mirror.psz_type = STRDUP( p_m->psz_type );
1073 if( p_uit->i_r >= 0 )
1075 p_r = p_uit->p_u->p_releases + p_uit->i_r;
1076 asprintf( &p_uit->release.psz_version, "%s.%s.%s-%s",
1081 p_uit->release.psz_svn_revision = STRDUP( p_r->psz_svn_revision );
1082 p_uit->release.i_type = p_r->i_type;
1083 p_uit->release.i_status = p_r->i_status;
1084 if( p_uit->i_f >= 0 )
1086 p_f = p_r->p_files + p_uit->i_f;
1087 p_uit->file.i_type = p_f->i_type;
1088 p_uit->file.psz_md5 = STRDUP( p_f->psz_md5 );
1089 p_uit->file.l_size = p_f->l_size;
1090 p_uit->file.psz_description = STRDUP( p_f->psz_description);
1091 if( p_f->psz_url[0] == '/' )
1095 asprintf( &p_uit->file.psz_url, "%s%s",
1096 p_m->psz_base_url, p_f->psz_url );
1101 p_uit->file.psz_url = STRDUP( p_f->psz_url );
1108 * Clears the iterator data (file, release and mirror structs)
1110 * \param p_uit update iterator
1113 void update_iterator_ClearData( update_iterator_t *p_uit )
1115 p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
1116 FREE( p_uit->file.psz_md5 );
1117 p_uit->file.l_size = 0;
1118 FREE( p_uit->file.psz_description );
1119 FREE( p_uit->file.psz_url );
1120 FREE( p_uit->release.psz_version );
1121 FREE( p_uit->release.psz_svn_revision );
1122 p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
1123 p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;
1124 FREE( p_uit->mirror.psz_name );
1125 FREE( p_uit->mirror.psz_location );
1126 FREE( p_uit->mirror.psz_type );
1130 * Perform an action on the update iterator
1131 * Only the first matching action is performed.
1133 * \param p_uit update iterator
1134 * \param i_action update action bitmask. can be a combination of UPDATE_NEXT, UPDATE_PREV, UPDATE_MIRROR, UPDATE_RELEASE, UPDATE_FILE, UPDATE_RESET
1135 * \return UPDATE_FAIL if action fails, UPDATE_SUCCESS|(combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE if these changed) otherwise
1137 unsigned int update_iterator_Action( update_iterator_t *p_uit, int i_action )
1139 if( i_action & UPDATE_RESET )
1141 return update_iterator_Reset( p_uit );
1144 if( i_action & UPDATE_MIRROR )
1146 if( i_action & UPDATE_PREV )
1148 return update_iterator_PrevMirror( p_uit );
1152 return update_iterator_NextMirror( p_uit );
1155 /*else if( i_action & UPDATE_RELEASE )
1157 if( i_action & UPDATE_PREV )
1159 return update_iterator_PrevRelease( p_uit );
1163 return update_iterator_NextRelease( p_uit );
1166 else if( i_action & UPDATE_FILE )
1168 if( i_action & UPDATE_PREV )
1170 return update_iterator_PrevFile( p_uit );
1174 return update_iterator_NextFile( p_uit );
1179 return UPDATE_SUCCESS;
1184 * Object to launch download thread in a different object
1188 char *psz_dest; //< Download destination
1189 char *psz_src; //< Download source
1190 char *psz_status; //< Download status displayed in progress dialog
1191 } download_thread_t;
1193 void update_download_for_real( download_thread_t *p_this );
1196 * Download the file selected by the update iterator. This function will
1197 * launch the download in a new thread (downloads can be long)
1199 * \param p_uit update iterator
1200 * \param psz_dest destination file path
1203 void update_download( update_iterator_t *p_uit, char *psz_dest )
1205 download_thread_t *p_dt =
1206 vlc_object_create( p_uit->p_u->p_vlc, sizeof( download_thread_t ) );
1208 p_dt->psz_dest = strdup( psz_dest );
1209 p_dt->psz_src = strdup( p_uit->file.psz_url );
1210 asprintf( &p_dt->psz_status, "%s - %s (%s)\nSource: %s\nDestination: %s",
1211 p_uit->file.psz_description, p_uit->release.psz_version,
1212 p_uit->release.psz_svn_revision, p_uit->file.psz_url,
1215 vlc_thread_create( p_dt, "download thread", update_download_for_real,
1216 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1220 * The true download function.
1222 * \param p_this the download_thread_t object
1225 void update_download_for_real( download_thread_t *p_this )
1227 char *psz_dest = p_this->psz_dest;
1228 char *psz_src = p_this->psz_src;
1230 vlc_t *p_vlc = p_this->p_vlc;
1232 FILE *p_file = NULL;
1241 vlc_thread_ready( p_this );
1243 asprintf( &psz_status, "%s\nDownloading... %.1f%% done",
1244 p_this->psz_status, 0.0 );
1245 i_progress = intf_UserProgress( p_vlc, "Downloading...",
1248 p_stream = stream_UrlNew( p_vlc, psz_src );
1251 msg_Err( p_vlc, "Failed to open %s for reading", psz_src );
1256 p_file = fopen( psz_dest, "w" );
1259 msg_Err( p_vlc, "Failed to open %s for writing", psz_dest );
1265 i_size = (int)(stream_Size(p_stream)/(1<<10));
1266 p_buffer = (void *)malloc( 1<<10 );
1268 while( ( i_read = stream_Read( p_stream, p_buffer, 1<<10 ) ) )
1272 fwrite( p_buffer, i_read, 1, p_file );
1276 f_progress = 100.0*(float)i_done/(float)i_size;
1277 asprintf( &psz_status, "%s\nDownloading... %.1f%% done",
1278 p_this->psz_status, f_progress );
1279 intf_UserProgressUpdate( p_vlc, i_progress,
1280 psz_status, f_progress );
1285 stream_Delete( p_stream );
1288 asprintf( &psz_status, "%s\nDone (100.00%%)",
1289 p_this->psz_status );
1290 intf_UserProgressUpdate( p_vlc, i_progress, psz_status, 100.0 );
1295 free( p_this->psz_dest );
1296 free( p_this->psz_src );
1297 free( p_this->psz_status );
1300 CloseHandle( p_this->thread_id );
1303 vlc_object_destroy( p_this );