/* $FreeBSD$ */
/* $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $ */
/*-
* SPDX-License-Identifier: BSD-4-Clause
*
* Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
* Copyright (C) 1994, 1995, 1997 TooLs GmbH.
* All rights reserved.
* Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
*
* 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 TooLs GmbH.
* 4. The name of TooLs GmbH may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``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 TOOLS GMBH 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.
*/
/*-
* Written by Paul Popelka (paulp@uts.amdahl.com)
*
* You can do anything you want with this software, just don't say you wrote
* it, and don't remove this notice.
*
* This software is provided "as is".
*
* The author supplies this software to be publicly redistributed on the
* understanding that the author is not responsible for the correct
* functioning of this software in any circumstances and is not liable for
* any damages caused by this software.
*
* October 1992
*/
#include <sys/param.h>
#include <sys/errno.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include "ffs/buf.h"
#include <fs/msdosfs/bpb.h>
#include "msdos/direntry.h"
#include <fs/msdosfs/denode.h>
#include <fs/msdosfs/fat.h>
#include <fs/msdosfs/msdosfsmount.h>
#include "makefs.h"
#include "msdos.h"
/*
* dep - directory entry to copy into the directory
* ddep - directory to add to
* depp - return the address of the denode for the created directory entry
* if depp != 0
* cnp - componentname needed for Win95 long filenames
*/
int
createde(struct denode *dep, struct denode *ddep, struct denode **depp,
struct componentname *cnp)
{
int error;
u_long dirclust, diroffset;
struct direntry *ndep;
struct msdosfsmount *pmp = ddep->de_pmp;
struct buf *bp;
daddr_t bn;
int blsize;
MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
dep, ddep, depp, cnp));
/*
* If no space left in the directory then allocate another cluster
* and chain it onto the end of the file. There is one exception
* to this. That is, if the root directory has no more space it
* can NOT be expanded. extendfile() checks for and fails attempts
* to extend the root directory. We just return an error in that
* case.
*/
if (ddep->de_fndoffset >= ddep->de_FileSize) {
diroffset = ddep->de_fndoffset + sizeof(struct direntry)
- ddep->de_FileSize;
dirclust = de_clcount(pmp, diroffset);
error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
if (error) {
(void)detrunc(ddep, ddep->de_FileSize, 0, NULL);
return error;
}
/*
* Update the size of the directory
*/
ddep->de_FileSize += de_cn2off(pmp, dirclust);
}
/*
* We just read in the cluster with space. Copy the new directory
* entry in. Then write it to disk. NOTE: DOS directories
* do not get smaller as clusters are emptied.
*/
error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
&bn, &dirclust, &blsize);
if (error)
return error;
diroffset = ddep->de_fndoffset;
if (dirclust != MSDOSFSROOT)
diroffset &= pmp->pm_crbomask;
if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
brelse(bp);
return error;
}
ndep = bptoep(pmp, bp, ddep->de_fndoffset);
DE_EXTERNALIZE(ndep, dep);
/*
* Now write the Win95 long name
*/
if (ddep->de_fndcnt > 0) {
uint8_t chksum = winChksum(ndep->deName);
const u_char *un = (const u_char *)cnp->cn_nameptr;
int unlen = cnp->cn_namelen;
int cnt = 1;
while (--ddep->de_fndcnt >= 0) {
if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
if ((error = bwrite(bp)) != 0)
return error;
ddep->de_fndoffset -= sizeof(struct direntry);
error = pcbmap(ddep,
de_cluster(pmp,
ddep->de_fndoffset),
&bn, 0, &blsize);
if (error)
return error;
error = bread(pmp->pm_devvp, bn, blsize,
NOCRED, &bp);
if (error) {
brelse(bp);
return error;
}
ndep = bptoep(pmp, bp, ddep->de_fndoffset);
} else {
ndep--;
ddep->de_fndoffset -= sizeof(struct direntry);
}
if (!unix2winfn(un, unlen, (struct winentry *)ndep,
cnt++, chksum))
break;
}
}
if ((error = bwrite(bp)) != 0)
return error;
/*
* If they want us to return with the denode gotten.
*/
if (depp) {
if (dep->de_Attributes & ATTR_DIRECTORY) {
dirclust = dep->de_StartCluster;
if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
dirclust = MSDOSFSROOT;
if (dirclust == MSDOSFSROOT)
diroffset = MSDOSFSROOT_OFS;
else
diroffset = 0;
}
return deget(pmp, dirclust, diroffset, depp);
}
return 0;
}
/*
* Read in the disk block containing the directory entry (dirclu, dirofs)
* and return the address of the buf header, and the address of the
* directory entry within the block.
*/
int
readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
struct buf **bpp, struct direntry **epp)
{
int error;
daddr_t bn;
int blsize;
blsize = pmp->pm_bpcluster;
if (dirclust == MSDOSFSROOT
&& de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
bn = detobn(pmp, dirclust, diroffset);
if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
brelse(*bpp);
*bpp = NULL;
return (error);
}
if (epp)
*epp = bptoep(pmp, *bpp, diroffset);
return (0);
}
/*
* Read in the disk block containing the directory entry dep came from and
* return the address of the buf header, and the address of the directory
* entry within the block.
*/
int
readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
{
return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
bpp, epp));
}
/*
* Create a unique DOS name in dvp
*/
int
uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
{
struct msdosfsmount *pmp = dep->de_pmp;
struct direntry *dentp;
int gen;
int blsize;
u_long cn;
daddr_t bn;
struct buf *bp;
int error;
if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
cnp->cn_namelen, 0) ? 0 : EINVAL);
for (gen = 1;; gen++) {
/*
* Generate DOS name with generation number
*/
if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
cnp->cn_namelen, gen))
return gen == 1 ? EINVAL : EEXIST;
/*
* Now look for a dir entry with this exact name
*/
for (cn = error = 0; !error; cn++) {
if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
if (error == E2BIG) /* EOF reached and not found */
return 0;
return error;
}
error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
if (error) {
brelse(bp);
return error;
}
for (dentp = (struct direntry *)bp->b_data;
(char *)dentp < bp->b_data + blsize;
dentp++) {
if (dentp->deName[0] == SLOT_EMPTY) {
/*
* Last used entry and not found
*/
brelse(bp);
return 0;
}
/*
* Ignore volume labels and Win95 entries
*/
if (dentp->deAttributes & ATTR_VOLUME)
continue;
if (!bcmp(dentp->deName, cp, 11)) {
error = EEXIST;
break;
}
}
brelse(bp);
}
}
}