]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/makefs/msdos/msdosfs_lookup.c
bhyvectl(8): Normalize the man page date
[FreeBSD/FreeBSD.git] / usr.sbin / makefs / msdos / msdosfs_lookup.c
1 /* $FreeBSD$ */
2 /*      $NetBSD: msdosfs_lookup.c,v 1.37 1997/11/17 15:36:54 ws Exp $   */
3
4 /*-
5  * SPDX-License-Identifier: BSD-4-Clause
6  *
7  * Copyright (C) 1994, 1995, 1997 Wolfgang Solfrank.
8  * Copyright (C) 1994, 1995, 1997 TooLs GmbH.
9  * All rights reserved.
10  * Original code by Paul Popelka (paulp@uts.amdahl.com) (see below).
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions and the following disclaimer.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. All advertising materials mentioning features or use of this software
21  *    must display the following acknowledgement:
22  *      This product includes software developed by TooLs GmbH.
23  * 4. The name of TooLs GmbH may not be used to endorse or promote products
24  *    derived from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
27  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
32  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
33  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
34  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 /*-
38  * Written by Paul Popelka (paulp@uts.amdahl.com)
39  *
40  * You can do anything you want with this software, just don't say you wrote
41  * it, and don't remove this notice.
42  *
43  * This software is provided "as is".
44  *
45  * The author supplies this software to be publicly redistributed on the
46  * understanding that the author is not responsible for the correct
47  * functioning of this software in any circumstances and is not liable for
48  * any damages caused by this software.
49  *
50  * October 1992
51  */
52
53 #include <sys/param.h>
54 #include <sys/errno.h>
55
56 #include <stdbool.h>
57 #include <stdio.h>
58 #include <string.h>
59
60 #include "ffs/buf.h"
61 #include <fs/msdosfs/bpb.h>
62 #include "msdos/direntry.h"
63 #include <fs/msdosfs/denode.h>
64 #include <fs/msdosfs/fat.h>
65 #include <fs/msdosfs/msdosfsmount.h>
66
67 #include "makefs.h"
68 #include "msdos.h"
69
70 /*
71  * dep  - directory entry to copy into the directory
72  * ddep - directory to add to
73  * depp - return the address of the denode for the created directory entry
74  *        if depp != 0
75  * cnp  - componentname needed for Win95 long filenames
76  */
77 int
78 createde(struct denode *dep, struct denode *ddep, struct denode **depp,
79     struct componentname *cnp)
80 {
81         int error;
82         u_long dirclust, diroffset;
83         struct direntry *ndep;
84         struct msdosfsmount *pmp = ddep->de_pmp;
85         struct buf *bp;
86         daddr_t bn;
87         int blsize;
88
89         MSDOSFS_DPRINTF(("createde(dep %p, ddep %p, depp %p, cnp %p)\n",
90             dep, ddep, depp, cnp));
91
92         /*
93          * If no space left in the directory then allocate another cluster
94          * and chain it onto the end of the file.  There is one exception
95          * to this.  That is, if the root directory has no more space it
96          * can NOT be expanded.  extendfile() checks for and fails attempts
97          * to extend the root directory.  We just return an error in that
98          * case.
99          */
100         if (ddep->de_fndoffset >= ddep->de_FileSize) {
101                 diroffset = ddep->de_fndoffset + sizeof(struct direntry)
102                     - ddep->de_FileSize;
103                 dirclust = de_clcount(pmp, diroffset);
104                 error = extendfile(ddep, dirclust, 0, 0, DE_CLEAR);
105                 if (error) {
106                         (void)detrunc(ddep, ddep->de_FileSize, 0, NULL);
107                         return error;
108                 }
109
110                 /*
111                  * Update the size of the directory
112                  */
113                 ddep->de_FileSize += de_cn2off(pmp, dirclust);
114         }
115
116         /*
117          * We just read in the cluster with space.  Copy the new directory
118          * entry in.  Then write it to disk. NOTE:  DOS directories
119          * do not get smaller as clusters are emptied.
120          */
121         error = pcbmap(ddep, de_cluster(pmp, ddep->de_fndoffset),
122                        &bn, &dirclust, &blsize);
123         if (error)
124                 return error;
125         diroffset = ddep->de_fndoffset;
126         if (dirclust != MSDOSFSROOT)
127                 diroffset &= pmp->pm_crbomask;
128         if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp)) != 0) {
129                 brelse(bp);
130                 return error;
131         }
132         ndep = bptoep(pmp, bp, ddep->de_fndoffset);
133
134         DE_EXTERNALIZE(ndep, dep);
135
136         /*
137          * Now write the Win95 long name
138          */
139         if (ddep->de_fndcnt > 0) {
140                 uint8_t chksum = winChksum(ndep->deName);
141                 const u_char *un = (const u_char *)cnp->cn_nameptr;
142                 int unlen = cnp->cn_namelen;
143                 int cnt = 1;
144
145                 while (--ddep->de_fndcnt >= 0) {
146                         if (!(ddep->de_fndoffset & pmp->pm_crbomask)) {
147                                 if ((error = bwrite(bp)) != 0)
148                                         return error;
149
150                                 ddep->de_fndoffset -= sizeof(struct direntry);
151                                 error = pcbmap(ddep,
152                                                de_cluster(pmp,
153                                                           ddep->de_fndoffset),
154                                                &bn, 0, &blsize);
155                                 if (error)
156                                         return error;
157
158                                 error = bread(pmp->pm_devvp, bn, blsize,
159                                               NOCRED, &bp);
160                                 if (error) {
161                                         brelse(bp);
162                                         return error;
163                                 }
164                                 ndep = bptoep(pmp, bp, ddep->de_fndoffset);
165                         } else {
166                                 ndep--;
167                                 ddep->de_fndoffset -= sizeof(struct direntry);
168                         }
169                         if (!unix2winfn(un, unlen, (struct winentry *)ndep,
170                                         cnt++, chksum))
171                                 break;
172                 }
173         }
174
175         if ((error = bwrite(bp)) != 0)
176                 return error;
177
178         /*
179          * If they want us to return with the denode gotten.
180          */
181         if (depp) {
182                 if (dep->de_Attributes & ATTR_DIRECTORY) {
183                         dirclust = dep->de_StartCluster;
184                         if (FAT32(pmp) && dirclust == pmp->pm_rootdirblk)
185                                 dirclust = MSDOSFSROOT;
186                         if (dirclust == MSDOSFSROOT)
187                                 diroffset = MSDOSFSROOT_OFS;
188                         else
189                                 diroffset = 0;
190                 }
191                 return deget(pmp, dirclust, diroffset, depp);
192         }
193
194         return 0;
195 }
196
197 /*
198  * Read in the disk block containing the directory entry (dirclu, dirofs)
199  * and return the address of the buf header, and the address of the
200  * directory entry within the block.
201  */
202 int
203 readep(struct msdosfsmount *pmp, u_long dirclust, u_long diroffset,
204     struct buf **bpp, struct direntry **epp)
205 {
206         int error;
207         daddr_t bn;
208         int blsize;
209
210         blsize = pmp->pm_bpcluster;
211         if (dirclust == MSDOSFSROOT
212             && de_blk(pmp, diroffset + blsize) > pmp->pm_rootdirsize)
213                 blsize = de_bn2off(pmp, pmp->pm_rootdirsize) & pmp->pm_crbomask;
214         bn = detobn(pmp, dirclust, diroffset);
215         if ((error = bread(pmp->pm_devvp, bn, blsize, NOCRED, bpp)) != 0) {
216                 brelse(*bpp);
217                 *bpp = NULL;
218                 return (error);
219         }
220         if (epp)
221                 *epp = bptoep(pmp, *bpp, diroffset);
222         return (0);
223 }
224
225 /*
226  * Read in the disk block containing the directory entry dep came from and
227  * return the address of the buf header, and the address of the directory
228  * entry within the block.
229  */
230 int
231 readde(struct denode *dep, struct buf **bpp, struct direntry **epp)
232 {
233
234         return (readep(dep->de_pmp, dep->de_dirclust, dep->de_diroffset,
235             bpp, epp));
236 }
237
238 /*
239  * Create a unique DOS name in dvp
240  */
241 int
242 uniqdosname(struct denode *dep, struct componentname *cnp, u_char *cp)
243 {
244         struct msdosfsmount *pmp = dep->de_pmp;
245         struct direntry *dentp;
246         int gen;
247         int blsize;
248         u_long cn;
249         daddr_t bn;
250         struct buf *bp;
251         int error;
252
253         if (pmp->pm_flags & MSDOSFSMNT_SHORTNAME)
254                 return (unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
255                     cnp->cn_namelen, 0) ? 0 : EINVAL);
256
257         for (gen = 1;; gen++) {
258                 /*
259                  * Generate DOS name with generation number
260                  */
261                 if (!unix2dosfn((const u_char *)cnp->cn_nameptr, cp,
262                     cnp->cn_namelen, gen))
263                         return gen == 1 ? EINVAL : EEXIST;
264
265                 /*
266                  * Now look for a dir entry with this exact name
267                  */
268                 for (cn = error = 0; !error; cn++) {
269                         if ((error = pcbmap(dep, cn, &bn, 0, &blsize)) != 0) {
270                                 if (error == E2BIG)     /* EOF reached and not found */
271                                         return 0;
272                                 return error;
273                         }
274                         error = bread(pmp->pm_devvp, bn, blsize, NOCRED, &bp);
275                         if (error) {
276                                 brelse(bp);
277                                 return error;
278                         }
279                         for (dentp = (struct direntry *)bp->b_data;
280                              (char *)dentp < bp->b_data + blsize;
281                              dentp++) {
282                                 if (dentp->deName[0] == SLOT_EMPTY) {
283                                         /*
284                                          * Last used entry and not found
285                                          */
286                                         brelse(bp);
287                                         return 0;
288                                 }
289                                 /*
290                                  * Ignore volume labels and Win95 entries
291                                  */
292                                 if (dentp->deAttributes & ATTR_VOLUME)
293                                         continue;
294                                 if (!bcmp(dentp->deName, cp, 11)) {
295                                         error = EEXIST;
296                                         break;
297                                 }
298                         }
299                         brelse(bp);
300                 }
301         }
302 }