2006-04-10 07:25:11 +04:00
|
|
|
/* $NetBSD: dir.c,v 1.19 2006/04/10 03:25:11 dbj Exp $ */
|
1996-05-14 21:39:21 +04:00
|
|
|
|
|
|
|
/*
|
1997-10-17 15:19:23 +04:00
|
|
|
* Copyright (C) 1995, 1996, 1997 Wolfgang Solfrank
|
1996-05-14 21:39:21 +04:00
|
|
|
* Copyright (c) 1995 Martin Husemann
|
1997-10-17 15:19:23 +04:00
|
|
|
* Some structure declaration borrowed from Paul Popelka
|
1996-05-14 21:39:21 +04:00
|
|
|
* (paulp@uts.amdahl.com), see /sys/msdosfs/ for reference.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Martin Husemann
|
|
|
|
* and Wolfgang Solfrank.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
1997-09-14 18:40:10 +04:00
|
|
|
#include <sys/cdefs.h>
|
1996-05-14 21:39:21 +04:00
|
|
|
#ifndef lint
|
2006-04-10 07:25:11 +04:00
|
|
|
__RCSID("$NetBSD: dir.c,v 1.19 2006/04/10 03:25:11 dbj Exp $");
|
1996-05-14 21:39:21 +04:00
|
|
|
#endif /* not lint */
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
2006-03-20 04:25:14 +03:00
|
|
|
#include <assert.h>
|
1996-05-14 21:39:21 +04:00
|
|
|
#include <time.h>
|
|
|
|
|
1996-05-25 21:09:43 +04:00
|
|
|
#include <sys/param.h>
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
#include "ext.h"
|
1996-09-28 03:22:50 +04:00
|
|
|
#include "fsutil.h"
|
1996-05-14 21:39:21 +04:00
|
|
|
|
|
|
|
#define SLOT_EMPTY 0x00 /* slot has never been used */
|
|
|
|
#define SLOT_E5 0x05 /* the real value is 0xe5 */
|
|
|
|
#define SLOT_DELETED 0xe5 /* file in this slot deleted */
|
|
|
|
|
|
|
|
#define ATTR_NORMAL 0x00 /* normal file */
|
|
|
|
#define ATTR_READONLY 0x01 /* file is readonly */
|
|
|
|
#define ATTR_HIDDEN 0x02 /* file is hidden */
|
|
|
|
#define ATTR_SYSTEM 0x04 /* file is a system file */
|
|
|
|
#define ATTR_VOLUME 0x08 /* entry is a volume label */
|
|
|
|
#define ATTR_DIRECTORY 0x10 /* entry is a directory name */
|
|
|
|
#define ATTR_ARCHIVE 0x20 /* file is new or modified */
|
|
|
|
|
|
|
|
#define ATTR_WIN95 0x0f /* long name record */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the format of the contents of the deTime field in the direntry
|
|
|
|
* structure.
|
|
|
|
* We don't use bitfields because we don't know how compilers for
|
|
|
|
* arbitrary machines will lay them out.
|
|
|
|
*/
|
|
|
|
#define DT_2SECONDS_MASK 0x1F /* seconds divided by 2 */
|
|
|
|
#define DT_2SECONDS_SHIFT 0
|
|
|
|
#define DT_MINUTES_MASK 0x7E0 /* minutes */
|
|
|
|
#define DT_MINUTES_SHIFT 5
|
|
|
|
#define DT_HOURS_MASK 0xF800 /* hours */
|
|
|
|
#define DT_HOURS_SHIFT 11
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is the format of the contents of the deDate field in the direntry
|
|
|
|
* structure.
|
|
|
|
*/
|
|
|
|
#define DD_DAY_MASK 0x1F /* day of month */
|
|
|
|
#define DD_DAY_SHIFT 0
|
|
|
|
#define DD_MONTH_MASK 0x1E0 /* month */
|
|
|
|
#define DD_MONTH_SHIFT 5
|
|
|
|
#define DD_YEAR_MASK 0xFE00 /* year - 1980 */
|
|
|
|
#define DD_YEAR_SHIFT 9
|
|
|
|
|
1996-09-23 20:27:56 +04:00
|
|
|
|
|
|
|
/* dir.c */
|
2005-01-19 23:00:45 +03:00
|
|
|
static struct dosDirEntry *newDosDirEntry(void);
|
|
|
|
static void freeDosDirEntry(struct dosDirEntry *);
|
|
|
|
static struct dirTodoNode *newDirTodo(void);
|
|
|
|
static void freeDirTodo(struct dirTodoNode *);
|
|
|
|
static char *fullpath(struct dosDirEntry *);
|
|
|
|
static u_char calcShortSum(u_char *);
|
|
|
|
static int delete(int, struct bootblock *, struct fatEntry *, cl_t, int,
|
|
|
|
cl_t, int, int);
|
|
|
|
static int removede(int, struct bootblock *, struct fatEntry *, u_char *,
|
|
|
|
u_char *, cl_t, cl_t, cl_t, char *, int);
|
|
|
|
static int checksize(struct bootblock *, struct fatEntry *, u_char *,
|
|
|
|
struct dosDirEntry *);
|
|
|
|
static int readDosDirSection(int, struct bootblock *, struct fatEntry *,
|
|
|
|
struct dosDirEntry *);
|
1996-09-23 20:27:56 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
/*
|
|
|
|
* Manage free dosDirEntry structures.
|
|
|
|
*/
|
|
|
|
static struct dosDirEntry *freede;
|
|
|
|
|
|
|
|
static struct dosDirEntry *
|
2005-01-19 23:00:45 +03:00
|
|
|
newDosDirEntry(void)
|
1996-05-28 23:51:11 +04:00
|
|
|
{
|
|
|
|
struct dosDirEntry *de;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
if (!(de = freede)) {
|
|
|
|
if (!(de = (struct dosDirEntry *)malloc(sizeof *de)))
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
freede = de->next;
|
|
|
|
return de;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-01-19 23:00:45 +03:00
|
|
|
freeDosDirEntry(struct dosDirEntry *de)
|
1996-05-28 23:51:11 +04:00
|
|
|
{
|
|
|
|
de->next = freede;
|
|
|
|
freede = de;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The same for dirTodoNode structures.
|
|
|
|
*/
|
|
|
|
static struct dirTodoNode *freedt;
|
|
|
|
|
|
|
|
static struct dirTodoNode *
|
2005-01-19 23:00:45 +03:00
|
|
|
newDirTodo(void)
|
1996-05-28 23:51:11 +04:00
|
|
|
{
|
|
|
|
struct dirTodoNode *dt;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
if (!(dt = freedt)) {
|
|
|
|
if (!(dt = (struct dirTodoNode *)malloc(sizeof *dt)))
|
|
|
|
return 0;
|
|
|
|
} else
|
|
|
|
freedt = dt->next;
|
|
|
|
return dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2005-01-19 23:00:45 +03:00
|
|
|
freeDirTodo(struct dirTodoNode *dt)
|
1996-05-28 23:51:11 +04:00
|
|
|
{
|
|
|
|
dt->next = freedt;
|
|
|
|
freedt = dt;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The stack of unread directories
|
|
|
|
*/
|
|
|
|
struct dirTodoNode *pendingDirectories = NULL;
|
|
|
|
|
1996-05-25 21:09:43 +04:00
|
|
|
/*
|
|
|
|
* Return the full pathname for a directory entry.
|
|
|
|
*/
|
|
|
|
static char *
|
2005-01-19 23:00:45 +03:00
|
|
|
fullpath(struct dosDirEntry *dir)
|
1996-05-25 21:09:43 +04:00
|
|
|
{
|
|
|
|
static char namebuf[MAXPATHLEN + 1];
|
|
|
|
char *cp, *np;
|
|
|
|
int nl;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-25 21:09:43 +04:00
|
|
|
cp = namebuf + sizeof namebuf - 1;
|
|
|
|
*cp = '\0';
|
|
|
|
do {
|
|
|
|
np = dir->lname[0] ? dir->lname : dir->name;
|
|
|
|
nl = strlen(np);
|
|
|
|
if ((cp -= nl) <= namebuf + 1)
|
|
|
|
break;
|
|
|
|
memcpy(cp, np, nl);
|
|
|
|
*--cp = '/';
|
1996-09-12 00:31:24 +04:00
|
|
|
} while ((dir = dir->parent) != NULL);
|
1996-09-17 19:34:05 +04:00
|
|
|
if (dir)
|
1996-05-25 21:09:43 +04:00
|
|
|
*--cp = '?';
|
1997-09-08 18:05:30 +04:00
|
|
|
else
|
|
|
|
cp++;
|
1996-05-25 21:09:43 +04:00
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Calculate a checksum over an 8.3 alias name
|
|
|
|
*/
|
|
|
|
static u_char
|
2005-01-19 23:00:45 +03:00
|
|
|
calcShortSum(u_char *p)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
u_char sum = 0;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 11; i++) {
|
|
|
|
sum = (sum << 7)|(sum >> 1); /* rotate right */
|
|
|
|
sum += p[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Global variables temporarily used during a directory scan
|
|
|
|
*/
|
|
|
|
static char longName[DOSLONGNAMELEN] = "";
|
|
|
|
static u_char *buffer = NULL;
|
|
|
|
static u_char *delbuf = NULL;
|
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
struct dosDirEntry *rootDir;
|
|
|
|
static struct dosDirEntry *lostDir;
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Init internal state for a new directory scan.
|
|
|
|
*/
|
|
|
|
int
|
2005-01-19 23:00:45 +03:00
|
|
|
resetDosDirSection(struct bootblock *boot, struct fatEntry *fat)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
int b1, b2;
|
1997-10-17 15:19:23 +04:00
|
|
|
cl_t cl;
|
|
|
|
int ret = FSOK;
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
b1 = boot->RootDirEnts * 32;
|
|
|
|
b2 = boot->SecPerClust * boot->BytesPerSec;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (!(buffer = malloc(b1 > b2 ? b1 : b2))
|
1996-05-28 23:51:11 +04:00
|
|
|
|| !(delbuf = malloc(b2))
|
|
|
|
|| !(rootDir = newDosDirEntry())) {
|
1996-05-14 21:39:21 +04:00
|
|
|
perror("No space for directory");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
1996-05-28 23:51:11 +04:00
|
|
|
memset(rootDir, 0, sizeof *rootDir);
|
1997-10-17 15:19:23 +04:00
|
|
|
if (boot->flags & FAT32) {
|
|
|
|
if (boot->RootCl < CLUST_FIRST || boot->RootCl >= boot->NumClusters) {
|
|
|
|
pfatal("Root directory starts with cluster out of range(%u)",
|
|
|
|
boot->RootCl);
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
cl = fat[boot->RootCl].next;
|
|
|
|
if (cl < CLUST_FIRST
|
|
|
|
|| (cl >= CLUST_RSRVD && cl< CLUST_EOFS)
|
|
|
|
|| fat[boot->RootCl].head != boot->RootCl) {
|
|
|
|
if (cl == CLUST_FREE)
|
|
|
|
pwarn("Root directory starts with free cluster\n");
|
|
|
|
else if (cl >= CLUST_RSRVD)
|
|
|
|
pwarn("Root directory starts with cluster marked %s\n",
|
|
|
|
rsrvdcltype(cl));
|
|
|
|
else {
|
|
|
|
pfatal("Root directory doesn't start a cluster chain");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
if (ask(1, "Fix")) {
|
|
|
|
fat[boot->RootCl].next = CLUST_FREE;
|
|
|
|
ret = FSFATMOD;
|
|
|
|
} else
|
|
|
|
ret = FSFATAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
fat[boot->RootCl].flags |= FAT_USED;
|
|
|
|
rootDir->head = boot->RootCl;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Cleanup after a directory scan
|
|
|
|
*/
|
|
|
|
void
|
2005-01-19 23:00:45 +03:00
|
|
|
finishDosDirSection(void)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
1996-05-28 23:51:11 +04:00
|
|
|
struct dirTodoNode *p, *np;
|
|
|
|
struct dosDirEntry *d, *nd;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
for (p = pendingDirectories; p; p = np) {
|
|
|
|
np = p->next;
|
|
|
|
freeDirTodo(p);
|
|
|
|
}
|
|
|
|
pendingDirectories = 0;
|
|
|
|
for (d = rootDir; d; d = nd) {
|
1996-09-12 00:31:24 +04:00
|
|
|
if ((nd = d->child) != NULL) {
|
1996-05-28 23:51:11 +04:00
|
|
|
d->child = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!(nd = d->next))
|
|
|
|
nd = d->parent;
|
|
|
|
freeDosDirEntry(d);
|
|
|
|
}
|
|
|
|
rootDir = lostDir = NULL;
|
1996-05-14 21:39:21 +04:00
|
|
|
free(buffer);
|
|
|
|
free(delbuf);
|
|
|
|
buffer = NULL;
|
|
|
|
delbuf = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Delete directory entries between startcl, startoff and endcl, endoff.
|
|
|
|
*/
|
|
|
|
static int
|
2005-01-19 23:00:45 +03:00
|
|
|
delete(int f, struct bootblock *boot, struct fatEntry *fat, cl_t startcl,
|
|
|
|
int startoff, cl_t endcl, int endoff, int notlast)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
u_char *s, *e;
|
|
|
|
off_t off;
|
|
|
|
int clsz = boot->SecPerClust * boot->BytesPerSec;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
s = delbuf + startoff;
|
|
|
|
e = delbuf + clsz;
|
|
|
|
while (startcl >= CLUST_FIRST && startcl < boot->NumClusters) {
|
|
|
|
if (startcl == endcl) {
|
|
|
|
if (notlast)
|
|
|
|
break;
|
|
|
|
e = delbuf + endoff;
|
|
|
|
}
|
|
|
|
off = startcl * boot->SecPerClust + boot->ClusterOffset;
|
|
|
|
off *= boot->BytesPerSec;
|
|
|
|
if (lseek(f, off, SEEK_SET) != off
|
|
|
|
|| read(f, delbuf, clsz) != clsz) {
|
|
|
|
perror("Unable to read directory");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
while (s < e) {
|
|
|
|
*s = SLOT_DELETED;
|
|
|
|
s += 32;
|
|
|
|
}
|
|
|
|
if (lseek(f, off, SEEK_SET) != off
|
|
|
|
|| write(f, delbuf, clsz) != clsz) {
|
|
|
|
perror("Unable to write directory");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
if (startcl == endcl)
|
|
|
|
break;
|
|
|
|
startcl = fat[startcl].next;
|
|
|
|
s = delbuf;
|
|
|
|
}
|
|
|
|
return FSOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2005-01-19 23:00:45 +03:00
|
|
|
removede(int f, struct bootblock *boot, struct fatEntry *fat, u_char *start,
|
|
|
|
u_char *end, cl_t startcl, cl_t endcl, cl_t curcl, char *path,
|
|
|
|
int type)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
1996-05-28 23:51:11 +04:00
|
|
|
switch (type) {
|
|
|
|
case 0:
|
1996-05-14 21:39:21 +04:00
|
|
|
pwarn("Invalid long filename entry for %s\n", path);
|
1996-05-28 23:51:11 +04:00
|
|
|
break;
|
|
|
|
case 1:
|
1996-05-14 21:39:21 +04:00
|
|
|
pwarn("Invalid long filename entry at end of directory %s\n", path);
|
1996-05-28 23:51:11 +04:00
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
pwarn("Invalid long filename entry for volume label\n");
|
|
|
|
break;
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(0, "Remove")) {
|
|
|
|
if (startcl != curcl) {
|
|
|
|
if (delete(f, boot, fat,
|
|
|
|
startcl, start - buffer,
|
|
|
|
endcl, end - buffer,
|
|
|
|
endcl == curcl) == FSFATAL)
|
|
|
|
return FSFATAL;
|
|
|
|
start = buffer;
|
|
|
|
}
|
2006-04-10 07:25:11 +04:00
|
|
|
/* startcl is < CLUST_FIRST for !fat32 root */
|
|
|
|
if ((endcl == curcl) || (startcl < CLUST_FIRST))
|
1996-05-14 21:39:21 +04:00
|
|
|
for (; start < end; start += 32)
|
|
|
|
*start = SLOT_DELETED;
|
|
|
|
return FSDIRMOD;
|
|
|
|
}
|
|
|
|
return FSERROR;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Check an in-memory file entry
|
|
|
|
*/
|
|
|
|
static int
|
2005-01-19 23:00:45 +03:00
|
|
|
checksize(struct bootblock *boot, struct fatEntry *fat, u_char *p,
|
|
|
|
struct dosDirEntry *dir)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Check size on ordinary files
|
|
|
|
*/
|
1996-05-28 23:51:11 +04:00
|
|
|
int32_t physicalSize;
|
1996-05-14 21:39:21 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
if (dir->head == CLUST_FREE)
|
|
|
|
physicalSize = 0;
|
|
|
|
else {
|
|
|
|
if (dir->head < CLUST_FIRST || dir->head >= boot->NumClusters)
|
|
|
|
return FSERROR;
|
|
|
|
physicalSize = fat[dir->head].length * boot->ClusterSize;
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
if (physicalSize < dir->size) {
|
1996-09-23 20:27:56 +04:00
|
|
|
pwarn("size of %s is %u, should at most be %u\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(dir), dir->size, physicalSize);
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(1, "Truncate")) {
|
|
|
|
dir->size = physicalSize;
|
|
|
|
p[28] = (u_char)physicalSize;
|
|
|
|
p[29] = (u_char)(physicalSize >> 8);
|
|
|
|
p[30] = (u_char)(physicalSize >> 16);
|
|
|
|
p[31] = (u_char)(physicalSize >> 24);
|
|
|
|
return FSDIRMOD;
|
|
|
|
} else
|
|
|
|
return FSERROR;
|
|
|
|
} else if (physicalSize - dir->size >= boot->ClusterSize) {
|
|
|
|
pwarn("%s has too many clusters allocated\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(dir));
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(1, "Drop superfluous clusters")) {
|
|
|
|
cl_t cl;
|
|
|
|
u_int32_t sz = 0;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
for (cl = dir->head; (sz += boot->ClusterSize) < dir->size;)
|
|
|
|
cl = fat[cl].next;
|
|
|
|
clearchain(boot, fat, fat[cl].next);
|
|
|
|
fat[cl].next = CLUST_EOF;
|
|
|
|
return FSFATMOD;
|
|
|
|
} else
|
|
|
|
return FSERROR;
|
|
|
|
}
|
|
|
|
return FSOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a directory and
|
|
|
|
* - resolve long name records
|
|
|
|
* - enter file and directory records into the parent's list
|
|
|
|
* - push directories onto the todo-stack
|
|
|
|
*/
|
1996-05-28 23:51:11 +04:00
|
|
|
static int
|
2005-01-19 23:00:45 +03:00
|
|
|
readDosDirSection(int f, struct bootblock *boot, struct fatEntry *fat,
|
|
|
|
struct dosDirEntry *dir)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
struct dosDirEntry dirent, *d;
|
|
|
|
u_char *p, *vallfn, *invlfn, *empty;
|
|
|
|
off_t off;
|
|
|
|
int i, j, k, last;
|
1996-09-12 00:31:24 +04:00
|
|
|
cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
|
1996-05-14 21:39:21 +04:00
|
|
|
char *t;
|
|
|
|
u_int lidx = 0;
|
|
|
|
int shortSum;
|
|
|
|
int mod = FSOK;
|
|
|
|
#define THISMOD 0x8000 /* Only used within this routine */
|
|
|
|
|
|
|
|
cl = dir->head;
|
1996-05-25 21:09:43 +04:00
|
|
|
if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Already handled somewhere else.
|
|
|
|
*/
|
|
|
|
return FSOK;
|
|
|
|
}
|
|
|
|
shortSum = -1;
|
|
|
|
vallfn = invlfn = empty = NULL;
|
|
|
|
do {
|
1997-10-17 15:19:23 +04:00
|
|
|
if (!(boot->flags & FAT32) && !dir->parent) {
|
1996-05-14 21:39:21 +04:00
|
|
|
last = boot->RootDirEnts * 32;
|
|
|
|
off = boot->ResSectors + boot->FATs * boot->FATsecs;
|
|
|
|
} else {
|
|
|
|
last = boot->SecPerClust * boot->BytesPerSec;
|
|
|
|
off = cl * boot->SecPerClust + boot->ClusterOffset;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
off *= boot->BytesPerSec;
|
|
|
|
if (lseek(f, off, SEEK_SET) != off
|
|
|
|
|| read(f, buffer, last) != last) {
|
|
|
|
perror("Unable to read directory");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
last /= 32;
|
|
|
|
/*
|
|
|
|
* Check `.' and `..' entries here? XXX
|
|
|
|
*/
|
|
|
|
for (p = buffer, i = 0; i < last; i++, p += 32) {
|
|
|
|
if (dir->fsckflags & DIREMPWARN) {
|
|
|
|
*p = SLOT_EMPTY;
|
|
|
|
continue;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
|
|
|
|
if (*p == SLOT_EMPTY) {
|
|
|
|
dir->fsckflags |= DIREMPTY;
|
|
|
|
empty = p;
|
|
|
|
empcl = cl;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (dir->fsckflags & DIREMPTY) {
|
|
|
|
if (!(dir->fsckflags & DIREMPWARN)) {
|
|
|
|
pwarn("%s has entries after end of directory\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(dir));
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(1, "Extend")) {
|
1996-09-24 17:17:18 +04:00
|
|
|
u_char *q;
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
dir->fsckflags &= ~DIREMPTY;
|
|
|
|
if (delete(f, boot, fat,
|
|
|
|
empcl, empty - buffer,
|
1996-09-24 17:17:18 +04:00
|
|
|
cl, p - buffer, 1) == FSFATAL)
|
1996-05-14 21:39:21 +04:00
|
|
|
return FSFATAL;
|
1996-09-24 17:17:18 +04:00
|
|
|
q = empcl == cl ? empty : buffer;
|
2006-03-20 04:25:14 +03:00
|
|
|
assert(q != NULL);
|
1996-09-24 17:17:18 +04:00
|
|
|
for (; q < p; q += 32)
|
|
|
|
*q = SLOT_DELETED;
|
|
|
|
mod |= THISMOD|FSDIRMOD;
|
1996-05-14 21:39:21 +04:00
|
|
|
} else if (ask(0, "Truncate"))
|
|
|
|
dir->fsckflags |= DIREMPWARN;
|
|
|
|
}
|
|
|
|
if (dir->fsckflags & DIREMPWARN) {
|
|
|
|
*p = SLOT_DELETED;
|
|
|
|
mod |= THISMOD|FSDIRMOD;
|
|
|
|
continue;
|
|
|
|
} else if (dir->fsckflags & DIREMPTY)
|
|
|
|
mod |= FSERROR;
|
|
|
|
empty = NULL;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (p[11] == ATTR_WIN95) {
|
|
|
|
if (*p & LRFIRST) {
|
|
|
|
if (shortSum != -1) {
|
|
|
|
if (!invlfn) {
|
|
|
|
invlfn = vallfn;
|
|
|
|
invcl = valcl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(longName, 0, sizeof longName);
|
|
|
|
shortSum = p[13];
|
|
|
|
vallfn = p;
|
|
|
|
valcl = cl;
|
|
|
|
} else if (shortSum != p[13]
|
1996-09-12 00:31:24 +04:00
|
|
|
|| lidx != (*p & LRNOMASK)) {
|
1996-05-14 21:39:21 +04:00
|
|
|
if (!invlfn) {
|
|
|
|
invlfn = vallfn;
|
|
|
|
invcl = valcl;
|
|
|
|
}
|
|
|
|
if (!invlfn) {
|
|
|
|
invlfn = p;
|
|
|
|
invcl = cl;
|
|
|
|
}
|
|
|
|
vallfn = NULL;
|
|
|
|
}
|
|
|
|
lidx = *p & LRNOMASK;
|
|
|
|
t = longName + --lidx * 13;
|
|
|
|
for (k = 1; k < 11 && t < longName + sizeof(longName); k += 2) {
|
|
|
|
if (!p[k] && !p[k + 1])
|
|
|
|
break;
|
|
|
|
*t++ = p[k];
|
|
|
|
/*
|
|
|
|
* Warn about those unusable chars in msdosfs here? XXX
|
|
|
|
*/
|
|
|
|
if (p[k + 1])
|
|
|
|
t[-1] = '?';
|
|
|
|
}
|
|
|
|
if (k >= 11)
|
|
|
|
for (k = 14; k < 26 && t < longName + sizeof(longName); k += 2) {
|
|
|
|
if (!p[k] && !p[k + 1])
|
|
|
|
break;
|
|
|
|
*t++ = p[k];
|
|
|
|
if (p[k + 1])
|
|
|
|
t[-1] = '?';
|
|
|
|
}
|
|
|
|
if (k >= 26)
|
|
|
|
for (k = 28; k < 32 && t < longName + sizeof(longName); k += 2) {
|
|
|
|
if (!p[k] && !p[k + 1])
|
|
|
|
break;
|
|
|
|
*t++ = p[k];
|
|
|
|
if (p[k + 1])
|
|
|
|
t[-1] = '?';
|
|
|
|
}
|
|
|
|
if (t >= longName + sizeof(longName)) {
|
|
|
|
pwarn("long filename too long\n");
|
|
|
|
if (!invlfn) {
|
|
|
|
invlfn = vallfn;
|
|
|
|
invcl = valcl;
|
|
|
|
}
|
|
|
|
vallfn = NULL;
|
|
|
|
}
|
|
|
|
if (p[26] | (p[27] << 8)) {
|
|
|
|
pwarn("long filename record cluster start != 0\n");
|
|
|
|
if (!invlfn) {
|
|
|
|
invlfn = vallfn;
|
|
|
|
invcl = cl;
|
|
|
|
}
|
|
|
|
vallfn = NULL;
|
|
|
|
}
|
|
|
|
continue; /* long records don't carry further
|
|
|
|
* information */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a standard msdosfs directory entry.
|
|
|
|
*/
|
|
|
|
memset(&dirent, 0, sizeof dirent);
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* it's a short name record, but we need to know
|
|
|
|
* more, so get the flags first.
|
|
|
|
*/
|
|
|
|
dirent.flags = p[11];
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Translate from 850 to ISO here XXX
|
|
|
|
*/
|
|
|
|
for (j = 0; j < 8; j++)
|
|
|
|
dirent.name[j] = p[j];
|
|
|
|
dirent.name[8] = '\0';
|
|
|
|
for (k = 7; k >= 0 && dirent.name[k] == ' '; k--)
|
|
|
|
dirent.name[k] = '\0';
|
|
|
|
if (dirent.name[k] != '\0')
|
|
|
|
k++;
|
|
|
|
if (dirent.name[0] == SLOT_E5)
|
|
|
|
dirent.name[0] = 0xe5;
|
1996-05-28 23:51:11 +04:00
|
|
|
|
|
|
|
if (dirent.flags & ATTR_VOLUME) {
|
|
|
|
if (vallfn || invlfn) {
|
|
|
|
mod |= removede(f, boot, fat,
|
|
|
|
invlfn ? invlfn : vallfn, p,
|
|
|
|
invlfn ? invcl : valcl, -1, 0,
|
|
|
|
fullpath(dir), 2);
|
|
|
|
vallfn = NULL;
|
|
|
|
invlfn = NULL;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p[8] != ' ')
|
1996-05-14 21:39:21 +04:00
|
|
|
dirent.name[k++] = '.';
|
|
|
|
for (j = 0; j < 3; j++)
|
|
|
|
dirent.name[k++] = p[j+8];
|
|
|
|
dirent.name[k] = '\0';
|
|
|
|
for (k--; k >= 0 && dirent.name[k] == ' '; k--)
|
|
|
|
dirent.name[k] = '\0';
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (vallfn && shortSum != calcShortSum(p)) {
|
|
|
|
if (!invlfn) {
|
|
|
|
invlfn = vallfn;
|
|
|
|
invcl = valcl;
|
|
|
|
}
|
|
|
|
vallfn = NULL;
|
|
|
|
}
|
|
|
|
dirent.head = p[26] | (p[27] << 8);
|
1997-10-17 15:19:23 +04:00
|
|
|
if (boot->ClustMask == CLUST32_MASK)
|
|
|
|
dirent.head |= (p[20] << 16) | (p[21] << 24);
|
1996-05-14 21:39:21 +04:00
|
|
|
dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
|
|
|
|
if (vallfn) {
|
2003-07-13 12:16:15 +04:00
|
|
|
strlcpy(dirent.lname, longName,
|
|
|
|
sizeof(dirent.lname));
|
1996-05-14 21:39:21 +04:00
|
|
|
longName[0] = '\0';
|
|
|
|
shortSum = -1;
|
|
|
|
}
|
1996-05-28 23:51:11 +04:00
|
|
|
|
1997-10-17 15:19:23 +04:00
|
|
|
dirent.parent = dir;
|
|
|
|
dirent.next = dir->child;
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (invlfn) {
|
|
|
|
mod |= k = removede(f, boot, fat,
|
|
|
|
invlfn, vallfn ? vallfn : p,
|
|
|
|
invcl, vallfn ? valcl : cl, cl,
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(&dirent), 0);
|
1996-05-14 21:39:21 +04:00
|
|
|
if (mod & FSFATAL)
|
|
|
|
return FSFATAL;
|
|
|
|
if (vallfn
|
|
|
|
? (valcl == cl && vallfn != buffer)
|
|
|
|
: p != buffer)
|
|
|
|
if (k & FSDIRMOD)
|
|
|
|
mod |= THISMOD;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
vallfn = NULL; /* not used any longer */
|
|
|
|
invlfn = NULL;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (dirent.size == 0 && !(dirent.flags & ATTR_DIRECTORY)) {
|
|
|
|
if (dirent.head != 0) {
|
|
|
|
pwarn("%s has clusters, but size 0\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(&dirent));
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(1, "Drop allocated clusters")) {
|
|
|
|
p[26] = p[27] = 0;
|
1997-10-17 15:19:23 +04:00
|
|
|
if (boot->ClustMask == CLUST32_MASK)
|
|
|
|
p[20] = p[21] = 0;
|
1996-05-14 21:39:21 +04:00
|
|
|
clearchain(boot, fat, dirent.head);
|
|
|
|
dirent.head = 0;
|
|
|
|
mod |= THISMOD|FSDIRMOD|FSFATMOD;
|
|
|
|
} else
|
|
|
|
mod |= FSERROR;
|
|
|
|
}
|
|
|
|
} else if (dirent.head == 0
|
|
|
|
&& !strcmp(dirent.name, "..")
|
1996-05-28 23:51:11 +04:00
|
|
|
&& dir->parent /* XXX */
|
1996-05-25 21:09:43 +04:00
|
|
|
&& !dir->parent->parent) {
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Do nothing, the parent is the root
|
|
|
|
*/
|
|
|
|
} else if (dirent.head < CLUST_FIRST
|
|
|
|
|| dirent.head >= boot->NumClusters
|
|
|
|
|| fat[dirent.head].next == CLUST_FREE
|
|
|
|
|| (fat[dirent.head].next >= CLUST_RSRVD
|
|
|
|
&& fat[dirent.head].next < CLUST_EOFS)
|
|
|
|
|| fat[dirent.head].head != dirent.head) {
|
|
|
|
if (dirent.head == 0)
|
|
|
|
pwarn("%s has no clusters\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(&dirent));
|
1996-05-14 21:39:21 +04:00
|
|
|
else if (dirent.head < CLUST_FIRST
|
|
|
|
|| dirent.head >= boot->NumClusters)
|
1997-10-17 15:19:23 +04:00
|
|
|
pwarn("%s starts with cluster out of range(%u)\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(&dirent),
|
1996-05-14 21:39:21 +04:00
|
|
|
dirent.head);
|
|
|
|
else if (fat[dirent.head].next == CLUST_FREE)
|
|
|
|
pwarn("%s starts with free cluster\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(&dirent));
|
1996-05-14 21:39:21 +04:00
|
|
|
else if (fat[dirent.head].next >= CLUST_RSRVD)
|
1997-10-17 15:19:23 +04:00
|
|
|
pwarn("%s starts with cluster marked %s\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(&dirent),
|
1996-05-14 21:39:21 +04:00
|
|
|
rsrvdcltype(fat[dirent.head].next));
|
|
|
|
else
|
|
|
|
pwarn("%s doesn't start a cluster chain\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(&dirent));
|
1996-05-14 21:39:21 +04:00
|
|
|
if (dirent.flags & ATTR_DIRECTORY) {
|
|
|
|
if (ask(0, "Remove")) {
|
|
|
|
*p = SLOT_DELETED;
|
|
|
|
mod |= THISMOD|FSDIRMOD;
|
|
|
|
} else
|
|
|
|
mod |= FSERROR;
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
if (ask(1, "Truncate")) {
|
|
|
|
p[28] = p[29] = p[30] = p[31] = 0;
|
1997-10-17 15:19:23 +04:00
|
|
|
p[26] = p[27] = 0;
|
|
|
|
if (boot->ClustMask == CLUST32_MASK)
|
|
|
|
p[20] = p[21] = 0;
|
1996-05-14 21:39:21 +04:00
|
|
|
dirent.size = 0;
|
|
|
|
mod |= THISMOD|FSDIRMOD;
|
|
|
|
} else
|
|
|
|
mod |= FSERROR;
|
|
|
|
}
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
|
|
|
|
fat[dirent.head].flags |= FAT_USED;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
if (dirent.flags & ATTR_DIRECTORY) {
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* gather more info for directories
|
|
|
|
*/
|
1997-10-17 15:19:23 +04:00
|
|
|
struct dirTodoNode *n;
|
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
if (dirent.size) {
|
1996-05-14 21:39:21 +04:00
|
|
|
pwarn("Directory %s has size != 0\n",
|
1996-05-28 23:51:11 +04:00
|
|
|
fullpath(&dirent));
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(1, "Correct")) {
|
|
|
|
p[28] = p[29] = p[30] = p[31] = 0;
|
1996-05-28 23:51:11 +04:00
|
|
|
dirent.size = 0;
|
1996-05-14 21:39:21 +04:00
|
|
|
mod |= THISMOD|FSDIRMOD;
|
|
|
|
} else
|
|
|
|
mod |= FSERROR;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* handle `.' and `..' specially
|
|
|
|
*/
|
1996-05-28 23:51:11 +04:00
|
|
|
if (strcmp(dirent.name, ".") == 0) {
|
|
|
|
if (dirent.head != dir->head) {
|
1996-05-14 21:39:21 +04:00
|
|
|
pwarn("`.' entry in %s has incorrect start cluster\n",
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(dir));
|
1996-05-14 21:39:21 +04:00
|
|
|
if (ask(1, "Correct")) {
|
1996-05-28 23:51:11 +04:00
|
|
|
dirent.head = dir->head;
|
|
|
|
p[26] = (u_char)dirent.head;
|
|
|
|
p[27] = (u_char)(dirent.head >> 8);
|
1997-10-17 15:19:23 +04:00
|
|
|
if (boot->ClustMask == CLUST32_MASK) {
|
|
|
|
p[20] = (u_char)(dirent.head >> 16);
|
|
|
|
p[21] = (u_char)(dirent.head >> 24);
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
mod |= THISMOD|FSDIRMOD;
|
|
|
|
} else
|
|
|
|
mod |= FSERROR;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
1996-05-28 23:51:11 +04:00
|
|
|
if (strcmp(dirent.name, "..") == 0) {
|
1998-08-25 23:18:12 +04:00
|
|
|
if (dir->parent) { /* XXX */
|
1997-10-17 15:19:23 +04:00
|
|
|
if (!dir->parent->parent) {
|
|
|
|
if (dirent.head) {
|
|
|
|
pwarn("`..' entry in %s has non-zero start cluster\n",
|
|
|
|
fullpath(dir));
|
|
|
|
if (ask(1, "Correct")) {
|
|
|
|
dirent.head = 0;
|
|
|
|
p[26] = p[27] = 0;
|
|
|
|
if (boot->ClustMask == CLUST32_MASK)
|
|
|
|
p[20] = p[21] = 0;
|
|
|
|
mod |= THISMOD|FSDIRMOD;
|
|
|
|
} else
|
|
|
|
mod |= FSERROR;
|
|
|
|
}
|
|
|
|
} else if (dirent.head != dir->parent->head) {
|
|
|
|
pwarn("`..' entry in %s has incorrect start cluster\n",
|
|
|
|
fullpath(dir));
|
|
|
|
if (ask(1, "Correct")) {
|
|
|
|
dirent.head = dir->parent->head;
|
|
|
|
p[26] = (u_char)dirent.head;
|
|
|
|
p[27] = (u_char)(dirent.head >> 8);
|
|
|
|
if (boot->ClustMask == CLUST32_MASK) {
|
|
|
|
p[20] = (u_char)(dirent.head >> 16);
|
|
|
|
p[21] = (u_char)(dirent.head >> 24);
|
|
|
|
}
|
|
|
|
mod |= THISMOD|FSDIRMOD;
|
|
|
|
} else
|
|
|
|
mod |= FSERROR;
|
|
|
|
}
|
1998-08-25 23:18:12 +04:00
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
continue;
|
|
|
|
}
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
/* create directory tree node */
|
|
|
|
if (!(d = newDosDirEntry())) {
|
|
|
|
perror("No space for directory");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
memcpy(d, &dirent, sizeof(struct dosDirEntry));
|
|
|
|
/* link it into the tree */
|
|
|
|
dir->child = d;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/* Enter this directory into the todo list */
|
1996-05-28 23:51:11 +04:00
|
|
|
if (!(n = newDirTodo())) {
|
1996-05-25 21:09:43 +04:00
|
|
|
perror("No space for todo list");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
n->next = pendingDirectories;
|
|
|
|
n->dir = d;
|
|
|
|
pendingDirectories = n;
|
|
|
|
} else {
|
1996-05-28 23:51:11 +04:00
|
|
|
mod |= k = checksize(boot, fat, p, &dirent);
|
1996-05-14 21:39:21 +04:00
|
|
|
if (k & FSDIRMOD)
|
|
|
|
mod |= THISMOD;
|
|
|
|
}
|
1997-09-08 18:05:30 +04:00
|
|
|
boot->NumFiles++;
|
1996-05-14 21:39:21 +04:00
|
|
|
}
|
2006-04-10 07:25:11 +04:00
|
|
|
|
|
|
|
if (!(boot->flags & FAT32) && !dir->parent)
|
|
|
|
break;
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
if (mod & THISMOD) {
|
|
|
|
last *= 32;
|
|
|
|
if (lseek(f, off, SEEK_SET) != off
|
|
|
|
|| write(f, buffer, last) != last) {
|
|
|
|
perror("Unable to write directory");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
mod &= ~THISMOD;
|
|
|
|
}
|
|
|
|
} while ((cl = fat[cl].next) >= CLUST_FIRST && cl < boot->NumClusters);
|
|
|
|
if (invlfn || vallfn)
|
|
|
|
mod |= removede(f, boot, fat,
|
|
|
|
invlfn ? invlfn : vallfn, p,
|
|
|
|
invlfn ? invcl : valcl, -1, 0,
|
1996-05-25 21:09:43 +04:00
|
|
|
fullpath(dir), 1);
|
2006-04-10 07:25:11 +04:00
|
|
|
|
|
|
|
/* The root directory of non fat32 filesystems is in a special
|
|
|
|
* area and may have been modified above without being written out.
|
|
|
|
*/
|
|
|
|
if ((mod & FSDIRMOD) && !(boot->flags & FAT32) && !dir->parent) {
|
|
|
|
last *= 32;
|
|
|
|
if (lseek(f, off, SEEK_SET) != off
|
|
|
|
|| write(f, buffer, last) != last) {
|
|
|
|
perror("Unable to write directory");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
mod &= ~THISMOD;
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
return mod & ~THISMOD;
|
|
|
|
}
|
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
int
|
2005-01-19 23:00:45 +03:00
|
|
|
handleDirTree(int dosfs, struct bootblock *boot, struct fatEntry *fat)
|
1996-05-28 23:51:11 +04:00
|
|
|
{
|
|
|
|
int mod;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
mod = readDosDirSection(dosfs, boot, fat, rootDir);
|
|
|
|
if (mod & FSFATAL)
|
|
|
|
return FSFATAL;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
/*
|
|
|
|
* process the directory todo list
|
|
|
|
*/
|
|
|
|
while (pendingDirectories) {
|
|
|
|
struct dosDirEntry *dir = pendingDirectories->dir;
|
|
|
|
struct dirTodoNode *n = pendingDirectories->next;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* remove TODO entry now, the list might change during
|
|
|
|
* directory reads
|
|
|
|
*/
|
|
|
|
freeDirTodo(pendingDirectories);
|
|
|
|
pendingDirectories = n;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* handle subdirectory
|
|
|
|
*/
|
|
|
|
mod |= readDosDirSection(dosfs, boot, fat, dir);
|
|
|
|
if (mod & FSFATAL)
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
2000-04-26 03:02:51 +04:00
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
return mod;
|
|
|
|
}
|
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
/*
|
|
|
|
* Try to reconnect a FAT chain into dir
|
|
|
|
*/
|
|
|
|
static u_char *lfbuf;
|
|
|
|
static cl_t lfcl;
|
|
|
|
static off_t lfoff;
|
|
|
|
|
|
|
|
int
|
2005-01-19 23:00:45 +03:00
|
|
|
reconnect(int dosfs, struct bootblock *boot, struct fatEntry *fat, cl_t head)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
struct dosDirEntry d;
|
|
|
|
u_char *p;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1998-01-22 21:48:43 +03:00
|
|
|
if (!ask(1, "Reconnect"))
|
1997-10-17 15:19:23 +04:00
|
|
|
return FSERROR;
|
|
|
|
|
1996-05-28 23:51:11 +04:00
|
|
|
if (!lostDir) {
|
|
|
|
for (lostDir = rootDir->child; lostDir; lostDir = lostDir->next) {
|
|
|
|
if (!strcmp(lostDir->name, LOSTDIR))
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!lostDir) { /* Create LOSTDIR? XXX */
|
|
|
|
pwarn("No %s directory\n", LOSTDIR);
|
|
|
|
return FSERROR;
|
|
|
|
}
|
|
|
|
}
|
1996-05-14 21:39:21 +04:00
|
|
|
if (!lfbuf) {
|
|
|
|
lfbuf = malloc(boot->ClusterSize);
|
|
|
|
if (!lfbuf) {
|
|
|
|
perror("No space for buffer");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
p = NULL;
|
|
|
|
} else
|
|
|
|
p = lfbuf;
|
|
|
|
while (1) {
|
|
|
|
if (p)
|
1997-09-08 18:05:30 +04:00
|
|
|
for (; p < lfbuf + boot->ClusterSize; p += 32)
|
1996-05-14 21:39:21 +04:00
|
|
|
if (*p == SLOT_EMPTY
|
|
|
|
|| *p == SLOT_DELETED)
|
|
|
|
break;
|
|
|
|
if (p && p < lfbuf + boot->ClusterSize)
|
|
|
|
break;
|
1996-05-28 23:51:11 +04:00
|
|
|
lfcl = p ? fat[lfcl].next : lostDir->head;
|
1996-05-14 21:39:21 +04:00
|
|
|
if (lfcl < CLUST_FIRST || lfcl >= boot->NumClusters) {
|
1996-05-28 23:51:11 +04:00
|
|
|
/* Extend LOSTDIR? XXX */
|
1996-05-14 21:39:21 +04:00
|
|
|
pwarn("No space in %s\n", LOSTDIR);
|
|
|
|
return FSERROR;
|
|
|
|
}
|
|
|
|
lfoff = lfcl * boot->ClusterSize
|
|
|
|
+ boot->ClusterOffset * boot->BytesPerSec;
|
|
|
|
if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
|
1997-09-08 18:05:30 +04:00
|
|
|
|| read(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
|
1996-05-14 21:39:21 +04:00
|
|
|
perror("could not read LOST.DIR");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
p = lfbuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
boot->NumFiles++;
|
|
|
|
/* Ensure uniqueness of entry here! XXX */
|
|
|
|
memset(&d, 0, sizeof d);
|
1998-07-28 23:22:54 +04:00
|
|
|
(void)snprintf(d.name, sizeof(d.name), "%u", head);
|
1996-05-14 21:39:21 +04:00
|
|
|
d.flags = 0;
|
|
|
|
d.head = head;
|
|
|
|
d.size = fat[head].length * boot->ClusterSize;
|
1997-10-17 15:19:23 +04:00
|
|
|
|
1996-05-14 21:39:21 +04:00
|
|
|
memset(p, 0, 32);
|
|
|
|
memset(p, ' ', 11);
|
1996-05-28 23:51:11 +04:00
|
|
|
memcpy(p, d.name, strlen(d.name));
|
|
|
|
p[26] = (u_char)d.head;
|
|
|
|
p[27] = (u_char)(d.head >> 8);
|
1997-10-17 15:19:23 +04:00
|
|
|
if (boot->ClustMask == CLUST32_MASK) {
|
|
|
|
p[20] = (u_char)(d.head >> 16);
|
|
|
|
p[21] = (u_char)(d.head >> 24);
|
|
|
|
}
|
1996-05-28 23:51:11 +04:00
|
|
|
p[28] = (u_char)d.size;
|
|
|
|
p[29] = (u_char)(d.size >> 8);
|
|
|
|
p[30] = (u_char)(d.size >> 16);
|
|
|
|
p[31] = (u_char)(d.size >> 24);
|
|
|
|
fat[head].flags |= FAT_USED;
|
1996-05-14 21:39:21 +04:00
|
|
|
if (lseek(dosfs, lfoff, SEEK_SET) != lfoff
|
1997-09-08 18:05:30 +04:00
|
|
|
|| write(dosfs, lfbuf, boot->ClusterSize) != boot->ClusterSize) {
|
1996-05-14 21:39:21 +04:00
|
|
|
perror("could not write LOST.DIR");
|
|
|
|
return FSFATAL;
|
|
|
|
}
|
|
|
|
return FSDIRMOD;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2005-01-19 23:00:45 +03:00
|
|
|
finishlf(void)
|
1996-05-14 21:39:21 +04:00
|
|
|
{
|
|
|
|
if (lfbuf)
|
|
|
|
free(lfbuf);
|
|
|
|
lfbuf = NULL;
|
|
|
|
}
|