xref: /trunk/main/solenv/bin/modules/packager/files.pm (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1#*************************************************************************
2#
3# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4#
5# Copyright 2000, 2010 Oracle and/or its affiliates.
6#
7# OpenOffice.org - a multi-platform office productivity suite
8#
9# This file is part of OpenOffice.org.
10#
11# OpenOffice.org is free software: you can redistribute it and/or modify
12# it under the terms of the GNU Lesser General Public License version 3
13# only, as published by the Free Software Foundation.
14#
15# OpenOffice.org is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU Lesser General Public License version 3 for more details
19# (a copy is included in the LICENSE file that accompanied this code).
20#
21# You should have received a copy of the GNU Lesser General Public License
22# version 3 along with OpenOffice.org.  If not, see
23# <http://www.openoffice.org/license.html>
24# for a copy of the LGPLv3 License.
25#
26#*************************************************************************
27
28
29package packager::files;
30
31use packager::exiter;
32
33############################################
34# File Operations
35############################################
36
37sub check_file
38{
39    my ($arg) = @_;
40
41    if(!( -f $arg ))
42    {
43        packager::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
44    }
45}
46
47sub read_file
48{
49    my ($localfile) = @_;
50
51    if ( ! open( IN, $localfile ) ) {
52        # try again - sometimes we get errors caused by race conditions in parallel builds
53        sleep 5;
54        open( IN, $localfile ) or packager::exiter::exit_program("ERROR: Cannot open file: $localfile", "read_file");
55    }
56    my @localfile = <IN>;
57    close( IN );
58
59    return \@localfile;
60}
61
62###########################################
63# Saving files
64###########################################
65
66sub save_file
67{
68    my ($savefile, $savecontent) = @_;
69    open( OUT, ">$savefile" );
70    print OUT @{$savecontent};
71    close( OUT);
72    if (! -f $savefile) { packager::exiter::exit_program("ERROR: Cannot write file: $savefile", "save_file"); }
73}
74
75######################################################
76# Creating a new direcotory
77######################################################
78
79sub create_directory
80{
81    my ($directory) = @_;
82
83    my $returnvalue = 1;
84
85    if (!(-d $directory))
86    {
87        $returnvalue = mkdir($directory, 0775);
88
89        if ($returnvalue)
90        {
91            $infoline = "\nCreated directory: $directory\n";
92            push(@packager::globals::logfileinfo, $infoline);
93
94            if ($packager::globals::isunix)
95            {
96                my $localcall = "chmod 775 $directory \>\/dev\/null 2\>\&1";
97                system($localcall);
98            }
99        }
100        else
101        {
102            packager::exiter::exit_program("ERROR: Could not create directory: $directory", "create_directory");
103        }
104    }
105}
106
107######################################################
108# Creating a unique directory with number extension
109######################################################
110
111sub create_unique_directory
112{
113    my ($directory) = @_;
114
115    $directory =~ s/\Q$packager::globals::separator\E\s*$//;
116    $directory = $directory . "_INCREASINGNUMBER";
117
118    my $counter = 1;
119    my $created = 0;
120    my $localdirectory = "";
121
122    do
123    {
124        $localdirectory = $directory;
125        $localdirectory =~ s/INCREASINGNUMBER/$counter/;
126        $counter++;
127
128        if ( ! -d $localdirectory )
129        {
130            create_directory($localdirectory);
131            $created = 1;
132        }
133    }
134    while ( ! $created );
135
136    return $localdirectory;
137}
138
139######################################################
140# Removing a complete directory with subdirectories
141######################################################
142
143sub remove_complete_directory
144{
145    my ($directory) = @_;
146
147    my @content = ();
148
149    $directory =~ s/\Q$packager::globals::separator\E\s*$//;
150
151    if ( -d $directory )
152    {
153        opendir(DIR, $directory);
154        @content = readdir(DIR);
155        closedir(DIR);
156
157        my $oneitem;
158
159        foreach $oneitem (@content)
160        {
161            if ((!($oneitem eq ".")) && (!($oneitem eq "..")))
162            {
163                my $item = $directory . $packager::globals::separator . $oneitem;
164
165                if ( -f $item )     # deleting files
166                {
167                    unlink($item);
168                }
169
170                if ( -d $item )     # recursive
171                {
172                    remove_complete_directory($item, 0);
173                }
174            }
175        }
176
177        # try to remove empty directory
178
179        rmdir $directory;
180
181    }
182}
183
1841;
185