xref: /aoo41x/main/solenv/bin/modules/packager/files.pm (revision 9780544f)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22
23
24
25package packager::files;
26
27use packager::exiter;
28
29############################################
30# File Operations
31############################################
32
33sub check_file
34{
35	my ($arg) = @_;
36
37	if(!( -f $arg ))
38	{
39		packager::exiter::exit_program("ERROR: Cannot find file $arg", "check_file");
40	}
41}
42
43sub read_file
44{
45	my ($localfile) = @_;
46
47	if ( ! open( IN, $localfile ) ) {
48		# try again - sometimes we get errors caused by race conditions in parallel builds
49		sleep 5;
50		open( IN, $localfile ) or packager::exiter::exit_program("ERROR: Cannot open file: $localfile", "read_file");
51	}
52	my @localfile = <IN>;
53	close( IN );
54
55	return \@localfile;
56}
57
58###########################################
59# Saving files
60###########################################
61
62sub save_file
63{
64	my ($savefile, $savecontent) = @_;
65	open( OUT, ">$savefile" );
66	print OUT @{$savecontent};
67	close( OUT);
68	if (! -f $savefile) { packager::exiter::exit_program("ERROR: Cannot write file: $savefile", "save_file"); }
69}
70
71######################################################
72# Creating a new direcotory
73######################################################
74
75sub create_directory
76{
77	my ($directory) = @_;
78
79	my $returnvalue = 1;
80
81	if (!(-d $directory))
82	{
83		$returnvalue = mkdir($directory, 0775);
84
85		if ($returnvalue)
86		{
87			$infoline = "\nCreated directory: $directory\n";
88			push(@packager::globals::logfileinfo, $infoline);
89
90			if ($packager::globals::isunix)
91			{
92				my $localcall = "chmod 775 $directory \>\/dev\/null 2\>\&1";
93				system($localcall);
94			}
95		}
96		else
97		{
98			packager::exiter::exit_program("ERROR: Could not create directory: $directory", "create_directory");
99		}
100	}
101}
102
103######################################################
104# Creating a unique directory with number extension
105######################################################
106
107sub create_unique_directory
108{
109	my ($directory) = @_;
110
111	$directory =~ s/\Q$packager::globals::separator\E\s*$//;
112	$directory = $directory . "_INCREASINGNUMBER";
113
114	my $counter = 1;
115	my $created = 0;
116	my $localdirectory = "";
117
118	do
119	{
120		$localdirectory = $directory;
121		$localdirectory =~ s/INCREASINGNUMBER/$counter/;
122		$counter++;
123
124		if ( ! -d $localdirectory )
125		{
126			create_directory($localdirectory);
127			$created = 1;
128		}
129	}
130	while ( ! $created );
131
132	return $localdirectory;
133}
134
135######################################################
136# Removing a complete directory with subdirectories
137######################################################
138
139sub remove_complete_directory
140{
141	my ($directory) = @_;
142
143	my @content = ();
144
145	$directory =~ s/\Q$packager::globals::separator\E\s*$//;
146
147	if ( -d $directory )
148	{
149		opendir(DIR, $directory);
150		@content = readdir(DIR);
151		closedir(DIR);
152
153		my $oneitem;
154
155		foreach $oneitem (@content)
156		{
157			if ((!($oneitem eq ".")) && (!($oneitem eq "..")))
158			{
159				my $item = $directory . $packager::globals::separator . $oneitem;
160
161				if ( -f $item ) 	# deleting files
162				{
163					unlink($item);
164				}
165
166				if ( -d $item ) 	# recursive
167				{
168					remove_complete_directory($item, 0);
169				}
170			}
171		}
172
173		# try to remove empty directory
174
175		rmdir $directory;
176
177	}
178}
179
1801;
181