/*= -*- c-file-style: "linux"; indent-tabs-mode: t; -*-
 *
 * n4: A fast userspace NFSv4 server and client library.
 * $Id: fattr.c,v 1.4 2001/07/20 07:40:09 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@samba.org>
 * http://n4.samba.org/
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

		/*
		 | Across planet Earth an idea is beginning to bear fruit.
		 | It is not a new idea, but more an idea based on a set of
		 | ideals which aren't predominant in the world today.
		 |		-- San 'NeTTwerk' Mehat
		 */


#include "includes.h"

typedef enum n4_attrtype {
	ATTR_UNIMPL,
	ATTR_U32,
	ATTR_U64,
	ATTR_BITMAP,
	ATTR_BOOL,
	ATTR_FSID,
	ATTR_ACL,
	ATTR_LOCATIONS,
	ATTR_FH,
	ATTR_STRING,
	ATTR_SPECDATA
} n4_attrtype_t;

#define ATTR_OFFSET(fldname) G_STRUCT_OFFSET(n4fattr, fldname)


 const n4attrmask_t N4_ATTR_MASK_SUPPORTED = ( ATTRMASK_SUPPORTED_ATTRS
					       | ATTRMASK_SIZE
					       | ATTRMASK_TYPE
					       | ATTRMASK_HOMOGENEOUS
					       | ATTRMASK_MODE
					       | ATTRMASK_NUMLINKS
					       | ATTRMASK_TIME_ACCESS
					       | ATTRMASK_TIME_MODIFY
					       | ATTRMASK_TIME_METADATA );

 const n4attrmask_t N4_ATTR_MASK_STAT = ( ATTRMASK_SIZE
					  | ATTRMASK_TYPE
					  | ATTRMASK_MODE
					  | ATTRMASK_NUMLINKS
					  | ATTRMASK_TIME_ACCESS
					  | ATTRMASK_TIME_MODIFY
					  | ATTRMASK_TIME_METADATA );
/**
 * Unpack file attributes.
 **/
n4err n4fattr_unpack(struct n4inbuf *inb,
		     struct n4fattr *fattr)
{
	uint32_t	    attrlist_len;
	n4attrmask_t	    mask;

	CHECK_ERR(n4inbuf_need(inb, 2));

	CHECK_ERR(n4attrmask_unpack(inb, &mask));

	attrlist_len = FROM_NET(UNPACK(inb));
	n4trace("unpack %d-word attributelist body", attrlist_len);

	CHECK_ERR (n4fattr_unpack_body (inb, fattr, mask));

	return N4OK;
}



n4err n4fattr_pack (struct n4outbuf *outb,
		    const struct n4fattr *fattr,
		    n4attrmask_t req_mask)
{
	n4attrmask_t	    mask;
	int		    attrlist_len_pos, attrlist_len;

	mask = fattr->present_attrs & req_mask;

	CHECK_ERR(n4outbuf_expand(outb, 3 + 1));	// mask + size
    
	/* Send returned bitmap */
	CHECK_ERR(n4attrmask_pack(outb, mask));

	/* Leave space for the count of bytes in the attribute values */
	attrlist_len_pos = n4outbuf_skip(outb);

	/* TODO: Perhaps pre-allocate a buffer large enough to hold
	 * all available attributes. */

	/* Call to dynamically-generated code which packs up all the
	 * selected attributes. */
	CHECK_ERR (n4fattr_pack_body (outb, fattr, mask));
	
	attrlist_len = 4 * (n4outbuf_tell(outb) - attrlist_len_pos);
	n4outbuf_pack_at(outb, attrlist_len_pos, TO_NET(attrlist_len));

	return N4OK;
}


/**
 * Retrieve and pack into \p outb the requested information about file
 * d_name.
 *
 * If an error occurs, \p fattr is still valid on return although it
 * may not contain all the requested data.
 **/
n4err n4fattr_stat_file(struct n4fattr *fattr,
			const char *d_name,
			n4attrmask_t req_mask)
{
	struct stat64 statbuf;

	n4fattr_clobber (fattr);
	
	fattr->supported_attrs = N4_ATTR_MASK_SUPPORTED;
	fattr->present_attrs = 0;

	/* If we need any attributes that come from stating a file,
	 * then do so. */
	if (req_mask & N4_ATTR_MASK_STAT) {
		if (lstat64(d_name, &statbuf) == -1) {
			n4log_error("lstat(%s) failed: %s", d_name, strerror(errno));
			return n4err_from_unix(errno);
		}

		fattr->mode = statbuf.st_mode;
		fattr->size = statbuf.st_size;
		fattr->numlinks = statbuf.st_nlink;
		fattr->type = n4ftype_from_mode(statbuf.st_mode);

		/* We'd like to pass in the microsecond time from the
		 * file, but this doesn't seem to be in the stat64
		 * definition on Debian at the moment, and ext2/3
		 * doesn't store it anyhow. */

		n4nfstime_from_unix (&fattr->time_access, statbuf.st_atime, 0);
		n4nfstime_from_unix (&fattr->time_modify, statbuf.st_mtime, 0);
		n4nfstime_from_unix (&fattr->time_metadata, statbuf.st_ctime, 0);
		
		fattr->present_attrs |= N4_ATTR_MASK_STAT;
	}

	return N4OK;
}


n4err n4fsid_pack (struct n4outbuf *outb,
		   const n4fsid_t * fsid)
{
	CHECK_ERR (n4outbuf_expand (outb, 4));
	n4pack64_nocheck (outb, fsid->major);
	n4pack64_nocheck (outb, fsid->minor);
	return N4OK;
}

n4err n4fsid_unpack (struct n4inbuf *inb,
		     n4fsid_t * fsid)
{
	CHECK_ERR (n4inbuf_need (inb, 4));
	n4unpack64_nocheck (inb, &fsid->major);
	n4unpack64_nocheck (inb, &fsid->minor);
	return N4OK;
}


n4err n4fattr_clobber (struct n4fattr *fattr)
{
	memset (fattr, '#', sizeof *fattr);
	return N4OK;
}
