1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24package installer::packagepool;
25
26use Digest::MD5;
27use installer::exiter;
28use installer::globals;
29use installer::logger;
30use installer::pathanalyzer;
31use installer::worker;
32
33######################################################
34# Checking the md5sum of a file
35######################################################
36
37sub get_md5sum
38{
39	my ($filename) = @_;
40
41	open(FILE, "<$filename") or die "ERROR: Can't open $filename for creating file hash";
42	binmode(FILE);
43	my $digest = Digest::MD5->new->addfile(*FILE)->hexdigest;
44	close(FILE);
45
46	return $digest;
47}
48
49####################################################
50# Setting a unique sessionid to identify this
51# packaging process.
52####################################################
53
54sub set_sessionid
55{
56	my $pid = $$;		# process id
57	my $timer = time();	# time
58	$installer::globals::sessionid = $pid . $timer;
59	$installer::globals::sessionidset = 1;
60	my $infoline = "\nPool: Setting session id: $installer::globals::sessionid.\n";
61	push( @installer::globals::logfileinfo, $infoline);
62}
63
64####################################################
65# Setting and creating pool path.
66####################################################
67
68sub set_pool_path
69{
70	$installer::globals::unpackpath =~ s/\Q$installer::globals::separator\E\s*$//;	# removing ending slashes and backslashes
71	$installer::globals::poolpath = $installer::globals::unpackpath . $installer::globals::separator . "pool_" . $installer::globals::packageformat;
72	installer::systemactions::create_directory($installer::globals::poolpath);
73	$installer::globals::poolpathset = 1;
74}
75
76####################################################
77# Comparing the content of two epm files.
78####################################################
79
80sub compare_epm_content
81{
82	my ($oldcontent, $newcontent) = @_;
83
84	my $identical = 1;
85	my $diffinfo = "";
86
87	# Removing empty lines and files from $newcontent
88
89	my @newlocalcontent = ();
90	for ( my $i = 0; $i <= $#{$newcontent}; $i++ )
91	{
92		if ( ${$newcontent}[$i] =~ /^\s*$/ ) { next; } # Removing empty lines from $newcontent. Empty lines are also not included into pcf file, from where $oldcontent was read.
93		if ( ${$newcontent}[$i] =~ /^\s*f\s+/ ) { next; } # Ignoring files, they can contain temporary pathes
94		if (( ${$newcontent}[$i] =~ /^\s*%readme\s+/ ) || ( ${$newcontent}[$i] =~ /^\s*%license\s+/ )) { next; } # ignoring license and readme (language specific!)
95		my $oneline = ${$newcontent}[$i];
96		$oneline =~ s/\s*$//; # Removing line ends. Also not included in old epm file, that is read from pcf file.
97		push(@newlocalcontent, $oneline);
98	}
99
100	my $oldmember = $#{$oldcontent} + 1;
101	my $newmember = $#newlocalcontent + 1;
102
103	# comparing the count
104	if ( $oldmember != $newmember )
105	{
106		$identical = 0;
107		installer::logger::print_message("\n...... changed length of EPM file\n");
108		$diffinfo = "Pool: EPM, different line count: old epm file: $oldmember, new epm file: $newmember\n";
109		push(@installer::globals::epmdifflist, $diffinfo);
110	}
111
112	# comparing the content line for line, so the order must not change
113
114	if ( $identical )
115	{
116		for ( my $i = 0; $i <= $#{$oldcontent}; $i++ )
117		{
118			if ( ${$oldcontent}[$i] ne $newlocalcontent[$i] )
119			{
120				$identical = 0;
121				my $line = $i + 1;
122				installer::logger::print_message("\n...... different content in EPM file\n");
123				$diffinfo = "Pool: EPM, line $line changed from \"${$oldcontent}[$i]\" to \"$newlocalcontent[$i]\".\n";
124				push(@installer::globals::epmdifflist, $diffinfo);
125				last;
126			}
127		}
128	}
129
130	return $identical;
131}
132
133####################################################
134# Comparing the content of two pcf files.
135####################################################
136
137sub compare_package_content
138{
139	my ($oldcontent, $newcontent) = @_;
140
141	my $identical = 1;
142	my $infoline = "";
143
144	my $oldmember = scalar keys %{$oldcontent};
145	my $newmember = scalar keys %{$newcontent};
146
147	# comparing the count
148
149	if ( $oldmember != $newmember )
150	{
151		# Logging the difference
152		$identical = 0;
153		installer::logger::print_message("\n...... different number of files in packages. New number: $newmember, old number: $oldmember\n");
154		$infoline = "Different number of files in packages. New number: $newmember, old number: $oldmember\n";
155		push(@installer::globals::pcfdiffcomment, $infoline);
156	}
157
158	# comparing the keys
159
160	if ( $identical )
161	{
162		my $first = 1;
163		my $start = "\n";
164		foreach my $dest ( keys %{$newcontent} )
165		{
166			if ( ! exists($oldcontent->{$dest}) )
167			{
168				$identical = 0;
169				installer::logger::print_message("$start...... file only in one package (A): $dest\n");
170				$infoline = "File only in existing pool package: $dest\n";
171				push(@installer::globals::pcfdiffcomment, $infoline);
172				if ( $first ) { $start = ""; }
173				$first = 0;
174			}
175		}
176
177		# collecting all differences
178		if ( ! $identical )
179		{
180			foreach my $dest ( keys %{$oldcontent} )
181			{
182				if ( ! exists($newcontent->{$dest}) )
183				{
184					$identical = 0;
185					installer::logger::print_message("$start...... file only in one package (B): $dest\n");
186					$infoline = "File only in new package: $dest\n";
187					push(@installer::globals::pcfdiffcomment, $infoline);
188					if ( $first ) { $start = ""; }
189					$first = 0;
190				}
191			}
192		}
193	}
194
195	# comparing the checksum
196
197	if ( $identical )
198	{
199		my $first = 1;
200
201		foreach my $dest ( keys %{$newcontent} )
202		{
203			if ( $newcontent->{$dest}->{'md5sum'} ne $oldcontent->{$dest}->{'md5sum'} )
204			{
205				$identical = 0;
206				if ( $first == 1 )
207				{
208					installer::logger::print_message("\n");
209					$first = 0;
210				}
211				$installer::globals::pcfdifflist{$dest} = 1;
212				installer::logger::print_message("...... different file: $dest\n");
213				# last;
214			}
215
216			if ( $installer::globals::iswindowsbuild )
217			{
218				if ( $newcontent->{$dest}->{'uniquename'} ne $oldcontent->{$dest}->{'uniquename'} )
219				{
220					$identical = 0;
221					$installer::globals::pcfdifflist{$dest} = 1;
222					installer::logger::print_message("\n...... different file: $dest");
223					# last;
224				}
225			}
226		}
227	}
228
229	return $identical;
230}
231
232####################################################
233# Calculating content of pcf file.
234####################################################
235
236sub calculate_current_content
237{
238	my ($filesarray, $packagename) = @_;
239
240	installer::logger::include_timestamp_into_logfile("\nCalculating content for package content file ($packagename), start");
241
242	my %globalcontent = ();
243
244	for ( my $i = 0; $i <= $#{$filesarray}; $i++ )
245	{
246		my %onefilehash = ();
247
248		my $onefile = ${$filesarray}[$i];
249		if ( ! $onefile->{'sourcepath'} ) { installer::exiter::exit_program("ERROR: No sourcepath found for file $onefile->{'gid'}", "calculate_current_content");  }
250		my $source = $onefile->{'sourcepath'};
251		if ( $onefile->{'zipfilesource'} ) { $source = $onefile->{'zipfilesource'}; }
252		if ( ! -f $source ) { installer::exiter::exit_program("ERROR: Sourcefile not found: $source ($onefile->{'gid'})", "calculate_current_content"); }
253
254		# For Windows the unique name inside the cabinet file also has to be saved
255		my $uniquename = "";
256		if ( $installer::globals::iswindowsbuild ) { $uniquename = $onefile->{'uniquename'};}
257
258		my $destination = $onefile->{'destination'};
259		my $checksum = get_md5sum($source);
260
261		$onefilehash{'md5sum'} = $checksum;
262		$onefilehash{'uniquename'} = $uniquename;
263
264		if ( exists($globalcontent{$destination}) ) { installer::exiter::exit_program("ERROR: Destination not unique: $destination ($onefile->{'gid'})", "calculate_current_content"); }
265		$globalcontent{$destination} = \%onefilehash;
266	}
267
268	installer::logger::include_timestamp_into_logfile("\nCalculating content for package content file ($packagename), start");
269
270	return \%globalcontent;
271}
272
273####################################################
274# Writing pcf file.
275####################################################
276
277sub create_pcfcontent_file
278{
279	my ($realpackagename, $md5sum, $filesize, $fullpackagename, $pkgversion, $epmfilecontent, $pcffilename) = @_;
280
281	my @content = ();
282	my $oneline = "PackageName: $realpackagename\n";
283	push(@content, $oneline);
284
285	$oneline = "md5sum: $md5sum\n";
286	push(@content, $oneline);
287
288	$oneline = "FileSize: $filesize\n";
289	push(@content, $oneline);
290
291	$oneline = "FullPackageName: $fullpackagename\n";
292	push(@content, $oneline);
293
294	$oneline = "PkgVersion: $pkgversion\n";
295	push(@content, $oneline);
296
297	foreach my $dest (keys %{$installer::globals::newpcfcontent} )
298	{
299		$oneline = "Files:\t$dest\t$installer::globals::newpcfcontent->{$dest}->{'md5sum'}\t$installer::globals::newpcfcontent->{$dest}->{'uniquename'}\n";
300		push(@content, $oneline);
301	}
302
303	for ( my $i = 0; $i <= $#{$epmfilecontent}; $i++ )
304	{
305		if ( ${$epmfilecontent}[$i] =~ /^\s*$/ ) { next; } # avoiding empty lines
306		if ( ${$epmfilecontent}[$i] =~ /^\s*f\s+/ ) { next; } # ignoring files, because they can contain temporary pathes
307		if (( ${$epmfilecontent}[$i] =~ /^\s*%readme\s+/ ) || ( ${$epmfilecontent}[$i] =~ /^\s*%license\s+/ )) { next; } # ignoring license and readme (language specific!)
308		$oneline = "EPM:\t${$epmfilecontent}[$i]";
309		push(@content, $oneline);
310	}
311
312	installer::files::save_file($pcffilename, \@content);
313}
314
315#######################################################
316# Reading the content of the package content file.
317#######################################################
318
319sub read_pcf_content
320{
321	my ($pcffilename) = @_;
322
323	my %allcontent = ();
324	my @epmfile = ();
325	my $realpackagename = "";
326
327	my $content = installer::files::read_file($pcffilename);
328
329	for ( my $i = 0; $i <= $#{$content}; $i++ )
330	{
331		my $line = ${$content}[$i];
332
333		if ( $line =~ /^\s*PackageName\:\s*(.*?)\s*$/ )
334		{
335			$realpackagename = $1;
336			$installer::globals::xpdpackageinfo{'RealPackageName'} = $realpackagename;
337			next;
338		}
339
340		if ( $line =~ /^\s*FullPackageName\:\s*(.*?)\s*$/ )
341		{
342			$installer::globals::xpdpackageinfo{'FullPackageName'} = $1;
343			next;
344		}
345
346		if ( $line =~ /^\s*FileSize\:\s*(.*?)\s*$/ )
347		{
348			$installer::globals::xpdpackageinfo{'FileSize'} = $1;
349			next;
350		}
351
352		if ( $line =~ /^\s*PkgVersion\:\s*(.*?)\s*$/ )
353		{
354			$installer::globals::xpdpackageinfo{'PkgVersion'} = $1;
355			next;
356		}
357
358		if ( $line =~ /^\s*md5sum\:\s*(.*?)\s*$/ )
359		{
360			$installer::globals::xpdpackageinfo{'md5sum'} = $1;
361			next;
362		}
363
364		if ( $line =~ /^\s*Files:\t(.+?)\t(.+?)\t(.*?)\s*$/ )
365		{
366			my $destination = $1;
367			my $checksum = $2;
368			my $uniquename = $3;
369
370			my %onefilehash = ();
371			$onefilehash{'md5sum'} = $checksum;
372			$onefilehash{'uniquename'} = $uniquename;
373
374			$allcontent{$destination} = \%onefilehash;
375			next;
376		}
377
378		if ( $line =~ /^\s*EPM:\t(.*?)\s*$/ )	 # A line can be empty in epm file
379		{
380			my $epmcontent = $1;
381			push(@epmfile, $epmcontent);
382			next;
383		}
384	}
385
386	if ( $realpackagename eq "" ) { installer::exiter::exit_program("ERROR: Real package name not found in pcf file: \"$pcffilename\"", "read_pcf_content"); }
387
388	return ($realpackagename, \%allcontent, \@epmfile);
389}
390
391####################################################
392# Checking, if a specific package can be
393# created at the moment.
394####################################################
395
396sub check_package_availability
397{
398	my ($packagename) = @_;
399
400	my $package_is_available = 1;
401
402	my $checkfilename = $installer::globals::poolpath . $installer::globals::separator . $packagename . ".pcf.check";
403	my $lockfilename = $installer::globals::poolpath . $installer::globals::separator . $packagename . ".pcf.lock";
404
405	if (( -f $checkfilename ) || ( -f $lockfilename )) { $package_is_available = 0; }
406
407	return $package_is_available;
408}
409
410####################################################
411# Check, if the existence of the check or lock
412# file requires an exit of packaging process.
413####################################################
414
415sub check_pool_exit
416{
417	my ( $lockfilename, $timecounter ) = @_;
418
419	# How old is this lock file?
420	my $timeage = installer::logger::get_file_age($lockfilename);
421
422	# if ( $timeage > 1800 ) # file is older than half an hour
423	if ( $timeage > 3600 ) # file is older than an hour
424	{
425		my $timestring = installer::logger::convert_timestring($timeage);
426		my $infoline = "\nPool: Attention: \"$lockfilename\" is too old ($timestring). Removing file!\n";
427		installer::logger::print_message( "... $infoline" );
428		push( @installer::globals::logfileinfo, $infoline);
429		unlink $lockfilename;
430		# installer::exiter::exit_program("ERROR: Waiting too long for removal of lock file \"$lockfilename\"", "check_pool_exit (packagepool)");
431	}
432	else
433	{
434		my $filecontent = installer::files::read_file($lockfilename);
435		my $waittime = $timecounter * 10;
436		$waittime = installer::logger::convert_timestring($waittime);
437		my $infoline = "\nPool: Warning: \"$lockfilename\" blocks this process for $waittime. Lock content: \"${$filecontent}[0]\"\n";
438		installer::logger::print_message( "... $infoline" );
439		push( @installer::globals::logfileinfo, $infoline);
440	}
441}
442
443############################################################################
444# This function logs some information, that can be used to find
445# pool problems.
446############################################################################
447
448sub log_pool_info
449{
450	my ( $file_exists ) = @_;
451
452	my $infoline = "";
453
454	# Content saved in
455	# $installer::globals::savelockfilecontent = installer::files::read_file($filename);
456	# $installer::globals::savelockfilename = $filename;
457
458	if ( $file_exists )
459	{
460		$infoline = "\nPool Problem: Lock file \"$installer::globals::savelockfilename\" belongs to another process. This process has session id: $installer::globals::sessionid .\n";
461		push( @installer::globals::logfileinfo, $infoline);
462		$infoline = "Content of Lock file:\n";
463		push( @installer::globals::logfileinfo, $infoline);
464		foreach my $line ( @{$installer::globals::savelockfilecontent} ) { push( @installer::globals::logfileinfo, $line); }
465	}
466	else
467	{
468		$infoline = "\nPool Problem: Lock file \"$installer::globals::savelockfilename\" does not exist anymore (this process has session id: $installer::globals::sessionid) .\n";
469		push( @installer::globals::logfileinfo, $infoline);
470	}
471}
472
473############################################################################
474# Checking, if this process is the owner of the lock file in the pool.
475# This can be determined by the Process ID, that is written at the
476# beginning of the first line into the lock file.
477############################################################################
478
479sub process_is_owner
480{
481	my ( $filename ) = @_;
482
483	my $process_is_owner = 0;
484
485	$installer::globals::savelockfilecontent = installer::files::read_file($filename);
486	$installer::globals::savelockfilename = $filename;
487
488	if ( ${$installer::globals::savelockfilecontent}[0] =~ /^\s*\Q$installer::globals::sessionid\E\s+/ ) { $process_is_owner = 1; }
489
490	return $process_is_owner;
491}
492
493####################################################
494# Removing a package from installation set, if
495# there were pooling problems.
496####################################################
497
498sub remove_package_from_installset
499{
500	my ($newpackagepath) = @_;
501
502	my $infoline = "Pool problem: Removing package \"$newpackagepath\" from installation set!\n";
503	push(@installer::globals::logfileinfo, $infoline);
504
505	if ( -f $newpackagepath ) { unlink $newpackagepath; }
506	if ( -d $newpackagepath ) { installer::systemactions::remove_complete_directory($newpackagepath, 1); }
507
508	# Keeping the content of @installer::globals::installsetcontent up to date. Removing the last package.
509	pop(@installer::globals::installsetcontent);
510}
511
512####################################################
513# Check, if the package is in the pool and if
514# there are no changes in the package.
515####################################################
516
517sub package_is_up_to_date
518{
519	my ($allvariables, $onepackage, $packagename, $newepmcontent, $filesinpackage, $installdir, $subdir, $languagestringref) = @_;
520
521	installer::logger::print_message_without_newline( "... checking pool package $packagename ..." );
522
523	installer::logger::include_header_into_logfile("Checking package in pool: $packagename");
524
525	if ( ! $installer::globals::poolpathset ) { installer::packagepool::set_pool_path(); }
526	if ( ! $installer::globals::sessionidset ) { installer::packagepool::set_sessionid(); }
527
528	my $infoline = "";
529	# Resetting some variables for this package
530	my $package_is_up_to_date = 0;
531	my $realpackagename = "";
532	my $oldepmcontent = "";
533	my $waited_for_check = 0;
534	my $waited_for_lock = 0;
535	$installer::globals::newpcfcontentcalculated = 0;
536	%installer::globals::pcfdifflist = ();
537	@installer::globals::pcfdiffcomment = ();
538	@installer::globals::epmdifflist = ();
539
540	# Reading the package content file, if this file exists (extension *.pcf)
541	my $filename = $installer::globals::poolpath . $installer::globals::separator . $packagename . ".pcf";
542	my $checkfilename = $installer::globals::poolpath . $installer::globals::separator . $packagename . ".pcf.check";
543	my $lockfilename = $installer::globals::poolpath . $installer::globals::separator . $packagename . ".pcf.lock";
544	# Saving name in global variable, so that this file can be removed somewhere else (at the end of "put_content_into_pool").
545	$installer::globals::poolcheckfilename = $checkfilename;
546	$installer::globals::poollockfilename = $lockfilename;
547
548	my @checkfilecontent = ("$installer::globals::sessionid $installer::globals::product $$languagestringref $checkfilename");	# $$ is the process id
549	my @lockfilecontent = ("$installer::globals::sessionid $installer::globals::product $$languagestringref $lockfilename");	# $$ is the process id
550
551	# Waiting, step 1
552	# Checking, if another process checks this package at the moment
553	my $timecounter = 0;
554	while ( -f $checkfilename )
555	{
556		$timecounter++;
557
558		# including an exit to enable creation of other packages
559		if (( $timecounter == 1 ) && ( ! exists($installer::globals::poolshiftedpackages{$packagename}) ))
560		{
561			$package_is_up_to_date = 3;	# repeat this package later
562			return $package_is_up_to_date;
563		}
564
565		$infoline = "Pool: $checkfilename exists. WAITING 10 seconds ($timecounter).\n";
566		if ( $timecounter == 1 ) { installer::logger::print_message( "\n" ); }
567		installer::logger::print_message( "... $infoline" );
568		push( @installer::globals::logfileinfo, $infoline);
569		# if ( $timecounter % 50 == 0 ) { check_pool_exit($checkfilename, $timecounter); }
570		if ( $timecounter % 100 == 0 ) { check_pool_exit($checkfilename, $timecounter); }
571		sleep 10; # process sleeps 10 seconds
572		$waited_for_check = 1;
573	}
574
575	# Creating file, showing that this package is checked at the moment by this process. No other process can reach this.
576	installer::files::save_file($checkfilename, \@checkfilecontent);	# Creating the Lock, to check this package. This blocks all other processes.
577	$installer::globals::processhaspoolcheckfile = 1;
578
579	# Check, if the Lock file creation was really successful
580	if ( ! -f $checkfilename )
581	{
582		$infoline = "Pool problem: Pool lock file \"$checkfilename\" could not be created successfully or was removed by another process (A)!\n";
583		push( @installer::globals::logfileinfo, $infoline);
584		log_pool_info(0);
585		$package_is_up_to_date = 4;	# repeat this package
586		return $package_is_up_to_date;
587	}
588
589	if ( ! process_is_owner($checkfilename) )
590	{
591		$infoline = "Pool problem: Pool lock file \"$checkfilename\" belongs to another process (A)!\n";
592		push( @installer::globals::logfileinfo, $infoline);
593		log_pool_info(1);
594		$package_is_up_to_date = 4;	# repeat this package
595		return $package_is_up_to_date;
596	}
597
598	$infoline = "Pool: Created file: $checkfilename\n";
599	push( @installer::globals::logfileinfo, $infoline);
600	if ( $waited_for_check ) { installer::logger::print_message( "... $infoline" ); }
601
602	# Waiting, step 2
603	# Checking, if another process creates this package at the moment
604	$timecounter = 0;
605	while ( -f $lockfilename )
606	{
607		$timecounter++;
608		$infoline = "Pool: $lockfilename exists. WAITING 10 seconds ($timecounter).\n";
609		if ( $timecounter == 1 ) { installer::logger::print_message( "\n" ); }
610		installer::logger::print_message( "... $infoline" );
611		push( @installer::globals::logfileinfo, $infoline);
612		# if ( $timecounter % 50 == 0 ) { check_pool_exit($lockfilename, $timecounter); }
613		if ( $timecounter % 100 == 0 ) { check_pool_exit($lockfilename, $timecounter); }
614		sleep 10; # process sleeps 10 seconds
615		$waited_for_lock = 1;
616	}
617
618	# No lock file exists, therefore no process creates this package at the moment. Check can be done now.
619	if ( $waited_for_lock ) { installer::logger::print_message( "... Pool: Proceeding, $lockfilename was removed.\n" ); }
620
621	my $package_already_exists = 0;
622
623	if ( -f $filename )
624	{
625		# Calculating content for pcf file
626		$installer::globals::newpcfcontent = calculate_current_content($filesinpackage, $packagename);
627		$installer::globals::newpcfcontentcalculated = 1;
628
629		# reading the existing pcf file
630		($realpackagename, $oldpcfcontent, $oldepmcontent) = read_pcf_content($filename);
631
632		# First check: Package has to exist in pool (directories on Solaris)
633		my $fullpackage = $installer::globals::poolpath . $installer::globals::separator . $realpackagename;
634		if ( $installer::globals::issolarisbuild ) { $fullpackage = $fullpackage . ".tar"; }
635		if ( -f $fullpackage )
636		{
637			$package_already_exists = 1;
638			# Second check: Only files
639			my $content_is_identical = compare_package_content($oldpcfcontent, $installer::globals::newpcfcontent);
640
641			# Third check for Unix: Changes in the epm file?
642			if (( $content_is_identical ) && ( ! $installer::globals::iswindowsbuild ))
643			{
644				$content_is_identical = compare_epm_content($oldepmcontent, $newepmcontent);
645			}
646
647			if ( $content_is_identical ) { $package_is_up_to_date = 1; }
648		}
649	}
650
651	if ( $package_is_up_to_date )
652	{
653		$infoline = "Pool: $packagename: No new content, using existing package\n";
654		push( @installer::globals::logfileinfo, $infoline);
655		installer::logger::print_message( "... using package from pool\n" );
656	}
657	else
658	{
659		if ( $package_already_exists )
660		{
661			$infoline = "Pool: $packagename: Contains new content, creating new package. Differences:\n";
662			push( @installer::globals::logfileinfo, $infoline);
663			foreach my $dest ( sort keys %installer::globals::pcfdifflist ) { push( @installer::globals::logfileinfo, "$dest\n"); }
664			foreach my $dest ( @installer::globals::pcfdiffcomment ) { push( @installer::globals::logfileinfo, "$dest"); }
665			foreach my $dest ( @installer::globals::epmdifflist ) { push( @installer::globals::logfileinfo, "$dest"); }
666		}
667		else
668		{
669			$infoline = "Pool: $packagename: Does not exist in pool.\n";
670			push( @installer::globals::logfileinfo, $infoline);
671		}
672
673		installer::logger::print_message( "... packaging required\n" );
674		%installer::globals::xpdpackageinfo = (); # reset the filled hash, because the package cannot be used.
675
676		# Creating lock mechanism, so that other processes do not create this package, too.
677		installer::files::save_file($lockfilename, \@lockfilecontent);		# Creating the Lock, to create this package (Lock for check still exists).
678		$installer::globals::processhaspoollockfile = 1;
679
680		# Check if creation of Lock file was really successful
681
682		if ( ! -f $lockfilename )
683		{
684			$infoline = "Pool problem: Pool lock file \"$lockfilename\" could not be created successfully or was removed by another process (D)!\n";
685			push( @installer::globals::logfileinfo, $infoline);
686			log_pool_info(0);
687			$package_is_up_to_date = 4;	# repeat this package
688			return $package_is_up_to_date;
689		}
690
691		if ( ! process_is_owner($lockfilename) )
692		{
693			$infoline = "Pool problem: Pool lock file \"$lockfilename\" belongs to another process (D)!\n";
694			push( @installer::globals::logfileinfo, $infoline);
695			log_pool_info(1);
696			$package_is_up_to_date = 4;	# repeat this package
697			return $package_is_up_to_date;
698		}
699
700		$infoline = "Pool: Created file: $lockfilename\n";
701		push( @installer::globals::logfileinfo, $infoline);
702	}
703
704	my $newpackagepath = "";
705
706	if ( $package_is_up_to_date )
707	{
708		# Before the package is copied into the installation set, it has to be checked, if this process is really the owner of this lock file..
709		# Check, if lock file still exists and if this process is the owner.
710
711		if ( ! -f $checkfilename )
712		{
713			$infoline = "Pool problem: Pool lock file \"$checkfilename\" was removed by another process (B)!\n";
714			push( @installer::globals::logfileinfo, $infoline);
715			log_pool_info(0);
716			$package_is_up_to_date = 4;	# repeat this package
717			return $package_is_up_to_date;
718		}
719
720		if ( ! process_is_owner($checkfilename) )
721		{
722			$infoline = "Pool problem: Pool lock file \"$checkfilename\" belongs to another process (B)!\n";
723			push( @installer::globals::logfileinfo, $infoline);
724			log_pool_info(1);
725			$package_is_up_to_date = 4;	# repeat this package
726			return $package_is_up_to_date;
727		}
728
729		# Copying the package from the pool into the installation set
730		$newpackagepath = copy_package_from_pool($installdir, $subdir, $realpackagename);
731	}
732
733	# Before the lock file in the pool can be removed, it has to be checked, if this process is still the owner of this lock file.
734	# Check, if lock file still exists and if this process is the owner.
735	if ( ! -f $checkfilename )
736	{
737		$infoline = "Pool problem: Pool lock file \"$checkfilename\" was removed by another process (C)!\n";
738		push( @installer::globals::logfileinfo, $infoline);
739		log_pool_info(0);
740
741		# removing new package from installation set
742		if ( $newpackagepath ne "" ) { remove_package_from_installset($newpackagepath); } 	# A file was copied and a problem occured with pooling
743
744		$package_is_up_to_date = 4;	# repeat this package
745		return $package_is_up_to_date;
746	}
747
748	if ( ! process_is_owner($checkfilename) )
749	{
750		$infoline = "Pool problem: Pool lock file \"$checkfilename\" belongs to another process (C)!\n";
751		push( @installer::globals::logfileinfo, $infoline);
752		log_pool_info(1);
753
754		# removing new package from installation set
755		if ( $newpackagepath ne "" ) { remove_package_from_installset($newpackagepath); } 	# A file was copied and a problem occured with pooling
756
757		$package_is_up_to_date = 4;	# repeat this package
758		return $package_is_up_to_date;
759	}
760
761	# Removing the check file, releasing this package for the next process.
762	# The Lock to create this package still exists, if required.
763	unlink $checkfilename;
764	$installer::globals::processhaspoolcheckfile = 0;
765	$infoline = "Pool: Removing file: $checkfilename\n";
766	push( @installer::globals::logfileinfo, $infoline);
767
768	# Last chance before packaging starts, to check, if this process is really still owner
769	# of the packaging lock file. If not, this packaging process can be repeated.
770	if ( $installer::globals::processhaspoollockfile )
771	{
772		if ( ! -f $lockfilename )
773		{
774			$infoline = "Pool problem: Pool lock file \"$lockfilename\" was removed by another process (E)!\n";
775			push( @installer::globals::logfileinfo, $infoline);
776			log_pool_info(0);
777			$package_is_up_to_date = 4;	# repeat this package
778			return $package_is_up_to_date;
779		}
780
781		if ( ! process_is_owner($lockfilename) )
782		{
783			$infoline = "Pool problem: Pool lock file \"$lockfilename\" belongs to another process (E)!\n";
784			push( @installer::globals::logfileinfo, $infoline);
785			log_pool_info(1);
786			$package_is_up_to_date = 4;	# repeat this package
787			return $package_is_up_to_date;
788		}
789	}
790
791	# Collecting log information
792	if ( $package_is_up_to_date == 1 ) { $installer::globals::poolpackages{$packagename} = 1; }
793	if ( $package_is_up_to_date == 0 )
794	{
795		my @packreasons = ();
796		if ( $package_already_exists )
797		{
798			$infoline = "\t\tPool: $packagename: Contains new content, creating new package. Differences:\n";
799			push( @packreasons, $infoline);
800			foreach my $dest ( sort keys %installer::globals::pcfdifflist ) { push( @packreasons, "\t\t$dest\n"); }
801			foreach my $dest ( @installer::globals::pcfdiffcomment ) { push( @packreasons, "\t\t$dest"); }
802			foreach my $dest ( @installer::globals::epmdifflist ) { push( @packreasons, "\t\t$dest"); }
803		}
804		else
805		{
806			$infoline = "\t\tPool: $packagename: Does not exist in pool.\n";
807			push( @packreasons, $infoline);
808		}
809
810		$installer::globals::createpackages{$packagename} = \@packreasons;
811	}
812
813	return $package_is_up_to_date;
814}
815
816###################################################
817# Determine, which package was created newly
818###################################################
819
820sub determine_new_packagename
821{
822	my ( $dir ) = @_;
823
824	my ($newcontent, $allcontent) = installer::systemactions::find_new_content_in_directory($dir, \@installer::globals::installsetcontent);
825	@installer::globals::installsetcontent = ();
826	foreach my $element ( @{$allcontent} ) { push(@installer::globals::installsetcontent, $element); }
827
828	my $newentriesnumber = $#{$newcontent} + 1;
829	if ( $newentriesnumber > 1 )
830	{
831		my $newpackages = "";
832		foreach my $onepackage ( @{$newcontent} ) { $newpackages = $newpackages . " " . $onepackage; }
833		installer::exiter::exit_program("ERROR: More than one new package in directory $dir ($newpackages)", "determine_new_packagename (packagepool)");
834	}
835	elsif ( $newentriesnumber < 1 )
836	{
837		installer::exiter::exit_program("ERROR: No new package in directory $dir", "determine_new_packagename (packagepool)");
838	}
839	my $newpackage = ${$newcontent}[0];
840
841	return $newpackage;
842}
843
844####################################################
845# Including content into the package pool
846####################################################
847
848sub put_content_into_pool
849{
850	my ($packagename, $installdir, $subdir, $filesinpackage, $epmfilecontent) = @_;
851
852	my $infoline = "";
853
854	my $fullinstalldir = $installdir . $installer::globals::separator . $subdir;
855	my $fullrealpackagename = determine_new_packagename($fullinstalldir);
856	my $realpackagename = $fullrealpackagename;
857	installer::pathanalyzer::make_absolute_filename_to_relative_filename(\$realpackagename);
858
859	installer::logger::include_header_into_logfile("Adding content into the package pool: $realpackagename (PackageName: $packagename)");
860
861	# Calculating content for pcf file, if not already done in "package_is_up_to_date"
862	if ( ! $installer::globals::newpcfcontentcalculated )
863	{
864		$installer::globals::newpcfcontent = calculate_current_content($filesinpackage, $packagename);
865		$installer::globals::newpcfcontentcalculated = 1;
866	}
867
868	# Determining md5sum and FileSize for the new package and saving in pcf file
869	my $md5sum = installer::xpdinstaller::get_md5_value($fullrealpackagename);
870	my $filesize = installer::xpdinstaller::get_size_value($fullrealpackagename);
871	my $fullpackagename = installer::xpdinstaller::get_fullpkgname_value($fullrealpackagename);
872	my $pkgversion = installer::xpdinstaller::get_pkgversion_value($fullrealpackagename);
873
874	# Put package content file (pcf) into pool
875	my $pcffilename = $installer::globals::poolpath . $installer::globals::separator . $packagename . ".pcf";
876	create_pcfcontent_file($realpackagename, $md5sum, $filesize, $fullpackagename, $pkgversion, $epmfilecontent, $pcffilename);
877
878	# Creating xpd info
879	$installer::globals::xpdpackageinfo{'FileSize'} = $filesize;
880	$installer::globals::xpdpackageinfo{'FullPackageName'} = $fullpackagename;
881	$installer::globals::xpdpackageinfo{'md5sum'} = $md5sum;
882	$installer::globals::xpdpackageinfo{'RealPackageName'} = $realpackagename;
883	$installer::globals::xpdpackageinfo{'PkgVersion'} = $pkgversion;
884
885	# Put package into pool
886	$infoline = "Pool: Adding package \"$packagename\" into pool.\n";
887	push( @installer::globals::logfileinfo, $infoline);
888
889	# Copying with unique name, containing PID. Only renaming if everything was fine.
890	my $realdestination = "";
891	my $uniquedestination = "";
892	if ( -f $fullrealpackagename )
893	{
894		$realdestination = $installer::globals::poolpath . $installer::globals::separator . $realpackagename;
895		$uniquedestination = $realdestination . "." . $installer::globals::sessionid;
896		installer::systemactions::copy_one_file($fullrealpackagename, $uniquedestination);
897	}
898
899	# Copying Solaris packages (as tar files)
900	if ( -d $fullrealpackagename )
901	{
902		my $tarfilename = $packagename . ".tar";
903		my $fulltarfilename = $fullinstalldir . $installer::globals::separator . $tarfilename;
904		my $size = installer::worker::tar_package($fullinstalldir, $packagename, $tarfilename, $installer::globals::getuidpath);
905		if (( ! -f $fulltarfilename ) || ( ! ( $size > 0 ))) { installer::exiter::exit_program("ERROR: Missing file: $fulltarfilename", "put_content_into_pool"); }
906		$realdestination = $installer::globals::poolpath . $installer::globals::separator . $tarfilename;
907		$uniquedestination = $realdestination . "." . $installer::globals::sessionid;
908		installer::systemactions::copy_one_file($fulltarfilename, $uniquedestination);
909		unlink $fulltarfilename;
910	}
911
912	# Before the new package is renamed in the pool, it has to be checked, if this process still has the lock for this package.
913	# Check, if lock file still exists and if this process is the owner. Otherwise a pool error occured.
914	if ( ! -f $installer::globals::poollockfilename )
915	{
916		unlink $uniquedestination;  # removing file from pool
917		log_pool_info(0);
918		installer::exiter::exit_program("ERROR: Pool lock file \"$installer::globals::poollockfilename\" was removed by another process (F)!", "put_content_into_pool");
919	}
920
921	if ( ! process_is_owner($installer::globals::poollockfilename) )
922	{
923		unlink $uniquedestination;  # removing file from pool
924		log_pool_info(1);
925		installer::exiter::exit_program("ERROR: Pool lock file \"$installer::globals::poollockfilename\" belongs to another process (F)!", "put_content_into_pool");
926	}
927
928	# Renaming the file in the pool (atomic step)
929	rename($uniquedestination, $realdestination);
930
931	$infoline = "Pool: Renamed file: \"$uniquedestination\" to \"$realdestination\".\n";
932	push( @installer::globals::logfileinfo, $infoline);
933
934	# Before the lock file in the pool can be removed, it has to be checked, if this process is still the owner of this lock file.
935	# Check, if lock file still exists and if this process is the owner. Otherwise a pool error occured.
936	if ( ! -f $installer::globals::poollockfilename )
937	{
938		log_pool_info(0);
939		installer::exiter::exit_program("ERROR: Pool lock file \"$installer::globals::poollockfilename\" was removed by another process (G)!", "put_content_into_pool");
940	}
941
942	if ( ! process_is_owner($installer::globals::poollockfilename) )
943	{
944		log_pool_info(1);
945		installer::exiter::exit_program("ERROR: Pool lock file \"$installer::globals::poollockfilename\" belongs to another process (G)!", "put_content_into_pool");
946	}
947
948	# Removing lock file, so that other processes can use this package now
949	unlink $installer::globals::poollockfilename;
950	$installer::globals::processhaspoollockfile = 0;
951	$infoline = "Pool: Removing file: $installer::globals::poollockfilename\n";
952	push( @installer::globals::logfileinfo, $infoline);
953}
954
955###################################################################
956# Copying a package from the pool into the installation set
957###################################################################
958
959sub copy_package_from_pool
960{
961	my ($installdir, $subdir, $packagename) = @_;
962
963	my $infoline = "Pool: Using package \"$packagename\" from pool.\n";
964	push( @installer::globals::logfileinfo, $infoline);
965	my $sourcefile = $installer::globals::poolpath . $installer::globals::separator . $packagename;
966	if ( $installer::globals::issolarisbuild ) { $sourcefile = $sourcefile . ".tar"; }
967	if ( ! -f $sourcefile ) { installer::exiter::exit_program("ERROR: Missing package in package pool: \"$sourcefile\"", "copy_package_from_pool"); }
968	my $destination = $installdir . $installer::globals::separator . $subdir;
969	if ( ! -d $destination ) { installer::systemactions::create_directory($destination); }
970	my $destinationfile = $destination . $installer::globals::separator . $packagename;
971	if ( $installer::globals::issolarisbuild ) { $destinationfile = $destinationfile . ".tar"; }
972	if ( -f $sourcefile ) { installer::systemactions::copy_one_file($sourcefile, $destinationfile); }
973	# Unpacking for Solaris
974	if ( $installer::globals::issolarisbuild )
975	{
976		my $tarfilename = $packagename . ".tar";
977		installer::worker::untar_package($destination, $tarfilename, $installer::globals::getuidpath);
978		unlink $destinationfile;
979		$destinationfile =~ s/.tar\s*$//;
980	}
981
982	# Keeping the content of @installer::globals::installsetcontent up to date (with full pathes):
983	push(@installer::globals::installsetcontent, $destinationfile);
984
985	return $destinationfile;
986}
987
988###################################################################
989# Counting keys in hash
990###################################################################
991
992sub get_count
993{
994	my ( $hashref ) = @_;
995
996	my $counter = 0;
997	foreach my $onekey ( keys %{$hashref} ) { $counter++; }
998	return $counter;
999}
1000
1001###################################################################
1002# Logging some pool information
1003###################################################################
1004
1005sub log_pool_statistics
1006{
1007	my $infoline = "";
1008
1009	installer::logger::include_header_into_logfile("Pool statistics:");
1010
1011	# Info collected in global hashes
1012	# %installer::globals::createpackages
1013	# %installer::globals::poolpackages
1014
1015	my $pool_packages = get_count(\%installer::globals::poolpackages);
1016	my $created_packages = get_count(\%installer::globals::createpackages);
1017
1018	$infoline = "Number of packages from pool: $pool_packages\n";
1019	push( @installer::globals::logfileinfo, $infoline);
1020
1021	foreach my $packagename ( sort keys(%installer::globals::poolpackages) )
1022	{
1023		$infoline = "\t$packagename\n";
1024		push( @installer::globals::logfileinfo, $infoline);
1025	}
1026
1027	$infoline = "\nNumber of packages that were created: $created_packages\n";
1028	push( @installer::globals::logfileinfo, $infoline);
1029
1030	foreach my $packagename ( sort keys(%installer::globals::createpackages) )
1031	{
1032		$infoline = "\t$packagename\n";
1033		push( @installer::globals::logfileinfo, $infoline);
1034		my $reason = $installer::globals::createpackages{$packagename};
1035
1036		for ( my $i = 0; $i <= $#{$reason}; $i++ )
1037		{
1038			$infoline = "${$reason}[$i]";
1039			push( @installer::globals::logfileinfo, $infoline);
1040		}
1041	}
1042}
1043
10441;
1045