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 *****************************************************************************/
40 #include <ctype.h> /* tolower() */
43 #include <vlc_update.h>
45 #include <vlc_block.h>
46 #include <vlc_stream.h>
48 #include <vlc_interface.h>
49 #include <vlc_charset.h>
51 /*****************************************************************************
53 *****************************************************************************/
55 /* All release notes and source packages should match on "*"
56 * Only binary installers are OS specific ( we only provide these
57 * for Win32, Mac OS X, WincCE, beos(?) ) */
58 #if defined( UNDER_CE )
59 # define UPDATE_VLC_OS "*"
60 # define UPDATE_VLC_ARCH "*"
61 #elif defined( WIN32 )
62 # define UPDATE_VLC_OS "windows"
63 # define UPDATE_VLC_ARCH "i386"
64 #elif defined( __APPLE__ )
65 # define UPDATE_VLC_OS "macosx"
66 # if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
67 # define UPDATE_VLC_ARCH "ppc"
69 # define UPDATE_VLC_ARCH "x86"
71 #elif defined( SYS_BEOS )
72 # define UPDATE_VLC_OS "beos"
73 # define UPDATE_VLC_ARCH "i386"
75 # define UPDATE_VLC_OS "*"
76 # define UPDATE_VLC_ARCH "*"
79 #define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status.xml"
80 #define UPDATE_VLC_MIRRORS_URL "http://update.videolan.org/mirrors.xml"
82 #define STRDUP( a ) ( a ? strdup( a ) : NULL )
84 /*****************************************************************************
86 *****************************************************************************/
88 void FreeMirrorsList( update_t * );
89 void FreeReleasesList( update_t * );
90 void GetMirrorsList( update_t *, vlc_bool_t );
91 void GetFilesList( update_t *, vlc_bool_t );
93 int CompareReleases( struct update_release_t *, struct update_release_t * );
94 int CompareReleaseToCurrent( struct update_release_t * );
96 unsigned int update_iterator_Reset( update_iterator_t * );
97 unsigned int update_iterator_NextFile( update_iterator_t * );
98 unsigned int update_iterator_PrevFile( update_iterator_t * );
99 unsigned int update_iterator_NextMirror( update_iterator_t * );
100 unsigned int update_iterator_PrevMirror( update_iterator_t * );
102 void update_iterator_GetData( update_iterator_t * );
103 void update_iterator_ClearData( update_iterator_t * );
105 /*****************************************************************************
107 *****************************************************************************/
110 * Create a new update VLC struct
112 * \param p_this the calling vlc_object
113 * \return pointer to new update_t or NULL
115 update_t *__update_New( vlc_object_t *p_this )
119 if( p_this == NULL ) return NULL;
121 p_update = (update_t *)malloc( sizeof( update_t ) );
123 vlc_mutex_init( p_this, &p_update->lock );
125 p_update->p_libvlc = p_this->p_libvlc;
127 p_update->p_releases = NULL;
128 p_update->i_releases = 0;
129 p_update->b_releases = VLC_FALSE;
131 p_update->p_mirrors = NULL;
132 p_update->i_mirrors = 0;
133 p_update->b_mirrors = VLC_FALSE;
139 * Delete an update_t struct
141 * \param p_update update_t* pointer
144 void update_Delete( update_t *p_update )
146 vlc_mutex_destroy( &p_update->lock );
147 FreeMirrorsList( p_update );
148 FreeReleasesList( p_update );
153 * Empty the mirrors list
154 * *p_update should be locked before using this function
156 * \param p_update pointer to the update struct
159 void FreeMirrorsList( update_t *p_update )
163 for( i = 0; i < p_update->i_mirrors; i++ )
165 free( p_update->p_mirrors[i].psz_name );
166 free( p_update->p_mirrors[i].psz_location );
167 free( p_update->p_mirrors[i].psz_type );
168 free( p_update->p_mirrors[i].psz_base_url );
170 FREENULL( p_update->p_mirrors );
171 p_update->i_mirrors = 0;
172 p_update->b_mirrors = VLC_FALSE;
176 * Empty the releases list
177 * *p_update should be locked before calling this function
179 * \param p_update pointer to the update struct
182 void FreeReleasesList( update_t *p_update )
186 for( i = 0; i < p_update->i_releases; i++ )
189 struct update_release_t *p_release = (p_update->p_releases + i);
190 for( j = 0; j < p_release->i_files; j++ )
192 free( p_release->p_files[j].psz_md5 );
193 free( p_release->p_files[j].psz_url );
194 free( p_release->p_files[j].psz_description );
196 free( p_release->psz_major );
197 free( p_release->psz_minor );
198 free( p_release->psz_revision );
199 free( p_release->psz_extra );
200 free( p_release->psz_svn_revision );
201 free( p_release->p_files );
203 FREENULL( p_update->p_releases );
204 p_update->i_releases = 0;
205 p_update->b_releases = VLC_FALSE;
209 * Get the mirrors list XML file and parse it
210 * *p_update has to be unlocked when calling this function
212 * \param p_update pointer to the update struct
213 * \param b_force set to VLC_TRUE if you want to force the mirrors list update
216 void GetMirrorsList( update_t *p_update, vlc_bool_t b_force )
218 stream_t *p_stream = NULL;
221 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;
226 struct update_mirror_t tmp_mirror;
228 vlc_mutex_lock( &p_update->lock );
230 memset( &tmp_mirror, 0, sizeof(struct update_mirror_t));
232 if( p_update->b_mirrors && b_force == VLC_FALSE )
234 vlc_mutex_unlock( &p_update->lock );
238 p_xml = xml_Create( p_update->p_libvlc );
241 msg_Err( p_update->p_libvlc, "Failed to open XML parser" );
245 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_MIRRORS_URL );
248 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
249 UPDATE_VLC_MIRRORS_URL );
253 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
257 msg_Err( p_update->p_libvlc, "Failed to open %s for parsing",
258 UPDATE_VLC_MIRRORS_URL );
262 if( p_update->p_mirrors )
264 FreeMirrorsList( p_update );
267 while( xml_ReaderRead( p_xml_reader ) == 1 )
269 switch( xml_ReaderNodeType( p_xml_reader ) )
272 msg_Err( p_update->p_libvlc, "Error while parsing %s",
273 UPDATE_VLC_MIRRORS_URL );
276 case XML_READER_STARTELEM:
277 psz_eltname = xml_ReaderName( p_xml_reader );
280 msg_Err( p_update->p_libvlc, "Error while parsing %s",
281 UPDATE_VLC_MIRRORS_URL );
285 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
287 psz_name = xml_ReaderName( p_xml_reader );
288 psz_value = xml_ReaderValue( p_xml_reader );
290 if( !psz_name || !psz_value )
292 msg_Err( p_update->p_libvlc, "Error while parsing %s",
293 UPDATE_VLC_MIRRORS_URL );
297 if( !strcmp( psz_eltname, "mirror" ) )
299 if( !strcmp( psz_name, "name" ) )
300 tmp_mirror.psz_name = STRDUP( psz_value );
301 else if( !strcmp( psz_name, "location" ) )
302 tmp_mirror.psz_location = STRDUP( psz_value );
304 else if( !strcmp( psz_eltname, "url" ) )
306 if( !strcmp( psz_name, "type" ) )
307 tmp_mirror.psz_type = STRDUP( psz_value );
308 else if( !strcmp( psz_name, "base" ) )
309 tmp_mirror.psz_base_url = STRDUP( psz_value );
311 FREENULL( psz_name );
312 FREENULL( psz_value );
314 if( !strcmp( psz_eltname, "url" ) )
316 /* append to mirrors list */
317 p_update->p_mirrors =
318 (struct update_mirror_t *)realloc( p_update->p_mirrors,
319 (++(p_update->i_mirrors))
320 *sizeof( struct update_mirror_t ) );
321 p_update->p_mirrors[ p_update->i_mirrors - 1 ] =
323 tmp_mirror.psz_name = STRDUP( tmp_mirror.psz_name );
324 tmp_mirror.psz_location = STRDUP( tmp_mirror.psz_location );
325 tmp_mirror.psz_type = NULL;
326 tmp_mirror.psz_base_url = NULL;
328 FREENULL( psz_eltname );
331 case XML_READER_ENDELEM:
332 psz_eltname = xml_ReaderName( p_xml_reader );
335 msg_Err( p_update->p_libvlc, "Error while parsing %s",
336 UPDATE_VLC_MIRRORS_URL );
340 if( !strcmp( psz_eltname, "mirror" ) )
342 FREENULL( tmp_mirror.psz_name );
343 FREENULL( tmp_mirror.psz_location );
346 FREENULL( psz_eltname );
349 /*case XML_READER_TEXT:
350 psz_eltvalue = xml_ReaderValue( p_xml_reader );
351 FREENULL( psz_eltvalue );
356 p_update->b_mirrors = VLC_TRUE;
359 vlc_mutex_unlock( &p_update->lock );
362 //free( psz_eltvalue );
366 free( tmp_mirror.psz_name );
367 free( tmp_mirror.psz_location );
368 free( tmp_mirror.psz_type );
369 free( tmp_mirror.psz_base_url );
371 if( p_xml_reader && p_xml )
372 xml_ReaderDelete( p_xml, p_xml_reader );
374 stream_Delete( p_stream );
380 * Get the files list XML file and parse it
381 * *p_update has to be unlocked when calling this function
383 * \param p_update pointer to update struct
384 * \param b_force set to VLC_TRUE if you want to force the files list update
387 void GetFilesList( update_t *p_update, vlc_bool_t b_force )
389 stream_t *p_stream = NULL;
392 xml_reader_t *p_xml_reader = NULL;
394 char *psz_eltname = NULL;
395 char *psz_eltvalue = NULL;
396 char *psz_name = NULL;
397 char *psz_value = NULL;
399 struct update_release_t *p_release = NULL;
400 struct update_release_t tmp_release;
401 struct update_file_t tmp_file;
403 vlc_bool_t b_os = VLC_FALSE, b_arch = VLC_FALSE;
405 memset( &tmp_release, 0, sizeof(struct update_release_t) );
406 memset( &tmp_file, 0, sizeof(struct update_file_t) );
408 tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
410 vlc_mutex_lock( &p_update->lock );
412 if( p_update->b_releases && b_force == VLC_FALSE )
414 vlc_mutex_unlock( &p_update->lock );
418 p_xml = xml_Create( p_update->p_libvlc );
421 msg_Err( p_update->p_libvlc, "Failed to open XML parser" );
425 p_stream = stream_UrlNew( p_update->p_libvlc, UPDATE_VLC_STATUS_URL );
428 msg_Err( p_update->p_libvlc, "Failed to open %s for reading",
429 UPDATE_VLC_STATUS_URL );
433 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
437 msg_Err( p_update->p_libvlc, "Failed to open %s for parsing",
438 UPDATE_VLC_STATUS_URL );
442 if( p_update->p_releases )
444 FreeReleasesList( p_update );
447 while( xml_ReaderRead( p_xml_reader ) == 1 )
449 switch( xml_ReaderNodeType( p_xml_reader ) )
452 msg_Err( p_update->p_libvlc, "Error while parsing %s",
453 UPDATE_VLC_STATUS_URL );
456 case XML_READER_STARTELEM:
457 psz_eltname = xml_ReaderName( p_xml_reader );
460 msg_Err( p_update->p_libvlc, "Error while parsing %s",
461 UPDATE_VLC_STATUS_URL );
465 while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
467 psz_name = xml_ReaderName( p_xml_reader );
468 psz_value = xml_ReaderValue( p_xml_reader );
470 if( !psz_name || !psz_value )
472 msg_Err( p_update->p_libvlc, "Error while parsing %s",
473 UPDATE_VLC_STATUS_URL );
479 if( strcmp( psz_eltname, "version" ) == 0 )
481 if( !strcmp( psz_name, "major" ) )
482 tmp_release.psz_major = STRDUP( psz_value );
483 else if( !strcmp( psz_name, "minor" ) )
484 tmp_release.psz_minor = STRDUP( psz_value );
485 else if( !strcmp( psz_name, "revision" ) )
486 tmp_release.psz_revision = STRDUP( psz_value );
487 else if( !strcmp( psz_name, "extra" ) )
488 tmp_release.psz_extra = STRDUP( psz_value );
489 else if( !strcmp( psz_name, "svn" ) )
490 tmp_release.psz_svn_revision =
492 else if( !strcmp( psz_name, "version" ) )
494 if( !strcmp( psz_value, "unstable" ) )
496 UPDATE_RELEASE_TYPE_UNSTABLE;
497 else if( !strcmp( psz_value, "testing" ) )
499 UPDATE_RELEASE_TYPE_TESTING;
502 UPDATE_RELEASE_TYPE_STABLE;
505 else if( !strcmp( psz_eltname, "file" ) )
507 if( !strcmp( psz_name, "type" ) )
509 if( !strcmp( psz_value, "info" ) )
510 tmp_file.i_type = UPDATE_FILE_TYPE_INFO;
511 else if( !strcmp( psz_value, "source" ) )
512 tmp_file.i_type = UPDATE_FILE_TYPE_SOURCE;
513 else if( !strcmp( psz_value, "binary" ) )
514 tmp_file.i_type = UPDATE_FILE_TYPE_BINARY;
515 else if( !strcmp( psz_value, "plugin" ) )
516 tmp_file.i_type = UPDATE_FILE_TYPE_PLUGIN;
518 tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
520 else if( !strcmp( psz_name, "md5" ) )
521 tmp_file.psz_md5 = STRDUP( psz_value );
522 else if( !strcmp( psz_name, "size" ) )
523 tmp_file.l_size = atol( psz_value );
524 else if( !strcmp( psz_name, "url" ) )
525 tmp_file.psz_url = STRDUP( psz_value );
528 if( !strcmp( psz_name, "name" )
529 && ( !strcmp( psz_value, UPDATE_VLC_OS )
530 || !strcmp( psz_value, "*" ) )
531 && !strcmp( psz_eltname, "os" ) )
535 if( b_os && !strcmp( psz_name, "name" )
536 && ( !strcmp( psz_value, UPDATE_VLC_ARCH )
537 || !strcmp( psz_value, "*" ) )
538 && !strcmp( psz_eltname, "arch" ) )
542 FREENULL( psz_name );
543 FREENULL( psz_value );
545 if( ( b_os && b_arch && strcmp( psz_eltname, "arch" ) ) )
547 if( !strcmp( psz_eltname, "version" ) )
550 /* look for a previous occurrence of this release */
551 for( i = 0; i < p_update->i_releases; i++ )
553 p_release = p_update->p_releases + i;
554 if( CompareReleases( p_release, &tmp_release )
555 == UPDATE_RELEASE_STATUS_EQUAL )
560 /* if this is the first time that we see this release,
561 * append it to the list of releases */
562 if( i == p_update->i_releases )
564 tmp_release.i_status =
565 CompareReleaseToCurrent( &tmp_release );
566 p_update->p_releases =
567 (struct update_release_t *)realloc( p_update->p_releases,
568 (++(p_update->i_releases))*sizeof( struct update_release_t ) );
569 p_update->p_releases[ p_update->i_releases - 1 ] =
572 p_update->p_releases + p_update->i_releases - 1;
573 tmp_release.psz_major = NULL;
574 tmp_release.psz_minor = NULL;
575 tmp_release.psz_revision = NULL;
576 tmp_release.psz_extra = NULL;
577 tmp_release.psz_svn_revision = NULL;
578 tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
579 tmp_release.i_status = 0;
580 tmp_release.p_files = NULL;
581 tmp_release.i_files = 0;
585 FREENULL( tmp_release.psz_major );
586 FREENULL( tmp_release.psz_minor );
587 FREENULL( tmp_release.psz_revision );
588 FREENULL( tmp_release.psz_extra );
589 FREENULL( tmp_release.psz_svn_revision );
590 tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
591 FREENULL( tmp_release.p_files );
592 tmp_release.i_files = 0;
595 else if( !strcmp( psz_eltname, "file" ) )
597 /* append file to p_release's file list */
598 if( p_release == NULL )
603 (struct update_file_t *)realloc( p_release->p_files,
604 (++(p_release->i_files))*sizeof( struct update_file_t ) );
605 p_release->p_files[ p_release->i_files - 1 ] = tmp_file;
606 tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
607 tmp_file.psz_md5 = NULL;
609 tmp_file.psz_url = NULL;
610 tmp_file.psz_description = NULL;
613 FREENULL( psz_eltname );
616 case XML_READER_ENDELEM:
617 psz_eltname = xml_ReaderName( p_xml_reader );
620 msg_Err( p_update->p_libvlc, "Error while parsing %s",
621 UPDATE_VLC_STATUS_URL );
625 if( !strcmp( psz_eltname, "os" ) )
627 else if( !strcmp( psz_eltname, "arch" ) )
629 FREENULL( psz_eltname );
632 case XML_READER_TEXT:
633 psz_eltvalue = xml_ReaderValue( p_xml_reader );
634 if( p_release && p_release->i_files )
635 p_release->p_files[ p_release->i_files - 1 ]
636 .psz_description = STRDUP( psz_eltvalue );
637 FREENULL( psz_eltvalue );
642 p_update->b_releases = VLC_TRUE;
645 vlc_mutex_unlock( &p_update->lock );
648 free( psz_eltvalue );
652 free( tmp_release.psz_major );
653 free( tmp_release.psz_minor );
654 free( tmp_release.psz_revision );
655 free( tmp_release.psz_extra );
656 free( tmp_release.psz_svn_revision );
658 free( tmp_file.psz_md5 );
659 free( tmp_file.psz_url );
660 free( tmp_file.psz_description );
662 if( p_xml_reader && p_xml )
663 xml_ReaderDelete( p_xml, p_xml_reader );
665 stream_Delete( p_stream );
673 * \param p_update pointer to update struct
674 * \param b_force set to VLC_TRUE if you want to force the update
677 void update_Check( update_t *p_update, vlc_bool_t b_force )
679 if( p_update == NULL ) return;
680 GetMirrorsList( p_update, b_force );
681 GetFilesList( p_update, b_force );
685 * Compare two release numbers
686 * The comparision algorith basically performs an alphabetical order (strcmp)
687 * comparision of each of the version number elements until it finds two
688 * different ones. This is the tricky function.
690 * \param p1 first release
691 * \param p2 second release
692 * \return like strcmp
694 int CompareReleases( struct update_release_t *p1, struct update_release_t *p2 )
697 if( ( d = strcmp( p1->psz_major, p2->psz_major ) ) ) ;
698 else if( ( d = strcmp( p1->psz_minor, p2->psz_minor ) ) ) ;
699 else if( ( d = strcmp( p1->psz_revision, p2->psz_revision ) ) ) ;
702 d = strcmp( p1->psz_extra, p2->psz_extra );
706 * not num < NULL < num
707 * -test and -svn releases are thus always considered older than
708 * -'' or -0 releases, which is the best i could come up with */
711 strtol( p1->psz_extra, &psz_end1, 10 );
712 strtol( p2->psz_extra, &psz_end2, 10 );
713 if( psz_end2 == p2->psz_extra
714 && ( psz_end1 != p1->psz_extra || *p1->psz_extra == '\0' ) )
719 return UPDATE_RELEASE_STATUS_OLDER;
721 return UPDATE_RELEASE_STATUS_EQUAL;
723 return UPDATE_RELEASE_STATUS_NEWER;
727 * Compare a given release's version number to the current VLC's one
730 * \return >0 if newer, 0 if equal and <0 if older
732 int CompareReleaseToCurrent( struct update_release_t *p )
734 struct update_release_t c;
737 memset( &c, 0, sizeof(struct update_release_t) );
738 c.psz_major = STRDUP( PACKAGE_VERSION_MAJOR );
739 c.psz_minor = STRDUP( PACKAGE_VERSION_MINOR );
740 c.psz_revision = STRDUP( PACKAGE_VERSION_REVISION );
741 c.psz_extra = STRDUP( PACKAGE_VERSION_EXTRA );
742 r = CompareReleases( p, &c );
745 free( c.psz_revision );
750 /*****************************************************************************
751 * Updatei_iterator_t functions
752 *****************************************************************************/
755 * Create a new update iterator structure. This structure can then be used to
756 * describe a position and move through the update and mirror trees/lists.
757 * This will use an existing update struct or create a new one if none is
760 * \param p_u the calling update_t
761 * \return a pointer to an update iterator
763 update_iterator_t *update_iterator_New( update_t *p_u )
765 update_iterator_t *p_uit = NULL;
770 p_uit = (update_iterator_t *)malloc( sizeof( update_iterator_t ) );
771 if( p_uit == NULL ) return NULL;
779 p_uit->i_t = UPDATE_FILE_TYPE_ALL;
780 p_uit->i_rs = UPDATE_RELEASE_STATUS_ALL;
781 p_uit->i_rt = UPDATE_RELEASE_TYPE_STABLE;
783 p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
784 p_uit->file.psz_md5 = NULL;
785 p_uit->file.psz_url = NULL;
786 p_uit->file.l_size = 0;
787 p_uit->file.psz_description = NULL;
789 p_uit->release.psz_version = NULL;
790 p_uit->release.psz_svn_revision = NULL;
791 p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
792 p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;
794 p_uit->mirror.psz_name = NULL;
795 p_uit->mirror.psz_location = NULL;
796 p_uit->mirror.psz_type = NULL;
802 * Delete an update iterator structure (duh!)
804 * \param p_uit pointer to an update iterator
807 void update_iterator_Delete( update_iterator_t *p_uit )
810 update_iterator_ClearData( p_uit );
815 * Reset an update_iterator_t structure
817 * \param p_uit pointer to an update iterator
818 * \return UPDATE_FAIL upon error, UPDATE_SUCCESS otherwise
820 unsigned int update_iterator_Reset( update_iterator_t *p_uit )
822 if( !p_uit ) return UPDATE_FAIL;
828 update_iterator_ClearData( p_uit );
829 return UPDATE_SUCCESS;
833 * Finds the next file in the update tree that matches status and type
834 * requirements set in the update_iterator
836 * \param p_uit update iterator
837 * \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
839 unsigned int update_iterator_NextFile( update_iterator_t *p_uit )
843 if( !p_uit ) return UPDATE_FAIL;
847 /* if the update iterator was already in a "no match" state, start over */
848 if( p_uit->i_r == -1 ) p_uit->i_r = 0;
849 //if( p_uit->i_f == -1 ) p_uit->i_f = 0;
851 vlc_mutex_lock( &p_uit->p_u->lock );
853 for( r = p_uit->i_r; r < p_uit->p_u->i_releases; r++ )
855 if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
856 for( f = ( r == p_uit->i_r ? p_uit->i_f + 1 : 0 );
857 f < p_uit->p_u->p_releases[r].i_files; f++ )
859 if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
861 goto done;/* "double break" */
869 r = p_uit->p_u->i_releases;
871 if( old_r == p_uit->i_r )
873 update_iterator_GetData( p_uit );
874 vlc_mutex_unlock( &p_uit->p_u->lock );
875 return UPDATE_SUCCESS|UPDATE_FILE;
877 else if( p_uit->i_r == r )
881 update_iterator_GetData( p_uit );
882 vlc_mutex_unlock( &p_uit->p_u->lock );
887 update_iterator_GetData( p_uit );
888 vlc_mutex_unlock( &p_uit->p_u->lock );
889 return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
894 * Finds the previous file in the update tree that matches status and type
895 * requirements set in the update_iterator
897 * \param p_uit update iterator
898 * \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
901 unsigned int update_iterator_PrevFile( update_iterator_t *p_uit )
905 if( !p_uit ) return UPDATE_FAIL;
909 /* if the update iterator was already in a "no match" state, start over
910 * (begin at the end of the list) */
911 if( p_uit->i_r == -1 ) p_uit->i_r = p_uit->p_u->i_releases - 1;
912 p_uit->i_f = p_uit->p_u->p_releases[p_uit->i_r].i_files + 1;
914 vlc_mutex_lock( &p_uit->p_u->lock );
916 for( r = p_uit->i_r; r >= 0; r-- )
918 if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
919 for( f =( r==p_uit->i_r ? p_uit->i_f - 1 : p_uit->p_u->p_releases[r].i_files );
922 if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
924 goto done;/* "double break" */
932 r = p_uit->p_u->i_releases;
934 if( old_r == p_uit->i_r )
936 update_iterator_GetData( p_uit );
937 vlc_mutex_unlock( &p_uit->p_u->lock );
938 return UPDATE_SUCCESS|UPDATE_FILE;
940 else if( p_uit->i_r == -1 )
944 update_iterator_GetData( p_uit );
945 vlc_mutex_unlock( &p_uit->p_u->lock );
950 update_iterator_GetData( p_uit );
951 vlc_mutex_unlock( &p_uit->p_u->lock );
952 return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
957 * Finds the next mirror in the update tree
959 * \param update iterator
960 * \return UPDATE_FAIL if we can't find the next mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
962 unsigned int update_iterator_NextMirror( update_iterator_t *p_uit )
964 if( !p_uit ) return UPDATE_FAIL;
965 vlc_mutex_lock( &p_uit->p_u->lock );
967 if( p_uit->i_m >= p_uit->p_u->i_mirrors ) p_uit->i_m = -1;
968 update_iterator_GetData( p_uit );
969 vlc_mutex_unlock( &p_uit->p_u->lock );
970 return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
974 * Finds the previous mirror in the update tree
976 * \param update iterator
977 * \return UPDATE_FAIL if we can't find a previous mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
979 unsigned int update_iterator_PrevMirror( update_iterator_t *p_uit )
981 if( !p_uit ) return UPDATE_FAIL;
982 vlc_mutex_lock( &p_uit->p_u->lock );
984 update_iterator_GetData( p_uit );
985 vlc_mutex_unlock( &p_uit->p_u->lock );
986 return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
990 * Change the update iterator's position in the file and mirrors tree
991 * If position is negative, don't change it
993 * \param i_m position in mirrors list
994 * \param i_r position in releases list
995 * \param i_f position in release's files list
996 * \return UPDATE_FAIL when changing position fails or position wasn't changed, a combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE otherwise
998 unsigned int update_iterator_ChooseMirrorAndFile( update_iterator_t *p_uit,
999 int i_m, int i_r, int i_f )
1001 unsigned int i_val = 0;
1003 if( !p_uit ) return 0;
1004 vlc_mutex_lock( &p_uit->p_u->lock );
1008 if( i_m < p_uit->p_u->i_mirrors )
1010 if( i_m != p_uit->i_m )
1011 i_val |= UPDATE_MIRROR;
1019 if( i_r < p_uit->p_u->i_releases )
1021 if( i_r != p_uit->i_r )
1022 i_val |= UPDATE_FILE;
1030 if( i_r >= 0 && i_r < p_uit->p_u->i_releases
1031 && i_f < p_uit->p_u->p_releases[p_uit->i_r].i_files )
1033 if( i_f != p_uit->i_f )
1034 i_val |= UPDATE_FILE;
1040 update_iterator_GetData( p_uit );
1041 vlc_mutex_unlock( &p_uit->p_u->lock );
1043 if( ( i_m < 0 || p_uit->i_m >= 0 )
1044 && ( i_r < 0 || p_uit->i_r >= 0 )
1045 && ( i_f < 0 || p_uit->i_f >= 0 ) )
1047 /* Everything worked */
1048 return UPDATE_SUCCESS|i_val;
1052 /* Something failed */
1058 * Fills the iterator data (file, release and mirror structs)
1059 * The update struct should be locked before calling this function.
1061 * \param p_uit update iterator
1064 void update_iterator_GetData( update_iterator_t *p_uit )
1066 struct update_release_t *p_r = NULL;
1067 struct update_file_t *p_f = NULL;
1068 struct update_mirror_t *p_m = NULL;
1070 update_iterator_ClearData( p_uit );
1072 if( p_uit->i_m >= 0 )
1074 p_m = p_uit->p_u->p_mirrors + p_uit->i_m;
1075 p_uit->mirror.psz_name = STRDUP( p_m->psz_name );
1076 p_uit->mirror.psz_location = STRDUP( p_m->psz_location );
1077 p_uit->mirror.psz_type = STRDUP( p_m->psz_type );
1080 if( p_uit->i_r >= 0 )
1082 p_r = p_uit->p_u->p_releases + p_uit->i_r;
1083 asprintf( &p_uit->release.psz_version, "%s.%s.%s-%s",
1088 p_uit->release.psz_svn_revision = STRDUP( p_r->psz_svn_revision );
1089 p_uit->release.i_type = p_r->i_type;
1090 p_uit->release.i_status = p_r->i_status;
1091 if( p_uit->i_f >= 0 )
1093 p_f = p_r->p_files + p_uit->i_f;
1094 p_uit->file.i_type = p_f->i_type;
1095 p_uit->file.psz_md5 = STRDUP( p_f->psz_md5 );
1096 p_uit->file.l_size = p_f->l_size;
1097 p_uit->file.psz_description = STRDUP( p_f->psz_description);
1098 if( p_f->psz_url[0] == '/' )
1102 asprintf( &p_uit->file.psz_url, "%s%s",
1103 p_m->psz_base_url, p_f->psz_url );
1108 p_uit->file.psz_url = STRDUP( p_f->psz_url );
1115 * Clears the iterator data (file, release and mirror structs)
1117 * \param p_uit update iterator
1120 void update_iterator_ClearData( update_iterator_t *p_uit )
1122 p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
1123 FREENULL( p_uit->file.psz_md5 );
1124 p_uit->file.l_size = 0;
1125 FREENULL( p_uit->file.psz_description );
1126 FREENULL( p_uit->file.psz_url );
1127 FREENULL( p_uit->release.psz_version );
1128 FREENULL( p_uit->release.psz_svn_revision );
1129 p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
1130 p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;
1131 FREENULL( p_uit->mirror.psz_name );
1132 FREENULL( p_uit->mirror.psz_location );
1133 FREENULL( p_uit->mirror.psz_type );
1137 * Perform an action on the update iterator
1138 * Only the first matching action is performed.
1140 * \param p_uit update iterator
1141 * \param i_action update action bitmask. can be a combination of UPDATE_NEXT, UPDATE_PREV, UPDATE_MIRROR, UPDATE_RELEASE, UPDATE_FILE, UPDATE_RESET
1142 * \return UPDATE_FAIL if action fails, UPDATE_SUCCESS|(combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE if these changed) otherwise
1144 unsigned int update_iterator_Action( update_iterator_t *p_uit, int i_action )
1146 if( i_action & UPDATE_RESET )
1148 return update_iterator_Reset( p_uit );
1151 if( i_action & UPDATE_MIRROR )
1153 if( i_action & UPDATE_PREV )
1155 return update_iterator_PrevMirror( p_uit );
1159 return update_iterator_NextMirror( p_uit );
1162 /*else if( i_action & UPDATE_RELEASE )
1164 if( i_action & UPDATE_PREV )
1166 return update_iterator_PrevRelease( p_uit );
1170 return update_iterator_NextRelease( p_uit );
1173 else if( i_action & UPDATE_FILE )
1175 if( i_action & UPDATE_PREV )
1177 return update_iterator_PrevFile( p_uit );
1181 return update_iterator_NextFile( p_uit );
1186 return UPDATE_SUCCESS;
1191 * Object to launch download thread in a different object
1195 char *psz_dest; //< Download destination
1196 char *psz_src; //< Download source
1197 char *psz_status; //< Download status displayed in progress dialog
1198 } download_thread_t;
1200 void update_download_for_real( download_thread_t *p_this );
1203 * Download the file selected by the update iterator. This function will
1204 * launch the download in a new thread (downloads can be long)
1206 * \param p_uit update iterator
1207 * \param psz_dest destination file path
1210 void update_download( update_iterator_t *p_uit, const char *psz_dest )
1212 download_thread_t *p_dt =
1213 vlc_object_create( p_uit->p_u->p_libvlc, sizeof( download_thread_t ) );
1215 p_dt->psz_dest = strdup( psz_dest );
1216 p_dt->psz_src = strdup( p_uit->file.psz_url );
1217 asprintf( &p_dt->psz_status, "%s - %s (%s)\nSource: %s\nDestination: %s",
1218 p_uit->file.psz_description, p_uit->release.psz_version,
1219 p_uit->release.psz_svn_revision, p_uit->file.psz_url,
1222 vlc_thread_create( p_dt, "download thread", update_download_for_real,
1223 VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
1227 * Convert a long int size in bytes to a string
1229 * \param l_size the size in bytes
1230 * \return the size as a string
1232 static char *size_str( long int l_size )
1236 asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
1238 asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
1239 else if( l_size >> 10 )
1240 asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
1242 asprintf( &psz_tmp, "%ld B", l_size );
1247 * The true download function.
1249 * \param p_this the download_thread_t object
1252 void update_download_for_real( download_thread_t *p_this )
1254 char *psz_dest = p_this->psz_dest;
1255 char *psz_src = p_this->psz_src;
1257 libvlc_int_t *p_libvlc = p_this->p_libvlc;
1259 FILE *p_file = NULL;
1265 long int l_size, l_done = 0;
1267 vlc_thread_ready( p_this );
1269 asprintf( &psz_status, "%s\nDownloading... 0.0/? %.1f%% done",
1270 p_this->psz_status, 0.0 );
1271 i_progress = intf_UserProgress( p_libvlc, "Downloading...",
1272 psz_status, 0.0, 0 );
1274 p_stream = stream_UrlNew( p_libvlc, psz_src );
1277 msg_Err( p_libvlc, "Failed to open %s for reading", psz_src );
1278 intf_UserFatal( p_libvlc, VLC_TRUE, "Error while Downloading...",
1279 "VLC failed to open %s for reading.", psz_src );
1280 intf_UserHide( p_libvlc, i_progress );
1285 p_file = utf8_fopen( psz_dest, "w" );
1288 msg_Err( p_libvlc, "Failed to open %s for writing", psz_dest );
1289 intf_UserFatal( p_libvlc, VLC_TRUE, "Error while Downloading...",
1290 "VLC failed to open %s for writing.", psz_dest );
1291 intf_UserHide( p_libvlc, i_progress );
1296 char *psz_s1; char *psz_s2;
1298 l_size = stream_Size(p_stream);
1299 p_buffer = (void *)malloc( 1<<10 );
1301 while( ( l_read = stream_Read( p_stream, p_buffer, 1<<10 ) ) )
1305 fwrite( p_buffer, l_read, 1, p_file );
1309 f_progress = 100.0*(float)l_done/(float)l_size;
1310 psz_s1 = size_str( l_done );
1311 psz_s2 = size_str( l_size );
1312 asprintf( &psz_status, "%s\nDownloading... %s/%s (%.1f%%) done",
1313 p_this->psz_status, psz_s1, psz_s2, f_progress );
1314 free( psz_s1 ); free( psz_s2 );
1316 intf_ProgressUpdate( p_libvlc, i_progress,
1317 psz_status, f_progress, 0 );
1322 stream_Delete( p_stream );
1325 psz_s2 = size_str( l_size );
1326 asprintf( &psz_status, "%s\nDone %s (100.00%%)",
1327 p_this->psz_status, psz_s2 );
1329 intf_ProgressUpdate( p_libvlc, i_progress, psz_status, 100.0, 0 );
1334 free( p_this->psz_dest );
1335 free( p_this->psz_src );
1336 free( p_this->psz_status );
1338 vlc_object_destroy( p_this );