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