1*cdf0e10cSrcweir#************************************************************************* 2*cdf0e10cSrcweir# 3*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir# 5*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir# 7*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir# 9*cdf0e10cSrcweir# This file is part of OpenOffice.org. 10*cdf0e10cSrcweir# 11*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir# only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir# 15*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir# 21*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir# version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir# <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir# for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir# 26*cdf0e10cSrcweir#************************************************************************* 27*cdf0e10cSrcweir 28*cdf0e10cSrcweirpackage installer::files; 29*cdf0e10cSrcweir 30*cdf0e10cSrcweiruse installer::exiter; 31*cdf0e10cSrcweiruse installer::logger; 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir############################################ 34*cdf0e10cSrcweir# File Operations 35*cdf0e10cSrcweir############################################ 36*cdf0e10cSrcweir 37*cdf0e10cSrcweirsub check_file 38*cdf0e10cSrcweir{ 39*cdf0e10cSrcweir my ($arg) = @_; 40*cdf0e10cSrcweir 41*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::check_file : $arg"); } 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir if(!( -f $arg )) 44*cdf0e10cSrcweir { 45*cdf0e10cSrcweir installer::exiter::exit_program("ERROR: Cannot find file $arg", "check_file"); 46*cdf0e10cSrcweir } 47*cdf0e10cSrcweir} 48*cdf0e10cSrcweir 49*cdf0e10cSrcweirsub read_file 50*cdf0e10cSrcweir{ 51*cdf0e10cSrcweir my ($localfile) = @_; 52*cdf0e10cSrcweir my @localfile = (); 53*cdf0e10cSrcweir 54*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_file : $localfile"); } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir open( IN, "<$localfile" ) || installer::exiter::exit_program("ERROR: Cannot open file $localfile for reading", "read_file"); 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir# Don't use "my @localfile = <IN>" here, because 59*cdf0e10cSrcweir# perl has a problem with the internal "large_and_huge_malloc" function 60*cdf0e10cSrcweir# when calling perl using MacOS 10.5 with a perl built with MacOS 10.4 61*cdf0e10cSrcweir while ( $line = <IN> ) { 62*cdf0e10cSrcweir push @localfile, $line; 63*cdf0e10cSrcweir } 64*cdf0e10cSrcweir 65*cdf0e10cSrcweir close( IN ); 66*cdf0e10cSrcweir 67*cdf0e10cSrcweir return \@localfile; 68*cdf0e10cSrcweir} 69*cdf0e10cSrcweir 70*cdf0e10cSrcweir########################################### 71*cdf0e10cSrcweir# Saving files, arrays and hashes 72*cdf0e10cSrcweir########################################### 73*cdf0e10cSrcweir 74*cdf0e10cSrcweirsub save_file 75*cdf0e10cSrcweir{ 76*cdf0e10cSrcweir my ($savefile, $savecontent) = @_; 77*cdf0e10cSrcweir 78*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_file : $savefile : $#{$savecontent}"); } 79*cdf0e10cSrcweir 80*cdf0e10cSrcweir if ( open( OUT, ">$savefile" ) ) 81*cdf0e10cSrcweir { 82*cdf0e10cSrcweir print OUT @{$savecontent}; 83*cdf0e10cSrcweir close( OUT); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir else 86*cdf0e10cSrcweir { 87*cdf0e10cSrcweir # it is useless to save a log file, if there is no write access 88*cdf0e10cSrcweir 89*cdf0e10cSrcweir if ( $savefile =~ /\.log/ ) 90*cdf0e10cSrcweir { 91*cdf0e10cSrcweir print "\n*************************************************\n"; 92*cdf0e10cSrcweir print "ERROR: Cannot write log file: $savefile"; 93*cdf0e10cSrcweir print "\n*************************************************\n"; 94*cdf0e10cSrcweir exit(-1); # exiting the program to avoid endless loops 95*cdf0e10cSrcweir } 96*cdf0e10cSrcweir 97*cdf0e10cSrcweir installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_file"); 98*cdf0e10cSrcweir } 99*cdf0e10cSrcweir} 100*cdf0e10cSrcweir 101*cdf0e10cSrcweirsub save_hash 102*cdf0e10cSrcweir{ 103*cdf0e10cSrcweir my ($savefile, $hashref) = @_; 104*cdf0e10cSrcweir 105*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_hash : $savefile"); } 106*cdf0e10cSrcweir 107*cdf0e10cSrcweir my @printcontent = (); 108*cdf0e10cSrcweir 109*cdf0e10cSrcweir my $itemkey; 110*cdf0e10cSrcweir 111*cdf0e10cSrcweir foreach $itemkey ( keys %{$hashref} ) 112*cdf0e10cSrcweir { 113*cdf0e10cSrcweir my $line = ""; 114*cdf0e10cSrcweir my $itemvalue = $hashref->{$itemkey}; 115*cdf0e10cSrcweir $line = $itemkey . "=" . $itemvalue . "\n"; 116*cdf0e10cSrcweir push(@printcontent, $line); 117*cdf0e10cSrcweir } 118*cdf0e10cSrcweir 119*cdf0e10cSrcweir open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_hash"); 120*cdf0e10cSrcweir print OUT @printcontent; 121*cdf0e10cSrcweir close( OUT); 122*cdf0e10cSrcweir} 123*cdf0e10cSrcweir 124*cdf0e10cSrcweirsub save_array_of_hashes 125*cdf0e10cSrcweir{ 126*cdf0e10cSrcweir my ($savefile, $arrayref) = @_; 127*cdf0e10cSrcweir 128*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); } 129*cdf0e10cSrcweir 130*cdf0e10cSrcweir my @printcontent = (); 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir for ( my $i = 0; $i <= $#{$arrayref}; $i++ ) 133*cdf0e10cSrcweir { 134*cdf0e10cSrcweir my $line = ""; 135*cdf0e10cSrcweir my $hashref = ${$arrayref}[$i]; 136*cdf0e10cSrcweir my $itemkey; 137*cdf0e10cSrcweir 138*cdf0e10cSrcweir foreach $itemkey ( keys %{$hashref} ) 139*cdf0e10cSrcweir { 140*cdf0e10cSrcweir my $itemvalue = $hashref->{$itemkey}; 141*cdf0e10cSrcweir $line = $line . $itemkey . "=" . $itemvalue . "\t"; 142*cdf0e10cSrcweir } 143*cdf0e10cSrcweir 144*cdf0e10cSrcweir $line = $line . "\n"; 145*cdf0e10cSrcweir 146*cdf0e10cSrcweir push(@printcontent, $line); 147*cdf0e10cSrcweir } 148*cdf0e10cSrcweir 149*cdf0e10cSrcweir open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes"); 150*cdf0e10cSrcweir print OUT @printcontent; 151*cdf0e10cSrcweir close( OUT); 152*cdf0e10cSrcweir} 153*cdf0e10cSrcweir 154*cdf0e10cSrcweirsub save_array_of_hashes_modules 155*cdf0e10cSrcweir{ 156*cdf0e10cSrcweir my ($savefile, $arrayref) = @_; 157*cdf0e10cSrcweir 158*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_array_of_hashes : $savefile : $#{$arrayref}"); } 159*cdf0e10cSrcweir 160*cdf0e10cSrcweir my @printcontent = (); 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir for ( my $i = 0; $i <= $#{$arrayref}; $i++ ) 163*cdf0e10cSrcweir { 164*cdf0e10cSrcweir my $line = "***************************************************\n"; 165*cdf0e10cSrcweir my $hashref = ${$arrayref}[$i]; 166*cdf0e10cSrcweir my $itemkey; 167*cdf0e10cSrcweir 168*cdf0e10cSrcweir foreach $itemkey ( keys %{$hashref} ) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir my $itemvalue = $hashref->{$itemkey}; 171*cdf0e10cSrcweir $line = $line . $itemkey . "=" . $itemvalue . "\n"; 172*cdf0e10cSrcweir } 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir $line = $line . "\n"; 175*cdf0e10cSrcweir 176*cdf0e10cSrcweir push(@printcontent, $line); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir 179*cdf0e10cSrcweir open( OUT, ">$savefile" ) || installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_array_of_hashes"); 180*cdf0e10cSrcweir print OUT @printcontent; 181*cdf0e10cSrcweir close( OUT); 182*cdf0e10cSrcweir} 183*cdf0e10cSrcweir 184*cdf0e10cSrcweir########################################### 185*cdf0e10cSrcweir# Binary file operations 186*cdf0e10cSrcweir########################################### 187*cdf0e10cSrcweir 188*cdf0e10cSrcweirsub read_binary_file 189*cdf0e10cSrcweir{ 190*cdf0e10cSrcweir my ($filename) = @_; 191*cdf0e10cSrcweir 192*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::read_binary_file : $filename"); } 193*cdf0e10cSrcweir 194*cdf0e10cSrcweir my $file; 195*cdf0e10cSrcweir 196*cdf0e10cSrcweir open( IN, "<$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "read_binary_file"); 197*cdf0e10cSrcweir binmode IN; 198*cdf0e10cSrcweir seek IN, 0, 2; 199*cdf0e10cSrcweir my $length = tell IN; 200*cdf0e10cSrcweir seek IN, 0, 0; 201*cdf0e10cSrcweir read IN, $file, $length; 202*cdf0e10cSrcweir close IN; 203*cdf0e10cSrcweir 204*cdf0e10cSrcweir return $file; 205*cdf0e10cSrcweir} 206*cdf0e10cSrcweir 207*cdf0e10cSrcweirsub save_binary_file 208*cdf0e10cSrcweir{ 209*cdf0e10cSrcweir my ($file, $filename) = @_; 210*cdf0e10cSrcweir 211*cdf0e10cSrcweir if ( $installer::globals::debug ) { installer::logger::debuginfo("installer::files::save_binary_file : $filename"); } 212*cdf0e10cSrcweir 213*cdf0e10cSrcweir open( OUT, ">$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for writing", "save_binary_file"); 214*cdf0e10cSrcweir binmode OUT; 215*cdf0e10cSrcweir print OUT $file; 216*cdf0e10cSrcweir close OUT; 217*cdf0e10cSrcweir} 218*cdf0e10cSrcweir 219*cdf0e10cSrcweir1; 220