/*= -*- c-file-style: "linux"; indent-tabs-mode: t; -*-
 *
 * n4: A fast userspace NFSv4 server.
 * $Id: attrmask.c,v 1.2 2001/07/20 07:36:42 mbp Exp $
 * 
 * Copyright (C) 2001 by Martin Pool <mbp@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.
 */

/**
 * \file attrmap.c Manage NFS4 attribute bitmaps.  These are used on
 * the READDIR and GETATTR calls to indicate which of the file's
 * attributes are of interest or available.  They're packed into a
 * variable-length bit vector.
 *
 * RFC3010 defines a fairly small set of mandatory and recommended
 * attributes, which are the only ones identified by name.  So for the
 * in-memory representation we simply allocate a fixed-length array.
 * When writing to the network, we write the minimum amount necessary
 * to represent the set bits.
 *
 * The last defined bit for this is 54 (FATTR4_TIME_MODIFY_SET), so we
 * can pack everything into a uint64_t.  Sweet!
 **/

#include "includes.h"

n4err n4attrmask_unpack(struct n4inbuf *inb,
                        n4attrmask_t *mask)
{
    int n_words;

    CHECK_ERR(n4inbuf_need(inb, 1));
    n_words = FROM_NET(UNPACK(inb));

//    n4trace("read %d word n4attrmask", n_words);
    if (n_words > 1000) {
        n4log_warning("attrmask length %d is unreasonably long", n_words);
        // continue anyhow
    }

    // All the bits we can handle fit in the lowest two words.  So, we
    // can just skip everything after that.

    // n4attrmask is defined to be a little-endian series of words.
    // (Argh!)

    CHECK_ERR(n4inbuf_need(inb, n_words));
    if (n_words == 0) {
        *mask = 0;
    }
    if (n_words >= 1) {
        *mask = FROM_NET(UNPACK(inb));
        if (n_words > 1) {
            *mask |= ((uint64_t) FROM_NET(UNPACK(inb))) << 32;
            if (n_words > 2) {
                n4inbuf_skip(inb, n_words - 2);
            }
        }
    }
    
    n4trace("got %d-word attrmask %#llx", n_words,
            (unsigned long long) *mask);

    return N4OK;
}



n4err n4attrmask_pack(struct n4outbuf *outb,
                      n4attrmask_t mask)
{
    // We always send the 64-bit quantity
    CHECK_ERR(n4outbuf_expand(outb, 3));

    PACK(outb, TO_NET(2));
    
    PACK(outb, TO_NET((uint32_t) mask));
    PACK(outb, TO_NET((uint32_t) (mask >> 32L)));

    n4trace("pack attrmask %#llx", (unsigned long long) mask);
              
    return N4OK;
}
