199767f8919635c4928607450d9e0abb932109ceToomas Soome/*-
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright (c) 1982, 1986, 1989, 1993
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The Regents of the University of California. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * (c) UNIX System Laboratories, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * All or some portions of this file are derived from material licensed
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to the University of California by American Telephone and Telegraph
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Co. or Unix System Laboratories, Inc. and are reproduced herein with
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the permission of UNIX System Laboratories, Inc.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Redistribution and use in source and binary forms, with or without
199767f8919635c4928607450d9e0abb932109ceToomas Soome * modification, are permitted provided that the following conditions
199767f8919635c4928607450d9e0abb932109ceToomas Soome * are met:
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1. Redistributions of source code must retain the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 2. Redistributions in binary form must reproduce the above copyright
199767f8919635c4928607450d9e0abb932109ceToomas Soome * notice, this list of conditions and the following disclaimer in the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * documentation and/or other materials provided with the distribution.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 4. Neither the name of the University nor the names of its contributors
199767f8919635c4928607450d9e0abb932109ceToomas Soome * may be used to endorse or promote products derived from this software
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without specific prior written permission.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
199767f8919635c4928607450d9e0abb932109ceToomas Soome * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
199767f8919635c4928607450d9e0abb932109ceToomas Soome * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
199767f8919635c4928607450d9e0abb932109ceToomas Soome * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
199767f8919635c4928607450d9e0abb932109ceToomas Soome * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
199767f8919635c4928607450d9e0abb932109ceToomas Soome * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
199767f8919635c4928607450d9e0abb932109ceToomas Soome * SUCH DAMAGE.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * @(#)dir.h 8.2 (Berkeley) 1/21/94
199767f8919635c4928607450d9e0abb932109ceToomas Soome * $FreeBSD$
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#ifndef _UFS_UFS_DIR_H_
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define _UFS_UFS_DIR_H_
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Theoretically, directories can be more than 2Gb in length, however, in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * practice this seems unlikely. So, we define the type doff_t as a 32-bit
199767f8919635c4928607450d9e0abb932109ceToomas Soome * quantity to keep down the cost of doing lookup on a 32-bit machine.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define doff_t int32_t
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAXDIRSIZE (0x7fffffff)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * A directory consists of some number of blocks of DIRBLKSIZ
199767f8919635c4928607450d9e0abb932109ceToomas Soome * bytes, where DIRBLKSIZ is chosen such that it can be transferred
199767f8919635c4928607450d9e0abb932109ceToomas Soome * to disk in a single atomic operation (e.g. 512 bytes on most machines).
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Each DIRBLKSIZ byte block contains some number of directory entry
199767f8919635c4928607450d9e0abb932109ceToomas Soome * structures, which are of variable length. Each directory entry has
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a struct direct at the front of it, containing its inode number,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the length of the entry, and the length of the name contained in
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the entry. These are followed by the name padded to a 4 byte boundary
199767f8919635c4928607450d9e0abb932109ceToomas Soome * with null bytes. All names are guaranteed null terminated.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The maximum length of a name in a directory is MAXNAMLEN.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The macro DIRSIZ(fmt, dp) gives the amount of space required to represent
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a directory entry. Free space in a directory is represented by
199767f8919635c4928607450d9e0abb932109ceToomas Soome * entries which have dp->d_reclen > DIRSIZ(fmt, dp). All DIRBLKSIZ bytes
199767f8919635c4928607450d9e0abb932109ceToomas Soome * in a directory block are claimed by the directory entries. This
199767f8919635c4928607450d9e0abb932109ceToomas Soome * usually results in the last entry in a directory having a large
199767f8919635c4928607450d9e0abb932109ceToomas Soome * dp->d_reclen. When entries are deleted from a directory, the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * space is returned to the previous entry in the same directory
199767f8919635c4928607450d9e0abb932109ceToomas Soome * block by increasing its dp->d_reclen. If the first entry of
199767f8919635c4928607450d9e0abb932109ceToomas Soome * a directory block is free, then its dp->d_ino is set to 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Entries other than the first in a directory do not normally have
199767f8919635c4928607450d9e0abb932109ceToomas Soome * dp->d_ino set to 0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DIRBLKSIZ DEV_BSIZE
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define MAXNAMLEN 255
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct direct {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t d_ino; /* inode number of entry */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t d_reclen; /* length of this record */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t d_namlen; /* length of string in d_name */
199767f8919635c4928607450d9e0abb932109ceToomas Soome char d_name[MAXNAMLEN + 1];/* name with length <= MAXNAMLEN */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * File types
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_UNKNOWN 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_FIFO 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_CHR 2
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_DIR 4
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_BLK 6
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_REG 8
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_LNK 10
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_SOCK 12
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DT_WHT 14
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Convert between stat structure types and directory types.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define IFTODT(mode) (((mode) & 0170000) >> 12)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DTTOIF(dirtype) ((dirtype) << 12)
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The DIRSIZ macro gives the minimum record length which will hold
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the directory entry. This requires the amount of space in struct direct
199767f8919635c4928607450d9e0abb932109ceToomas Soome * without the d_name field, plus enough space for the name with a terminating
199767f8919635c4928607450d9e0abb932109ceToomas Soome * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DIRECTSIZ(namlen) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((__offsetof(struct direct, d_name) + \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((namlen)+1)*sizeof(((struct direct *)0)->d_name[0]) + 3) & ~3)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#if (BYTE_ORDER == LITTLE_ENDIAN)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DIRSIZ(oldfmt, dp) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome ((oldfmt) ? DIRECTSIZ((dp)->d_type) : DIRECTSIZ((dp)->d_namlen))
199767f8919635c4928607450d9e0abb932109ceToomas Soome#else
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define DIRSIZ(oldfmt, dp) \
199767f8919635c4928607450d9e0abb932109ceToomas Soome DIRECTSIZ((dp)->d_namlen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define OLDDIRFMT 1
199767f8919635c4928607450d9e0abb932109ceToomas Soome#define NEWDIRFMT 0
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Template for manipulating directories. Should use struct direct's,
199767f8919635c4928607450d9e0abb932109ceToomas Soome * but the name field is MAXNAMLEN - 1, and this just won't do.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct dirtemplate {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t dot_ino;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int16_t dot_reclen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t dot_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t dot_namlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char dot_name[4]; /* must be multiple of 4 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t dotdot_ino;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int16_t dotdot_reclen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t dotdot_type;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int8_t dotdot_namlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char dotdot_name[4]; /* ditto */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This is the old format of directories, sanz type element.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestruct odirtemplate {
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t dot_ino;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int16_t dot_reclen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t dot_namlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char dot_name[4]; /* must be multiple of 4 */
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int32_t dotdot_ino;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int16_t dotdot_reclen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome u_int16_t dotdot_namlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome char dotdot_name[4]; /* ditto */
199767f8919635c4928607450d9e0abb932109ceToomas Soome};
199767f8919635c4928607450d9e0abb932109ceToomas Soome#endif /* !_DIR_H_ */