summaryrefslogtreecommitdiff
path: root/solenv/bin/modules/installer/windows/update.pm
blob: ae6cd4a0693ef8c6772559f8780d121f4a711eca (plain)
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
48
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
405
406
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
#*************************************************************************
#
# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
#
# Copyright 2000, 2010 Oracle and/or its affiliates.
#
# OpenOffice.org - a multi-platform office productivity suite
#
# This file is part of OpenOffice.org.
#
# OpenOffice.org is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License version 3
# only, as published by the Free Software Foundation.
#
# OpenOffice.org 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 Lesser General Public License version 3 for more details
# (a copy is included in the LICENSE file that accompanied this code).
#
# You should have received a copy of the GNU Lesser General Public License
# version 3 along with OpenOffice.org.  If not, see
# <http://www.openoffice.org/license.html>
# for a copy of the LGPLv3 License.
#
#*************************************************************************

package installer::windows::update;

use installer::converter;
use installer::exiter;
use installer::files;
use installer::globals;
use installer::pathanalyzer;
use installer::systemactions;

#################################################################################
# Extracting all tables from an msi database
#################################################################################

sub extract_all_tables_from_msidatabase
{
    my ($fulldatabasepath, $workdir) = @_;

    my $msidb = "msidb.exe";    # Has to be in the path
    my $infoline = "";
    my $systemcall = "";
    my $returnvalue = "";
    my $extraslash = "";        # Has to be set for non-ActiveState perl

    # Export of all tables by using "*"

    if ( $^O =~ /cygwin/i ) {
        # msidb.exe really wants backslashes. (And double escaping because system() expands the string.)
        $fulldatabasepath =~ s/\//\\\\/g;
        $workdir =~ s/\//\\\\/g;
        $extraslash = "\\";
    }
    if ( $^O =~ /linux/i) {
        $extraslash = "\\";
    }

    $systemcall = $msidb . " -d " . $fulldatabasepath . " -f " . $workdir . " -e " . $extraslash . "*";
    $returnvalue = system($systemcall);

    $infoline = "Systemcall: $systemcall\n";
    push( @installer::globals::logfileinfo, $infoline);

    if ($returnvalue)
    {
        $infoline = "ERROR: Could not execute $systemcall !\n";
        push( @installer::globals::logfileinfo, $infoline);
        installer::exiter::exit_program("ERROR: Could not exclude tables from msi database: $fulldatabasepath !", "extract_all_tables_from_msidatabase");
    }
    else
    {
        $infoline = "Success: Executed $systemcall successfully!\n";
        push( @installer::globals::logfileinfo, $infoline);
    }
}

#################################################################################
# Collecting the keys from the first line of the idt file
#################################################################################

sub collect_all_keys
{
    my ($line) = @_;

    my @allkeys = ();
    my $rownumber = 0;
    my $onekey = "";

    while ( $line =~ /^\s*(\S+?)\t(.*)$/ )
    {
        $onekey = $1;
        $line = $2;
        $rownumber++;
        push(@allkeys, $onekey);
    }

    # and the last key

    $onekey = $line;
    $onekey =~ s/^\s*//g;
    $onekey =~ s/\s*$//g;

    $rownumber++;
    push(@allkeys, $onekey);

    return (\@allkeys, $rownumber);
}

#################################################################################
# Analyzing the content of one line of an idt file
#################################################################################

sub get_oneline_hash
{
    my ($line, $allkeys, $rownumber) = @_;

    my $counter = 0;
    my %linehash = ();

    $line =~ s/^\s*//;
    $line =~ s/\s*$//;

    my $value = "";
    my $onekey = "";

    while ( $line =~ /^(.*?)\t(.*)$/ )
    {
        $value = $1;
        $line = $2;
        $onekey = ${$allkeys}[$counter];
        $linehash{$onekey} = $value;
        $counter++;
    }

    # the last column

    $value = $line;
    $onekey = ${$allkeys}[$counter];

    $linehash{$onekey} = $value;

    return \%linehash;
}

#################################################################################
# Analyzing the content of an idt file
#################################################################################

sub analyze_idt_file
{
    my ($filecontent) = @_;

    my %table = ();
    # keys are written in first line
    my ($allkeys, $rownumber) = collect_all_keys(${$filecontent}[0]);

    for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
    {
        if (( $i == 0 ) || ( $i == 1 ) || ( $i == 2 )) { next; }

        my $onelinehash = get_oneline_hash(${$filecontent}[$i], $allkeys, $rownumber);
        my $linekey = $i - 2;  # ! : The linenumber is the unique key !? Always decrease by two, because of removed first three lines.
        $table{$linekey} = $onelinehash;
    }

    return \%table;
}

#################################################################################
# Reading all idt files in a specified directory
#################################################################################

sub read_all_tables_from_msidatabase
{
    my ($workdir) = @_;

    my %database = ();

    my $ext = "idt";

    my $allidtfiles = installer::systemactions::find_file_with_file_extension($ext, $workdir);

    for ( my $i = 0; $i <= $#{$allidtfiles}; $i++ )
    {
        my $onefilename = ${$allidtfiles}[$i];
        my $longonefilename = $workdir . $installer::globals::separator . $onefilename;
        if ( ! -f $longonefilename ) { installer::exiter::exit_program("ERROR: Could not find idt file: $longonefilename!", "read_all_tables_from_msidatabase"); }
        my $filecontent = installer::files::read_file($longonefilename);
        my $idtcontent = analyze_idt_file($filecontent);
        my $key = $onefilename;
        $key =~ s/\.idt\s*$//;
        $database{$key} = $idtcontent;
    }

    return \%database;
}

#################################################################################
# Checking, if this is the correct database.
#################################################################################

sub correct_database
{
    my ($product, $pro, $langs, $languagestringref) = @_;

    my $correct_database = 0;

    # Comparing $product with $installer::globals::product and
    # $pro with $installer::globals::pro and
    # $langs with $languagestringref

    my $product_is_good = 0;

    my $localproduct = $installer::globals::product;
    if ( $installer::globals::languagepack ) { $localproduct = $localproduct . "LanguagePack"; }
    elsif ( $installer::globals::helppack ) { $localproduct = $localproduct . "HelpPack"; }

    if ( $product eq $localproduct ) { $product_is_good = 1; }

    if ( $product_is_good )
    {
        my $pro_is_good = 0;

        if ((( $pro eq "pro" ) && ( $installer::globals::pro )) || (( $pro eq "nonpro" ) && ( ! $installer::globals::pro ))) { $pro_is_good = 1; }

        if ( $pro_is_good )
        {
            my $langlisthash = installer::converter::convert_stringlist_into_hash(\$langs, ",");
            my $langstringhash = installer::converter::convert_stringlist_into_hash($languagestringref, "_");

            my $not_included = 0;
            foreach my $onelang ( keys %{$langlisthash} )
            {
                if ( ! exists($langstringhash->{$onelang}) )
                {
                    $not_included = 1;
                    last;
                }
            }

            if ( ! $not_included )
            {
                foreach my $onelanguage ( keys %{$langstringhash} )
                {
                    if ( ! exists($langlisthash->{$onelanguage}) )
                    {
                        $not_included = 1;
                        last;
                    }
                }

                if ( ! $not_included ) { $correct_database = 1; }
            }
        }
    }

    return $correct_database;
}

#################################################################################
# Searching for the path to the reference database for this special product.
#################################################################################

sub get_databasename_from_list
{
    my ($filecontent, $languagestringref, $filename) = @_;

    my $databasepath = "";

    for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
    {
        my $line = ${$filecontent}[$i];
        if ( $line =~ /^\s*$/ ) { next; } # empty line
        if ( $line =~ /^\s*\#/ ) { next; } # comment line

        if ( $line =~ /^\s*(.+?)\s*\t+\s*(.+?)\s*\t+\s*(.+?)\s*\t+\s*(.+?)\s*$/ )
        {
            my $product = $1;
            my $pro = $2;
            my $langs = $3;
            my $path = $4;

            if (( $pro ne "pro" ) && ( $pro ne "nonpro" )) { installer::exiter::exit_program("ERROR: Wrong syntax in file: $filename. Only \"pro\" or \"nonpro\" allowed in column 1! Line: \"$line\"", "get_databasename_from_list"); }

            if ( correct_database($product, $pro, $langs, $languagestringref) )
            {
                $databasepath = $path;
                last;
            }
        }
        else
        {
            installer::exiter::exit_program("ERROR: Wrong syntax in file: $filename! Line: \"$line\"", "get_databasename_from_list");
        }
    }

    return $databasepath;
}

#################################################################################
# Reading an existing database completely
#################################################################################

sub readdatabase
{
    my ($allvariables, $languagestringref, $includepatharrayref) = @_;

    my $database = "";
    my $infoline = "";

    if ( ! $allvariables->{'UPDATE_DATABASE_LISTNAME'} ) { installer::exiter::exit_program("ERROR: If \"UPDATE_DATABASE\" is set, \"UPDATE_DATABASE_LISTNAME\" is required.", "Main"); }
    my $listfilename = $allvariables->{'UPDATE_DATABASE_LISTNAME'};

    # Searching the list in the include paths
    my $listname = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$listfilename, $includepatharrayref, 1);
    if ( $$listname eq "" ) { installer::exiter::exit_program("ERROR: List file not found: $listfilename !", "readdatabase"); }
    my $completelistname = $$listname;

    # Reading list file
    my $listfile = installer::files::read_file($completelistname);

    # Get name and path of reference database
    my $databasename = get_databasename_from_list($listfile, $languagestringref, $completelistname);

    # If the correct database was not found, this is not necessarily an error. But in this case, this is not an update packaging process!
    if (( $databasename ) && ( $databasename ne "" )) # This is an update packaging process!
    {
        $installer::globals::updatedatabase = 1;
        installer::logger::print_message( "... update process, using database $databasename ...\n" );
        $infoline = "\nDatabase found in $completelistname: \"$databasename\"\n\n";
        # Saving in global variable
        $installer::globals::updatedatabasepath = $databasename;
    }
    else
    {
        $infoline = "\nNo database found in $completelistname. This is no update process!\n\n";
    }
    push( @installer::globals::logfileinfo, $infoline);

    if ( $installer::globals::updatedatabase )
    {
        if ( ! -f $databasename ) { installer::exiter::exit_program("ERROR: Could not find reference database: $databasename!", "readdatabase"); }

        my $msifilename = $databasename;
        installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$msifilename);

        installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase start");

        # create directory for unpacking
        my $databasedir = installer::systemactions::create_directories("database", $languagestringref);

        # copy database
        my $fulldatabasepath = $databasedir . $installer::globals::separator . $msifilename;
        installer::systemactions::copy_one_file($databasename, $fulldatabasepath);

        installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase: before extracting tables");

        # extract all tables from database
        extract_all_tables_from_msidatabase($fulldatabasepath, $databasedir);

        installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase: before reading tables");

        # read all tables
        $database = read_all_tables_from_msidatabase($databasedir);

        # Test output:

        #   foreach my $key1 ( keys %{$database} )
        #   {
        #       print "Test1: $key1\n";
        #       foreach my $key2 ( keys %{$database->{$key1}} )
        #       {
        #           print "\tTest2: $key2\n";
        #           foreach my $key3 ( keys %{$database->{$key1}->{$key2}} )
        #           {
        #               print "\t\tTest3: $key3: $database->{$key1}->{$key2}->{$key3}\n";
        #           }
        #       }
        #   }

        # Example: File table

        # my $filetable = $database->{'File'};
        # foreach my $linenumber ( keys  %{$filetable} )
        # {
        #   print "Test Filenumber: $linenumber\n";
        #   foreach my $key ( keys %{$filetable->{$linenumber}} )
        #   {
        #       print "\t\tTest: $key: $filetable->{$linenumber}->{$key}\n";
        #   }
        # }

        # Example: Searching for ProductCode in table Property

        # my $column1 = "Property";
        # my $column2 = "Value";
        # my $searchkey = "ProductCode";
        # my $propertytable = $database->{'Property'};
        # foreach my $linenumber ( keys  %{$propertytable} )
        # {
        #   if ( $propertytable->{$linenumber}->{$column1} eq $searchkey )
        #   {
        #       print("Test: $searchkey : $propertytable->{$linenumber}->{$column2}\n");
        #   }
        # }

        installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase end");
    }

    return $database;
}

#################################################################################
# Files can be included in merge modules. This is also important for update.
#################################################################################

sub readmergedatabase
{
    my ( $mergemodules, $languagestringref, $includepatharrayref ) = @_;

    installer::logger::include_timestamp_into_logfile("Performance Info: readmergedatabase start");

    my $mergemoduledir = installer::systemactions::create_directories("mergedatabase", $languagestringref);

    my %allmergefiles = ();

    $installer::globals::mergemodulenumber = $#{$mergemodules} + 1;

    foreach my $mergemodule ( @{$mergemodules} )
    {
        my $filename = $mergemodule->{'Name'};
        my $mergefile = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$filename, $includepatharrayref, 1);

        if ( $$mergefile eq "" ) { installer::exiter::exit_program("ERROR: msm file not found: $filename !", "readmergedatabase"); }
        my $completesource = $$mergefile;

        my $mergegid = $mergemodule->{'gid'};
        my $workdir = $mergemoduledir . $installer::globals::separator . $mergegid;
        if ( ! -d $workdir ) { installer::systemactions::create_directory($workdir); }

        my $completedest = $workdir . $installer::globals::separator . $filename;
        installer::systemactions::copy_one_file($completesource, $completedest);
        if ( ! -f $completedest ) { installer::exiter::exit_program("ERROR: msm file not found: $completedest !", "readmergedatabase"); }

        # extract all tables from database
        extract_all_tables_from_msidatabase($completedest, $workdir);

        # read all tables
        my $onemergefile = read_all_tables_from_msidatabase($workdir);

        $allmergefiles{$mergegid} = $onemergefile;
    }

    foreach my $mergefilegid ( keys %allmergefiles )
    {
        my $onemergefile = $allmergefiles{$mergefilegid};
        my $filetable = $onemergefile->{'File'};

        foreach my $linenumber ( keys %{$filetable} )
        {
            # Collecting all files from merge modules in global hash
            $installer::globals::mergemodulefiles{$filetable->{$linenumber}->{'File'}} = 1;
        }
    }

    installer::logger::include_timestamp_into_logfile("Performance Info: readmergedatabase end");
}

#################################################################################
# Creating several useful hashes from old database
#################################################################################

sub create_database_hashes
{
    my ( $database ) = @_;

    # 1. Hash ( Component -> UniqueFileName ), required in File table.
    # Read from File table.

    my %uniquefilename = ();
    my %allupdatesequences = ();
    my %allupdatecomponents = ();
    my %allupdatefileorder = ();
    my %allupdatecomponentorder = ();
    my %revuniquefilename = ();
    my %revshortfilename = ();
    my %shortdirname = ();
    my %componentid = ();
    my %componentidkeypath = ();
    my %alloldproperties = ();
    my %allupdatelastsequences = ();
    my %allupdatediskids = ();

    my $filetable = $database->{'File'};

    foreach my $linenumber ( keys  %{$filetable} )
    {
        my $comp = $filetable->{$linenumber}->{'Component_'};
        my $uniquename = $filetable->{$linenumber}->{'File'};
        my $filename = $filetable->{$linenumber}->{'FileName'};
        my $sequence = $filetable->{$linenumber}->{'Sequence'};

        my $shortname = "";
        if ( $filename =~ /^\s*(.*?)\|\s*(.*?)\s*$/ )
        {
            $shortname = $1;
            $filename = $2;
        }

        # unique is the combination of $component and $filename
        my $key = "$comp/$filename";

        if ( exists($uniquefilename{$key}) ) { installer::exiter::exit_program("ERROR: Component/FileName \"$key\" is not unique in table \"File\" !", "create_database_hashes"); }

        my $value = $uniquename;
        if ( $shortname ne "" ) { $value = "$uniquename;$shortname"; }
        $uniquefilename{$key} = $value; # saving the unique keys and short names in hash

        # Saving reverse keys too
        $revuniquefilename{$uniquename} = $key;
        if ( $shortname ne "" ) { $revshortfilename{$shortname} = $key; }

        # Saving Sequences for unique names (and also components)
        $allupdatesequences{$uniquename} = $sequence;
        $allupdatecomponents{$uniquename} = $comp;

        # Saving unique names and components for sequences
        $allupdatefileorder{$sequence} = $uniquename;
        $allupdatecomponentorder{$sequence} = $comp;
    }

    # 2. Hash, required in Directory table.

    my $dirtable = $database->{'Directory'};

    foreach my $linenumber ( keys  %{$dirtable} )
    {
        my $dir = $dirtable->{$linenumber}->{'Directory'}; # this is a unique name
        my $defaultdir = $dirtable->{$linenumber}->{'DefaultDir'};

        my $shortname = "";
        if ( $defaultdir =~ /^\s*(.*?)\|\s*(.*?)\s*$/ )
        {
            $shortname = $1;
            $shortdirname{$dir} = $shortname;   # collecting only the short names
        }
    }

    # 3. Hash, collecting info from Component table.
    # ComponentID and KeyPath have to be reused.

    my $comptable = $database->{'Component'};

    foreach my $linenumber ( keys  %{$comptable} )
    {
        my $comp = $comptable->{$linenumber}->{'Component'};
        my $compid = $comptable->{$linenumber}->{'ComponentId'};
        my $keypath = $comptable->{$linenumber}->{'KeyPath'};

        $componentid{$comp} = $compid;
        $componentidkeypath{$comp} = $keypath;
    }

    # 4. Hash, property table, required for ProductCode and Installlocation.

    my $proptable = $database->{'Property'};

    foreach my $linenumber ( keys  %{$proptable} )
    {
        my $prop = $proptable->{$linenumber}->{'Property'};
        my $value = $proptable->{$linenumber}->{'Value'};

        $alloldproperties{$prop} = $value;
    }

    # 5. Media table, getting last sequence

    my $mediatable = $database->{'Media'};
    $installer::globals::updatelastsequence = 0;

    foreach my $linenumber ( keys  %{$mediatable} )
    {
        my $cabname = $mediatable->{$linenumber}->{'Cabinet'};
        my $lastsequence = $mediatable->{$linenumber}->{'LastSequence'};
        my $diskid = $mediatable->{$linenumber}->{'DiskId'};
        $allupdatelastsequences{$cabname} = $lastsequence;
        $allupdatediskids{$cabname} = $diskid;

        if ( $lastsequence > $installer::globals::updatelastsequence ) { $installer::globals::updatelastsequence = $lastsequence; }
    }

    $installer::globals::updatesequencecounter = $installer::globals::updatelastsequence;

    return (\%uniquefilename, \%revuniquefilename, \%revshortfilename, \%allupdatesequences, \%allupdatecomponents, \%allupdatefileorder, \%allupdatecomponentorder, \%shortdirname, \%componentid, \%componentidkeypath, \%alloldproperties, \%allupdatelastsequences, \%allupdatediskids);
}


1;