#include <sys/param.h>
#include <sys/errno.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fs/msdosfs/bpb.h>
#include "msdos/denode.h"
#include <fs/msdosfs/fat.h>
#include <fs/msdosfs/msdosfsmount.h>
#include "makefs.h"
#include "msdos.h"
static int msdosfs_wfile(const char *, struct denode *, fsnode *);
static void unix2fattime(const struct timespec *tsp, uint16_t *ddp,
uint16_t *dtp);
static void
msdosfs_times(struct denode *dep, const struct stat *st)
{
#if HAVE_STRUCT_STAT_BIRTHTIME
unix2fattime(&st->st_birthtim, &dep->de_CDate, &dep->de_CTime);
#else
unix2fattime(&st->st_ctim, &dep->de_CDate, &dep->de_CTime);
#endif
unix2fattime(&st->st_atim, &dep->de_ADate, NULL);
unix2fattime(&st->st_mtim, &dep->de_MDate, &dep->de_MTime);
}
static void
unix2fattime(const struct timespec *tsp, uint16_t *ddp, uint16_t *dtp)
{
time_t t1;
struct tm lt = {0};
t1 = tsp->tv_sec;
gmtime_r(&t1, <);
unsigned long fat_time = ((lt.tm_year - 80) << 25) |
((lt.tm_mon + 1) << 21) |
(lt.tm_mday << 16) |
(lt.tm_hour << 11) |
(lt.tm_min << 5) |
(lt.tm_sec >> 1);
if (ddp != NULL)
*ddp = (uint16_t)(fat_time >> 16);
if (dtp != NULL)
*dtp = (uint16_t)fat_time;
}
static int
msdosfs_findslot(struct denode *dp, struct componentname *cnp)
{
daddr_t bn;
int error;
int slotcount;
int slotoffset = 0;
int frcn;
u_long cluster;
int blkoff;
u_int diroff;
int blsize;
struct msdosfsmount *pmp;
struct m_buf *bp = 0;
struct direntry *dep;
u_char dosfilename[12];
int wincnt = 1;
int chksum = -1, chksum_ok;
int olddos = 1;
pmp = dp->de_pmp;
switch (unix2dosfn((const u_char *)cnp->cn_nameptr, dosfilename,
cnp->cn_namelen, 0)) {
case 0:
return (EINVAL);
case 1:
break;
case 2:
wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
cnp->cn_namelen) + 1;
break;
case 3:
olddos = 0;
wincnt = winSlotCnt((const u_char *)cnp->cn_nameptr,
cnp->cn_namelen) + 1;
break;
}
if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
wincnt = 1;
slotcount = 0;
MSDOSFS_DPRINTF(("%s(): dos filename: %s\n", __func__, dosfilename));
diroff = 0;
for (frcn = 0; diroff < dp->de_FileSize; frcn++) {
if ((error = pcbmap(dp, frcn, &bn, &cluster, &blsize)) != 0) {
if (error == E2BIG)
break;
return (error);
}
error = bread((void *)pmp->pm_devvp, bn, blsize, 0, &bp);
if (error) {
return (error);
}
for (blkoff = 0; blkoff < blsize;
blkoff += sizeof(struct direntry),
diroff += sizeof(struct direntry)) {
dep = (struct direntry *)(bp->b_data + blkoff);
if (dep->deName[0] == SLOT_EMPTY ||
dep->deName[0] == SLOT_DELETED) {
chksum = -1;
if (slotcount < wincnt) {
slotcount++;
slotoffset = diroff;
}
if (dep->deName[0] == SLOT_EMPTY) {
brelse(bp);
goto notfound;
}
} else {
if (slotcount < wincnt)
slotcount = 0;
if (dep->deAttributes == ATTR_WIN95) {
if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
continue;
chksum = winChkName(
(const u_char *)cnp->cn_nameptr,
cnp->cn_namelen,
(struct winentry *)dep, chksum);
continue;
}
if (dep->deAttributes & ATTR_VOLUME) {
chksum = -1;
continue;
}
chksum_ok = (chksum == winChksum(dep->deName));
if (!chksum_ok
&& (!olddos || memcmp(dosfilename, dep->deName, 11))) {
chksum = -1;
continue;
}
MSDOSFS_DPRINTF(("%s(): match blkoff %d, diroff %u\n",
__func__, blkoff, diroff));
dp->de_fndoffset = diroff;
dp->de_fndcnt = 0;
return EEXIST;
}
}
brelse(bp);
}
notfound:
MSDOSFS_DPRINTF(("%s(): refcnt %ld, slotcount %d, slotoffset %d\n",
__func__, dp->de_refcnt, slotcount, slotoffset));
if (!slotcount) {
slotcount = 1;
slotoffset = diroff;
}
if (wincnt > slotcount) {
slotoffset += sizeof(struct direntry) * (wincnt - slotcount);
}
dp->de_fndoffset = slotoffset;
dp->de_fndcnt = wincnt - 1;
return 0;
}
struct denode *
msdosfs_mkfile(const char *path, struct denode *pdep, fsnode *node)
{
struct componentname cn;
struct denode ndirent;
struct denode *dep;
int error;
struct stat *st = &node->inode->st;
cn.cn_nameptr = node->name;
cn.cn_namelen = strlen(node->name);
MSDOSFS_DPRINTF(("%s(name %s, mode 0%o size %zu)\n",
__func__, node->name, st->st_mode, (size_t)st->st_size));
if (pdep->de_StartCluster == MSDOSFSROOT
&& pdep->de_fndoffset >= pdep->de_FileSize) {
error = ENOSPC;
goto bad;
}
memset(&ndirent, 0, sizeof(ndirent));
if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
goto bad;
ndirent.de_Attributes = (st->st_mode & S_IWUSR) ?
ATTR_ARCHIVE : ATTR_ARCHIVE | ATTR_READONLY;
ndirent.de_StartCluster = 0;
ndirent.de_FileSize = 0;
ndirent.de_pmp = pdep->de_pmp;
ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
msdosfs_times(&ndirent, &node->inode->st);
if ((error = msdosfs_findslot(pdep, &cn)) != 0)
goto bad;
if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
goto bad;
if ((error = msdosfs_wfile(path, dep, node)) != 0)
goto bad;
return dep;
bad:
errno = error;
return NULL;
}
static int
msdosfs_updatede(struct denode *dep)
{
struct m_buf *bp;
struct direntry *dirp;
int error;
dep->de_flag &= ~DE_MODIFIED;
error = m_readde(dep, &bp, &dirp);
if (error)
return error;
DE_EXTERNALIZE(dirp, dep);
error = bwrite(bp);
return error;
}
static int
msdosfs_wfile(const char *path, struct denode *dep, fsnode *node)
{
int error, fd;
size_t osize = dep->de_FileSize;
struct stat *st = &node->inode->st;
size_t nsize, offs;
struct msdosfsmount *pmp = dep->de_pmp;
struct m_buf *bp;
char *dat;
u_long cn = 0;
error = 0;
MSDOSFS_DPRINTF(("%s(diroff %lu, dirclust %lu, startcluster %lu)\n",
__func__, dep->de_diroffset, dep->de_dirclust,
dep->de_StartCluster));
if (st->st_size == 0)
return 0;
if (st->st_size > MSDOSFS_FILESIZE_MAX)
return EFBIG;
nsize = st->st_size;
MSDOSFS_DPRINTF(("%s(nsize=%zu, osize=%zu)\n", __func__, nsize, osize));
if (nsize > osize) {
if ((error = deextend(dep, nsize, NULL)) != 0)
return error;
if ((error = msdosfs_updatede(dep)) != 0)
return error;
}
if ((fd = open(path, O_RDONLY)) == -1) {
error = errno;
fprintf(stderr, "open %s: %s\n", path, strerror(error));
return error;
}
if ((dat = mmap(0, nsize, PROT_READ, MAP_FILE | MAP_PRIVATE, fd, 0))
== MAP_FAILED) {
error = errno;
fprintf(stderr, "%s: mmap %s: %s\n", __func__, node->name,
strerror(error));
close(fd);
goto out;
}
close(fd);
for (offs = 0; offs < nsize;) {
int blsize, cpsize;
daddr_t bn;
u_long on = offs & pmp->pm_crbomask;
if ((error = pcbmap(dep, cn++, &bn, NULL, &blsize)) != 0) {
MSDOSFS_DPRINTF(("%s: pcbmap %lu",
__func__, (unsigned long)bn));
goto out;
}
MSDOSFS_DPRINTF(("%s(cn=%lu, bn=%llu, blsize=%d)\n",
__func__, cn, (unsigned long long)bn, blsize));
if ((error = bread((void *)pmp->pm_devvp, bn, blsize, 0,
&bp)) != 0) {
MSDOSFS_DPRINTF(("bread %d\n", error));
goto out;
}
cpsize = MIN((nsize - offs), blsize - on);
memcpy(bp->b_data + on, dat + offs, cpsize);
bwrite(bp);
offs += cpsize;
}
munmap(dat, nsize);
return 0;
out:
munmap(dat, nsize);
return error;
}
static const struct {
struct direntry dot;
struct direntry dotdot;
} dosdirtemplate = {
{ ". ",
ATTR_DIRECTORY,
0,
0, { 0, 0 }, { 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 210, 4 }, { 210, 4 },
{ 0, 0 },
{ 0, 0, 0, 0 }
},
{ ".. ",
ATTR_DIRECTORY,
0,
0, { 0, 0 }, { 0, 0 },
{ 0, 0 },
{ 0, 0 },
{ 210, 4 }, { 210, 4 },
{ 0, 0 },
{ 0, 0, 0, 0 }
}
};
struct denode *
msdosfs_mkdire(const char *path __unused, struct denode *pdep, fsnode *node)
{
struct denode ndirent;
struct denode *dep;
struct componentname cn;
struct msdosfsmount *pmp = pdep->de_pmp;
int error;
u_long newcluster, pcl, bn;
struct direntry *denp;
struct m_buf *bp;
cn.cn_nameptr = node->name;
cn.cn_namelen = strlen(node->name);
if (pdep->de_StartCluster == MSDOSFSROOT
&& pdep->de_fndoffset >= pdep->de_FileSize) {
error = ENOSPC;
goto bad2;
}
error = clusteralloc(pmp, 0, 1, CLUST_EOFE, &newcluster, NULL);
if (error)
goto bad2;
memset(&ndirent, 0, sizeof(ndirent));
ndirent.de_pmp = pmp;
ndirent.de_flag = DE_ACCESS | DE_CREATE | DE_UPDATE;
msdosfs_times(&ndirent, &node->inode->st);
bn = cntobn(pmp, newcluster);
MSDOSFS_DPRINTF(("%s(newcluster %lu, bn=%lu)\n",
__func__, newcluster, bn));
bp = getblk((void *)pmp->pm_devvp, bn, pmp->pm_bpcluster, 0, 0, 0);
memset(bp->b_data, 0, pmp->pm_bpcluster);
memcpy(bp->b_data, &dosdirtemplate, sizeof dosdirtemplate);
denp = (struct direntry *)bp->b_data;
putushort(denp[0].deStartCluster, newcluster);
putushort(denp[0].deCDate, ndirent.de_CDate);
putushort(denp[0].deCTime, ndirent.de_CTime);
denp[0].deCHundredth = ndirent.de_CHun;
putushort(denp[0].deADate, ndirent.de_ADate);
putushort(denp[0].deMDate, ndirent.de_MDate);
putushort(denp[0].deMTime, ndirent.de_MTime);
pcl = pdep->de_StartCluster;
MSDOSFS_DPRINTF(("%s(pcl %lu, rootdirblk=%lu)\n", __func__, pcl,
pmp->pm_rootdirblk));
if (FAT32(pmp) && pcl == pmp->pm_rootdirblk)
pcl = 0;
putushort(denp[1].deStartCluster, pcl);
putushort(denp[1].deCDate, ndirent.de_CDate);
putushort(denp[1].deCTime, ndirent.de_CTime);
denp[1].deCHundredth = ndirent.de_CHun;
putushort(denp[1].deADate, ndirent.de_ADate);
putushort(denp[1].deMDate, ndirent.de_MDate);
putushort(denp[1].deMTime, ndirent.de_MTime);
if (FAT32(pmp)) {
putushort(denp[0].deHighClust, newcluster >> 16);
putushort(denp[1].deHighClust, pdep->de_StartCluster >> 16);
} else {
putushort(denp[0].deHighClust, 0);
putushort(denp[1].deHighClust, 0);
}
if ((error = bwrite(bp)) != 0)
goto bad;
if ((error = uniqdosname(pdep, &cn, ndirent.de_Name)) != 0)
goto bad;
ndirent.de_Attributes = ATTR_DIRECTORY;
ndirent.de_StartCluster = newcluster;
ndirent.de_FileSize = 0;
ndirent.de_pmp = pdep->de_pmp;
if ((error = msdosfs_findslot(pdep, &cn)) != 0)
goto bad;
if ((error = createde(&ndirent, pdep, &dep, &cn)) != 0)
goto bad;
if ((error = msdosfs_updatede(dep)) != 0)
goto bad;
return dep;
bad:
clusterfree(pmp, newcluster);
bad2:
errno = error;
return NULL;
}