dir.c revision 1.1 1 1.1 ws /* $NetBSD: dir.c,v 1.1 1996/05/14 17:39:30 ws Exp $ */
2 1.1 ws
3 1.1 ws /*
4 1.1 ws * Copyright (C) 1995, 1996 Wolfgang Solfrank
5 1.1 ws * Copyright (c) 1995 Martin Husemann
6 1.1 ws * Some structure declaration borrowed from Paul Popelka
7 1.1 ws * (paulp (at) uts.amdahl.com), see /sys/msdosfs/ for reference.
8 1.1 ws *
9 1.1 ws * Redistribution and use in source and binary forms, with or without
10 1.1 ws * modification, are permitted provided that the following conditions
11 1.1 ws * are met:
12 1.1 ws * 1. Redistributions of source code must retain the above copyright
13 1.1 ws * notice, this list of conditions and the following disclaimer.
14 1.1 ws * 2. Redistributions in binary form must reproduce the above copyright
15 1.1 ws * notice, this list of conditions and the following disclaimer in the
16 1.1 ws * documentation and/or other materials provided with the distribution.
17 1.1 ws * 3. All advertising materials mentioning features or use of this software
18 1.1 ws * must display the following acknowledgement:
19 1.1 ws * This product includes software developed by Martin Husemann
20 1.1 ws * and Wolfgang Solfrank.
21 1.1 ws * 4. Neither the name of the University nor the names of its contributors
22 1.1 ws * may be used to endorse or promote products derived from this software
23 1.1 ws * without specific prior written permission.
24 1.1 ws *
25 1.1 ws * THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
26 1.1 ws * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
27 1.1 ws * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 1.1 ws * IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 1.1 ws * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 1.1 ws * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 1.1 ws * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 1.1 ws * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 1.1 ws * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 1.1 ws * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 1.1 ws */
36 1.1 ws
37 1.1 ws
38 1.1 ws #ifndef lint
39 1.1 ws static char rcsid[] = "$NetBSD: dir.c,v 1.1 1996/05/14 17:39:30 ws Exp $";
40 1.1 ws #endif /* not lint */
41 1.1 ws
42 1.1 ws #include <stdio.h>
43 1.1 ws #include <stdlib.h>
44 1.1 ws #include <string.h>
45 1.1 ws #include <ctype.h>
46 1.1 ws #include <stdio.h>
47 1.1 ws #include <unistd.h>
48 1.1 ws #include <time.h>
49 1.1 ws
50 1.1 ws #include "ext.h"
51 1.1 ws
52 1.1 ws #define SLOT_EMPTY 0x00 /* slot has never been used */
53 1.1 ws #define SLOT_E5 0x05 /* the real value is 0xe5 */
54 1.1 ws #define SLOT_DELETED 0xe5 /* file in this slot deleted */
55 1.1 ws
56 1.1 ws #define ATTR_NORMAL 0x00 /* normal file */
57 1.1 ws #define ATTR_READONLY 0x01 /* file is readonly */
58 1.1 ws #define ATTR_HIDDEN 0x02 /* file is hidden */
59 1.1 ws #define ATTR_SYSTEM 0x04 /* file is a system file */
60 1.1 ws #define ATTR_VOLUME 0x08 /* entry is a volume label */
61 1.1 ws #define ATTR_DIRECTORY 0x10 /* entry is a directory name */
62 1.1 ws #define ATTR_ARCHIVE 0x20 /* file is new or modified */
63 1.1 ws
64 1.1 ws #define ATTR_WIN95 0x0f /* long name record */
65 1.1 ws
66 1.1 ws /*
67 1.1 ws * This is the format of the contents of the deTime field in the direntry
68 1.1 ws * structure.
69 1.1 ws * We don't use bitfields because we don't know how compilers for
70 1.1 ws * arbitrary machines will lay them out.
71 1.1 ws */
72 1.1 ws #define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
73 1.1 ws #define DT_2SECONDS_SHIFT 0
74 1.1 ws #define DT_MINUTES_MASK 0x7E0 /* minutes */
75 1.1 ws #define DT_MINUTES_SHIFT 5
76 1.1 ws #define DT_HOURS_MASK 0xF800 /* hours */
77 1.1 ws #define DT_HOURS_SHIFT 11
78 1.1 ws
79 1.1 ws /*
80 1.1 ws * This is the format of the contents of the deDate field in the direntry
81 1.1 ws * structure.
82 1.1 ws */
83 1.1 ws #define DD_DAY_MASK 0x1F /* day of month */
84 1.1 ws #define DD_DAY_SHIFT 0
85 1.1 ws #define DD_MONTH_MASK 0x1E0 /* month */
86 1.1 ws #define DD_MONTH_SHIFT 5
87 1.1 ws #define DD_YEAR_MASK 0xFE00 /* year - 1980 */
88 1.1 ws #define DD_YEAR_SHIFT 9
89 1.1 ws
90 1.1 ws /*
91 1.1 ws * Calculate a checksum over an 8.3 alias name
92 1.1 ws */
93 1.1 ws static u_char
94 1.1 ws calcShortSum(p)
95 1.1 ws u_char *p;
96 1.1 ws {
97 1.1 ws u_char sum = 0;
98 1.1 ws int i;
99 1.1 ws
100 1.1 ws for (i = 0; i < 11; i++) {
101 1.1 ws sum = (sum << 7)|(sum >> 1); /* rotate right */
102 1.1 ws sum += p[i];
103 1.1 ws }
104 1.1 ws
105 1.1 ws return sum;
106 1.1 ws }
107 1.1 ws
108 1.1 ws /*
109 1.1 ws * Global variables temporarily used during a directory scan
110 1.1 ws */
111 1.1 ws static char longName[DOSLONGNAMELEN] = "";
112 1.1 ws static u_char *buffer = NULL;
113 1.1 ws static u_char *delbuf = NULL;
114 1.1 ws
115 1.1 ws /*
116 1.1 ws * Init internal state for a new directory scan.
117 1.1 ws */
118 1.1 ws int
119 1.1 ws resetDosDirSection(boot)
120 1.1 ws struct bootblock *boot;
121 1.1 ws {
122 1.1 ws int b1, b2;
123 1.1 ws
124 1.1 ws b1 = boot->RootDirEnts * 32;
125 1.1 ws b2 = boot->SecPerClust * boot->BytesPerSec;
126 1.1 ws
127 1.1 ws if (!(buffer = malloc(b1 > b2 ? b1 : b2))
128 1.1 ws || !(delbuf = malloc(b2))) {
129 1.1 ws perror("No space for directory");
130 1.1 ws return FSFATAL;
131 1.1 ws }
132 1.1 ws return FSOK;
133 1.1 ws }
134 1.1 ws
135 1.1 ws /*
136 1.1 ws * Cleanup after a directory scan
137 1.1 ws */
138 1.1 ws void
139 1.1 ws finishDosDirSection()
140 1.1 ws {
141 1.1 ws free(buffer);
142 1.1 ws free(delbuf);
143 1.1 ws buffer = NULL;
144 1.1 ws delbuf = NULL;
145 1.1 ws }
146 1.1 ws
147 1.1 ws /*
148 1.1 ws * Delete directory entries between startcl, startoff and endcl, endoff.
149 1.1 ws */
150 1.1 ws static int
151 1.1 ws delete(f, boot, fat, startcl, startoff, endcl, endoff, notlast)
152 1.1 ws int f;
153 1.1 ws struct bootblock *boot;
154 1.1 ws struct fatEntry *fat;
155 1.1 ws cl_t startcl;
156 1.1 ws int startoff;
157 1.1 ws cl_t endcl;
158 1.1 ws int endoff;
159 1.1 ws int notlast;
160 1.1 ws {
161 1.1 ws u_char *s, *e;
162 1.1 ws off_t off;
163 1.1 ws int clsz = boot->SecPerClust * boot->BytesPerSec;
164 1.1 ws
165 1.1 ws s = delbuf + startoff;
166 1.1 ws e = delbuf + clsz;
167 1.1 ws while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
168 1.1 ws if (startcl == endcl) {
169 1.1 ws if (notlast)
170 1.1 ws break;
171 1.1 ws e = delbuf + endoff;
172 1.1 ws }
173 1.1 ws off = startcl * boot->SecPerClust + boot->ClusterOffset;
174 1.1 ws off *= boot->BytesPerSec;
175 1.1 ws if (lseek(f, off, SEEK_SET) != off
176 1.1 ws || read(f, delbuf, clsz) != clsz) {
177 1.1 ws perror("Unable to read directory");
178 1.1 ws return FSFATAL;
179 1.1 ws }
180 1.1 ws while (s < e) {
181 1.1 ws *s = SLOT_DELETED;
182 1.1 ws s += 32;
183 1.1 ws }
184 1.1 ws if (lseek(f, off, SEEK_SET) != off
185 1.1 ws || write(f, delbuf, clsz) != clsz) {
186 1.1 ws perror("Unable to write directory");
187 1.1 ws return FSFATAL;
188 1.1 ws }
189 1.1 ws if (startcl == endcl)
190 1.1 ws break;
191 1.1 ws startcl = fat[startcl].next;
192 1.1 ws s = delbuf;
193 1.1 ws }
194 1.1 ws return FSOK;
195 1.1 ws }
196 1.1 ws
197 1.1 ws static int
198 1.1 ws removede(f, boot, fat, start, end, startcl, endcl, curcl, path, eof)
199 1.1 ws int f;
200 1.1 ws struct bootblock *boot;
201 1.1 ws struct fatEntry *fat;
202 1.1 ws u_char *start;
203 1.1 ws u_char *end;
204 1.1 ws cl_t startcl;
205 1.1 ws cl_t endcl;
206 1.1 ws cl_t curcl;
207 1.1 ws char *path;
208 1.1 ws int eof;
209 1.1 ws {
210 1.1 ws if (!eof)
211 1.1 ws pwarn("Invalid long filename entry for %s\n", path);
212 1.1 ws else
213 1.1 ws pwarn("Invalid long filename entry at end of directory %s\n", path);
214 1.1 ws if (ask(0, "Remove")) {
215 1.1 ws if (startcl != curcl) {
216 1.1 ws if (delete(f, boot, fat,
217 1.1 ws startcl, start - buffer,
218 1.1 ws endcl, end - buffer,
219 1.1 ws endcl == curcl) == FSFATAL)
220 1.1 ws return FSFATAL;
221 1.1 ws start = buffer;
222 1.1 ws }
223 1.1 ws if (endcl == curcl)
224 1.1 ws for (; start < end; start += 32)
225 1.1 ws *start = SLOT_DELETED;
226 1.1 ws return FSDIRMOD;
227 1.1 ws }
228 1.1 ws return FSERROR;
229 1.1 ws }
230 1.1 ws
231 1.1 ws /*
232 1.1 ws * Check an in-memory file entry
233 1.1 ws */
234 1.1 ws static int
235 1.1 ws checksize(boot, fat, p, dir)
236 1.1 ws struct bootblock *boot;
237 1.1 ws struct fatEntry *fat;
238 1.1 ws u_char *p;
239 1.1 ws struct dosDirEntry *dir;
240 1.1 ws {
241 1.1 ws /*
242 1.1 ws * Check size on ordinary files
243 1.1 ws */
244 1.1 ws u_int32_t physicalSize;
245 1.1 ws
246 1.1 ws if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
247 1.1 ws return FSERROR;
248 1.1 ws physicalSize = fat[dir->head].length * boot->ClusterSize;
249 1.1 ws if (physicalSize < dir->size) {
250 1.1 ws pwarn("size of %s is %lu, should at most be %lu\n",
251 1.1 ws dir->fullpath, dir->size, physicalSize);
252 1.1 ws if (ask(1, "Truncate")) {
253 1.1 ws dir->size = physicalSize;
254 1.1 ws p[28] = (u_char)physicalSize;
255 1.1 ws p[29] = (u_char)(physicalSize >> 8);
256 1.1 ws p[30] = (u_char)(physicalSize >> 16);
257 1.1 ws p[31] = (u_char)(physicalSize >> 24);
258 1.1 ws return FSDIRMOD;
259 1.1 ws } else
260 1.1 ws return FSERROR;
261 1.1 ws } else if (physicalSize - dir->size >= boot->ClusterSize) {
262 1.1 ws pwarn("%s has too many clusters allocated\n",
263 1.1 ws dir->fullpath);
264 1.1 ws if (ask(1, "Drop superfluous clusters")) {
265 1.1 ws cl_t cl;
266 1.1 ws u_int32_t sz = 0;
267 1.1 ws
268 1.1 ws for (cl = dir->head; (sz += boot->ClusterSize) < dir->size;)
269 1.1 ws cl = fat[cl].next;
270 1.1 ws clearchain(boot, fat, fat[cl].next);
271 1.1 ws fat[cl].next = CLUST_EOF;
272 1.1 ws return FSFATMOD;
273 1.1 ws } else
274 1.1 ws return FSERROR;
275 1.1 ws }
276 1.1 ws return FSOK;
277 1.1 ws }
278 1.1 ws
279 1.1 ws /*
280 1.1 ws * The stack of unread directories
281 1.1 ws */
282 1.1 ws struct dirTodoNode *pendingDirectories = NULL;
283 1.1 ws
284 1.1 ws /*
285 1.1 ws * Read a directory and
286 1.1 ws * - resolve long name records
287 1.1 ws * - enter file and directory records into the parent's list
288 1.1 ws * - push directories onto the todo-stack
289 1.1 ws */
290 1.1 ws int
291 1.1 ws readDosDirSection(f, boot, fat, dir)
292 1.1 ws int f;
293 1.1 ws struct bootblock *boot;
294 1.1 ws struct fatEntry *fat;
295 1.1 ws struct dosDirEntry *dir;
296 1.1 ws {
297 1.1 ws struct dosDirEntry dirent, *d;
298 1.1 ws u_char *p, *vallfn, *invlfn, *empty;
299 1.1 ws off_t off;
300 1.1 ws int i, j, k, last;
301 1.1 ws cl_t cl, valcl, invcl, empcl;
302 1.1 ws char *t;
303 1.1 ws u_int lidx = 0;
304 1.1 ws int shortSum;
305 1.1 ws int mod = FSOK;
306 1.1 ws #define THISMOD 0x8000 /* Only used within this routine */
307 1.1 ws
308 1.1 ws cl = dir->head;
309 1.1 ws if (dir->fullpath[1] && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
310 1.1 ws /*
311 1.1 ws * Already handled somewhere else.
312 1.1 ws */
313 1.1 ws return FSOK;
314 1.1 ws }
315 1.1 ws shortSum = -1;
316 1.1 ws vallfn = invlfn = empty = NULL;
317 1.1 ws do {
318 1.1 ws if (!dir->fullpath[1]) {
319 1.1 ws last = boot->RootDirEnts * 32;
320 1.1 ws off = boot->ResSectors + boot->FATs * boot->FATsecs;
321 1.1 ws } else {
322 1.1 ws last = boot->SecPerClust * boot->BytesPerSec;
323 1.1 ws off = cl * boot->SecPerClust + boot->ClusterOffset;
324 1.1 ws }
325 1.1 ws
326 1.1 ws off *= boot->BytesPerSec;
327 1.1 ws if (lseek(f, off, SEEK_SET) != off
328 1.1 ws || read(f, buffer, last) != last) {
329 1.1 ws perror("Unable to read directory");
330 1.1 ws return FSFATAL;
331 1.1 ws }
332 1.1 ws last /= 32;
333 1.1 ws /*
334 1.1 ws * Check `.' and `..' entries here? XXX
335 1.1 ws */
336 1.1 ws for (p = buffer, i = 0; i < last; i++, p += 32) {
337 1.1 ws if (dir->fsckflags & DIREMPWARN) {
338 1.1 ws *p = SLOT_EMPTY;
339 1.1 ws continue;
340 1.1 ws }
341 1.1 ws
342 1.1 ws if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
343 1.1 ws if (*p == SLOT_EMPTY) {
344 1.1 ws dir->fsckflags |= DIREMPTY;
345 1.1 ws empty = p;
346 1.1 ws empcl = cl;
347 1.1 ws }
348 1.1 ws continue;
349 1.1 ws }
350 1.1 ws
351 1.1 ws if (dir->fsckflags & DIREMPTY) {
352 1.1 ws if (!(dir->fsckflags & DIREMPWARN)) {
353 1.1 ws pwarn("%s has entries after end of directory\n",
354 1.1 ws dir->fullpath);
355 1.1 ws if (ask(1, "Extend")) {
356 1.1 ws dir->fsckflags &= ~DIREMPTY;
357 1.1 ws if (delete(f, boot, fat,
358 1.1 ws empcl, empty - buffer,
359 1.1 ws cl, p - buffer) == FSFATAL)
360 1.1 ws return FSFATAL;
361 1.1 ws } else if (ask(0, "Truncate"))
362 1.1 ws dir->fsckflags |= DIREMPWARN;
363 1.1 ws }
364 1.1 ws if (dir->fsckflags & DIREMPWARN) {
365 1.1 ws *p = SLOT_DELETED;
366 1.1 ws mod |= THISMOD|FSDIRMOD;
367 1.1 ws continue;
368 1.1 ws } else if (dir->fsckflags & DIREMPTY)
369 1.1 ws mod |= FSERROR;
370 1.1 ws empty = NULL;
371 1.1 ws }
372 1.1 ws
373 1.1 ws if (p[11] == ATTR_WIN95) {
374 1.1 ws if (*p & LRFIRST) {
375 1.1 ws if (shortSum != -1) {
376 1.1 ws if (!invlfn) {
377 1.1 ws invlfn = vallfn;
378 1.1 ws invcl = valcl;
379 1.1 ws }
380 1.1 ws }
381 1.1 ws memset(longName, 0, sizeof longName);
382 1.1 ws shortSum = p[13];
383 1.1 ws vallfn = p;
384 1.1 ws valcl = cl;
385 1.1 ws } else if (shortSum != p[13]
386 1.1 ws || lidx != *p & LRNOMASK) {
387 1.1 ws if (!invlfn) {
388 1.1 ws invlfn = vallfn;
389 1.1 ws invcl = valcl;
390 1.1 ws }
391 1.1 ws if (!invlfn) {
392 1.1 ws invlfn = p;
393 1.1 ws invcl = cl;
394 1.1 ws }
395 1.1 ws vallfn = NULL;
396 1.1 ws }
397 1.1 ws lidx = *p & LRNOMASK;
398 1.1 ws t = longName + --lidx * 13;
399 1.1 ws for (k = 1; k < 11 && t < longName + sizeof(longName); k += 2) {
400 1.1 ws if (!p[k] && !p[k + 1])
401 1.1 ws break;
402 1.1 ws *t++ = p[k];
403 1.1 ws /*
404 1.1 ws * Warn about those unusable chars in msdosfs here? XXX
405 1.1 ws */
406 1.1 ws if (p[k + 1])
407 1.1 ws t[-1] = '?';
408 1.1 ws }
409 1.1 ws if (k >= 11)
410 1.1 ws for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
411 1.1 ws if (!p[k] && !p[k + 1])
412 1.1 ws break;
413 1.1 ws *t++ = p[k];
414 1.1 ws if (p[k + 1])
415 1.1 ws t[-1] = '?';
416 1.1 ws }
417 1.1 ws if (k >= 26)
418 1.1 ws for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
419 1.1 ws if (!p[k] && !p[k + 1])
420 1.1 ws break;
421 1.1 ws *t++ = p[k];
422 1.1 ws if (p[k + 1])
423 1.1 ws t[-1] = '?';
424 1.1 ws }
425 1.1 ws if (t >= longName + sizeof(longName)) {
426 1.1 ws pwarn("long filename too long\n");
427 1.1 ws if (!invlfn) {
428 1.1 ws invlfn = vallfn;
429 1.1 ws invcl = valcl;
430 1.1 ws }
431 1.1 ws vallfn = NULL;
432 1.1 ws }
433 1.1 ws if (p[26] | (p[27] << 8)) {
434 1.1 ws pwarn("long filename record cluster start != 0\n");
435 1.1 ws if (!invlfn) {
436 1.1 ws invlfn = vallfn;
437 1.1 ws invcl = cl;
438 1.1 ws }
439 1.1 ws vallfn = NULL;
440 1.1 ws }
441 1.1 ws continue; /* long records don't carry further
442 1.1 ws * information */
443 1.1 ws }
444 1.1 ws
445 1.1 ws /*
446 1.1 ws * This is a standard msdosfs directory entry.
447 1.1 ws */
448 1.1 ws memset(&dirent, 0, sizeof dirent);
449 1.1 ws
450 1.1 ws /*
451 1.1 ws * it's a short name record, but we need to know
452 1.1 ws * more, so get the flags first.
453 1.1 ws */
454 1.1 ws dirent.flags = p[11];
455 1.1 ws
456 1.1 ws /*
457 1.1 ws * Translate from 850 to ISO here XXX
458 1.1 ws */
459 1.1 ws for (j = 0; j < 8; j++)
460 1.1 ws dirent.name[j] = p[j];
461 1.1 ws dirent.name[8] = '\0';
462 1.1 ws for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
463 1.1 ws dirent.name[k] = '\0';
464 1.1 ws if (dirent.name[k] != '\0')
465 1.1 ws k++;
466 1.1 ws if (dirent.name[0] == SLOT_E5)
467 1.1 ws dirent.name[0] = 0xe5;
468 1.1 ws /*
469 1.1 ws * What about volume names with extensions? XXX
470 1.1 ws */
471 1.1 ws if ((dirent.flags & ATTR_VOLUME) == 0 && p[8] != ' ')
472 1.1 ws dirent.name[k++] = '.';
473 1.1 ws for (j = 0; j < 3; j++)
474 1.1 ws dirent.name[k++] = p[j+8];
475 1.1 ws dirent.name[k] = '\0';
476 1.1 ws for (k--; k >= 0 && dirent.name[k] == ' '; k--)
477 1.1 ws dirent.name[k] = '\0';
478 1.1 ws
479 1.1 ws if (vallfn && shortSum != calcShortSum(p)) {
480 1.1 ws if (!invlfn) {
481 1.1 ws invlfn = vallfn;
482 1.1 ws invcl = valcl;
483 1.1 ws }
484 1.1 ws vallfn = NULL;
485 1.1 ws }
486 1.1 ws dirent.head = p[26] | (p[27] << 8);
487 1.1 ws dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
488 1.1 ws if (vallfn) {
489 1.1 ws strcpy(dirent.lname, longName);
490 1.1 ws longName[0] = '\0';
491 1.1 ws shortSum = -1;
492 1.1 ws }
493 1.1 ws
494 1.1 ws k = strlen(dirent.lname[0] ? dirent.lname : dirent.name);
495 1.1 ws k += strlen(dir->fullpath) + 2;
496 1.1 ws dirent.fullpath = malloc(k);
497 1.1 ws strcpy(dirent.fullpath, dir->fullpath);
498 1.1 ws if (dir->fullpath[1])
499 1.1 ws strcat(dirent.fullpath, "/");
500 1.1 ws strcat(dirent.fullpath,
501 1.1 ws dirent.lname[0] ? dirent.lname : dirent.name);
502 1.1 ws if (invlfn) {
503 1.1 ws mod |= k = removede(f, boot, fat,
504 1.1 ws invlfn, vallfn ? vallfn : p,
505 1.1 ws invcl, vallfn ? valcl : cl, cl,
506 1.1 ws dirent.fullpath, 0);
507 1.1 ws if (mod & FSFATAL)
508 1.1 ws return FSFATAL;
509 1.1 ws if (vallfn
510 1.1 ws ? (valcl == cl && vallfn != buffer)
511 1.1 ws : p != buffer)
512 1.1 ws if (k & FSDIRMOD)
513 1.1 ws mod |= THISMOD;
514 1.1 ws }
515 1.1 ws vallfn = NULL; /* not used any longer */
516 1.1 ws invlfn = NULL;
517 1.1 ws
518 1.1 ws if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
519 1.1 ws if (dirent.head != 0) {
520 1.1 ws pwarn("%s has clusters, but size 0\n",
521 1.1 ws dirent.fullpath);
522 1.1 ws if (ask(1, "Drop allocated clusters")) {
523 1.1 ws p[26] = p[27] = 0;
524 1.1 ws clearchain(boot, fat, dirent.head);
525 1.1 ws dirent.head = 0;
526 1.1 ws mod |= THISMOD|FSDIRMOD|FSFATMOD;
527 1.1 ws } else
528 1.1 ws mod |= FSERROR;
529 1.1 ws }
530 1.1 ws } else if (dirent.head == 0
531 1.1 ws && !strcmp(dirent.name, "..")
532 1.1 ws && !strcmp(dir->parent->fullpath, "/")) {
533 1.1 ws /*
534 1.1 ws * Do nothing, the parent is the root
535 1.1 ws */
536 1.1 ws } else if (dirent.head < CLUST_FIRST
537 1.1 ws || dirent.head >= boot->NumClusters
538 1.1 ws || fat[dirent.head].next == CLUST_FREE
539 1.1 ws || (fat[dirent.head].next >= CLUST_RSRVD
540 1.1 ws && fat[dirent.head].next < CLUST_EOFS)
541 1.1 ws || fat[dirent.head].head != dirent.head) {
542 1.1 ws if (dirent.head == 0)
543 1.1 ws pwarn("%s has no clusters\n",
544 1.1 ws dirent.fullpath);
545 1.1 ws else if (dirent.head < CLUST_FIRST
546 1.1 ws || dirent.head >= boot->NumClusters)
547 1.1 ws pwarn("%s starts with cluster out of range(%d)\n",
548 1.1 ws dirent.fullpath,
549 1.1 ws dirent.head);
550 1.1 ws else if (fat[dirent.head].next == CLUST_FREE)
551 1.1 ws pwarn("%s starts with free cluster\n",
552 1.1 ws dirent.fullpath);
553 1.1 ws else if (fat[dirent.head].next >= CLUST_RSRVD)
554 1.1 ws pwarn("%s starts with %s cluster\n",
555 1.1 ws dirent.fullpath,
556 1.1 ws rsrvdcltype(fat[dirent.head].next));
557 1.1 ws else
558 1.1 ws pwarn("%s doesn't start a cluster chain\n",
559 1.1 ws dirent.fullpath);
560 1.1 ws if (dirent.flags & ATTR_DIRECTORY) {
561 1.1 ws if (ask(0, "Remove")) {
562 1.1 ws *p = SLOT_DELETED;
563 1.1 ws mod |= THISMOD|FSDIRMOD;
564 1.1 ws } else
565 1.1 ws mod |= FSERROR;
566 1.1 ws continue;
567 1.1 ws } else {
568 1.1 ws if (ask(1, "Truncate")) {
569 1.1 ws p[28] = p[29] = p[30] = p[31] = 0;
570 1.1 ws dirent.size = 0;
571 1.1 ws mod |= THISMOD|FSDIRMOD;
572 1.1 ws } else
573 1.1 ws mod |= FSERROR;
574 1.1 ws }
575 1.1 ws }
576 1.1 ws
577 1.1 ws /* create directory tree node */
578 1.1 ws d = malloc(sizeof(struct dosDirEntry));
579 1.1 ws memcpy(d, &dirent, sizeof(struct dosDirEntry));
580 1.1 ws /* link it into the directory tree */
581 1.1 ws d->parent = dir;
582 1.1 ws d->next = dir->child;
583 1.1 ws dir->child = d;
584 1.1 ws if (d->head >= CLUST_FIRST && d->head < boot->NumClusters)
585 1.1 ws fat[d->head].dirp = d;
586 1.1 ws
587 1.1 ws if (d->flags & ATTR_DIRECTORY) {
588 1.1 ws /*
589 1.1 ws * gather more info for directories
590 1.1 ws */
591 1.1 ws struct dirTodoNode * n;
592 1.1 ws
593 1.1 ws if (d->size) {
594 1.1 ws pwarn("Directory %s has size != 0\n",
595 1.1 ws d->fullpath);
596 1.1 ws if (ask(1, "Correct")) {
597 1.1 ws p[28] = p[29] = p[30] = p[31] = 0;
598 1.1 ws d->size = 0;
599 1.1 ws mod |= THISMOD|FSDIRMOD;
600 1.1 ws } else
601 1.1 ws mod |= FSERROR;
602 1.1 ws }
603 1.1 ws /*
604 1.1 ws * handle `.' and `..' specially
605 1.1 ws */
606 1.1 ws if (strcmp(d->name, ".") == 0) {
607 1.1 ws if (d->head != dir->head) {
608 1.1 ws pwarn("`.' entry in %s has incorrect start cluster\n",
609 1.1 ws dir->fullpath);
610 1.1 ws if (ask(1, "Correct")) {
611 1.1 ws d->head = dir->head;
612 1.1 ws p[26] = (u_char)d->head;
613 1.1 ws p[27] = (u_char)(d->head >> 8);
614 1.1 ws mod |= THISMOD|FSDIRMOD;
615 1.1 ws } else
616 1.1 ws mod |= FSERROR;
617 1.1 ws }
618 1.1 ws continue;
619 1.1 ws }
620 1.1 ws if (strcmp(d->name, "..") == 0) {
621 1.1 ws if (d->head != dir->parent->head) {
622 1.1 ws pwarn("`..' entry in %s has incorrect start cluster\n",
623 1.1 ws dir->fullpath);
624 1.1 ws if (ask(1, "Correct")) {
625 1.1 ws d->head = dir->parent->head;
626 1.1 ws p[26] = (u_char)d->head;
627 1.1 ws p[27] = (u_char)(d->head >> 8);
628 1.1 ws mod |= THISMOD|FSDIRMOD;
629 1.1 ws } else
630 1.1 ws mod |= FSERROR;
631 1.1 ws }
632 1.1 ws continue;
633 1.1 ws }
634 1.1 ws
635 1.1 ws boot->NumFiles++;
636 1.1 ws /* Enter this directory into the todo list */
637 1.1 ws n = malloc(sizeof(struct dirTodoNode));
638 1.1 ws n->next = pendingDirectories;
639 1.1 ws n->dir = d;
640 1.1 ws pendingDirectories = n;
641 1.1 ws } else {
642 1.1 ws mod |= k = checksize(boot, fat, p, d);
643 1.1 ws if (k & FSDIRMOD)
644 1.1 ws mod |= THISMOD;
645 1.1 ws boot->NumFiles++;
646 1.1 ws }
647 1.1 ws }
648 1.1 ws if (mod & THISMOD) {
649 1.1 ws last *= 32;
650 1.1 ws if (lseek(f, off, SEEK_SET) != off
651 1.1 ws || write(f, buffer, last) != last) {
652 1.1 ws perror("Unable to write directory");
653 1.1 ws return FSFATAL;
654 1.1 ws }
655 1.1 ws mod &= ~THISMOD;
656 1.1 ws }
657 1.1 ws } while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
658 1.1 ws if (invlfn || vallfn)
659 1.1 ws mod |= removede(f, boot, fat,
660 1.1 ws invlfn ? invlfn : vallfn, p,
661 1.1 ws invlfn ? invcl : valcl, -1, 0,
662 1.1 ws dir->fullpath, 1);
663 1.1 ws return mod & ~THISMOD;
664 1.1 ws }
665 1.1 ws
666 1.1 ws /*
667 1.1 ws * Try to reconnect a FAT chain into dir
668 1.1 ws */
669 1.1 ws static u_char *lfbuf;
670 1.1 ws static cl_t lfcl;
671 1.1 ws static off_t lfoff;
672 1.1 ws
673 1.1 ws int
674 1.1 ws reconnect(dosfs, boot, fat, head, dir)
675 1.1 ws int dosfs;
676 1.1 ws struct bootblock *boot;
677 1.1 ws struct fatEntry *fat;
678 1.1 ws cl_t head;
679 1.1 ws struct dosDirEntry *dir;
680 1.1 ws {
681 1.1 ws struct dosDirEntry d;
682 1.1 ws u_char *p;
683 1.1 ws
684 1.1 ws if (!dir) /* Create lfdir? XXX */
685 1.1 ws return FSERROR;
686 1.1 ws if (!lfbuf) {
687 1.1 ws lfbuf = malloc(boot->ClusterSize);
688 1.1 ws if (!lfbuf) {
689 1.1 ws perror("No space for buffer");
690 1.1 ws return FSFATAL;
691 1.1 ws }
692 1.1 ws p = NULL;
693 1.1 ws } else
694 1.1 ws p = lfbuf;
695 1.1 ws while (1) {
696 1.1 ws if (p)
697 1.1 ws while (p < lfbuf + boot->ClusterSize)
698 1.1 ws if (*p == SLOT_EMPTY
699 1.1 ws || *p == SLOT_DELETED)
700 1.1 ws break;
701 1.1 ws if (p && p < lfbuf + boot->ClusterSize)
702 1.1 ws break;
703 1.1 ws lfcl = p ? fat[lfcl].next : dir->head;
704 1.1 ws if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
705 1.1 ws /* Extend lfdir? XXX */
706 1.1 ws pwarn("No space in %s\n", LOSTDIR);
707 1.1 ws return FSERROR;
708 1.1 ws }
709 1.1 ws lfoff = lfcl * boot->ClusterSize
710 1.1 ws + boot->ClusterOffset * boot->BytesPerSec;
711 1.1 ws if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
712 1.1 ws || read(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
713 1.1 ws perror("could not read LOST.DIR");
714 1.1 ws return FSFATAL;
715 1.1 ws }
716 1.1 ws p = lfbuf;
717 1.1 ws }
718 1.1 ws
719 1.1 ws if (!ask(0, "Reconnect"))
720 1.1 ws return FSERROR;
721 1.1 ws
722 1.1 ws boot->NumFiles++;
723 1.1 ws /* Ensure uniqueness of entry here! XXX */
724 1.1 ws memset(&d, 0, sizeof d);
725 1.1 ws sprintf(d.name, "%d", head);
726 1.1 ws d.fullpath = malloc(strlen(dir->fullpath) + strlen(d.name) + 2);
727 1.1 ws sprintf(d.fullpath, "%s/%s", dir->fullpath, d.name);
728 1.1 ws d.flags = 0;
729 1.1 ws d.head = head;
730 1.1 ws d.size = fat[head].length * boot->ClusterSize;
731 1.1 ws d.parent = dir;
732 1.1 ws d.next = dir->child;
733 1.1 ws dir->child = malloc(sizeof(struct dosDirEntry));
734 1.1 ws memcpy(dir->child, &d, sizeof(struct dosDirEntry));
735 1.1 ws
736 1.1 ws memset(p, 0, 32);
737 1.1 ws memset(p, ' ', 11);
738 1.1 ws memcpy(p, dir->name, strlen(dir->name));
739 1.1 ws p[26] = (u_char)dir->head;
740 1.1 ws p[27] = (u_char)(dir->head >> 8);
741 1.1 ws p[28] = (u_char)dir->size;
742 1.1 ws p[29] = (u_char)(dir->size >> 8);
743 1.1 ws p[30] = (u_char)(dir->size >> 16);
744 1.1 ws p[31] = (u_char)(dir->size >> 24);
745 1.1 ws fat[head].dirp = dir;
746 1.1 ws if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
747 1.1 ws || write(dosfs, buffer, boot->ClusterSize) != boot->ClusterSize) {
748 1.1 ws perror("could not write LOST.DIR");
749 1.1 ws return FSFATAL;
750 1.1 ws }
751 1.1 ws return FSDIRMOD;
752 1.1 ws }
753 1.1 ws
754 1.1 ws void
755 1.1 ws finishlf()
756 1.1 ws {
757 1.1 ws if (lfbuf)
758 1.1 ws free(lfbuf);
759 1.1 ws lfbuf = NULL;
760 1.1 ws }
761