/*
 * This file is part of the UCB release of Plan 9. It is subject to the license
 * terms in the LICENSE file found in the top-level directory of this
 * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
 * part of the UCB release of Plan 9, including this file, may be copied,
 * modified, propagated, or distributed except according to the terms contained
 * in the LICENSE file.
 */

#include <u.h>
#include <libc.h>
#include <9P2000.h>

enum
{
	DIRSIZE	= STATFIXLEN + 16 * 4		/* enough for encoded stat buf + some reasonable strings */
};

Dir*
jehanne_dirstat(const char *name)
{
	Dir *d;
	uint8_t *buf;
	int n, nd, i;

	nd = DIRSIZE;
	for(i=0; i<2; i++){	/* should work by the second try */
		d = jehanne_malloc(sizeof(Dir) + BIT16SZ + nd);
		if(d == nil)
			return nil;
		buf = (uint8_t*)&d[1];
		n = jehanne_stat(name, buf, BIT16SZ+nd);
		if(n < BIT16SZ){
			jehanne_free(d);
			return nil;
		}
		nd = GBIT16((uint8_t*)buf);	/* upper bound on size of Dir + strings */
		if(nd <= n){
			jehanne_convM2D(buf, n, d, (char*)&d[1]);
			return d;
		}
		/* else sizeof(Dir)+BIT16SZ+nd is plenty */
		jehanne_free(d);
	}
	return nil;
}
