19780544fSAndrew Rist#**************************************************************
29780544fSAndrew Rist#
39780544fSAndrew Rist#  Licensed to the Apache Software Foundation (ASF) under one
49780544fSAndrew Rist#  or more contributor license agreements.  See the NOTICE file
59780544fSAndrew Rist#  distributed with this work for additional information
69780544fSAndrew Rist#  regarding copyright ownership.  The ASF licenses this file
79780544fSAndrew Rist#  to you under the Apache License, Version 2.0 (the
89780544fSAndrew Rist#  "License"); you may not use this file except in compliance
99780544fSAndrew Rist#  with the License.  You may obtain a copy of the License at
109780544fSAndrew Rist#
119780544fSAndrew Rist#    http://www.apache.org/licenses/LICENSE-2.0
129780544fSAndrew Rist#
139780544fSAndrew Rist#  Unless required by applicable law or agreed to in writing,
149780544fSAndrew Rist#  software distributed under the License is distributed on an
159780544fSAndrew Rist#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169780544fSAndrew Rist#  KIND, either express or implied.  See the License for the
179780544fSAndrew Rist#  specific language governing permissions and limitations
189780544fSAndrew Rist#  under the License.
199780544fSAndrew Rist#
209780544fSAndrew Rist#**************************************************************
219780544fSAndrew Rist
229780544fSAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweirpackage installer::files;
25cdf0e10cSrcweir
26cdf0e10cSrcweiruse installer::exiter;
27cdf0e10cSrcweiruse installer::logger;
28cdf0e10cSrcweir
29cdf0e10cSrcweir############################################
30cdf0e10cSrcweir# File Operations
31cdf0e10cSrcweir############################################
32cdf0e10cSrcweir
33cdf0e10cSrcweirsub check_file
34cdf0e10cSrcweir{
35cdf0e10cSrcweir	my ($arg) = @_;
36cdf0e10cSrcweir
37cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::check_file : $arg"); }
38cdf0e10cSrcweir
39cdf0e10cSrcweir	if(!( -f $arg ))
40cdf0e10cSrcweir	{
41cdf0e10cSrcweir		installer::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
42cdf0e10cSrcweir	}
43cdf0e10cSrcweir}
44cdf0e10cSrcweir
45cdf0e10cSrcweirsub read_file
46cdf0e10cSrcweir{
47cdf0e10cSrcweir	my ($localfile) = @_;
48cdf0e10cSrcweir	my @localfile = ();
49cdf0e10cSrcweir
50cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_file : $localfile"); }
51cdf0e10cSrcweir
52cdf0e10cSrcweir	open( IN, "<$localfile" ) || installer::exiter::exit_program("ERROR: Cannot open file $localfile for reading", "read_file");
53cdf0e10cSrcweir
54cdf0e10cSrcweir#	Don't use "my @localfile = <IN>" here, because
55cdf0e10cSrcweir#	perl has a problem with the internal "large_and_huge_malloc" function
56cdf0e10cSrcweir#	when calling perl using MacOS 10.5 with a perl built with MacOS 10.4
57cdf0e10cSrcweir	while ( $line = <IN> ) {
58cdf0e10cSrcweir		push @localfile, $line;
59cdf0e10cSrcweir	}
60cdf0e10cSrcweir
61cdf0e10cSrcweir	close( IN );
62cdf0e10cSrcweir
63cdf0e10cSrcweir	return \@localfile;
64cdf0e10cSrcweir}
65cdf0e10cSrcweir
66cdf0e10cSrcweir###########################################
67cdf0e10cSrcweir# Saving files, arrays and hashes
68cdf0e10cSrcweir###########################################
69cdf0e10cSrcweir
70cdf0e10cSrcweirsub save_file
71cdf0e10cSrcweir{
72cdf0e10cSrcweir	my ($savefile, $savecontent) = @_;
73cdf0e10cSrcweir
74cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_file : $savefile : $#{$savecontent}"); }
75cdf0e10cSrcweir
76cdf0e10cSrcweir	if ( open( OUT, ">$savefile" ) )
77cdf0e10cSrcweir	{
78cdf0e10cSrcweir		print OUT @{$savecontent};
79cdf0e10cSrcweir		close( OUT);
80cdf0e10cSrcweir	}
81cdf0e10cSrcweir	else
82cdf0e10cSrcweir	{
83cdf0e10cSrcweir		# it is useless to save a log file, if there is no write access
84cdf0e10cSrcweir
85cdf0e10cSrcweir		if ( $savefile =~ /\.log/ )
86cdf0e10cSrcweir		{
87*b274bc22SAndre Fischer			print "*************************************************\n";
88cdf0e10cSrcweir			print "ERROR: Cannot write log file: $savefile";
89*b274bc22SAndre Fischer			print "*************************************************\n";
90cdf0e10cSrcweir			exit(-1);	# exiting the program to avoid endless loops
91cdf0e10cSrcweir		}
92cdf0e10cSrcweir
93cdf0e10cSrcweir		installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_file");
94cdf0e10cSrcweir	}
95cdf0e10cSrcweir}
96cdf0e10cSrcweir
97cdf0e10cSrcweirsub save_hash
98cdf0e10cSrcweir{
99cdf0e10cSrcweir	my ($savefile, $hashref) = @_;
100cdf0e10cSrcweir
101cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_hash : $savefile"); }
102cdf0e10cSrcweir
103cdf0e10cSrcweir	my @printcontent = ();
104cdf0e10cSrcweir
105cdf0e10cSrcweir	my $itemkey;
106cdf0e10cSrcweir
107cdf0e10cSrcweir	foreach $itemkey ( keys %{$hashref} )
108cdf0e10cSrcweir	{
109cdf0e10cSrcweir		my $line = "";
110cdf0e10cSrcweir		my $itemvalue = $hashref->{$itemkey};
111cdf0e10cSrcweir		$line = $itemkey . "=" . $itemvalue . "\n";
112cdf0e10cSrcweir		push(@printcontent, $line);
113cdf0e10cSrcweir	}
114cdf0e10cSrcweir
115cdf0e10cSrcweir	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_hash");
116cdf0e10cSrcweir	print OUT @printcontent;
117cdf0e10cSrcweir	close( OUT);
118cdf0e10cSrcweir}
119cdf0e10cSrcweir
120cdf0e10cSrcweirsub save_array_of_hashes
121cdf0e10cSrcweir{
122cdf0e10cSrcweir	my ($savefile, $arrayref) = @_;
123cdf0e10cSrcweir
124cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
125cdf0e10cSrcweir
126cdf0e10cSrcweir	my @printcontent = ();
127cdf0e10cSrcweir
128cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
129cdf0e10cSrcweir	{
130cdf0e10cSrcweir		my $line = "";
131cdf0e10cSrcweir		my $hashref = ${$arrayref}[$i];
132cdf0e10cSrcweir		my $itemkey;
133cdf0e10cSrcweir
134cdf0e10cSrcweir		foreach $itemkey ( keys %{$hashref} )
135cdf0e10cSrcweir		{
136cdf0e10cSrcweir			my $itemvalue = $hashref->{$itemkey};
137cdf0e10cSrcweir			$line = $line . $itemkey . "=" . $itemvalue . "\t";
138cdf0e10cSrcweir		}
139cdf0e10cSrcweir
140cdf0e10cSrcweir		$line = $line . "\n";
141cdf0e10cSrcweir
142cdf0e10cSrcweir		push(@printcontent, $line);
143cdf0e10cSrcweir	}
144cdf0e10cSrcweir
145cdf0e10cSrcweir	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
146cdf0e10cSrcweir	print OUT @printcontent;
147cdf0e10cSrcweir	close( OUT);
148cdf0e10cSrcweir}
149cdf0e10cSrcweir
150cdf0e10cSrcweirsub save_array_of_hashes_modules
151cdf0e10cSrcweir{
152cdf0e10cSrcweir	my ($savefile, $arrayref) = @_;
153cdf0e10cSrcweir
154cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
155cdf0e10cSrcweir
156cdf0e10cSrcweir	my @printcontent = ();
157cdf0e10cSrcweir
158cdf0e10cSrcweir	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
159cdf0e10cSrcweir	{
160cdf0e10cSrcweir		my $line = "***************************************************\n";
161cdf0e10cSrcweir		my $hashref = ${$arrayref}[$i];
162cdf0e10cSrcweir		my $itemkey;
163cdf0e10cSrcweir
164cdf0e10cSrcweir		foreach $itemkey ( keys %{$hashref} )
165cdf0e10cSrcweir		{
166cdf0e10cSrcweir			my $itemvalue = $hashref->{$itemkey};
167cdf0e10cSrcweir			$line = $line . $itemkey . "=" . $itemvalue . "\n";
168cdf0e10cSrcweir		}
169cdf0e10cSrcweir
170cdf0e10cSrcweir		$line = $line . "\n";
171cdf0e10cSrcweir
172cdf0e10cSrcweir		push(@printcontent, $line);
173cdf0e10cSrcweir	}
174cdf0e10cSrcweir
175cdf0e10cSrcweir	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
176cdf0e10cSrcweir	print OUT @printcontent;
177cdf0e10cSrcweir	close( OUT);
178cdf0e10cSrcweir}
179cdf0e10cSrcweir
180cdf0e10cSrcweir###########################################
181cdf0e10cSrcweir# Binary file operations
182cdf0e10cSrcweir###########################################
183cdf0e10cSrcweir
184cdf0e10cSrcweirsub read_binary_file
185cdf0e10cSrcweir{
186cdf0e10cSrcweir	my ($filename) = @_;
187cdf0e10cSrcweir
188cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_binary_file : $filename"); }
189cdf0e10cSrcweir
190cdf0e10cSrcweir	my $file;
191cdf0e10cSrcweir
192cdf0e10cSrcweir	open( IN, "<$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "read_binary_file");
193cdf0e10cSrcweir	binmode IN;
194cdf0e10cSrcweir	seek IN, 0, 2;
195cdf0e10cSrcweir	my $length = tell IN;
196cdf0e10cSrcweir	seek IN, 0, 0;
197cdf0e10cSrcweir	read IN, $file, $length;
198cdf0e10cSrcweir	close IN;
199cdf0e10cSrcweir
200cdf0e10cSrcweir	return $file;
201cdf0e10cSrcweir}
202cdf0e10cSrcweir
203cdf0e10cSrcweirsub save_binary_file
204cdf0e10cSrcweir{
205cdf0e10cSrcweir	my ($file, $filename) = @_;
206cdf0e10cSrcweir
207cdf0e10cSrcweir	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_binary_file : $filename"); }
208cdf0e10cSrcweir
209cdf0e10cSrcweir	open( OUT, ">$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for writing", "save_binary_file");
210cdf0e10cSrcweir	binmode OUT;
211cdf0e10cSrcweir	print OUT $file;
212cdf0e10cSrcweir	close OUT;
213cdf0e10cSrcweir}
214cdf0e10cSrcweir
215cdf0e10cSrcweir1;
216