/*= -*- c-file-style: "linux"; indent-tabs-mode: t; -*-
 *
 * n4: A fast userspace NFSv4 server and client library.
 * $Id: pathname.c,v 1.11 2001/07/19 03:55:17 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.
 */

		/*
		 | [F]or those who ignore the obstacles and stay upon the
		 | path the best reward awaits... not at the end of the
		 | path, for this path can never truly end, but along the
		 | journey.
		 |
		 | Because everybody deserves a dream, and every dream
		 | deserves a soundtrack.
		 |		-- San 'NeTTwerk' Mehat
		 */


#include "includes.h"


/*
 * TODO: Functions to squish out '.' and '..'
 */

n4err n4pathname_fromstr(TALLOC_CTX *pool,
                         struct n4pathname **pname,
                         char const *s)
{
    int i = 0;
    char const *orig_s = s; 
    struct n4pathname *p;
    char const *part;
 
    /* Split components off the directory, and gradually build up a
     * linked list.  This is a common pattern (well, there's two.)
     * Perhaps we can abstract it? */
    n4_assert(!*pname);
    n4_assert(s);
    
    /* while there are components remaining */
    while ((part = n4_next_token(pool, &s, "/"))) {
        /* allocate a new node */
        /* make the appropriate pointer point here */
        /* copy in this component of the name */
        /* the next one links on to this */
        
        *pname = p = (struct n4pathname *) talloc(pool, sizeof **pname);
        if (!p) {
            n4log_error("couldn't allocate a n4pathname");
            return N4ERR_MEM;
        }
        pname = &p->next;
        p->component = part;
        
        if (++i > N4_MAX_PATH_COMPONENTS)
            n4fatal("too many components generated while trying to parse %s",
                    orig_s);
    }
    *pname = NULL;              /* terminate chain */
    
    return N4OK;
}


/**
 * Convert an n4pathname object back into a Unix path.
 *
 * \param absolute TRUE if you want a leading slash.
 **/
n4err n4pathname_tostr(TALLOC_CTX *pool, struct n4pathname const *pname,
                       char const **s, int absolute)
{
    size_t len;
    struct n4pathname const *p;
    char   *target;

    if (!pname) {
        /* quick, common cases. */
        if (absolute)
            *s = "/";
        else
            *s = "";
        return N4OK;
    }
    
    for (p = pname, len = 2; p; p = p->next) {
        len += strlen(p->component) + 1;
    }

    *s = target = talloc(pool, len);
    if (!target) {
        n4_outofmem();
        return N4ERR_MEM;
    }

    if (absolute)
        *target++ = '/';
    
    for (p = pname; p; p = p->next) {
        target = stpcpy(target, p->component);
        *target++ = '/';
    }

    /* We did leave a terminal /, replace it with nul */
    *--target = '\0';

    return N4OK;
}



n4err n4pathname_len(struct n4pathname const *pname, int *len)
{
    *len = 0;

    while (pname) {
        (*len)++;
        pname = pname->next;
    }
    return N4OK;
}


n4err n4pack_pathname(n4outbuf *outb, const struct n4pathname *path)
{
    int path_len;

    CHECK_ERR(n4pathname_check(path));
    CHECK_ERR(n4pathname_len(path, &path_len));
    PACK(outb, TO_NET(path_len));

    /* component strings */
    for (; path; path = path->next) {
	CHECK_ERR(n4pack_string(outb, path->component));
    }
    
    return N4OK;
}



/**
 * Pathnames must correspond exactly to components of a native path,
 * so we check that there are no slashes.
 *
 * I guess if you were a server on a system that allowed slashes and
 * did not use them to separate components then this would be wrong.
 * But I don't think we need to worry about that at the moment.
 **/
n4err n4pathname_check(const struct n4pathname *path)
{
    for (; path; path = path->next) 
	if (strchr(path->component, '/'))
	    return NFS4ERR_INVAL;

    return N4OK;
}

    
	
