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
24package par2script::check;
25
26use par2script::globals;
27
28################################
29# Checks of the setup script
30################################
31
32########################################################
33# Checking if all defined directories are needed
34########################################################
35
36sub check_needed_directories
37{
38	my $allfiles = $par2script::globals::definitions{'File'};
39	my $alldirs = $par2script::globals::definitions{'Directory'};
40
41	# checking if all defined directories are needed
42
43	my $dir;
44	foreach $dir ( keys %{$alldirs} )
45	{
46		# I. directory has create flag
47		if (( exists($alldirs->{$dir}->{'Styles'}) ) && ( $alldirs->{$dir}->{'Styles'} =~ /\bCREATE\b/ )) { next; }
48
49		# II. there is at least one file in the directory
50		my $fileinside = 0;
51		my $file;
52		foreach $file ( keys %{$allfiles} )
53		{
54			if (( $allfiles->{$file}->{'Dir'} eq $dir ) || ( $allfiles->{$file}->{'NetDir'} eq $dir ))
55			{
56				$fileinside = 1;
57				last;
58			}
59		}
60		if ( $fileinside ) { next; }
61
62		# III. the directory is parent for another directory
63		my $isparent = 0;
64		my $onedir;
65		foreach $onedir ( keys %{$alldirs} )
66		{
67			if ( $alldirs->{$onedir}->{'ParentID'} eq $dir )
68			{
69				$isparent = 1;
70				last;
71			}
72		}
73		if ( $isparent ) { next; }
74
75		# no condition is true -> directory definition is superfluous
76		my $infoline = "\tINFO: Directory definition $dir is superfluous\n";
77		# print $infoline;
78		push(@par2script::globals::logfileinfo, $infoline);
79	}
80}
81
82##################################################
83# Checking if the directories in the item
84# definitions are defined.
85##################################################
86
87sub check_directories_in_item_definitions
88{
89	my $item;
90	foreach $item ( @par2script::globals::items_with_directories )
91	{
92		my $allitems = $par2script::globals::definitions{$item};
93
94		my $onegid;
95		foreach $onegid ( keys %{$allitems} )
96		{
97			if ( ! exists($allitems->{$onegid}->{'Dir'}) ) { die "\nERROR: No directory defined for item: $onegid!\n\n"; }
98			my $dir = $allitems->{$onegid}->{'Dir'};
99			if (( $dir eq "PD_PROGDIR" ) || ( $dir =~ /PREDEFINED_/ )) { next; }
100
101			# checking if this directoryid is defined
102			if ( ! exists($par2script::globals::definitions{'Directory'}->{$dir}) )
103			{
104				die "\nERROR: Directory $dir in item $onegid not defined!\n\n";
105			}
106		}
107	}
108}
109
110########################################################
111# Checking for all Items, that know their modules,
112# whether these modules exist.
113########################################################
114
115sub check_module_existence
116{
117	my $item;
118	foreach $item ( @par2script::globals::items_with_moduleid )
119	{
120		my $allitems = $par2script::globals::definitions{$item};
121
122		my $onegid;
123		foreach $onegid ( keys %{$allitems} )
124		{
125			if ( ! exists($allitems->{$onegid}->{'ModuleID'}) ) { die "\nERROR: No ModuleID defined for item: $onegid!\n\n"; }
126			my $moduleid = $allitems->{$onegid}->{'ModuleID'};
127
128			# checking if this directoryid is defined
129			if ( ! exists($par2script::globals::definitions{'Module'}->{$moduleid}) )
130			{
131				die "\nERROR: ModuleID $moduleid in item $onegid not defined!\n\n";
132			}
133		}
134	}
135}
136
137########################################################
138# Every script has to contain exactly one root module.
139# This module has no ParentID or an empty ParentID.
140########################################################
141
142sub check_rootmodule
143{
144	my $rootgid = "";
145	my $foundroot = 0;
146
147	my $allmodules = $par2script::globals::definitions{'Module'};
148
149	my $modulegid = "";
150	foreach $modulegid (keys %{$allmodules} )
151	{
152		if (( ! exists($allmodules->{$modulegid}->{'ParentID'}) ) || ( $allmodules->{$modulegid}->{'ParentID'} eq "" ))
153		{
154			if ( $foundroot )
155			{
156				die "\nERROR: More than one Root module. Only one module without ParentID or with empty ParentID allowed ($rootgid and $modulegid).\n";
157			}
158			$rootgid = $modulegid;
159			$foundroot = 1;
160		}
161	}
162
163	if ( ! $foundroot )
164	{
165		die "\nERROR: Could not find Root module. Did not find module without ParentID or with empty ParentID.\n";
166	}
167
168	print " $rootgid\n";
169
170}
171
172########################################################
173# File, Shortcut, Directory, Unixlink must not
174# contain a ModuleID
175########################################################
176
177sub check_moduleid_at_items
178{
179	my $item;
180	foreach $item ( @par2script::globals::items_without_moduleid )
181	{
182		my $allitems = $par2script::globals::definitions{$item};
183
184		my $onegid;
185		foreach $onegid ( keys %{$allitems} )
186		{
187			if ( exists($allitems->{$onegid}->{'ModuleID'}) )
188			{
189				die "\nERROR: ModuleID assigned to $onegid! No module assignment to $item!\n\n";
190			}
191		}
192	}
193}
194
195########################################################
196# Controlling existence of multi assignments
197########################################################
198
199sub check_multiple_assignments
200{
201	my @multiassignments = ();
202	my $error;
203
204	my $topitem;
205	foreach $topitem ( keys %par2script::globals::assignedgids )
206	{
207		my $item;
208		foreach $item ( keys %{$par2script::globals::assignedgids{$topitem}} )
209		{
210			if ( $par2script::globals::assignedgids{$topitem}->{$item} > 1 )
211			{
212				$error = 1;
213				my $string = "\tGID: $item Assignments: $par2script::globals::assignedgids{$topitem}->{$item}";
214				push(@multiassignments, $string);
215			}
216		}
217	}
218
219	if ( $error ) { par2script::exiter::multiassignmenterror(\@multiassignments); }
220}
221
222########################################################
223# Check, if a defined directory has a flag CREATE
224########################################################
225
226sub contains_create_flag
227{
228	my ($gid) = @_;
229
230	my $createflag = 0;
231
232	if (( exists($par2script::globals::definitions{'Directory'}->{$gid}->{'Styles'}) ) &&
233		( $par2script::globals::definitions{'Directory'}->{$gid}->{'Styles'} =~ /\bCREATE\b/ ))
234	{
235		$createflag = 1;
236	}
237
238	return $createflag;
239}
240
241########################################################
242# Controlling existence of definitions without
243# any assignment
244########################################################
245
246sub check_missing_assignments
247{
248	# If defined gids for "File", "Directory" or "Unixlink" are not assigned,
249	# this causes an error.
250	# Directories only have to be assigned, if they have the flag "CREATE".
251
252	my @missingassignments = ();
253	$error = 0;
254
255	my $item;
256	foreach $item ( @par2script::globals::items_assigned_at_modules )
257	{
258		my $assignedgids = $par2script::globals::assignedgids{$item};
259		my $definedgids = $par2script::globals::definitions{$item};
260
261		my $gid;
262		foreach $gid ( keys %{$definedgids} )
263		{
264			if ( $item eq "Directory" ) { if ( ! contains_create_flag($gid) ) { next; }	}
265
266			if ( ! exists( $assignedgids->{$gid} ))
267			{
268				$error = 1;
269				push(@missingassignments, $gid);
270			}
271		}
272	}
273
274	if ( $error ) { par2script::exiter::missingassignmenterror(\@missingassignments); }
275}
276
277#############################################################
278# Controlling if for all shortcuts with file assignment
279# the file is defined. And for all shortcuts with
280# shortcut assignment the shortcut has to be defined.
281#############################################################
282
283sub check_shortcut_assignments
284{
285	my $allshortcuts = $par2script::globals::definitions{'Shortcut'};
286	my $allfiles = $par2script::globals::definitions{'File'};
287
288	my $shortcut;
289	foreach $shortcut ( keys %{$allshortcuts} )
290	{
291		if (( exists($allshortcuts->{$shortcut}->{'FileID'}) ) &&
292			( ! exists($allfiles->{$allshortcuts->{$shortcut}->{'FileID'}}) ))
293		{
294			# die "\nERROR: FileID $allshortcuts->{$shortcut}->{'FileID'} has no definition at shortcut $shortcut !\n";
295			print "\n\tWARNING: FileID $allshortcuts->{$shortcut}->{'FileID'} has no definition at shortcut $shortcut !\n";
296		}
297
298		if (( exists($allshortcuts->{$shortcut}->{'ShortcutID'}) ) &&
299			( ! exists($allshortcuts->{$allshortcuts->{$shortcut}->{'ShortcutID'}}) ))
300		{
301			die "\nERROR: ShortcutID $allshortcuts->{$shortcut}->{'ShortcutID'} has no definition at shortcut $shortcut !\n";
302		}
303
304		if (( ! exists($allshortcuts->{$shortcut}->{'ShortcutID'}) ) &&
305			( ! exists($allshortcuts->{$shortcut}->{'FileID'}) ))
306		{
307			die "\nERROR: Shortcut requires assignment to \"ShortcutID\" or \"FileID\". Missing at shortcut $shortcut !\n";
308		}
309	}
310}
311
312#############################################################
313# Controlling if for Modules and Directories, the parents
314# are defined. If not, this can lead to a problem during
315# script creation, because only recursively added
316# Modules or Directories are added to the script.
317#############################################################
318
319sub check_missing_parents
320{
321	my @parentitems = ("Module", "Directory");
322	my %rootparents = ("PREDEFINED_PROGDIR" => "1");
323
324	my $oneitem;
325	foreach $oneitem ( @parentitems )
326	{
327		my $alldefinitions = $par2script::globals::definitions{$oneitem};
328
329		my $onegid;
330		foreach $onegid ( keys %{$alldefinitions} )
331		{
332			# If there is a ParentID used, it must be defined
333			if (( exists($alldefinitions->{$onegid}->{'ParentID'}) ) &&
334				( ! exists($alldefinitions->{$alldefinitions->{$onegid}->{'ParentID'}}) ) &&
335				( ! exists($rootparents{$alldefinitions->{$onegid}->{'ParentID'}}) ))
336			{
337				die "\nERROR: Parent \"$alldefinitions->{$onegid}->{'ParentID'}\" at $oneitem \"$onegid\" is not defined!\n";
338			}
339		}
340	}
341}
342
3431;
344