#include <sys/cdefs.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
#include <sys/param.h>
#include "ext.h"
#include "fsutil.h"
#define SLOT_EMPTY 0x00
#define SLOT_E5 0x05
#define SLOT_DELETED 0xe5
#define ATTR_NORMAL 0x00
#define ATTR_READONLY 0x01
#define ATTR_HIDDEN 0x02
#define ATTR_SYSTEM 0x04
#define ATTR_VOLUME 0x08
#define ATTR_DIRECTORY 0x10
#define ATTR_ARCHIVE 0x20
#define ATTR_WIN95 0x0f
#define DT_2SECONDS_MASK 0x1F
#define DT_2SECONDS_SHIFT 0
#define DT_MINUTES_MASK 0x7E0
#define DT_MINUTES_SHIFT 5
#define DT_HOURS_MASK 0xF800
#define DT_HOURS_SHIFT 11
#define DD_DAY_MASK 0x1F
#define DD_DAY_SHIFT 0
#define DD_MONTH_MASK 0x1E0
#define DD_MONTH_SHIFT 5
#define DD_YEAR_MASK 0xFE00
#define DD_YEAR_SHIFT 9
static struct dosDirEntry *newDosDirEntry __P((void));
static void freeDosDirEntry __P((struct dosDirEntry *));
static struct dirTodoNode *newDirTodo __P((void));
static void freeDirTodo __P((struct dirTodoNode *));
static char *fullpath __P((struct dosDirEntry *));
static u_char calcShortSum __P((u_char *));
static int delete __P((int, struct bootblock *, cl_t, int,
cl_t, int, int));
static int msdosfs_removede __P((int, struct bootblock *, u_char *,
u_char *, cl_t, cl_t, cl_t, char *, int));
static int checksize __P((struct bootblock *, u_char *, struct dosDirEntry *));
static int readDosDirSection __P((int, struct bootblock *, struct dosDirEntry *));
static struct dosDirEntry *freede;
static struct dosDirEntry *
newDosDirEntry()
{
struct dosDirEntry *de;
if (!(de = freede)) {
if (!(de = (struct dosDirEntry *)malloc(sizeof *de)))
return 0;
} else
freede = de->next;
return de;
}
static void
freeDosDirEntry(de)
struct dosDirEntry *de;
{
de->next = freede;
freede = de;
}
static struct dirTodoNode *freedt;
static struct dirTodoNode *
newDirTodo()
{
struct dirTodoNode *dt;
if (!(dt = freedt)) {
if (!(dt = (struct dirTodoNode *)malloc(sizeof *dt)))
return 0;
} else
freedt = dt->next;
return dt;
}
static void
freeDirTodo(dt)
struct dirTodoNode *dt;
{
dt->next = freedt;
freedt = dt;
}
struct dirTodoNode *pendingDirectories = NULL;
static char *
fullpath(dir)
struct dosDirEntry *dir;
{
static char namebuf[MAXPATHLEN + 1];
char *cp, *np;
int nl;
if (dir == rootDir)
{
namebuf[0] = '/';
namebuf[1] = '\0';
return namebuf;
}
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 = '/';
} while ((dir = dir->parent) != NULL);
if (dir)
*--cp = '?';
else
cp++;
return cp;
}
static u_char
calcShortSum(p)
u_char *p;
{
u_char sum = 0;
int i;
for (i = 0; i < 11; i++) {
sum = (sum << 7)|(sum >> 1);
sum += p[i];
}
return sum;
}
static int
markDosDirChain(struct bootblock *boot, struct dosDirEntry *dir)
{
int err = FSOK;
cl_t cluster, prev, value;
cl_t count;
cluster = dir->head;
prev = 0;
count = 0;
while (cluster >= CLUST_FIRST && cluster < boot->NumClusters && !isUsed(cluster))
{
value = fat_get(cluster);
if (value == CLUST_RSRVD || value == CLUST_BAD)
{
cluster = value;
break;
}
markUsed(cluster);
++count;
prev = cluster;
cluster = fat_get(cluster);
}
if (cluster < CLUST_EOFS)
{
if (cluster == CLUST_FREE || cluster >= CLUST_RSRVD)
pwarn("%s: Cluster chain starting at %u ends with cluster marked %s\n",
fullpath(dir), dir->head, rsrvdcltype(cluster));
else if (cluster < CLUST_FIRST || cluster >= boot->NumClusters)
pwarn("%s: Cluster chain starting at %u continues with cluster out of range (%u)\n",
fullpath(dir), dir->head, cluster);
else
pwarn("%s: Cluster chain starting at %u is cross-linked at cluster %u\n",
fullpath(dir), dir->head, cluster);
if (ask(1, "Truncate"))
{
err = fat_set(prev, CLUST_EOF);
if (err)
cluster = CLUST_ERROR;
else
cluster = CLUST_EOF;
}
}
dir->end = cluster;
dir->physicalSize = (u_int64_t)count * boot->ClusterSize;
if (cluster == CLUST_ERROR)
return FSFATAL;
return err;
}
static char longName[DOSLONGNAMELEN] = "";
static u_char *buffer = NULL;
static u_char *delbuf = NULL;
struct dosDirEntry *rootDir;
static struct dosDirEntry *lostDir;
int
resetDosDirSection(struct bootblock *boot)
{
int b1, b2;
cl_t cl;
int ret = FSOK;
b1 = boot->RootDirEnts * 32;
b2 = boot->SecPerClust * boot->BytesPerSec;
if (!(buffer = malloc(b1 > b2 ? b1 : b2))
|| !(delbuf = malloc(b2))
|| !(rootDir = newDosDirEntry())) {
perr("No space for directory");
return FSFATAL;
}
memset(rootDir, 0, sizeof *rootDir);
if (boot->flags & FAT32) {
if (boot->RootCl < CLUST_FIRST || boot->RootCl >= boot->NumClusters) {
pfatal("Root directory starts with cluster out of range(%u)\n",
boot->RootCl);
return FSFATAL;
}
cl = fat_get(boot->RootCl);
if (cl == CLUST_ERROR)
return FSFATAL;
if (cl < CLUST_FIRST
|| (cl >= CLUST_RSRVD && cl< CLUST_EOFS)) {
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));
if (ask(1, "Fix")) {
ret = fat_set(boot->RootCl, CLUST_EOF);
if (!ret)
ret = FSFATMOD;
markFree(boot->RootCl);
} else
return FSFATAL;
}
rootDir->head = boot->RootCl;
ret |= markDosDirChain(boot, rootDir);
}
return ret;
}
void
finishDosDirSection()
{
struct dirTodoNode *p, *np;
struct dosDirEntry *d, *nd;
for (p = pendingDirectories; p; p = np) {
np = p->next;
freeDirTodo(p);
}
pendingDirectories = 0;
for (d = rootDir; d; d = nd) {
if ((nd = d->child) != NULL) {
d->child = 0;
continue;
}
if (!(nd = d->next))
nd = d->parent;
freeDosDirEntry(d);
}
rootDir = lostDir = NULL;
free(buffer);
free(delbuf);
buffer = NULL;
delbuf = NULL;
}
static int
delete(f, boot, startcl, startoff, endcl, endoff, notlast)
int f;
struct bootblock *boot;
cl_t startcl;
int startoff;
cl_t endcl;
int endoff;
int notlast;
{
u_char *s, *e;
off_t off;
int clsz = boot->SecPerClust * boot->BytesPerSec;
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) {
perr("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) {
perr("Unable to write directory");
return FSFATAL;
}
if (startcl == endcl)
break;
startcl = fat_get(startcl);
if (startcl == CLUST_ERROR)
return FSFATAL;
s = delbuf;
}
return FSOK;
}
static int
msdosfs_removede(f, boot, start, end, startcl, endcl, curcl, path, type)
int f;
struct bootblock *boot;
u_char *start;
u_char *end;
cl_t startcl;
cl_t endcl;
cl_t curcl;
char *path;
int type;
{
switch (type) {
case 0:
pwarn("Invalid long filename entry for %s\n", path);
break;
case 1:
pwarn("Invalid long filename entry at end of directory %s\n", path);
break;
case 2:
pwarn("Invalid long filename entry for volume label\n");
break;
}
if (ask(0, "Remove")) {
if (startcl != curcl) {
if (delete(f, boot,
startcl, start - buffer,
endcl, end - buffer,
endcl == curcl) == FSFATAL)
return FSFATAL;
start = buffer;
}
if (endcl == curcl)
for (; start < end; start += 32)
*start = SLOT_DELETED;
return FSDIRMOD;
}
return FSERROR;
}
static int
checksize(boot, p, dir)
struct bootblock *boot;
u_char *p;
struct dosDirEntry *dir;
{
if (dir->physicalSize < dir->size) {
pwarn("size of %s is %u, should at most be %llu\n",
fullpath(dir), dir->size, dir->physicalSize);
if (ask(1, "Truncate")) {
dir->size = dir->physicalSize;
p[28] = (u_char)dir->physicalSize;
p[29] = (u_char)(dir->physicalSize >> 8);
p[30] = (u_char)(dir->physicalSize >> 16);
p[31] = (u_char)(dir->physicalSize >> 24);
return FSDIRMOD;
} else
return FSERROR;
} else if (dir->physicalSize - dir->size >= boot->ClusterSize) {
pwarn("%s has too many clusters allocated (logical=%u, physical=%llu)\n",
fullpath(dir), dir->size, dir->physicalSize);
if (ask(1, "Drop superfluous clusters")) {
int mod = 0;
cl_t cl, next;
u_int64_t sz = 0;
if (dir->size == 0)
{
p[20]=p[21]=p[26]=p[27] = 0;
mod = FSDIRMOD;
cl = dir->head;
}
else
{
for (cl = dir->head; (sz += boot->ClusterSize) < dir->size;)
{
cl = fat_get(cl);
if (cl == CLUST_ERROR)
return FSFATAL;
}
next = fat_get(cl);
if (next == CLUST_ERROR)
return FSFATAL;
if (fat_set(cl, CLUST_EOF))
return FSFATAL;
cl = next;
}
while (sz < dir->physicalSize)
{
next = fat_get(cl);
if (next == CLUST_ERROR)
return FSFATAL;
if (fat_set(cl, CLUST_FREE))
return FSFATAL;
cl = next;
sz += boot->ClusterSize;
}
return mod | FSFATMOD;
} else
return FSERROR;
}
return FSOK;
}
static int
readDosDirSection(f, boot, dir)
int f;
struct bootblock *boot;
struct dosDirEntry *dir;
{
struct dosDirEntry dirent, *d;
u_char *p, *vallfn, *invlfn, *empty;
off_t off;
int i, j, k, last;
cl_t cl, valcl = ~0, invcl = ~0, empcl = ~0;
cl_t last_cl;
char *t;
u_int lidx = 0;
int shortSum;
int mod = FSOK;
#define THISMOD 0x8000
cl = dir->head;
if (dir->parent && (cl < CLUST_FIRST || cl >= boot->NumClusters)) {
fprintf(stderr, "readDosDirSection: Start cluster (%u) out of range; ignoring\n", cl);
return FSOK;
}
shortSum = -1;
vallfn = invlfn = empty = NULL;
do {
last_cl = cl;
if (!(boot->flags & FAT32) && !dir->parent) {
last = boot->RootDirEnts * 32;
off = boot->ResSectors + boot->FATs * boot->FATsecs;
} else {
last = boot->SecPerClust * boot->BytesPerSec;
off = cl * boot->SecPerClust + boot->ClusterOffset;
}
off *= boot->BytesPerSec;
if (lseek(f, off, SEEK_SET) != off
|| read(f, buffer, last) != last) {
perr("Unable to read directory");
return FSFATAL;
}
last /= 32;
for (p = buffer, i = 0; i < last; i++, p += 32) {
if (dir->fsckflags & DIREMPWARN) {
*p = SLOT_EMPTY;
continue;
}
if (*p == SLOT_EMPTY || *p == SLOT_DELETED) {
if (*p == SLOT_EMPTY) {
dir->fsckflags |= DIREMPTY;
empty = p;
empcl = cl;
}
continue;
}
if (dir->fsckflags & DIREMPTY) {
if (!(dir->fsckflags & DIREMPWARN)) {
pwarn("%s has entries after end of directory\n",
fullpath(dir));
if (ask(1, "Truncate"))
dir->fsckflags |= DIREMPWARN;
else if (ask(0, "Extend")) {
u_char *q;
dir->fsckflags &= ~DIREMPTY;
if (delete(f, boot,
empcl, empty - buffer,
cl, p - buffer, 1) == FSFATAL)
return FSFATAL;
q = empcl == cl ? empty : buffer;
for (; q < p; q += 32)
*q = SLOT_DELETED;
mod |= THISMOD|FSDIRMOD;
}
}
if (dir->fsckflags & DIREMPWARN) {
*p = SLOT_DELETED;
mod |= THISMOD|FSDIRMOD;
continue;
} else if (dir->fsckflags & DIREMPTY)
mod |= FSERROR;
empty = NULL;
}
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]
|| lidx != (*p & LRNOMASK)) {
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];
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;
}
memset(&dirent, 0, sizeof dirent);
dirent.flags = p[11];
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;
if (dirent.flags & ATTR_VOLUME) {
if (vallfn || invlfn) {
mod |= msdosfs_removede(f, boot,
invlfn ? invlfn : vallfn, p,
invlfn ? invcl : valcl, cl, cl,
fullpath(dir), 2);
vallfn = NULL;
invlfn = NULL;
}
continue;
}
if (p[8] != ' ')
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';
if (vallfn && shortSum != calcShortSum(p)) {
if (!invlfn) {
invlfn = vallfn;
invcl = valcl;
}
vallfn = NULL;
}
dirent.head = p[26] | (p[27] << 8);
if (boot->ClustMask == CLUST32_MASK)
dirent.head |= (p[20] << 16) | (p[21] << 24);
dirent.size = p[28] | (p[29] << 8) | (p[30] << 16) | (p[31] << 24);
if (vallfn) {
strlcpy(dirent.lname, longName, sizeof(dirent.lname));
longName[0] = '\0';
shortSum = -1;
}
dirent.parent = dir;
dirent.next = dir->child;
if (invlfn) {
mod |= k = msdosfs_removede(f, boot,
invlfn, vallfn ? vallfn : p,
invcl, vallfn ? valcl : cl, cl,
fullpath(&dirent), 0);
if (mod & FSFATAL)
return FSFATAL;
if (vallfn
? (valcl == cl && vallfn != buffer)
: p != buffer)
if (k & FSDIRMOD)
mod |= THISMOD;
}
vallfn = NULL;
invlfn = NULL;
if (!strcmp(dirent.name, ".") || !strcmp(dirent.name,".."))
{
goto MarkedChain;
}
else
{
if (dirent.head == CLUST_FREE)
{
if (dirent.flags & ATTR_DIRECTORY || dirent.size != 0)
{
pwarn("%s has no clusters\n", fullpath(&dirent));
goto remove_or_truncate;
}
}
else
{
cl_t next;
if (dirent.head < CLUST_FIRST || dirent.head >= boot->NumClusters)
{
pwarn("%s starts with cluster out of range (%u)\n",
fullpath(&dirent), dirent.head);
goto remove_or_truncate;
}
if (isUsed(dirent.head))
{
pwarn("%s starts with cross-linked cluster (%u)\n",
fullpath(&dirent), dirent.head);
goto remove_or_truncate;
}
next = fat_get(dirent.head);
if (next == CLUST_ERROR)
return FSFATAL;
if (next == CLUST_FREE)
{
pwarn("%s starts with free cluster\n", fullpath(&dirent));
goto remove_or_truncate;
}
if (next >= CLUST_RSRVD && next < CLUST_EOFS)
{
pwarn("%s starts with cluster marked %s\n",
fullpath(&dirent),
rsrvdcltype(next));
remove_or_truncate:
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;
p[26] = p[27] = 0;
if (boot->ClustMask == CLUST32_MASK)
p[20] = p[21] = 0;
dirent.head = 0;
dirent.size = 0;
mod |= THISMOD|FSDIRMOD;
} else
mod |= FSERROR;
}
}
}
}
if (dirent.head >= CLUST_FIRST && dirent.head < boot->NumClusters)
{
mod |= markDosDirChain(boot, &dirent);
if (mod & FSFATAL)
return FSFATAL;
}
MarkedChain:
if (dirent.flags & ATTR_DIRECTORY) {
struct dirTodoNode *n;
if (dirent.size) {
pwarn("Directory %s has size != 0\n",
fullpath(&dirent));
if (ask(1, "Correct")) {
p[28] = p[29] = p[30] = p[31] = 0;
dirent.size = 0;
mod |= THISMOD|FSDIRMOD;
} else
mod |= FSERROR;
}
if (strcmp(dirent.name, ".") == 0) {
if (dirent.head != dir->head) {
pwarn("`.' entry in %s has incorrect start cluster\n",
fullpath(dir));
if (ask(1, "Correct")) {
dirent.head = dir->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;
}
continue;
}
if (strcmp(dirent.name, "..") == 0) {
if (dir->parent) {
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;
}
}
continue;
}
if (!(d = newDosDirEntry())) {
perr("No space for directory");
return FSFATAL;
}
memcpy(d, &dirent, sizeof(struct dosDirEntry));
dir->child = d;
if (!(n = newDirTodo())) {
perr("No space for todo list");
return FSFATAL;
}
n->next = pendingDirectories;
n->dir = d;
pendingDirectories = n;
} else {
mod |= k = checksize(boot, p, &dirent);
if (k & FSDIRMOD)
mod |= THISMOD;
}
boot->NumFiles++;
}
if (mod & THISMOD) {
if (lseek(f, off, SEEK_SET) != off
|| write(f, buffer, last*32) != last*32) {
perr("Unable to write directory");
return FSFATAL;
}
mod &= ~THISMOD;
}
if (!(boot->flags & FAT32) && !dir->parent)
break;
} while ((cl = fat_get(cl)) >= CLUST_FIRST && cl < boot->NumClusters && cl != dir->end);
if (cl == CLUST_ERROR)
mod |= FSFATAL;
if (invlfn || vallfn)
{
mod |= msdosfs_removede(f, boot,
invlfn ? invlfn : vallfn, p,
invlfn ? invcl : valcl, last_cl, last_cl,
fullpath(dir), 1);
if (lseek(f, off, SEEK_SET) != off
|| write(f, buffer, last*32) != last*32) {
perr("Unable to write directory");
return FSFATAL;
}
}
return mod & ~THISMOD;
}
int
handleDirTree(int dosfs, struct bootblock *boot)
{
int mod;
mod = readDosDirSection(dosfs, boot, rootDir);
if (mod & FSFATAL)
return FSFATAL;
while (pendingDirectories) {
struct dosDirEntry *dir = pendingDirectories->dir;
struct dirTodoNode *n = pendingDirectories->next;
freeDirTodo(pendingDirectories);
pendingDirectories = n;
mod |= readDosDirSection(dosfs, boot, dir);
if (mod & FSFATAL)
return FSFATAL;
}
return mod;
}