#!/usr/bin/perl # DiskDrake # Copyright (C) 1999 MandrakeSoft (pixel@mandrakesoft.com) # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # This is mainly a perl rewrite of the work of Andrew Clausen (libresize) package resize_fat::main; # $Id$ use diagnostics; use strict; use log; use common; use MDK::Common::System; use resize_fat::boot_sector; use resize_fat::info_sector; use resize_fat::directory; use resize_fat::io; use resize_fat::fat; use resize_fat::any; 1; #- - reads in the boot sector/partition info., and tries to make some sense of it sub new($$$) { my ($type, $device, $fs_name) = @_; my $fs = { device => $device, fs_name => $fs_name } ; eval { resize_fat::io::open($fs); resize_fat::boot_sector::read($fs); $resize_fat::isFAT32 and eval { resize_fat::info_sector::read($fs) }; resize_fat::fat::read($fs); resize_fat::any::flag_clusters($fs); }; if ($@) { close $fs->{fd}; die; } bless $fs, $type; } sub DESTROY { my ($fs) = @_; close $fs->{fd}; resize_fat::c_rewritten::free_all(); } #- copy all clusters >= to a new place on the partition, less #- than . Only copies files, not directories. #- (use of buffer needed because the seeks slow like hell the hard drive) sub copy_clusters { my ($fs, $cluster) = @_; my @buffer; my $flush = sub { while (@buffer) { my $cluster = shift @buffer; resize_fat::io::write_cluster($fs, $cluster, shift @buffer); } }; for (; $cluster < $fs->{nb_clusters} + 2; $cluster++) { resize_fat::c_rewritten::flag($cluster) == $resize_fat::any::FILE or next; push @buffer, resize_fat::c_rewritten::fat_remap($cluster), resize_fat::io::read_cluster($fs, $cluster); @buffer > 50 and &$flush(); } &$flush(); } #- Constructs the new directory tree to match the new file locations. sub construct_dir_tree { my ($fs) = @_; if ($resize_fat::isFAT32) { #- fat32's root must remain in the first 64k clusters #- so don't set it as DIRECTORY, it will be specially handled resize_fat::c_rewritten::set_flag($fs->{fat32_root_dir_cluster}, $resize_fat::any::FREE); } for (my $cluster = 2; $cluster < $fs->{nb_clusters} + 2; $cluster++) { resize_fat::c_rewritten::flag($cluster) == $resize_fat::any::DIRECTORY or next; resize_fat::io::write_cluster($fs, resize_fat::c_rewritten::fat_remap($cluster), resize_fat::directory::remap($fs, resize_fat::io::read_cluster($fs, $cluster))); } MDK::Common::System::sync(); #- until now, only free clusters have been written. it's a null operation if we stop here. #- it means no corruption :) # #- now we must be as fast as possible! #- remapping non movable root directory if ($resize_fat::isFAT32) { my $cluster = $fs->{fat32_root_dir_cluster}; resize_fat::io::write_cluster($fs, resize_fat::c_rewritten::fat_remap($cluster), resize_fat::directory::remap($fs, resize_fat::io::read_cluster($fs, $cluster))); } else { resize_fat::io::write($fs, $fs->{root_dir_offset}, $fs->{root_dir_size}, resize_fat::directory::remap($fs, resize_fat::io::read($fs, $fs->{root_dir_offset}, $fs->{root_dir_size}))); } } sub min_size($) { &resize_fat::any::min_size } sub max_size($) { &resize_fat::any::max_size } sub used_size($) { &resize_fat::any::used_size } #- resize #- - size is in sectors #- - checks boundaries before starting #- - copies all data beyond new_cluster_count behind the frontier sub resize { my ($fs, $size) = @_; my ($min, $max) = (min_size($fs), max_size($fs)); $size += $min if $size =~ /^\+/; $size >= $min or die "Minimum filesystem size is $min sectors"; $size <= $max or die "Maximum filesystem size is $max sectors"; log::l("resize_fat: Partition size will be ". ($size * $SECTORSIZE >> 20) ."Mb (well exactly ${size} sectors)"); my $new_data_size = $size * $SECTORSIZE - $fs->{cluster_offset}; my $new_nb_clusters = divide($new_data_size, $fs->{cluster_size}); my $used_size = used_size($fs); log::l("resize_fat: Break point for moving files is ". ($used_size * $SECTORSIZE >> 20) ." Mb ($used_size sectors)"); if ($size < $used_size) { log::l("resize_fat: Allocating new clusters"); resize_fat::fat::allocate_remap($fs, $new_nb_clusters); log::l("resize_fat: Copying files"); copy_clusters($fs, $new_nb_clusters); log::l("resize_fat: Copying directories"); construct_dir_tree($fs); log::l("Writing new FAT..."); resize_fat::fat::update($fs); resize_fat::fat::write($fs); } else { log::l("resize_fat: Nothing need to be moved"); } $fs->{nb_sectors} = $size; $fs->{nb_clusters} = $new_nb_clusters; $fs->{clusters}{count}->{free} = $fs->{nb_clusters} - $fs->{clusters}{count}->{used} - $fs->{clusters}->{count}->{bad} - 2; $fs->{system_id} = 'was here!'; $fs->{small_nb_sectors} = 0; $fs->{big_nb_sectors} = $size; log::l("resize_fat: Writing new boot sector..."); resize_fat::boot_sector::write($fs); $resize_fat::isFAT32 and eval { resize_fat::info_sector::write($fs) }; #- doesn't matter if this fails - its pretty useless! MDK::Common::System::sync(); close $fs->{fd}; log::l("resize_fat: done"); } 581'>perl-install/standalone.pm
blob: 38f0edfc3783b3915445225346567ba238bea3be (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416