merge the formfield patch from ooo-build
[ooovba.git] / solenv / bin / modules / packager / files.pm
blob0fee7b1e2fcbefe11c0f6a8eef65cd7643452203
1 #*************************************************************************
3 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 #
5 # Copyright 2008 by Sun Microsystems, Inc.
7 # OpenOffice.org - a multi-platform office productivity suite
9 # $RCSfile: files.pm,v $
11 # $Revision: 1.8 $
13 # This file is part of OpenOffice.org.
15 # OpenOffice.org is free software: you can redistribute it and/or modify
16 # it under the terms of the GNU Lesser General Public License version 3
17 # only, as published by the Free Software Foundation.
19 # OpenOffice.org is distributed in the hope that it will be useful,
20 # but WITHOUT ANY WARRANTY; without even the implied warranty of
21 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 # GNU Lesser General Public License version 3 for more details
23 # (a copy is included in the LICENSE file that accompanied this code).
25 # You should have received a copy of the GNU Lesser General Public License
26 # version 3 along with OpenOffice.org. If not, see
27 # <http://www.openoffice.org/license.html>
28 # for a copy of the LGPLv3 License.
30 #*************************************************************************
33 package packager::files;
35 use packager::exiter;
37 ############################################
38 # File Operations
39 ############################################
41 sub check_file
43 my ($arg) = @_;
45 if(!( -f $arg ))
47 packager::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
51 sub read_file
53 my ($localfile) = @_;
55 if ( ! open( IN, $localfile ) ) {
56 # try again - sometimes we get errors caused by race conditions in parallel builds
57 sleep 5;
58 open( IN, $localfile ) or packager::exiter::exit_program("ERROR: Cannot open file: $localfile", "read_file");
60 my @localfile = <IN>;
61 close( IN );
63 return \@localfile;
66 ###########################################
67 # Saving files
68 ###########################################
70 sub save_file
72 my ($savefile, $savecontent) = @_;
73 open( OUT, ">$savefile" );
74 print OUT @{$savecontent};
75 close( OUT);
76 if (! -f $savefile) { packager::exiter::exit_program("ERROR: Cannot write file: $savefile", "save_file"); }
79 ######################################################
80 # Creating a new direcotory
81 ######################################################
83 sub create_directory
85 my ($directory) = @_;
87 my $returnvalue = 1;
89 if (!(-d $directory))
91 $returnvalue = mkdir($directory, 0775);
93 if ($returnvalue)
95 $infoline = "\nCreated directory: $directory\n";
96 push(@packager::globals::logfileinfo, $infoline);
98 if ($packager::globals::isunix)
100 my $localcall = "chmod 775 $directory \>\/dev\/null 2\>\&1";
101 system($localcall);
104 else
106 packager::exiter::exit_program("ERROR: Could not create directory: $directory", "create_directory");
111 ######################################################
112 # Creating a unique directory with number extension
113 ######################################################
115 sub create_unique_directory
117 my ($directory) = @_;
119 $directory =~ s/\Q$packager::globals::separator\E\s*$//;
120 $directory = $directory . "_INCREASINGNUMBER";
122 my $counter = 1;
123 my $created = 0;
124 my $localdirectory = "";
128 $localdirectory = $directory;
129 $localdirectory =~ s/INCREASINGNUMBER/$counter/;
130 $counter++;
132 if ( ! -d $localdirectory )
134 create_directory($localdirectory);
135 $created = 1;
138 while ( ! $created );
140 return $localdirectory;
143 ######################################################
144 # Removing a complete directory with subdirectories
145 ######################################################
147 sub remove_complete_directory
149 my ($directory) = @_;
151 my @content = ();
153 $directory =~ s/\Q$packager::globals::separator\E\s*$//;
155 if ( -d $directory )
157 opendir(DIR, $directory);
158 @content = readdir(DIR);
159 closedir(DIR);
161 my $oneitem;
163 foreach $oneitem (@content)
165 if ((!($oneitem eq ".")) && (!($oneitem eq "..")))
167 my $item = $directory . $packager::globals::separator . $oneitem;
169 if ( -f $item ) # deleting files
171 unlink($item);
174 if ( -d $item ) # recursive
176 remove_complete_directory($item, 0);
181 # try to remove empty directory
183 rmdir $directory;