cid#1607171 Data race condition
[LibreOffice.git] / solenv / bin / modules / installer / windows / update.pm
blob5d1cfed619e0cc771130bb10067ba2951ca400f9
2 # This file is part of the LibreOffice project.
4 # This Source Code Form is subject to the terms of the Mozilla Public
5 # License, v. 2.0. If a copy of the MPL was not distributed with this
6 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # This file incorporates work covered by the following license notice:
10 # Licensed to the Apache Software Foundation (ASF) under one or more
11 # contributor license agreements. See the NOTICE file distributed
12 # with this work for additional information regarding copyright
13 # ownership. The ASF licenses this file to you under the Apache
14 # License, Version 2.0 (the "License"); you may not use this file
15 # except in compliance with the License. You may obtain a copy of
16 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package installer::windows::update;
21 use strict;
22 use warnings;
24 use installer::converter;
25 use installer::exiter;
26 use installer::files;
27 use installer::globals;
28 use installer::pathanalyzer;
29 use installer::systemactions;
31 #################################################################################
32 # Extracting all tables from an msi database
33 #################################################################################
35 sub extract_all_tables_from_msidatabase
37 my ($fulldatabasepath, $workdir) = @_;
39 my $msidb = "msidb.exe"; # Has to be in the path
40 my $infoline = "";
41 my $systemcall = "";
42 my $returnvalue = "";
43 my $extraslash = ""; # Has to be set for non-ActiveState perl
44 $extraslash = "\\" if ( $^O =~ /cygwin/i );
46 # Export of all tables by using "*"
47 $systemcall = $msidb . " -d " . $fulldatabasepath . " -f " . $workdir . " -e " . $extraslash . "*";
48 # msidb.exe really wants backslashes. (And double escaping because system() expands the string.)
49 $systemcall =~ s/\//\\\\/g;
51 my $systemcall_output = `$systemcall`;
52 my $returnvalue = $? >> 8;
54 $infoline = "Systemcall: $systemcall\n";
55 push( @installer::globals::logfileinfo, $infoline);
57 if ($returnvalue)
59 $infoline = "ERROR: Could not execute $systemcall - returncode: $returnvalue - output: $systemcall_output\n";
60 push( @installer::globals::logfileinfo, $infoline);
61 installer::exiter::exit_program("ERROR: Could not exclude tables from msi database: $fulldatabasepath !", "extract_all_tables_from_msidatabase");
63 else
65 $infoline = "Success: Executed $systemcall successfully!\n";
66 push( @installer::globals::logfileinfo, $infoline);
70 #################################################################################
71 # Collecting the keys from the first line of the idt file
72 #################################################################################
74 sub collect_all_keys
76 my ($line) = @_;
78 my @allkeys = ();
79 my $rownumber = 0;
80 my $onekey = "";
82 while ( $line =~ /^\s*(\S+?)\t(.*)$/ )
84 $onekey = $1;
85 $line = $2;
86 $rownumber++;
87 push(@allkeys, $onekey);
90 # and the last key
92 $onekey = $line;
93 $onekey =~ s/^\s*//g;
94 $onekey =~ s/\s*$//g;
96 $rownumber++;
97 push(@allkeys, $onekey);
99 return (\@allkeys, $rownumber);
102 #################################################################################
103 # Analyzing the content of one line of an idt file
104 #################################################################################
106 sub get_oneline_hash
108 my ($line, $allkeys, $rownumber) = @_;
110 my $counter = 0;
111 my %linehash = ();
113 $line =~ s/^\s*//;
114 $line =~ s/\s*$//;
116 my $value = "";
117 my $onekey = "";
119 while ( $line =~ /^(.*?)\t(.*)$/ )
121 $value = $1;
122 $line = $2;
123 $onekey = ${$allkeys}[$counter];
124 $linehash{$onekey} = $value;
125 $counter++;
128 # the last column
130 $value = $line;
131 $onekey = ${$allkeys}[$counter];
133 $linehash{$onekey} = $value;
135 return \%linehash;
138 #################################################################################
139 # Analyzing the content of an idt file
140 #################################################################################
142 sub analyze_idt_file
144 my ($filecontent) = @_;
146 my %table = ();
147 # keys are written in first line
148 my ($allkeys, $rownumber) = collect_all_keys(${$filecontent}[0]);
150 for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
152 if (( $i == 0 ) || ( $i == 1 ) || ( $i == 2 )) { next; }
154 my $onelinehash = get_oneline_hash(${$filecontent}[$i], $allkeys, $rownumber);
155 my $linekey = $i - 2; # ! : The linenumber is the unique key !? Always decrease by two, because of removed first three lines.
156 $table{$linekey} = $onelinehash;
159 return \%table;
162 #################################################################################
163 # Reading all idt files in a specified directory
164 #################################################################################
166 sub read_all_tables_from_msidatabase
168 my ($workdir) = @_;
170 my %database = ();
172 my $ext = "idt";
174 my $allidtfiles = installer::systemactions::find_file_with_file_extension($ext, $workdir);
176 for ( my $i = 0; $i <= $#{$allidtfiles}; $i++ )
178 my $onefilename = ${$allidtfiles}[$i];
179 my $longonefilename = $workdir . $installer::globals::separator . $onefilename;
180 if ( ! -f $longonefilename ) { installer::exiter::exit_program("ERROR: Could not find idt file: $longonefilename!", "read_all_tables_from_msidatabase"); }
181 my $filecontent = installer::files::read_file($longonefilename);
182 my $idtcontent = analyze_idt_file($filecontent);
183 if ($onefilename eq "Directory.idt") {
184 collect_directories($filecontent, $longonefilename);
186 my $key = $onefilename;
187 $key =~ s/\.idt\s*$//;
188 $database{$key} = $idtcontent;
191 return \%database;
194 #################################################################################
195 # Checking, if this is the correct database.
196 #################################################################################
198 sub correct_database
200 my ($product, $pro, $langs, $languagestringref) = @_;
202 my $correct_database = 0;
204 # Comparing $product with $installer::globals::product and
205 # $pro with $installer::globals::pro and
206 # $langs with $languagestringref
208 my $product_is_good = 0;
210 my $localproduct = $installer::globals::product;
211 if ( $installer::globals::languagepack ) { $localproduct = $localproduct . "LanguagePack"; }
212 elsif ( $installer::globals::helppack ) { $localproduct = $localproduct . "HelpPack"; }
214 if ( $product eq $localproduct ) { $product_is_good = 1; }
216 if ( $product_is_good )
218 my $pro_is_good = 0;
220 if ((( $pro eq "pro" ) && ( $installer::globals::pro )) || (( $pro eq "nonpro" ) && ( ! $installer::globals::pro ))) { $pro_is_good = 1; }
222 if ( $pro_is_good )
224 my $langlisthash = installer::converter::convert_stringlist_into_hash(\$langs, ",");
225 my $langstringhash = installer::converter::convert_stringlist_into_hash($languagestringref, "_");
227 my $not_included = 0;
228 foreach my $onelang ( keys %{$langlisthash} )
230 if ( ! exists($langstringhash->{$onelang}) )
232 $not_included = 1;
233 last;
237 if ( ! $not_included )
239 foreach my $onelanguage ( keys %{$langstringhash} )
241 if ( ! exists($langlisthash->{$onelanguage}) )
243 $not_included = 1;
244 last;
248 if ( ! $not_included ) { $correct_database = 1; }
253 return $correct_database;
256 #################################################################################
257 # Searching for the path to the reference database for this special product.
258 #################################################################################
260 sub get_databasename_from_list
262 my ($filecontent, $languagestringref, $filename) = @_;
264 my $databasepath = "";
266 for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
268 my $line = ${$filecontent}[$i];
269 if ( $line =~ /^\s*$/ ) { next; } # empty line
270 if ( $line =~ /^\s*\#/ ) { next; } # comment line
272 if ( $line =~ /^\s*(.+?)\s*\t+\s*(.+?)\s*\t+\s*(.+?)\s*\t+\s*(.+?)\s*$/ )
274 my $product = $1;
275 my $pro = $2;
276 my $langs = $3;
277 my $path = $4;
279 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"); }
281 if ( correct_database($product, $pro, $langs, $languagestringref) )
283 $databasepath = $path;
284 last;
287 else
289 installer::exiter::exit_program("ERROR: Wrong syntax in file: $filename! Line: \"$line\"", "get_databasename_from_list");
293 return $databasepath;
296 #################################################################################
297 # Reading an existing database completely
298 #################################################################################
300 sub readdatabase
302 my ($allvariables, $languagestringref, $includepatharrayref) = @_;
304 my $database = "";
305 my $infoline = "";
307 if ( ! $allvariables->{'UPDATE_DATABASE_LISTNAME'} ) { installer::exiter::exit_program("ERROR: If \"UPDATE_DATABASE\" is set, \"UPDATE_DATABASE_LISTNAME\" is required.", "Main"); }
308 my $listfilename = $allvariables->{'UPDATE_DATABASE_LISTNAME'};
310 # Searching the list in the include paths
311 my $listname = installer::scriptitems::get_sourcepath_from_filename_and_includepath(\$listfilename, $includepatharrayref, 1);
312 if ( $$listname eq "" ) { installer::exiter::exit_program("ERROR: List file not found: $listfilename !", "readdatabase"); }
313 my $completelistname = $$listname;
315 # Reading list file
316 my $listfile = installer::files::read_file($completelistname);
318 # Get name and path of reference database
319 my $databasename = get_databasename_from_list($listfile, $languagestringref, $completelistname);
321 # If the correct database was not found, this is not necessarily an error. But in this case, this is not an update packaging process!
322 if (( $databasename ) && ( $databasename ne "" )) # This is an update packaging process!
324 $installer::globals::updatedatabase = 1;
325 installer::logger::print_message( "... update process, using database $databasename ...\n" );
326 $infoline = "\nDatabase found in $completelistname: \"$databasename\"\n\n";
327 # Saving in global variable
328 $installer::globals::updatedatabasepath = $databasename;
330 else
332 $infoline = "\nNo database found in $completelistname. This is no update process!\n\n";
334 push( @installer::globals::logfileinfo, $infoline);
336 if ( $installer::globals::updatedatabase )
338 if ( ! -f $databasename ) { installer::exiter::exit_program("ERROR: Could not find reference database: $databasename!", "readdatabase"); }
340 my $msifilename = $databasename;
341 installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$msifilename);
343 installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase start");
345 # create directory for unpacking
346 my $databasedir = installer::systemactions::create_directories("database", $languagestringref);
348 # copy database
349 my $fulldatabasepath = $databasedir . $installer::globals::separator . $msifilename;
350 installer::systemactions::copy_one_file($databasename, $fulldatabasepath);
352 installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase: before extracting tables");
354 # extract all tables from database
355 extract_all_tables_from_msidatabase($fulldatabasepath, $databasedir);
357 installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase: before reading tables");
359 # read all tables
360 $database = read_all_tables_from_msidatabase($databasedir);
362 # Test output:
364 # foreach my $key1 ( keys %{$database} )
366 # print "Test1: $key1\n";
367 # foreach my $key2 ( keys %{$database->{$key1}} )
369 # print "\tTest2: $key2\n";
370 # foreach my $key3 ( keys %{$database->{$key1}->{$key2}} )
372 # print "\t\tTest3: $key3: $database->{$key1}->{$key2}->{$key3}\n";
377 # Example: File table
379 # my $filetable = $database->{'File'};
380 # foreach my $linenumber ( keys %{$filetable} )
382 # print "Test Filenumber: $linenumber\n";
383 # foreach my $key ( keys %{$filetable->{$linenumber}} )
385 # print "\t\tTest: $key: $filetable->{$linenumber}->{$key}\n";
389 # Example: Searching for ProductCode in table Property
391 # my $column1 = "Property";
392 # my $column2 = "Value";
393 # my $searchkey = "ProductCode";
394 # my $propertytable = $database->{'Property'};
395 # foreach my $linenumber ( keys %{$propertytable} )
397 # if ( $propertytable->{$linenumber}->{$column1} eq $searchkey )
399 # print("Test: $searchkey : $propertytable->{$linenumber}->{$column2}\n");
403 installer::logger::include_timestamp_into_logfile("Performance Info: readdatabase end");
406 return $database;
409 #########################################################################
410 # Reading the file "Directory.idt".
411 #########################################################################
413 sub collect_directories
415 my ($filecontent, $idtfilename) = @_;
417 for ( my $i = 0; $i <= $#{$filecontent}; $i++ )
419 if ( $i <= 2 ) { next; } # ignoring first three lines
420 if ( ${$filecontent}[$i] =~ /^\s*$/ ) { next; } # ignoring empty lines
421 # Format: Directory Directory_Parent DefaultDir
422 if ( ${$filecontent}[$i] =~ /^\s*(.+?)\t(.*?)\t(.*?)\s*$/ )
424 $installer::globals::merge_directory_hash{$1} = 1;
426 else
428 my $linecount = $i + 1;
429 installer::exiter::exit_program("ERROR: Unknown line format in table \"$idtfilename\" (line $linecount) !", "collect_directories");
434 #################################################################################
435 # Files can be included in merge modules. This is also important for update.
436 #################################################################################
438 sub readmergedatabase
440 my ( $mergemodules, $languagestringref, $includepatharrayref ) = @_;
442 installer::logger::include_timestamp_into_logfile("Performance Info: readmergedatabase start");
444 my $mergemoduledir = installer::systemactions::create_directories("mergedatabase", $languagestringref);
446 my %allmergefiles = ();
448 foreach my $mergemodule ( @{$mergemodules} )
450 my $filename = $mergemodule->{'Name'};
451 my $mergefile = $ENV{'MSM_PATH'} . $filename;
453 if ( ! -f $mergefile ) { installer::exiter::exit_program("ERROR: msm file not found: $mergefile !", "readmergedatabase"); }
454 my $completesource = $mergefile;
456 my $mergegid = $mergemodule->{'gid'};
457 my $workdir = $mergemoduledir . $installer::globals::separator . $mergegid;
458 if ( ! -d $workdir ) { installer::systemactions::create_directory($workdir); }
460 my $completedest = $workdir . $installer::globals::separator . $filename;
461 installer::systemactions::copy_one_file($completesource, $completedest);
462 if ( ! -f $completedest ) { installer::exiter::exit_program("ERROR: msm file not found: $completedest !", "readmergedatabase"); }
464 # extract all tables from database
465 extract_all_tables_from_msidatabase($completedest, $workdir);
467 # read all tables
468 my $onemergefile = read_all_tables_from_msidatabase($workdir);
470 $allmergefiles{$mergegid} = $onemergefile;
473 foreach my $mergefilegid ( keys %allmergefiles )
475 my $onemergefile = $allmergefiles{$mergefilegid};
476 my $filetable = $onemergefile->{'File'};
478 foreach my $linenumber ( keys %{$filetable} )
480 # Collecting all files from merge modules in global hash
481 $installer::globals::mergemodulefiles{$filetable->{$linenumber}->{'File'}} = 1;
485 installer::logger::include_timestamp_into_logfile("Performance Info: readmergedatabase end");
488 #################################################################################
489 # Creating several useful hashes from old database
490 #################################################################################
492 sub create_database_hashes
494 my ( $database ) = @_;
496 # 1. Hash ( Component -> UniqueFileName ), required in File table.
497 # Read from File table.
499 my %uniquefilename = ();
500 my %allupdatesequences = ();
501 my %allupdatecomponents = ();
502 my %allupdatefileorder = ();
503 my %allupdatecomponentorder = ();
504 my %revuniquefilename = ();
505 my %revshortfilename = ();
506 my %shortdirname = ();
507 my %componentid = ();
508 my %componentidkeypath = ();
509 my %alloldproperties = ();
510 my %allupdatelastsequences = ();
511 my %allupdatediskids = ();
513 my $filetable = $database->{'File'};
515 foreach my $linenumber ( keys %{$filetable} )
517 my $comp = $filetable->{$linenumber}->{'Component_'};
518 my $uniquename = $filetable->{$linenumber}->{'File'};
519 my $filename = $filetable->{$linenumber}->{'FileName'};
520 my $sequence = $filetable->{$linenumber}->{'Sequence'};
522 my $shortname = "";
523 if ( $filename =~ /^\s*(.*?)\|\s*(.*?)\s*$/ )
525 $shortname = $1;
526 $filename = $2;
529 # unique is the combination of $component and $filename
530 my $key = "$comp/$filename";
532 if ( exists($uniquefilename{$key}) ) { installer::exiter::exit_program("ERROR: Component/FileName \"$key\" is not unique in table \"File\" !", "create_database_hashes"); }
534 my $value = $uniquename;
535 if ( $shortname ne "" ) { $value = "$uniquename;$shortname"; }
536 $uniquefilename{$key} = $value; # saving the unique keys and short names in hash
538 # Saving reverse keys too
539 $revuniquefilename{$uniquename} = $key;
540 if ( $shortname ne "" ) { $revshortfilename{$shortname} = $key; }
542 # Saving Sequences for unique names (and also components)
543 $allupdatesequences{$uniquename} = $sequence;
544 $allupdatecomponents{$uniquename} = $comp;
546 # Saving unique names and components for sequences
547 $allupdatefileorder{$sequence} = $uniquename;
548 $allupdatecomponentorder{$sequence} = $comp;
551 # 2. Hash, required in Directory table.
553 my $dirtable = $database->{'Directory'};
555 foreach my $linenumber ( keys %{$dirtable} )
557 my $dir = $dirtable->{$linenumber}->{'Directory'}; # this is a unique name
558 my $defaultdir = $dirtable->{$linenumber}->{'DefaultDir'};
560 my $shortname = "";
561 if ( $defaultdir =~ /^\s*(.*?)\|\s*(.*?)\s*$/ )
563 $shortname = $1;
564 $shortdirname{$dir} = $shortname; # collecting only the short names
568 # 3. Hash, collecting info from Component table.
569 # ComponentID and KeyPath have to be reused.
571 my $comptable = $database->{'Component'};
573 foreach my $linenumber ( keys %{$comptable} )
575 my $comp = $comptable->{$linenumber}->{'Component'};
576 my $compid = $comptable->{$linenumber}->{'ComponentId'};
577 my $keypath = $comptable->{$linenumber}->{'KeyPath'};
579 $componentid{$comp} = $compid;
580 $componentidkeypath{$comp} = $keypath;
583 # 4. Hash, property table, required for ProductCode and Installlocation.
585 my $proptable = $database->{'Property'};
587 foreach my $linenumber ( keys %{$proptable} )
589 my $prop = $proptable->{$linenumber}->{'Property'};
590 my $value = $proptable->{$linenumber}->{'Value'};
592 $alloldproperties{$prop} = $value;
595 # 5. Media table, getting last sequence
597 my $mediatable = $database->{'Media'};
598 $installer::globals::updatelastsequence = 0;
600 foreach my $linenumber ( keys %{$mediatable} )
602 my $cabname = $mediatable->{$linenumber}->{'Cabinet'};
603 my $lastsequence = $mediatable->{$linenumber}->{'LastSequence'};
604 my $diskid = $mediatable->{$linenumber}->{'DiskId'};
605 $allupdatelastsequences{$cabname} = $lastsequence;
606 $allupdatediskids{$cabname} = $diskid;
608 if ( $lastsequence > $installer::globals::updatelastsequence ) { $installer::globals::updatelastsequence = $lastsequence; }
611 $installer::globals::updatesequencecounter = $installer::globals::updatelastsequence;
613 return (\%uniquefilename, \%revuniquefilename, \%revshortfilename, \%allupdatesequences, \%allupdatecomponents, \%allupdatefileorder, \%allupdatecomponentorder, \%shortdirname, \%componentid, \%componentidkeypath, \%alloldproperties, \%allupdatelastsequences, \%allupdatediskids);