/*= -*- c-file-style: "bsd"; indent-tabs-mode: t; -*-
 *
 * n4: A fast userspace NFSv4 server.
 * $Id: fh.c,v 1.6 2001/06/25 13:46:44 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.
 */

#include "includes.h"

// TODO: Make sure file descriptors are closed up after use.  

/**
 * Server-side representation of filehandles.  We lazily initialize
 * different aspects of the filehandle structure as appropriate to the
 * situation.
 *
 * The caller must ensure the filename is allocated from memory that
 * will last at least as long as the fh remains in use.
 **/
n4err n4fh_from_filename(struct n4fh *fh, char const *filename)
{
    n4_assert(fh);
    n4_assert(filename);

    fh->filename = filename;
    fh->open_fd = N4_NO_FD;
    fh->packed_form = NULL;
    fh->packed_len = 0;

    return N4OK;
}


n4err n4fh_putroot(struct n4fh *fh)
{
    n4_assert(fh);

    fh->filename = "/";
    fh->open_fd = N4_NO_FD;
    fh->packed_len = 0;
    fh->packed_form = NULL;
    fh->path = NULL;

    return N4OK;
}


n4err n4fh_check(const struct n4fh *fh)
{
    if (!fh->filename) {
        n4log_error("filehandle path is invalid");
        return NFS4ERR_NOFILEHANDLE;
    }
    // fh->path may be NULL iff this points to the root directory
    
    return N4OK;
}


n4err n4fh_clear(struct n4fh *fh)
{
    fh->filename = NULL;
    fh->open_fd = N4_NO_FD;
    fh->packed_len = 0;
    fh->packed_form = NULL;
    fh->path = NULL;

    return N4OK;
}


void n4fh_copy(struct n4fh *to,
               const struct n4fh *from)
{
    memcpy(to, from, sizeof *to);
}


n4err n4fh_getname(struct n4fh *fh, char const **name)
{
    *name = fh->filename;
    /* XXX: Eventually this might have to look up the name from the
       hash in a TDB */
    return N4OK;
}



n4err n4fh_getpath(struct n4fh *fh, struct n4pathname **path) 
{
    *path = fh->path;
    return N4OK;
}


n4err n4fh_close(struct n4fh *fh)
{
    if (fh->open_fd != N4_NO_FD) {
        if (close(fh->open_fd) == -1) {
            n4log_error("error closing fd%d: %s", fh->open_fd, strerror(errno));
        }
    }
    fh->open_fd = N4_NO_FD;
    return N4OK;
}


n4err n4fh_open(struct n4fh *fh,
                int *fd,
                int mode)
{
    const char                  *filename;
    
    if (!fh) {
        n4log_error("NULL filehandle");
        return NFS4ERR_INVAL;
    }
    if (!(filename = fh->filename)) {
        n4log_error("NULL filename");
        return NFS4ERR_INVAL;
    }

    n4fh_close(fh);
    
    if ((*fd = fh->open_fd = open(filename, mode)) == -1) {
        n4log_error("error opening %s for mode %#o: %s",
                    filename, mode, strerror(errno));
        return n4err_from_unix(errno);
    }

    n4log_info("opened \"%s\" as fd%d, mode %#o",
               filename, *fd, mode);

    return N4OK;
}


/**
 * Unpack an NFS4 pathname list and put it in the path area of \p fh.
 **/
n4err n4fh_unpack_path(n4scomp *comp, struct n4fh *fh)
{
    /* typedef component4      pathname4<>; */
    /* typedef utf8string      component4; */
    guint32 n_components;
    struct n4pathname **tail = &fh->path;

    n_components = FROM_NET(UNPACK(comp->inb));
    n4trace("unpack pathname with %d components", n_components);
    if (n_components > N4_MAX_PATH_COMPONENTS) {
        n4log_crit("%d components in so-called nfs4_pathname is unbelievable!",
                   n_components);
        return N4ERR_RPC_GARBAGE;
    }

    n4fh_clear(fh);
    
    while (n_components-- > 0) {
        struct n4pathname *p;
        char *s;

        *tail = p = talloc(comp->pool, sizeof *p);
        if (!p)
            n4_outofmem();
        
        CHECK_ERR(n4unpack_string(comp->pool, comp->inb, &s));
        n4trace("unpacked pathname component \"%s\"", s);
        p->component = s;
        tail = &p->next;
    }

    *tail = 0;

    CHECK_ERR(n4pathname_check(fh->path));
    CHECK_ERR(n4pathname_tostr(comp->pool, fh->path, &fh->filename, 1));

    return N4OK;
}


