cid#1607171 Data race condition
[LibreOffice.git] / solenv / bin / modules / installer / windows / registry.pm
blob609116afc76c89b8309316b42dcb56b24d372a2f
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::registry;
21 use strict;
22 use warnings;
24 use installer::files;
25 use installer::globals;
26 use installer::worker;
27 use installer::windows::msiglobal;
28 use installer::windows::idtglobal;
30 #####################################################
31 # Generating the component name from a registryitem
32 #####################################################
34 sub get_registry_component_name
36 my ($registryref, $allvariables) = @_;
38 # In this function exists the rule to create components from registryitems
39 # Rule:
40 # The componentname can be directly taken from the ModuleID.
41 # All registryitems belonging to one module can get the same component.
43 my $componentname = "";
44 my $isrootmodule = 0;
46 if ( $registryref->{'ModuleID'} ) { $componentname = $registryref->{'ModuleID'}; }
48 $componentname =~ s/\\/\_/g;
49 $componentname =~ s/\//\_/g;
50 $componentname =~ s/\-/\_/g;
51 $componentname =~ s/\_\s*$//g;
53 $componentname = lc($componentname); # componentnames always lowercase
55 if ( $componentname eq "gid_module_root" ) { $isrootmodule = 1; }
57 # Attention: Maximum length for the componentname is 72
59 # identifying this component as registryitem component
60 $componentname = "registry_" . $componentname;
62 $componentname =~ s/gid_module_/g_m_/g;
63 $componentname =~ s/_optional_/_o_/g;
65 # This componentname must be more specific
66 my $addon = "_";
67 if ( $allvariables->{'PRODUCTNAME'} ) { $addon = $addon . $allvariables->{'PRODUCTNAME'}; }
68 if ( $allvariables->{'PRODUCTVERSION'} ) { $addon = $addon . $allvariables->{'PRODUCTVERSION'}; }
69 $addon = lc($addon);
70 $addon =~ s/ //g;
71 $addon =~ s/-//g;
72 $addon =~ s/\.//g;
74 my $styles = "";
75 if ( $registryref->{'Styles'} ) { $styles = $registryref->{'Styles'}; }
77 # Layer links must have unique Component GUID for all products. This is necessary, because only the
78 # uninstallation of the last product has to delete registry keys.
79 if ( $styles =~ /\bLAYER_REGISTRY\b/ )
81 $componentname = "g_m_root_registry_layer_ooo_reglayer";
82 # Styles USE_URELAYERVERSION, USE_PRODUCTVERSION
83 if ( $styles =~ /\bUSE_URELAYERVERSION\b/ ) { $addon = "_ure_" . $allvariables->{'URELAYERVERSION'}; }
84 if ( $styles =~ /\bUSE_PRODUCTVERSION\b/ ) { $addon = "_basis_" . $allvariables->{'PRODUCTVERSION'}; }
85 $addon =~ s/\.//g;
88 $componentname = $componentname . $addon;
90 if (( $styles =~ /\bLANGUAGEPACK\b/ ) && ( $installer::globals::languagepack )) { $componentname = $componentname . "_lang"; }
91 elsif (( $styles =~ /\bHELPPACK\b/ ) && ( $installer::globals::helppack )) { $componentname = $componentname . "_help"; }
92 if ( $styles =~ /\bALWAYS_REQUIRED\b/ ) { $componentname = $componentname . "_forced"; }
94 # Attention: Maximum length for the componentname is 72
95 # %installer::globals::allregistrycomponents_in_this_database_ : resetted for each database
96 # %installer::globals::allregistrycomponents_ : not resetted for each database
97 # Component strings must be unique for the complete product, because they are used for
98 # the creation of the globally unique identifier.
100 my $fullname = $componentname; # This can be longer than 72
102 if (( exists($installer::globals::allregistrycomponents_{$fullname}) ) && ( ! exists($installer::globals::allregistrycomponents_in_this_database_{$fullname}) ))
104 # This is not allowed: One component cannot be installed with different packages.
105 installer::exiter::exit_program("ERROR: Windows registry component \"$fullname\" is already included into another package. This is not allowed.", "get_registry_component_name");
108 if ( exists($installer::globals::allregistrycomponents_{$fullname}) )
110 $componentname = $installer::globals::allregistrycomponents_{$fullname};
112 else
114 if ( length($componentname) > 70 )
116 $componentname = generate_new_short_registrycomponentname($componentname); # This has to be unique for the complete product, not only one package
119 $installer::globals::allregistrycomponents_{$fullname} = $componentname;
120 $installer::globals::allregistrycomponents_in_this_database_{$fullname} = 1;
123 if ( $isrootmodule ) { $installer::globals::registryrootcomponent = $componentname; }
125 return $componentname;
128 #########################################################
129 # Create a shorter version of a long component name,
130 # because maximum length in msi database is 72.
131 # Attention: In multi msi installation sets, the short
132 # names have to be unique over all packages, because
133 # this string is used to create the globally unique id
134 # -> no resetting of
135 # %installer::globals::allshortregistrycomponents
136 # after a package was created.
137 #########################################################
139 sub generate_new_short_registrycomponentname
141 my ($componentname) = @_;
143 my $startversion = substr($componentname, 0, 60); # taking only the first 60 characters
144 my $subid = installer::windows::msiglobal::calculate_id($componentname, 9); # taking only the first 9 digits
145 my $shortcomponentname = $startversion . "_" . $subid;
147 if ( exists($installer::globals::allshortregistrycomponents{$shortcomponentname}) ) { installer::exiter::exit_program("Failed to create unique component name: \"$shortcomponentname\"", "generate_new_short_registrycomponentname"); }
149 $installer::globals::allshortregistrycomponents{$shortcomponentname} = 1;
151 return $shortcomponentname;
154 ##############################################################
155 # Returning identifier for registry table.
156 ##############################################################
158 sub get_registry_identifier
160 my ($registry) = @_;
162 my $identifier = "";
164 if ( $registry->{'gid'} ) { $identifier = $registry->{'gid'}; }
166 $identifier = lc($identifier); # always lower case
168 # Attention: Maximum length is 72
170 $identifier =~ s/gid_regitem_/g_r_/;
171 $identifier =~ s/_soffice_/_s_/;
172 $identifier =~ s/_clsid_/_c_/;
173 $identifier =~ s/_currentversion_/_cv_/;
174 $identifier =~ s/_microsoft_/_ms_/;
175 $identifier =~ s/_manufacturer_/_mf_/;
176 $identifier =~ s/_productname_/_pn_/;
177 $identifier =~ s/_productversion_/_pv_/;
178 $identifier =~ s/_staroffice_/_so_/;
179 $identifier =~ s/_software_/_sw_/;
180 $identifier =~ s/_capabilities_/_cap_/;
181 $identifier =~ s/_classpath_/_cp_/;
182 $identifier =~ s/_extension_/_ex_/;
183 $identifier =~ s/_fileassociations_/_fa_/;
184 $identifier =~ s/_propertysheethandlers_/_psh_/;
185 $identifier =~ s/__/_/g;
187 # Saving this in the registry collector
189 $registry->{'uniquename'} = $identifier;
191 return $identifier;
194 ##################################################################
195 # Returning root value for registry table.
196 ##################################################################
198 sub get_registry_root
200 my ($registry) = @_;
202 my $rootvalue = 0; # Default: Parent is KKEY_CLASSES_ROOT
203 my $scproot = "";
205 if ( $registry->{'ParentID'} ) { $scproot = $registry->{'ParentID'}; }
207 if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE" ) { $rootvalue = -1; }
209 if ( $scproot eq "PREDEFINED_HKEY_CLASSES_ROOT" ) { $rootvalue = 0; }
211 if ( $scproot eq "PREDEFINED_HKEY_CURRENT_USER_ONLY" ) { $rootvalue = 1; }
213 if ( $scproot eq "PREDEFINED_HKEY_LOCAL_MACHINE_ONLY" ) { $rootvalue = 2; }
215 return $rootvalue;
218 ##############################################################
219 # Returning key for registry table.
220 ##############################################################
222 sub get_registry_key
224 my ($registry, $allvariableshashref) = @_;
226 my $key = "";
228 if ( $registry->{'Subkey'} ) { $key = $registry->{'Subkey'}; }
230 if ( $key =~ /\%/ ) { $key = installer::worker::replace_variables_in_string($key, $allvariableshashref); }
232 return $key;
235 ##############################################################
236 # Returning name for registry table.
237 ##############################################################
239 sub get_registry_name
241 my ($registry, $allvariableshashref) = @_;
243 my $name = "";
245 if ( $registry->{'Name'} ) { $name = $registry->{'Name'}; }
247 if ( $name =~ /\%/ ) { $name = installer::worker::replace_variables_in_string($name, $allvariableshashref); }
249 return $name;
252 ##############################################################
253 # Returning value for registry table.
254 ##############################################################
256 sub get_registry_value
258 my ($registry, $allvariableshashref) = @_;
260 my $value = "";
262 if ( $registry->{'Value'} ) { $value = $registry->{'Value'}; }
264 $value =~ s/\\\"/\"/g; # no more masquerading of '"'
265 $value =~ s/\\\\\s*$/\\/g; # making "\\" at end of value to "\"
266 $value =~ s/\<progpath\>/\[INSTALLLOCATION\]/;
267 $value =~ s/\[INSTALLLOCATION\]\\/\[INSTALLLOCATION\]/; # removing "\" after "[INSTALLLOCATION]"
269 if ( $value =~ /\%/ ) { $value = installer::worker::replace_variables_in_string($value, $allvariableshashref); }
271 return $value;
274 ##############################################################
275 # Returning component for registry table.
276 ##############################################################
278 sub get_registry_component
280 my ($registry, $allvariables) = @_;
282 # All registry items belonging to one module can
283 # be included into one component
285 my $componentname = get_registry_component_name($registry, $allvariables);
287 # saving componentname in the registryitem collector
289 $registry->{'componentname'} = $componentname;
291 return $componentname;
294 ######################################################
295 # Adding the content of
296 # @installer::globals::userregistrycollector
297 # to the registry table. The content was collected
298 # in create_files_table() in file.pm.
299 ######################################################
301 sub add_userregs_to_registry_table
303 my ( $registrytable, $allvariables ) = @_;
305 for ( my $i = 0; $i <= $#installer::globals::userregistrycollector; $i++ )
307 my $onefile = $installer::globals::userregistrycollector[$i];
309 my %registry = ();
311 $registry{'Registry'} = $onefile->{'userregkeypath'};
312 $registry{'Root'} = "1"; # always HKCU
313 $registry{'Key'} = "Software\\$allvariables->{'MANUFACTURER'}\\$allvariables->{'PRODUCTNAME'} $allvariables->{'PRODUCTVERSION'}\\";
314 if ( $onefile->{'needs_user_registry_key'} ) { $registry{'Key'} = $registry{'Key'} . "StartMenu"; }
315 $registry{'Name'} = $onefile->{'Name'};
316 $registry{'Value'} = "1";
317 $registry{'Component_'} = $onefile->{'componentname'};
319 my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
320 . $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
322 push(@{$registrytable}, $oneline);
326 ######################################################
327 # Creating the file Registry.idt dynamically
328 # Content:
329 # Registry Root Key Name Value Component_
330 ######################################################
332 sub create_registry_table
334 my ($registryref, $allregistrycomponentsref, $basedir, $languagesarrayref, $allvariableshashref) = @_;
336 for ( my $m = 0; $m <= $#{$languagesarrayref}; $m++ )
338 my $onelanguage = ${$languagesarrayref}[$m];
340 my @registrytable = ();
342 installer::windows::idtglobal::write_idt_header(\@registrytable, "registry");
344 for ( my $i = 0; $i <= $#{$registryref}; $i++ )
346 my $oneregistry = ${$registryref}[$i];
348 # Controlling the language!
349 # Only language independent folderitems or folderitems with the correct language
350 # will be included into the table
352 if (! (!(( $oneregistry->{'ismultilingual'} )) || ( $oneregistry->{'specificlanguage'} eq $onelanguage )) ) { next; }
354 my %registry = ();
356 $registry{'Registry'} = get_registry_identifier($oneregistry);
357 $registry{'Root'} = get_registry_root($oneregistry);
358 $registry{'Key'} = get_registry_key($oneregistry, $allvariableshashref);
359 $registry{'Name'} = get_registry_name($oneregistry, $allvariableshashref);
360 $registry{'Value'} = get_registry_value($oneregistry, $allvariableshashref);
361 $registry{'Component_'} = get_registry_component($oneregistry, $allvariableshashref);
363 # Collecting all components
364 if (! grep {$_ eq $registry{'Component_'}} @{$allregistrycomponentsref})
366 push(@{$allregistrycomponentsref}, $registry{'Component_'});
369 my $style = "";
370 if ( $oneregistry->{'Styles'} ) { $style = $oneregistry->{'Styles'}; }
371 # Collecting all registry components with ALWAYS_REQUIRED style
372 if ( ! ( $style =~ /\bALWAYS_REQUIRED\b/ ))
374 # Setting a component condition for unforced registry components!
375 # Only write into registry, if WRITE_REGISTRY is set.
376 if ( $oneregistry->{'ComponentCondition'} ) { $oneregistry->{'ComponentCondition'} = "(" . $oneregistry->{'ComponentCondition'} . ") AND (WRITE_REGISTRY=1)"; }
377 else { $oneregistry->{'ComponentCondition'} = "WRITE_REGISTRY=1"; }
380 # Collecting all component conditions
381 if ( $oneregistry->{'ComponentCondition'} )
383 if ( ! exists($installer::globals::componentcondition{$registry{'Component_'}}))
385 $installer::globals::componentcondition{$registry{'Component_'}} = $oneregistry->{'ComponentCondition'};
389 my $oneline = $registry{'Registry'} . "\t" . $registry{'Root'} . "\t" . $registry{'Key'} . "\t"
390 . $registry{'Name'} . "\t" . $registry{'Value'} . "\t" . $registry{'Component_'} . "\n";
392 push(@registrytable, $oneline);
395 # If there are added user registry keys for files collected in
396 # @installer::globals::userregistrycollector (file.pm), then
397 # this registry keys have to be added now.
399 if ( $installer::globals::addeduserregitrykeys ) { add_userregs_to_registry_table(\@registrytable, $allvariableshashref); }
401 # Saving the file
403 my $registrytablename = $basedir . $installer::globals::separator . "Registry.idt" . "." . $onelanguage;
404 installer::files::save_file($registrytablename ,\@registrytable);
405 my $infoline = "Created idt file: $registrytablename\n";
406 push(@installer::globals::logfileinfo, $infoline);