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 installer::windows::media;
25
26use installer::exiter;
27use installer::files;
28use installer::globals;
29use installer::windows::idtglobal;
30
31##############################################################
32# Returning the diskid for the media table.
33##############################################################
34
35sub get_media_diskid
36{
37	my ($id) = @_;
38
39	return $id;
40}
41
42##############################################################
43# Returning the lastsequence for the media table.
44##############################################################
45
46sub get_media_lastsequence
47{
48	my ($fileref) = @_;
49
50	return $fileref->{'sequencenumber'};
51}
52
53##############################################################
54# Returning the diskprompt for the media table.
55##############################################################
56
57sub get_media_diskprompt
58{
59	return 1;
60}
61
62##############################################################
63# Returning the cabinet file name for the media table.
64##############################################################
65
66sub get_media_cabinet
67{
68	my ($id) = @_;
69
70	my $number = 1000 + $id;
71	my $filename = "f_" . $number . ".cab";
72
73	if ( $installer::globals::include_cab_in_msi ) { $filename = "\#" . $filename; }
74
75	return $filename;
76}
77
78##############################################################
79# Returning the volumelabel for the media table.
80##############################################################
81
82sub get_media_volumelabel
83{
84	return "DISK1";
85}
86
87##############################################################
88# Returning the source for the media table.
89##############################################################
90
91sub get_media_source
92{
93	return "";
94}
95
96##############################################################
97# Saving the cabinet file name in the files collector.
98# This is useful for making a list to connect the
99# source of each file with the destination cabinet file.
100##############################################################
101
102sub set_cabinetfilename_for_component_in_file_collector
103{
104	my ($cabinetfilename, $filesref, $componentname, $max) = @_;
105
106	for ( my $i = 0; $i <= $max; $i++ )
107	{
108		my $onefile = ${$filesref}[$i];
109		my $component = $onefile->{'componentname'};
110
111		if ( $component eq $componentname )
112		{
113			my $cabinet = "";
114
115			if ( $onefile->{'cabinet'} ) { $cabinet = $onefile->{'cabinet'}; }
116
117			if ( $cabinet eq "" )
118			{
119				$onefile->{'cabinet'} = $cabinetfilename;
120			}
121		}
122	}
123}
124
125#################################################
126# Creating the cab file name dynamically
127#################################################
128
129sub generate_cab_filename_for_some_cabs
130{
131	my ( $allvariables, $id ) = @_;
132
133	my $name = $allvariables->{'PRODUCTNAME'};
134
135	$name = lc($name);
136	$name =~ s/\.//g;
137	$name =~ s/\s//g;
138
139	# possibility to overwrite the name with variable CABFILENAME
140	if ( $allvariables->{'CABFILENAME'} ) { $name = $allvariables->{'CABFILENAME'}; }
141
142	$name = $name . $id . ".cab";
143
144	if ( $installer::globals::include_cab_in_msi ) { $name = "\#" . $name; }
145
146	return $name;
147}
148
149#################################################
150# Creating the cab file name for cab files
151# defined in packages.
152#################################################
153
154sub get_cabfilename
155{
156	my ($name) = @_;
157
158	if ( $installer::globals::include_cab_in_msi ) { $name = "\#" . $name; }
159
160	return $name;
161}
162
163#################################################
164# Creating the cab file name dynamically
165#################################################
166
167sub generate_cab_filename
168{
169	my ( $allvariables ) = @_;
170
171	my $name = $allvariables->{'PRODUCTNAME'};
172
173	$name = lc($name);
174	$name =~ s/\.//g;
175	$name =~ s/\s//g;
176
177	# possibility to overwrite the name with variable CABFILENAME
178	if ( $allvariables->{'CABFILENAME'} ) { $name = $allvariables->{'CABFILENAME'}; }
179
180	$name = $name . ".cab";
181
182	if ( $installer::globals::include_cab_in_msi ) { $name = "\#" . $name; }
183
184	return $name;
185}
186
187sub get_maximum_filenumber
188{
189	my ($allfiles, $maxcabfilenumber) = @_;
190
191	my $maxfile = 0;
192
193	while ( ! ( $allfiles%$maxcabfilenumber == 0 ))
194	{
195		$allfiles++;
196	}
197
198	$maxfile = $allfiles / $maxcabfilenumber;
199
200	$maxfile++;					# for securitry
201
202	return $maxfile;
203}
204
205#################################################################################
206# Setting the last sequence for the cabinet files
207#################################################################################
208
209sub get_last_sequence
210{
211	my ( $cabfilename, $alludpatelastsequences ) = @_;
212
213	my $sequence = $installer::globals::lastsequence{$cabfilename};
214
215	return $sequence;
216}
217
218#################################################################################
219# Creating the file Media.idt dynamically
220# Content:
221# DiskId LastSequence DiskPrompt Cabinet VolumeLabel Source
222# Idea: Every component is packed into each own cab file
223#################################################################################
224
225sub create_media_table
226{
227	my ($filesref, $basedir, $allvariables) = @_;
228
229	my @mediatable = ();
230
231	my $diskid = 0;
232
233	installer::windows::idtglobal::write_idt_header(\@mediatable, "media");
234
235	if ( $allvariables->{'INCLUDE_CAB_IN_MSI'} ) { $installer::globals::include_cab_in_msi = 1; }
236
237	if ( $installer::globals::fix_number_of_cab_files )
238	{
239		# number of cabfiles
240		my $maxcabfilenumber = $installer::globals::number_of_cabfiles;
241		if ( $allvariables->{'CABFILENUMBER'} ) { $maxcabfilenumber = $allvariables->{'CABFILENUMBER'}; }
242		my $allfiles = $#{$filesref} + 1;
243		my $maxfilenumber = get_maximum_filenumber($allfiles, $maxcabfilenumber);
244		# my $maxfilenumber = 1000;	# maximum 1000 files in each cabinet file
245		my $cabfilenumber = 0;
246		my $cabfull = 0;
247		my $counter = 0;
248
249		# Sorting of files collector files required !
250		# Attention: The order in the cab file is not guaranteed (especially in udpate process)
251
252		for ( my $i = 0; $i <= $#{$filesref}; $i++ )
253		{
254			if (( $counter >= $maxfilenumber ) || ( $i == $#{$filesref} )) { $cabfull = 1; }
255
256			$counter++; 	 # counting the files in the cab file
257
258			my $onefile = ${$filesref}[$i];
259			my $nextfile = ${$filesref}[$i+1];
260
261			my $filecomponent = "";
262			my $nextcomponent = "";
263
264			if ( $onefile->{'componentname'} ) { $filecomponent = $onefile->{'componentname'}; }
265			if ( $nextfile->{'componentname'} ) { $nextcomponent = $nextfile->{'componentname'}; }
266
267			if ( $filecomponent eq $nextcomponent )	# all files of one component have to be in one cab file
268			{
269				next;		# nothing to do, this is not the last file of a component
270			}
271
272			if ( $cabfull )
273			{
274				my %media = ();
275				$cabfilenumber++;
276
277				$media{'DiskId'} = get_media_diskid($cabfilenumber);
278				# $media{'LastSequence'} = get_media_lastsequence($onefile);
279				$media{'LastSequence'} = $i + 1;	# This should be correct, also for unsorted files collectors
280				$media{'DiskPrompt'} = get_media_diskprompt();
281				$media{'Cabinet'} = generate_cab_filename_for_some_cabs($allvariables, $cabfilenumber);
282				$media{'VolumeLabel'} = get_media_volumelabel();
283				$media{'Source'} = get_media_source();
284
285				my $oneline = $media{'DiskId'} . "\t" . $media{'LastSequence'} . "\t" . $media{'DiskPrompt'} . "\t"
286						. $media{'Cabinet'} . "\t" . $media{'VolumeLabel'} . "\t" . $media{'Source'} . "\n";
287
288				push(@mediatable, $oneline);
289
290				# Saving the cabinet file name in the file collector
291
292				$media{'Cabinet'} =~ s/^\s*\#//;	# removing leading hash
293
294				for ( my $j = 0; $j <= $i; $j++ )
295				{
296					my $onefile = ${$filesref}[$j];
297					if ( ! $onefile->{'cabinet'} ) { $onefile->{'cabinet'} = $media{'Cabinet'}; }
298				}
299
300				$cabfull = 0;
301				$counter = 0;
302			}
303		}
304	}
305	else
306	{
307		installer::exiter::exit_program("ERROR: No cab file specification in globals.pm !", "create_media_table");
308	}
309
310	# Saving the file
311
312	my $mediatablename = $basedir . $installer::globals::separator . "Media.idt";
313	installer::files::save_file($mediatablename ,\@mediatable);
314    $installer::logger::Lang->printf("Created idt file: %s\n", $mediatablename);
315}
316
3171;
318