update.c 42.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/*****************************************************************************
 * update.c: VLC update and plugins download
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 * $Id: $
 *
 * Authors: Antoine Cellerier <dionoea -at- videolan -dot- org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either release 2 of the License, or
 * (at your option) any later release.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/* TODO
 * --> check release types.
 * --> make sure that the version comparision method is ok.
 */

/**
 *   \file
 *   This file contains functions related to VLC and plugins update management
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <ctype.h>                                              /* tolower() */

#include <vlc/vlc.h>

#include "vlc_update.h"

#include "vlc_block.h"
#include "vlc_stream.h"
#include "vlc_xml.h"
#include "vlc_interaction.h"
48
#include "charset.h"
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404

/*****************************************************************************
 * Misc defines
 *****************************************************************************/

/* All release notes and source packages should match on "*"
 * Only binary installers are OS specific ( we only provide these
 * for Win32, Mac OS X, WincCE, beos(?) ) */
#if defined( UNDER_CE )
#   define UPDATE_VLC_OS "*"
#   define UPDATE_VLC_ARCH "*"
#elif defined( WIN32 )
#   define UPDATE_VLC_OS "windows"
#   define UPDATE_VLC_ARCH "i386"
#elif defined( __APPLE__ )
#   define UPDATE_VLC_OS "macosx"
#   if defined( __powerpc__ ) || defined( __ppc__ ) || defined( __ppc64__ )
#       define UPDATE_VLC_ARCH "ppc"
#   else
#       define UPDATE_VLC_ARCH "x86"
#   endif
#elif defined( SYS_BEOS )
#   define UPDATE_VLC_OS "beos"
#   define UPDATE_VLC_ARCH "i386"
#else
#   define UPDATE_VLC_OS "*"
#   define UPDATE_VLC_ARCH "*"
#endif

#define UPDATE_VLC_STATUS_URL "http://update.videolan.org/vlc/status.xml"
#define UPDATE_VLC_MIRRORS_URL "http://update.videolan.org/mirrors.xml"

#define FREE( a ) free(a);a=NULL;
#define STRDUP( a ) ( a ? strdup( a ) : NULL )

/*****************************************************************************
 * Local Prototypes
 *****************************************************************************/

void FreeMirrorsList( update_t * );
void FreeReleasesList( update_t * );
void GetMirrorsList( update_t *, vlc_bool_t );
void GetFilesList( update_t *, vlc_bool_t );

int CompareReleases( struct update_release_t *, struct update_release_t * );
int CompareReleaseToCurrent( struct update_release_t * );

unsigned int update_iterator_Reset( update_iterator_t * );
unsigned int update_iterator_NextFile( update_iterator_t * );
unsigned int update_iterator_PrevFile( update_iterator_t * );
unsigned int update_iterator_NextMirror( update_iterator_t * );
unsigned int update_iterator_PrevMirror( update_iterator_t * );

void update_iterator_GetData( update_iterator_t * );
void update_iterator_ClearData( update_iterator_t * );

/*****************************************************************************
 * Update_t functions
 *****************************************************************************/

/**
 * 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 *p_update;

    if( p_this == NULL ) return NULL;

    p_update = (update_t *)malloc( sizeof( update_t ) );

    vlc_mutex_init( p_this, &p_update->lock );

    p_update->p_vlc = p_this->p_vlc;

    p_update->p_releases = NULL;
    p_update->i_releases = 0;
    p_update->b_releases = VLC_FALSE;

    p_update->p_mirrors = NULL;
    p_update->i_mirrors = 0;
    p_update->b_mirrors = VLC_FALSE;

    return p_update;
}

/**
 * Delete an update_t struct
 *
 * \param p_update update_t* pointer
 * \return nothing
 */
void update_Delete( update_t *p_update )
{
    vlc_mutex_destroy( &p_update->lock );
    FreeMirrorsList( p_update );
    FreeReleasesList( p_update );
    free( p_update );
}

/**
 * Empty the mirrors list
 * *p_update should be locked before using this function
 *
 * \param p_update pointer to the update struct
 * \return nothing
 */
void FreeMirrorsList( update_t *p_update )
{
    int i;

    for( i = 0; i < p_update->i_mirrors; i++ )
    {
        free( p_update->p_mirrors[i].psz_name );
        free( p_update->p_mirrors[i].psz_location );
        free( p_update->p_mirrors[i].psz_type );
        free( p_update->p_mirrors[i].psz_base_url );
    }
    FREE( p_update->p_mirrors );
    p_update->i_mirrors = 0;
    p_update->b_mirrors = VLC_FALSE;
}

/**
 * Empty the releases list
 * *p_update should be locked before calling this function
 *
 * \param p_update pointer to the update struct
 * \return nothing
 */
void FreeReleasesList( update_t *p_update )
{
    int i;

    for( i = 0; i < p_update->i_releases; i++ )
    {
        int j;
        struct update_release_t *p_release = (p_update->p_releases + i);
        for( j = 0; j < p_release->i_files; j++ )
        {
            free( p_release->p_files[j].psz_md5 );
            free( p_release->p_files[j].psz_url );
            free( p_release->p_files[j].psz_description );
        }
        free( p_release->psz_major );
        free( p_release->psz_minor );
        free( p_release->psz_revision );
        free( p_release->psz_extra );
        free( p_release->psz_svn_revision );
        free( p_release->p_files );
    }
    FREE( p_update->p_releases );
    p_update->i_releases = 0;
    p_update->b_releases = VLC_FALSE;
}

/**
 * Get the mirrors list XML file and parse it
 * *p_update has to be unlocked when calling this function
 *
 * \param p_update pointer to the update struct
 * \param b_force set to VLC_TRUE if you want to force the mirrors list update
 * \return nothing
 */
void GetMirrorsList( update_t *p_update, vlc_bool_t b_force )
{
    stream_t *p_stream = NULL;

    xml_t *p_xml = NULL;
    xml_reader_t *p_xml_reader = NULL;

    char *psz_eltname = NULL;
    //char *psz_eltvalue = NULL;
    char *psz_name = NULL;
    char *psz_value = NULL;

    struct update_mirror_t tmp_mirror = {0};

    vlc_mutex_lock( &p_update->lock );

    if( p_update->b_mirrors && b_force == VLC_FALSE )
    {
        vlc_mutex_unlock( &p_update->lock );
        return;
    }

    p_xml = xml_Create( p_update->p_vlc );
    if( !p_xml )
    {
        msg_Err( p_update->p_vlc, "Failed to open XML parser" );
        goto error;
    }

    p_stream = stream_UrlNew( p_update->p_vlc, UPDATE_VLC_MIRRORS_URL );
    if( !p_stream )
    {
        msg_Err( p_update->p_vlc, "Failed to open %s for reading",
                 UPDATE_VLC_MIRRORS_URL );
        goto error;
    }

    p_xml_reader = xml_ReaderCreate( p_xml, p_stream );

    if( !p_xml_reader )
    {
        msg_Err( p_update->p_vlc, "Failed to open %s for parsing",
                 UPDATE_VLC_MIRRORS_URL );
        goto error;
    }

    if( p_update->p_mirrors )
    {
        FreeMirrorsList( p_update );
    }

    while( xml_ReaderRead( p_xml_reader ) == 1 )
    {
        switch( xml_ReaderNodeType( p_xml_reader ) )
        {
            case -1:
                msg_Err( p_update->p_vlc, "Error while parsing %s",
                         UPDATE_VLC_MIRRORS_URL );
                goto error;

            case XML_READER_STARTELEM:
                psz_eltname = xml_ReaderName( p_xml_reader );
                if( !psz_eltname )
                {
                    msg_Err( p_update->p_vlc, "Error while parsing %s",
                             UPDATE_VLC_MIRRORS_URL );
                    goto error;
                }

                while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
                {
                    psz_name = xml_ReaderName( p_xml_reader );
                    psz_value = xml_ReaderValue( p_xml_reader );

                    if( !psz_name || !psz_value )
                    {
                        msg_Err( p_update->p_vlc, "Error while parsing %s",
                                 UPDATE_VLC_MIRRORS_URL );
                        goto error;
                    }

                    if( !strcmp( psz_eltname, "mirror" ) )
                    {
                        if( !strcmp( psz_name, "name" ) )
                            tmp_mirror.psz_name = STRDUP( psz_value );
                        else if( !strcmp( psz_name, "location" ) )
                            tmp_mirror.psz_location = STRDUP( psz_value );
                    }
                    else if( !strcmp( psz_eltname, "url" ) )
                    {
                        if( !strcmp( psz_name, "type" ) )
                            tmp_mirror.psz_type = STRDUP( psz_value );
                        else if( !strcmp( psz_name, "base" ) )
                            tmp_mirror.psz_base_url = STRDUP( psz_value );
                    }
                    FREE( psz_name );
                    FREE( psz_value );
                }
                if( !strcmp( psz_eltname, "url" ) )
                {
                    /* append to mirrors list */
                    p_update->p_mirrors =
                    (struct update_mirror_t *)realloc( p_update->p_mirrors,
                                       (++(p_update->i_mirrors))
                                       *sizeof( struct update_mirror_t ) );
                    p_update->p_mirrors[ p_update->i_mirrors - 1 ] =
                        tmp_mirror;
                    tmp_mirror.psz_name = STRDUP( tmp_mirror.psz_name );
                    tmp_mirror.psz_location = STRDUP( tmp_mirror.psz_location );
                    tmp_mirror.psz_type = NULL;
                    tmp_mirror.psz_base_url = NULL;
                }
                FREE( psz_eltname );
                break;

            case XML_READER_ENDELEM:
                psz_eltname = xml_ReaderName( p_xml_reader );
                if( !psz_eltname )
                {
                    msg_Err( p_update->p_vlc, "Error while parsing %s",
                             UPDATE_VLC_MIRRORS_URL );
                    goto error;
                }

                if( !strcmp( psz_eltname, "mirror" ) )
                {
                    FREE( tmp_mirror.psz_name );
                    FREE( tmp_mirror.psz_location );
                }

                FREE( psz_eltname );
                break;

            /*case XML_READER_TEXT:
                psz_eltvalue = xml_ReaderValue( p_xml_reader );
                FREE( psz_eltvalue );
                break;*/
        }
    }

    p_update->b_mirrors = VLC_TRUE;

    error:
        vlc_mutex_unlock( &p_update->lock );

        free( psz_eltname );
        //free( psz_eltvalue );
        free( psz_name );
        free( psz_value );

        free( tmp_mirror.psz_name );
        free( tmp_mirror.psz_location );
        free( tmp_mirror.psz_type );
        free( tmp_mirror.psz_base_url );

        if( p_xml_reader && p_xml )
            xml_ReaderDelete( p_xml, p_xml_reader );
        if( p_stream )
            stream_Delete( p_stream );
        if( p_xml )
            xml_Delete( p_xml );
}

/**
 * Get the files list XML file and parse it
 * *p_update has to be unlocked when calling this function
 *
 * \param p_update pointer to update struct
 * \param b_force set to VLC_TRUE if you want to force the files list update
 * \return nothing
 */
void GetFilesList( update_t *p_update, vlc_bool_t b_force )
{
    stream_t *p_stream = NULL;

    xml_t *p_xml = NULL;
    xml_reader_t *p_xml_reader = NULL;

    char *psz_eltname = NULL;
    char *psz_eltvalue = NULL;
    char *psz_name = NULL;
    char *psz_value = NULL;

    struct update_release_t *p_release = NULL;
    struct update_release_t tmp_release = {0};
    struct update_file_t tmp_file = {0};

    vlc_bool_t b_os = VLC_FALSE, b_arch = VLC_FALSE;

405
406
    tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
    vlc_mutex_lock( &p_update->lock );

    if( p_update->b_releases && b_force == VLC_FALSE )
    {
        vlc_mutex_unlock( &p_update->lock );
        return;
    }

    p_xml = xml_Create( p_update->p_vlc );
    if( !p_xml )
    {
        msg_Err( p_update->p_vlc, "Failed to open XML parser" );
        goto error;
    }

    p_stream = stream_UrlNew( p_update->p_vlc, UPDATE_VLC_STATUS_URL );
    if( !p_stream )
    {
        msg_Err( p_update->p_vlc, "Failed to open %s for reading",
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }

    p_xml_reader = xml_ReaderCreate( p_xml, p_stream );

    if( !p_xml_reader )
    {
        msg_Err( p_update->p_vlc, "Failed to open %s for parsing",
                 UPDATE_VLC_STATUS_URL );
        goto error;
    }

    if( p_update->p_releases )
    {
        FreeReleasesList( p_update );
    }

    while( xml_ReaderRead( p_xml_reader ) == 1 )
    {
        switch( xml_ReaderNodeType( p_xml_reader ) )
        {
            case -1:
                msg_Err( p_update->p_vlc, "Error while parsing %s",
                         UPDATE_VLC_STATUS_URL );
                goto error;

            case XML_READER_STARTELEM:
                psz_eltname = xml_ReaderName( p_xml_reader );
                if( !psz_eltname )
                {
                    msg_Err( p_update->p_vlc, "Error while parsing %s",
                             UPDATE_VLC_STATUS_URL );
                    goto error;
                }

                while( xml_ReaderNextAttr( p_xml_reader ) == VLC_SUCCESS )
                {
                    psz_name = xml_ReaderName( p_xml_reader );
                    psz_value = xml_ReaderValue( p_xml_reader );

                    if( !psz_name || !psz_value )
                    {
                        msg_Err( p_update->p_vlc, "Error while parsing %s",
                                 UPDATE_VLC_STATUS_URL );
                        goto error;
                    }

                    if( b_os && b_arch )
                    {
                        if( strcmp( psz_eltname, "version" ) == 0 )
                        {
                            if( !strcmp( psz_name, "major" ) )
                                tmp_release.psz_major = STRDUP( psz_value );
                            else if( !strcmp( psz_name, "minor" ) )
                                tmp_release.psz_minor = STRDUP( psz_value );
                            else if( !strcmp( psz_name, "revision" ) )
                                tmp_release.psz_revision = STRDUP( psz_value );
                            else if( !strcmp( psz_name, "extra" ) )
                                tmp_release.psz_extra = STRDUP( psz_value );
                            else if( !strcmp( psz_name, "svn" ) )
                                tmp_release.psz_svn_revision =
                                                           STRDUP( psz_value );
                            else if( !strcmp( psz_name, "version" ) )
                            {
                                if( !strcmp( psz_value, "unstable" ) )
                                    tmp_release.i_type =
                                                  UPDATE_RELEASE_TYPE_UNSTABLE;
                                else if( !strcmp( psz_value, "testing" ) )
                                    tmp_release.i_type =
                                                  UPDATE_RELEASE_TYPE_TESTING;
                                else
                                    tmp_release.i_type =
                                                  UPDATE_RELEASE_TYPE_STABLE;
                            }
                        }
                        else if( !strcmp( psz_eltname, "file" ) )
                        {
                            if( !strcmp( psz_name, "type" ) )
                            {
                                if( !strcmp( psz_value, "info" ) )
                                    tmp_file.i_type = UPDATE_FILE_TYPE_INFO;
                                else if( !strcmp( psz_value, "source" ) )
                                    tmp_file.i_type = UPDATE_FILE_TYPE_SOURCE;
                                else if( !strcmp( psz_value, "binary" ) )
                                    tmp_file.i_type = UPDATE_FILE_TYPE_BINARY;
                                else if( !strcmp( psz_value, "plugin" ) )
                                    tmp_file.i_type = UPDATE_FILE_TYPE_PLUGIN;
                                else
                                    tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
                            }
                            else if( !strcmp( psz_name, "md5" ) )
                                tmp_file.psz_md5 = STRDUP( psz_value );
                            else if( !strcmp( psz_name, "size" ) )
                                tmp_file.l_size = atol( psz_value );
                            else if( !strcmp( psz_name, "url" ) )
                                tmp_file.psz_url = STRDUP( psz_value );
                        }
                    }
                    if( !strcmp( psz_name, "name" )
                        && ( !strcmp( psz_value, UPDATE_VLC_OS )
                           || !strcmp( psz_value, "*" ) )
                        && !strcmp( psz_eltname, "os" ) )
                    {
                        b_os = VLC_TRUE;
                    }
                    if( b_os && !strcmp( psz_name, "name" )
                        && ( !strcmp( psz_value, UPDATE_VLC_ARCH )
                           || !strcmp( psz_value, "*" ) )
                        && !strcmp( psz_eltname, "arch" ) )
                    {
                        b_arch = VLC_TRUE;
                    }
                    FREE( psz_name );
                    FREE( psz_value );
                }
                if( ( b_os && b_arch && strcmp( psz_eltname, "arch" ) ) )
                {
                    if( !strcmp( psz_eltname, "version" ) )
                    {
                        int i;
                        /* look for a previous occurence of this release */
                        for( i = 0; i < p_update->i_releases; i++ )
                        {
                            p_release = p_update->p_releases + i;
                            if( CompareReleases( p_release, &tmp_release )
                                == UPDATE_RELEASE_STATUS_EQUAL )
                            {
                                break;
                            }
                        }
                        /* if this is the first time that we see this release,
                         * append it to the list of releases */
                        if( i == p_update->i_releases )
                        {
                            tmp_release.i_status =
                                CompareReleaseToCurrent( &tmp_release );
                            p_update->p_releases =
               (struct update_release_t *)realloc( p_update->p_releases,
               (++(p_update->i_releases))*sizeof( struct update_release_t ) );
                            p_update->p_releases[ p_update->i_releases - 1 ] =
                                tmp_release;
                            p_release =
                                p_update->p_releases + p_update->i_releases - 1;
                            tmp_release.psz_major = NULL;
                            tmp_release.psz_minor = NULL;
                            tmp_release.psz_revision = NULL;
                            tmp_release.psz_extra = NULL;
                            tmp_release.psz_svn_revision = NULL;
                            tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
                            tmp_release.i_status = 0;
                            tmp_release.p_files = NULL;
                            tmp_release.i_files = 0;
                        }
                        else
                        {
                            FREE( tmp_release.psz_major );
                            FREE( tmp_release.psz_minor );
                            FREE( tmp_release.psz_revision );
                            FREE( tmp_release.psz_extra );
                            FREE( tmp_release.psz_svn_revision );
                            tmp_release.i_type = UPDATE_RELEASE_TYPE_STABLE;
                            FREE( tmp_release.p_files );
                            tmp_release.i_files = 0;
                        }
                    }
                    else if( !strcmp( psz_eltname, "file" ) )
                    {
                        /* append file to p_release's file list */
                        if( p_release == NULL )
                        {
                            goto error;
                        }
                        p_release->p_files =
                    (struct update_file_t *)realloc( p_release->p_files,
                    (++(p_release->i_files))*sizeof( struct update_file_t ) );
                        p_release->p_files[ p_release->i_files - 1 ] = tmp_file;
                        tmp_file.i_type = UPDATE_FILE_TYPE_UNDEF;
                        tmp_file.psz_md5 = NULL;
                        tmp_file.l_size = 0;
                        tmp_file.psz_url = NULL;
                        tmp_file.psz_description = NULL;
                    }
                }
                FREE( psz_eltname );
                break;

            case XML_READER_ENDELEM:
                psz_eltname = xml_ReaderName( p_xml_reader );
                if( !psz_eltname )
                {
                    msg_Err( p_update->p_vlc, "Error while parsing %s",
                             UPDATE_VLC_STATUS_URL );
                    goto error;
                }

                if( !strcmp( psz_eltname, "os" ) )
                    b_os = VLC_FALSE;
                else if( !strcmp( psz_eltname, "arch" ) )
                    b_arch = VLC_FALSE;
                FREE( psz_eltname );
                break;

            case XML_READER_TEXT:
                psz_eltvalue = xml_ReaderValue( p_xml_reader );
                if( p_release && p_release->i_files )
                    p_release->p_files[ p_release->i_files - 1 ]
                               .psz_description = STRDUP( psz_eltvalue );
                FREE( psz_eltvalue );
                break;
        }
    }

    p_update->b_releases = VLC_TRUE;

    error:
        vlc_mutex_unlock( &p_update->lock );

        free( psz_eltname );
        free( psz_eltvalue );
        free( psz_name );
        free( psz_value );

        free( tmp_release.psz_major );
        free( tmp_release.psz_minor );
        free( tmp_release.psz_revision );
        free( tmp_release.psz_extra );
        free( tmp_release.psz_svn_revision );

        free( tmp_file.psz_md5 );
        free( tmp_file.psz_url );
        free( tmp_file.psz_description );

        if( p_xml_reader && p_xml )
            xml_ReaderDelete( p_xml, p_xml_reader );
        if( p_stream )
            stream_Delete( p_stream );
        if( p_xml )
            xml_Delete( p_xml );
}

/**
 * Check for updates
 *
 * \param p_update pointer to update struct
 * \param b_force set to VLC_TRUE if you want to force the update
 * \returns nothing
 */
void update_Check( update_t *p_update, vlc_bool_t b_force )
{
    if( p_update == NULL ) return;
    GetMirrorsList( p_update, b_force );
    GetFilesList( p_update, b_force );
}

/**
 * Compare two release numbers
 * The comparision algorith basically performs an alphabetical order (strcmp)
 * comparision of each of the version number elements until it finds two
 * different ones. This is the tricky function.
 *
 * \param p1 first release
 * \param p2 second release
 * \return like strcmp
 */
int CompareReleases( struct update_release_t *p1, struct update_release_t *p2 )
{
    int d;
    if( ( d = strcmp( p1->psz_major, p2->psz_major ) ) ) ;
    else if( ( d = strcmp( p1->psz_minor, p2->psz_minor ) ) ) ;
    else if( ( d = strcmp( p1->psz_revision, p2->psz_revision ) ) ) ;
    else
    {
        d = strcmp( p1->psz_extra, p2->psz_extra );
        if( d<0 )
        {
        /* FIXME:
         * not num < NULL < num
         * -test and -svn releases are thus always considered older than
         * -'' or -0 releases, which is the best i could come up with */
            char *psz_end1;
            char *psz_end2;
            strtol( p1->psz_extra, &psz_end1, 10 );
            strtol( p2->psz_extra, &psz_end2, 10 );
            if( psz_end2 == p2->psz_extra
             && ( psz_end1 != p1->psz_extra || *p1->psz_extra == '\0' ) )
                d = 1;
        }
    }
    if( d < 0 )
        return UPDATE_RELEASE_STATUS_OLDER;
    else if( d == 0 )
        return UPDATE_RELEASE_STATUS_EQUAL;
    else
        return UPDATE_RELEASE_STATUS_NEWER;
}

/**
 * Compare a given release's version number to the current VLC's one
 *
 * \param p a release
 * \return >0 if newer, 0 if equal and <0 if older
 */
int CompareReleaseToCurrent( struct update_release_t *p )
{
    struct update_release_t c = {0};
    int r;
    c.psz_major = STRDUP( PACKAGE_VERSION_MAJOR );
    c.psz_minor = STRDUP( PACKAGE_VERSION_MINOR );
    c.psz_revision = STRDUP( PACKAGE_VERSION_REVISION );
    c.psz_extra = STRDUP( PACKAGE_VERSION_EXTRA );
    r =  CompareReleases( p, &c );
    free( c.psz_major );
    free( c.psz_minor );
    free( c.psz_revision );
    free( c.psz_extra );
    return r;
}

/*****************************************************************************
 * Updatei_iterator_t functions
 *****************************************************************************/

/**
 * Create a new update iterator structure. This structure can then be used to
 * describe a position and move through the update and mirror trees/lists.
 * This will use an existing update struct or create a new one if none is
 * found
 *
 * \param p_u the calling update_t
 * \return a pointer to an update iterator
 */
update_iterator_t *update_iterator_New( update_t *p_u )
{
    update_iterator_t *p_uit = NULL;

    if( p_u == NULL )
        return NULL;

    p_uit = (update_iterator_t *)malloc( sizeof( update_iterator_t ) );
    if( p_uit == NULL ) return NULL;

    p_uit->p_u = p_u;

    p_uit->i_m = -1;
    p_uit->i_r = -1;
    p_uit->i_f = -1;

    p_uit->i_t = UPDATE_FILE_TYPE_ALL;
    p_uit->i_rs = UPDATE_RELEASE_STATUS_ALL;
    p_uit->i_rt = UPDATE_RELEASE_TYPE_STABLE;

    p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
    p_uit->file.psz_md5 = NULL;
    p_uit->file.psz_url = NULL;
    p_uit->file.l_size = 0;
    p_uit->file.psz_description = NULL;

    p_uit->release.psz_version = NULL;
    p_uit->release.psz_svn_revision = NULL;
    p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
    p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;

    p_uit->mirror.psz_name = NULL;
    p_uit->mirror.psz_location = NULL;
    p_uit->mirror.psz_type = NULL;

    return p_uit;
}

/**
 * Delete an update iterator structure (duh!)
 *
 * \param p_uit pointer to an update iterator
 * \return nothing
 */
void update_iterator_Delete( update_iterator_t *p_uit )
{
    if( !p_uit ) return;
    update_iterator_ClearData( p_uit );
    free( p_uit );
}

/**
 * Reset an update_iterator_t structure
 *
 * \param p_uit pointer to an update iterator
 * \return UPDATE_FAIL upon error, UPDATE_SUCCESS otherwise
 */
unsigned int update_iterator_Reset( update_iterator_t *p_uit )
{
    if( !p_uit ) return UPDATE_FAIL;

    p_uit->i_r = -1;
    p_uit->i_f = -1;
    p_uit->i_m = -1;

    update_iterator_ClearData( p_uit );
    return UPDATE_SUCCESS;
}

/**
 * Finds the next file in the update tree that matches status and type
 * requirements set in the update_iterator
 *
 * \param p_uit update iterator
 * \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
 */
unsigned int update_iterator_NextFile( update_iterator_t *p_uit )
{
    int r,f=-1,old_r;

    if( !p_uit ) return UPDATE_FAIL;

    old_r=p_uit->i_r;

    /* if the update iterator was already in a "no match" state, start over */
    if( p_uit->i_r == -1 ) p_uit->i_r = 0;
    //if( p_uit->i_f == -1 ) p_uit->i_f = 0;

    vlc_mutex_lock( &p_uit->p_u->lock );

    for( r = p_uit->i_r; r < p_uit->p_u->i_releases; r++ )
    {
        if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
        for( f = ( r == p_uit->i_r ? p_uit->i_f + 1 : 0 );
             f < p_uit->p_u->p_releases[r].i_files; f++ )
        {
            if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
            {
                goto done;/* "double break" */
            }
        }
    }
    done:
    p_uit->i_r = r;
    p_uit->i_f = f;

    r = p_uit->p_u->i_releases;

    if( old_r == p_uit->i_r )
    {
        update_iterator_GetData( p_uit );
        vlc_mutex_unlock( &p_uit->p_u->lock );
        return UPDATE_SUCCESS|UPDATE_FILE;
    }
    else if( p_uit->i_r == r )
    {
        p_uit->i_r = -1;
        p_uit->i_f = -1;
        update_iterator_GetData( p_uit );
        vlc_mutex_unlock( &p_uit->p_u->lock );
        return UPDATE_FAIL;
    }
    else
    {
        update_iterator_GetData( p_uit );
        vlc_mutex_unlock( &p_uit->p_u->lock );
        return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
    }
}

/**
 * Finds the previous file in the update tree that matches status and type
 * requirements set in the update_iterator
 *
 * \param p_uit update iterator
 * \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
 */
//TODO: test
unsigned int update_iterator_PrevFile( update_iterator_t *p_uit )
{
    int r,f=-1,old_r;

    if( !p_uit ) return UPDATE_FAIL;

    old_r=p_uit->i_r;

    /* if the update iterator was already in a "no match" state, start over
     * (begin at the end of the list) */
    if( p_uit->i_r == -1 ) p_uit->i_r = p_uit->p_u->i_releases - 1;
    p_uit->i_f = p_uit->p_u->p_releases[p_uit->i_r].i_files + 1;

    vlc_mutex_lock( &p_uit->p_u->lock );

    for( r = p_uit->i_r; r >= 0; r-- )
    {
        if( !( p_uit->p_u->p_releases[r].i_status & p_uit->i_rs ) ) continue;
        for( f =( r==p_uit->i_r ? p_uit->i_f - 1 : p_uit->p_u->p_releases[r].i_files );
             f >= 0; f-- )
        {
            if( p_uit->p_u->p_releases[r].p_files[f].i_type & p_uit->i_t )
            {
                goto done;/* "double break" */
            }
        }
    }
    done:
    p_uit->i_r = r;
    p_uit->i_f = f;

    r = p_uit->p_u->i_releases;

    if( old_r == p_uit->i_r )
    {
        update_iterator_GetData( p_uit );
        vlc_mutex_unlock( &p_uit->p_u->lock );
        return UPDATE_SUCCESS|UPDATE_FILE;
    }
    else if( p_uit->i_r == -1 )
    {
        p_uit->i_r = -1;
        p_uit->i_f = -1;
        update_iterator_GetData( p_uit );
        vlc_mutex_unlock( &p_uit->p_u->lock );
        return UPDATE_FAIL;
    }
    else
    {
        update_iterator_GetData( p_uit );
        vlc_mutex_unlock( &p_uit->p_u->lock );
        return UPDATE_SUCCESS|UPDATE_RELEASE|UPDATE_FILE;
    }
}

/**
 * Finds the next mirror in the update tree
 *
 * \param update iterator
 * \return UPDATE_FAIL if we can't find the next mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
 */
unsigned int update_iterator_NextMirror( update_iterator_t *p_uit )
{
    if( !p_uit ) return UPDATE_FAIL;
    vlc_mutex_lock( &p_uit->p_u->lock );
    p_uit->i_m++;
    if( p_uit->i_m >= p_uit->p_u->i_mirrors ) p_uit->i_m = -1;
    update_iterator_GetData( p_uit );
    vlc_mutex_unlock( &p_uit->p_u->lock );
    return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
}

/**
 * Finds the previous mirror in the update tree
 *
 * \param update iterator
 * \return UPDATE_FAIL if we can't find a previous mirror, UPDATE_SUCCESS|UPDATE_MIRROR otherwise
 */
unsigned int update_iterator_PrevMirror( update_iterator_t *p_uit )
{
    if( !p_uit ) return UPDATE_FAIL;
    vlc_mutex_lock( &p_uit->p_u->lock );
    p_uit->i_m--;
    update_iterator_GetData( p_uit );
    vlc_mutex_unlock( &p_uit->p_u->lock );
    return p_uit->i_m == -1 ? UPDATE_FAIL : UPDATE_SUCCESS|UPDATE_MIRROR;
}

/**
 * Change the update iterator's position in the file and mirrors tree
 * If position is negative, don't change it
 *
 * \param i_m position in mirrors list
 * \param i_r position in releases list
 * \param i_f position in release's files list
 * \return UPDATE_FAIL when changing position fails or position wasn't changed, a combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE otherwise
 */
unsigned int update_iterator_ChooseMirrorAndFile( update_iterator_t *p_uit,
                                        int i_m, int i_r, int i_f )
{
    unsigned int i_val = 0;

    if( !p_uit ) return 0;
    vlc_mutex_lock( &p_uit->p_u->lock );

    if( i_m >= 0 )
    {
        if( i_m < p_uit->p_u->i_mirrors )
        {
            if( i_m != p_uit->i_m )
                i_val |= UPDATE_MIRROR;
            p_uit->i_m = i_m;
        }
        else i_m = -1;
    }

    if( i_r >= 0 )
    {
        if( i_r < p_uit->p_u->i_releases )
        {
            if( i_r != p_uit->i_r )
                i_val |= UPDATE_FILE;
            p_uit->i_r = i_r;
        }
        else i_r = -1;
    }

    if( i_f >= 0 )
    {
        if( i_r >= 0 && i_r < p_uit->p_u->i_releases
            && i_f < p_uit->p_u->p_releases[p_uit->i_r].i_files )
        {
            if( i_f != p_uit->i_f )
                i_val |= UPDATE_FILE;
            p_uit->i_f = i_f;
        }
        else i_f = -1;
    }

    update_iterator_GetData( p_uit );
    vlc_mutex_unlock( &p_uit->p_u->lock );

    if(    ( i_m < 0 || p_uit->i_m >= 0 )
        && ( i_r < 0 || p_uit->i_r >= 0 )
        && ( i_f < 0 || p_uit->i_f >= 0 ) )
    {
        /* Everything worked */
        return UPDATE_SUCCESS|i_val;
    }
    else
    {
        /* Something failed */
        return UPDATE_FAIL;
    }
}

/**
 * Fills the iterator data (file, release and mirror structs)
 * The update struct should be locked before calling this function.
 *
 * \param p_uit update iterator
 * \return nothing
 */
void update_iterator_GetData( update_iterator_t *p_uit )
{
    struct update_release_t *p_r = NULL;
    struct update_file_t *p_f = NULL;
    struct update_mirror_t *p_m = NULL;

    update_iterator_ClearData( p_uit );

    if( p_uit->i_m >= 0 )
    {
        p_m = p_uit->p_u->p_mirrors + p_uit->i_m;
        p_uit->mirror.psz_name = STRDUP( p_m->psz_name );
        p_uit->mirror.psz_location = STRDUP( p_m->psz_location );
        p_uit->mirror.psz_type = STRDUP( p_m->psz_type );
    }

    if( p_uit->i_r >= 0 )
    {
        p_r = p_uit->p_u->p_releases + p_uit->i_r;
        asprintf( &p_uit->release.psz_version, "%s.%s.%s-%s",
                                              p_r->psz_major,
                                              p_r->psz_minor,
                                              p_r->psz_revision,
                                              p_r->psz_extra );
        p_uit->release.psz_svn_revision = STRDUP( p_r->psz_svn_revision );
        p_uit->release.i_type = p_r->i_type;
        p_uit->release.i_status = p_r->i_status;
        if( p_uit->i_f >= 0 )
        {
            p_f = p_r->p_files + p_uit->i_f;
            p_uit->file.i_type = p_f->i_type;
            p_uit->file.psz_md5 = STRDUP( p_f->psz_md5 );
            p_uit->file.l_size = p_f->l_size;
            p_uit->file.psz_description = STRDUP( p_f->psz_description);
            if( p_f->psz_url[0] == '/' )
            {
                if( p_m )
                {
                    asprintf( &p_uit->file.psz_url, "%s%s",
                              p_m->psz_base_url, p_f->psz_url );
                }
            }
            else
            {
                p_uit->file.psz_url = STRDUP( p_f->psz_url );
            }
        }
    }
}

/**
 * Clears the iterator data (file, release and mirror structs)
 *
 * \param p_uit update iterator
 * \return nothing
 */
void update_iterator_ClearData( update_iterator_t *p_uit )
{
    p_uit->file.i_type = UPDATE_FILE_TYPE_NONE;
    FREE( p_uit->file.psz_md5 );
    p_uit->file.l_size = 0;
    FREE( p_uit->file.psz_description );
    FREE( p_uit->file.psz_url );
    FREE( p_uit->release.psz_version );
    FREE( p_uit->release.psz_svn_revision );
    p_uit->release.i_type = UPDATE_RELEASE_TYPE_UNSTABLE;
    p_uit->release.i_status = UPDATE_RELEASE_STATUS_NONE;
    FREE( p_uit->mirror.psz_name );
    FREE( p_uit->mirror.psz_location );
    FREE( p_uit->mirror.psz_type );
}

/**
 * Perform an action on the update iterator
 * Only the first matching action is performed.
 *
 * \param p_uit update iterator
 * \param i_action update action bitmask. can be a combination of UPDATE_NEXT, UPDATE_PREV, UPDATE_MIRROR, UPDATE_RELEASE, UPDATE_FILE, UPDATE_RESET
 * \return UPDATE_FAIL if action fails, UPDATE_SUCCESS|(combination of UPDATE_MIRROR, UPDATE_RELEASE and UPDATE_FILE if these changed) otherwise
 */
unsigned int update_iterator_Action( update_iterator_t *p_uit, int i_action )
{
    if( i_action & UPDATE_RESET )
    {
        return update_iterator_Reset( p_uit );
    }
    else
    if( i_action & UPDATE_MIRROR )
    {
        if( i_action & UPDATE_PREV )
        {
            return update_iterator_PrevMirror( p_uit );
        }
        else
        {
            return update_iterator_NextMirror( p_uit );
        }
    }
    /*else if( i_action & UPDATE_RELEASE )
    {
        if( i_action & UPDATE_PREV )
        {
            return update_iterator_PrevRelease( p_uit );
        }
        else
        {
            return update_iterator_NextRelease( p_uit );
        }
    }*/
    else if( i_action & UPDATE_FILE )
    {
        if( i_action & UPDATE_PREV )
        {
            return update_iterator_PrevFile( p_uit );
        }
        else
        {
            return update_iterator_NextFile( p_uit );
        }
    }
    else
    {
        return UPDATE_SUCCESS;
    }
}

/**
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
 * Object to launch download thread in a different object
 */
typedef struct {
    VLC_COMMON_MEMBERS
    char *psz_dest;     //< Download destination
    char *psz_src;      //< Download source
    char *psz_status;   //< Download status displayed in progress dialog
} download_thread_t;

void update_download_for_real( download_thread_t *p_this );

/**
 * Download the file selected by the update iterator. This function will
 * launch the download in a new thread (downloads can be long)
1200
1201
1202
1203
1204
1205
1206
 *
 * \param p_uit update iterator
 * \param psz_dest destination file path
 * \return nothing
 */
void update_download( update_iterator_t *p_uit, char *psz_dest )
{
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
    download_thread_t *p_dt =
        vlc_object_create( p_uit->p_u->p_vlc, sizeof( download_thread_t ) );

    p_dt->psz_dest = strdup( psz_dest );
    p_dt->psz_src = strdup( p_uit->file.psz_url );
    asprintf( &p_dt->psz_status, "%s - %s (%s)\nSource: %s\nDestination: %s",
              p_uit->file.psz_description, p_uit->release.psz_version,
              p_uit->release.psz_svn_revision, p_uit->file.psz_url,
              psz_dest);

    vlc_thread_create( p_dt, "download thread", update_download_for_real,
                       VLC_THREAD_PRIORITY_LOW, VLC_FALSE );
}

1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
/**
 * Convert a long int size in bytes to a string
 *
 * \param l_size the size in bytes
 * \return the size as a string
 */
char *size_str( long int l_size )
{
    char *psz_tmp;
    if( l_size>> 30 )
        asprintf( &psz_tmp, "%.1f GB", (float)l_size/(1<<30) );
    if( l_size >> 20 )
        asprintf( &psz_tmp, "%.1f MB", (float)l_size/(1<<20) );
    else if( l_size >> 10 )
        asprintf( &psz_tmp, "%.1f kB", (float)l_size/(1<<10) );
    else
        asprintf( &psz_tmp, "%ld B", l_size );
    return psz_tmp;
}

1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
/**
 * The true download function.
 *
 * \param p_this the download_thread_t object
 * \return nothing
 */
void update_download_for_real( download_thread_t *p_this )
{
    char *psz_dest = p_this->psz_dest;
    char *psz_src = p_this->psz_src;
1251
    stream_t *p_stream;
1252
    vlc_t *p_vlc = p_this->p_vlc;
1253
1254
1255
1256
1257
1258
1259

    FILE *p_file = NULL;
    void *p_buffer;

    char *psz_status;

    int i_progress;
1260
    long int l_size, l_done = 0;
1261

1262
1263
    vlc_thread_ready( p_this );

1264
    asprintf( &psz_status, "%s\nDownloading... 0.0/? %.1f%% done",
1265
1266
              p_this->psz_status, 0.0 );
    i_progress = intf_UserProgress( p_vlc, "Downloading...",
1267
                                    psz_status, 0.0, 0 );
1268

1269
1270
1271
1272
    p_stream = stream_UrlNew( p_vlc, psz_src );
    if( !p_stream )
    {
        msg_Err( p_vlc, "Failed to open %s for reading", psz_src );
1273
        intf_UserFatal( p_vlc, VLC_TRUE, "Error while Downloading...",
1274
                        "VLC failed to open %s for reading.", psz_src );
1275
        intf_UserHide( p_vlc, i_progress );
1276
    }
1277
    else
1278
1279
    {

1280
        p_file = utf8_fopen( psz_dest, "w" );
1281
1282
1283
        if( !p_file )
        {
            msg_Err( p_vlc, "Failed to open %s for writing", psz_dest );
1284
            intf_UserFatal( p_vlc, VLC_TRUE, "Error while Downloading...",
1285
                            "VLC failed to open %s for writing.", psz_dest );
1286
            intf_UserHide( p_vlc, i_progress );
1287
1288
1289
        }
        else
        {
1290
1291
            long int l_read;
            char *psz_s1; char *psz_s2;
1292

1293
            l_size = stream_Size(p_stream);
1294
            p_buffer = (void *)malloc( 1<<10 );
1295

1296
            while( ( l_read = stream_Read( p_stream, p_buffer, 1<<10 ) ) )
1297
1298
1299
            {
                float f_progress;

1300
                fwrite( p_buffer, l_read, 1, p_file );
1301

1302
                l_done += l_read;
1303
                free( psz_status );
1304
1305
1306
1307
1308
1309
1310
                f_progress = 100.0*(float)l_done/(float)l_size;
                psz_s1 = size_str( l_done );
                psz_s2 = size_str( l_size );
                asprintf( &psz_status, "%s\nDownloading... %s/%s (%.1f%%) done",
                           p_this->psz_status, psz_s1, psz_s2, f_progress );
                free( psz_s1 ); free( psz_s2 );

1311
                intf_UserProgressUpdate( p_vlc, i_progress,
1312
                                         psz_status, f_progress, 0 );
1313
1314
1315
1316
1317
1318
            }

            free( p_buffer );
            fclose( p_file );
            stream_Delete( p_stream );

1319
            free( psz_status );
1320
1321
1322
1323
            psz_s2 = size_str( l_size );
            asprintf( &psz_status, "%s\nDone %s (100.00%%)",
                       p_this->psz_status, psz_s2 );
            free( psz_s2 );
1324
            intf_UserProgressUpdate( p_vlc, i_progress, psz_status, 100.0, 0 );
1325
            free( psz_status );
1326
        }
1327
1328
    }

1329
1330
1331
1332
1333
1334
1335
1336
1337
    free( p_this->psz_dest );
    free( p_this->psz_src );
    free( p_this->psz_status );

#ifdef WIN32
    CloseHandle( p_this->thread_id );
#endif

    vlc_object_destroy( p_this );
1338
}