FreeBSD/sys/kern/vfs_default.c
/*
 * Check if a named file exists in a given directory vnode.
 */
Check if a named file exists in a given directory vnode.
Check if a named file exists in a given directory vnode .
static int
dirent_exists(struct vnode *vp, const char *dirname, struct thread *td)
{
	char *dirbuf, *cpos;
	int error, eofflag, dirbuflen, len, found;
	off_t off;
	struct dirent *dp;
	struct vattr va;
	KASSERT(VOP_ISLOCKED(vp), ("vp %p is not locked", vp));
	KASSERT(vp->v_type == VDIR, ("vp %p is not a directory", vp));
	found = 0;
	error = VOP_GETATTR(vp, &va, td->td_ucred);
	if (error)
		return (found);
	dirbuflen = DEV_BSIZE;
	if (dirbuflen < va.va_blocksize)
		dirbuflen = va.va_blocksize;
	dirbuf = (char *)malloc(dirbuflen, M_TEMP, M_WAITOK);
	off = 0;
	len = 0;
	do {
		error = get_next_dirent(vp, &dp, dirbuf, dirbuflen, &off,
					&cpos, &len, &eofflag, td);
		if (error)
			goto out;
		if (dp->d_type != DT_WHT && dp->d_fileno != 0 &&
		    strcmp(dp->d_name, dirname) == 0) {
			found = 1;
			goto out;
		}
	} while (len > 0 || !eofflag);
out:
	free(dirbuf, M_TEMP);
	return (found);
}
static int dirent_exists ( struct vnode * vp , const char * dirname , struct thread * td ) { char * dirbuf , * cpos ; int error , eofflag , dirbuflen , len , found ; off_t off ; struct dirent * dp ; struct vattr va ; KASSERT ( VOP_ISLOCKED ( vp ) , ( " " , vp ) ) ; KASSERT ( vp -> v_type == VDIR , ( " " , vp ) ) ; found = 0 ; error = VOP_GETATTR ( vp , & va , td -> td_ucred ) ; if ( error ) return ( found ) ; dirbuflen = DEV_BSIZE ; if ( dirbuflen < va . va_blocksize ) dirbuflen = va . va_blocksize ; dirbuf = ( char * ) malloc ( dirbuflen , M_TEMP , M_WAITOK ) ; off = 0 ; len = 0 ; do { error = get_next_dirent ( vp , & dp , dirbuf , dirbuflen , & off , & cpos , & len , & eofflag , td ) ; if ( error ) goto out ; if ( dp -> d_type != DT_WHT && dp -> d_fileno != 0 && strcmp ( dp -> d_name , dirname ) == 0 ) { found = 1 ; goto out ; } } while ( len > 0 || ! eofflag ) ; out : free ( dirbuf , M_TEMP ) ; return ( found ) ; }
Check if a named file exists in a given directory vnode.
Check if a named file exists in a given directory vnode .