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::files;
25
26use installer::exiter;
27use installer::logger;
28
29############################################
30# File Operations
31############################################
32
33sub check_file
34{
35	my ($arg) = @_;
36
37	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::check_file : $arg"); }
38
39	if(!( -f $arg ))
40	{
41		installer::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
42	}
43}
44
45sub read_file
46{
47	my ($localfile) = @_;
48	my @localfile = ();
49
50	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_file : $localfile"); }
51
52	open( IN, "<$localfile" ) || installer::exiter::exit_program("ERROR: Cannot open file $localfile for reading", "read_file");
53
54#	Don't use "my @localfile = <IN>" here, because
55#	perl has a problem with the internal "large_and_huge_malloc" function
56#	when calling perl using MacOS 10.5 with a perl built with MacOS 10.4
57	while ( $line = <IN> ) {
58		push @localfile, $line;
59	}
60
61	close( IN );
62
63	return \@localfile;
64}
65
66###########################################
67# Saving files, arrays and hashes
68###########################################
69
70sub save_file
71{
72	my ($savefile, $savecontent) = @_;
73
74	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_file : $savefile : $#{$savecontent}"); }
75
76	if ( open( OUT, ">$savefile" ) )
77	{
78		print OUT @{$savecontent};
79		close( OUT);
80	}
81	else
82	{
83		# it is useless to save a log file, if there is no write access
84
85		if ( $savefile =~ /\.log/ )
86		{
87			print "*************************************************\n";
88			print "ERROR: Cannot write log file: $savefile";
89			print "*************************************************\n";
90			exit(-1);	# exiting the program to avoid endless loops
91		}
92
93		installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_file");
94	}
95}
96
97sub save_hash
98{
99	my ($savefile, $hashref) = @_;
100
101	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_hash : $savefile"); }
102
103	my @printcontent = ();
104
105	my $itemkey;
106
107	foreach $itemkey ( keys %{$hashref} )
108	{
109		my $line = "";
110		my $itemvalue = $hashref->{$itemkey};
111		$line = $itemkey . "=" . $itemvalue . "\n";
112		push(@printcontent, $line);
113	}
114
115	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_hash");
116	print OUT @printcontent;
117	close( OUT);
118}
119
120sub save_array_of_hashes
121{
122	my ($savefile, $arrayref) = @_;
123
124	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
125
126	my @printcontent = ();
127
128	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
129	{
130		my $line = "";
131		my $hashref = ${$arrayref}[$i];
132		my $itemkey;
133
134		foreach $itemkey ( keys %{$hashref} )
135		{
136			my $itemvalue = $hashref->{$itemkey};
137			$line = $line . $itemkey . "=" . $itemvalue . "\t";
138		}
139
140		$line = $line . "\n";
141
142		push(@printcontent, $line);
143	}
144
145	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
146	print OUT @printcontent;
147	close( OUT);
148}
149
150sub save_array_of_hashes_modules
151{
152	my ($savefile, $arrayref) = @_;
153
154	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); }
155
156	my @printcontent = ();
157
158	for ( my $i = 0; $i <= $#{$arrayref}; $i++ )
159	{
160		my $line = "***************************************************\n";
161		my $hashref = ${$arrayref}[$i];
162		my $itemkey;
163
164		foreach $itemkey ( keys %{$hashref} )
165		{
166			my $itemvalue = $hashref->{$itemkey};
167			$line = $line . $itemkey . "=" . $itemvalue . "\n";
168		}
169
170		$line = $line . "\n";
171
172		push(@printcontent, $line);
173	}
174
175	open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes");
176	print OUT @printcontent;
177	close( OUT);
178}
179
180###########################################
181# Binary file operations
182###########################################
183
184sub read_binary_file
185{
186	my ($filename) = @_;
187
188	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_binary_file : $filename"); }
189
190	my $file;
191
192	open( IN, "<$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "read_binary_file");
193	binmode IN;
194	seek IN, 0, 2;
195	my $length = tell IN;
196	seek IN, 0, 0;
197	read IN, $file, $length;
198	close IN;
199
200	return $file;
201}
202
203sub save_binary_file
204{
205	my ($file, $filename) = @_;
206
207	if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_binary_file : $filename"); }
208
209	open( OUT, ">$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for writing", "save_binary_file");
210	binmode OUT;
211	print OUT $file;
212	close OUT;
213}
214
2151;
216