Branch libreoffice-5-0-4
[LibreOffice.git] / solenv / bin / modules / installer / files.pm
blob1eb41482cfa67a6e11d02d28654f20583b8083ff
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::files;
21 use strict;
22 use warnings;
24 use installer::exiter;
25 use installer::logger;
27 ############################################
28 # File Operations
29 ############################################
31 sub check_file
33 my ($arg) = @_;
35 if(!( -f $arg ))
37 installer::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
41 sub read_file
43 my ($localfile) = @_;
44 my @localfile = ();
46 open( IN, "<$localfile" ) || installer::exiter::exit_program("ERROR: Cannot open file $localfile for reading", "read_file");
48 # Don't use "my @localfile = <IN>" here, because
49 # perl has a problem with the internal "large_and_huge_malloc" function
50 # when calling perl using MacOS 10.5 with a perl built with MacOS 10.4
51 while ( my $line = <IN> ) {
52 push @localfile, $line;
55 close( IN );
57 return \@localfile;
60 ###########################################
61 # Saving files, arrays and hashes
62 ###########################################
64 sub save_file
66 my ($savefile, $savecontent) = @_;
68 if ( open( OUT, ">$savefile" ) )
70 print OUT @{$savecontent};
71 close( OUT);
73 else
75 # it is useless to save a log file, if there is no write access
77 if ( $savefile =~ /\.log/ )
79 print "\n*************************************************\n";
80 print "ERROR: Cannot write log file $savefile, $!";
81 print "\n*************************************************\n";
82 exit(-1); # exiting the program to avoid endless loops
85 installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_file");
89 ###########################################
90 # Binary file operations
91 ###########################################
93 sub read_binary_file
95 my ($filename) = @_;
97 my $file;
99 open( IN, "<$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for reading", "read_binary_file");
100 binmode IN;
101 seek IN, 0, 2;
102 my $length = tell IN;
103 seek IN, 0, 0;
104 read IN, $file, $length;
105 close IN;
107 return $file;
110 sub save_binary_file
112 my ($file, $filename) = @_;
114 open( OUT, ">$filename" ) || installer::exiter::exit_program("ERROR: Cannot open file $filename for writing", "save_binary_file");
115 binmode OUT;
116 print OUT $file;
117 close OUT;