Home | History | Annotate | Line # | Download | only in libpuffs
pnode.c revision 1.7
      1 /*	$NetBSD: pnode.c,v 1.7 2007/06/24 17:55:07 pooka Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2006 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 #if !defined(lint)
     30 __RCSID("$NetBSD: pnode.c,v 1.7 2007/06/24 17:55:07 pooka Exp $");
     31 #endif /* !lint */
     32 
     33 #include <sys/types.h>
     34 
     35 #include <assert.h>
     36 #include <puffs.h>
     37 #include <stdlib.h>
     38 #include <stdio.h>
     39 #include <string.h>
     40 
     41 #include "puffs_priv.h"
     42 
     43 /*
     44  * Well, you're probably wondering why this isn't optimized.
     45  * The reason is simple: my available time is not optimized for
     46  * size ... so please be patient ;)
     47  */
     48 struct puffs_node *
     49 puffs_pn_new(struct puffs_usermount *pu, void *privdata)
     50 {
     51 	struct puffs_node *pn;
     52 
     53 	pn = calloc(1, sizeof(struct puffs_node));
     54 	if (pn == NULL)
     55 		return NULL;
     56 
     57 	pn->pn_data = privdata;
     58 	pn->pn_mnt = pu;
     59 	puffs_vattr_null(&pn->pn_va);
     60 
     61 	LIST_INSERT_HEAD(&pu->pu_pnodelst, pn, pn_entries);
     62 
     63 	return pn;
     64 }
     65 
     66 void
     67 puffs_pn_remove(struct puffs_node *pn)
     68 {
     69 
     70 	LIST_REMOVE(pn, pn_entries);
     71 	pn->pn_flags |= PUFFS_NODE_REMOVED;
     72 }
     73 
     74 void
     75 puffs_pn_put(struct puffs_node *pn)
     76 {
     77 	struct puffs_usermount *pu = pn->pn_mnt;
     78 
     79 	pu->pu_pathfree(pu, &pn->pn_po);
     80 	if ((pn->pn_flags & PUFFS_NODE_REMOVED) == 0)
     81 		LIST_REMOVE(pn, pn_entries);
     82 	free(pn);
     83 }
     84 
     85 /* walk list, rv can be used either to halt or to return a value */
     86 void *
     87 puffs_pn_nodewalk(struct puffs_usermount *pu, puffs_nodewalk_fn fn, void *arg)
     88 {
     89 	struct puffs_node *pn_cur, *pn_next;
     90 	void *rv;
     91 
     92 	pn_cur = LIST_FIRST(&pu->pu_pnodelst);
     93 	while (pn_cur) {
     94 		pn_next = LIST_NEXT(pn_cur, pn_entries);
     95 		rv = fn(pu, pn_cur, arg);
     96 		if (rv)
     97 			return rv;
     98 		pn_cur = pn_next;
     99 	}
    100 
    101 	return NULL;
    102 }
    103 
    104 /* convenience / shortcut */
    105 void *
    106 puffs_pn_getmntspecific(struct puffs_node *pn)
    107 {
    108 
    109 	return pn->pn_mnt->pu_privdata;
    110 }
    111