1#
2# This file is part of the LibreOffice project.
3#
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/.
7#
8# This file incorporates work covered by the following license notice:
9#
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 .
17#
18
19package installer::files;
20
21use strict;
22use warnings;
23
24use installer::exiter;
25use installer::logger;
26
27############################################
28# File Operations
29############################################
30
31sub check_file
32{
33    my ($arg) = @_;
34
35    if(!( -f $arg ))
36    {
37        installer::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
38    }
39}
40
41sub read_file
42{
43    my ($localfile) = @_;
44    my @localfile = ();
45
46    open( IN, "<$localfile" ) || installer::exiter::exit_program("ERROR: Cannot open file $localfile for reading", "read_file");
47
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;
53    }
54
55    close( IN );
56
57    return \@localfile;
58}
59
60###########################################
61# Saving files, arrays and hashes
62###########################################
63
64sub save_file
65{
66    my ($savefile, $savecontent) = @_;
67
68    if ( open( OUT, ">$savefile" ) )
69    {
70        print OUT @{$savecontent};
71        close( OUT);
72    }
73    else
74    {
75        # it is useless to save a log file, if there is no write access
76
77        if ( $savefile =~ /\.log/ )
78        {
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
83        }
84
85        installer::exiter::exit_program("ERROR: Cannot open file $savefile for writing", "save_file");
86    }
87}
88
89###########################################
90# Binary file operations
91###########################################
92
93sub read_binary_file
94{
95    my ($filename) = @_;
96
97    my $file;
98
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;
106
107    return $file;
108}
109
110sub save_binary_file
111{
112    my ($file, $filename) = @_;
113
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;
118}
119
1201;
121