summaryrefslogtreecommitdiff
path: root/lnode.c
blob: 10d32ca86ce79ea007c9c7a62c5b6f6903a58a10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/* Hurd unionfs
   Copyright (C) 2001, 2002 Free Software Foundation, Inc.
   Written by Moritz Schulte <moritz@duesseldorf.ccc.de>.

   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 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
   General Public License for more details.
 
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
   USA.  */

/* `light node' management.  See unionfs.h for an explanation of light
   nodes.  */

#define _GNU_SOURCE

#include <pthread.h>
#include <error.h>
#include <stdlib.h>
#include <string.h>

#include "lnode.h"
#include "lib.h"
#include "unionfs.h"

/* Create a new light node as an entry with the name NAME and store it
   in *NODE.  The new node is not locked and contains a single
   reference.  */
error_t
lnode_create (char *name, lnode_t **node)
{
  lnode_t *node_new = malloc (sizeof (lnode_t));
  error_t err = 0;
  
  debug_msg ("lnode_create for name: %s", name);

  if (! node_new)
    err = ENOMEM;
  else
    {
      char *name_cp = NULL;

      if (name)
	name_cp = strdup (name);
      if (name && (! name_cp))
	{
	  err = ENOMEM;
	  free (node_new);
	}
      else
	{
	  node_new->name = name_cp;
	  node_new->name_len = name_cp ? strlen (name_cp) : 0;
	  node_new->flags = 0;
	  node_new->node = NULL;
	  node_new->next = NULL;
	  node_new->prevp = NULL;
	  node_new->dir = NULL;
	  node_new->entries = NULL;
	  node_new->references = 1;
	  pthread_mutex_init (&node_new->lock, NULL);
	  pthread_mutex_lock (&node_new->lock);
	  *node = node_new;
	}
    }
  return err;
}

/* Destroy a light node.  */
void
lnode_destroy (lnode_t *node)
{
  debug_msg ("lnode_destroy for name: %s", node->name);
  free (node->name);
  free (node);
}

/* Install the node in the node tree; add a reference to DIR, which
   must be locked.  */
void
lnode_install (lnode_t *dir, lnode_t *node)
{
  lnode_ref_add (dir);
  node->next = dir->entries;
  node->prevp = &dir->entries;
  if (dir->entries)
    dir->entries->prevp = &node->next;
  dir->entries = node;
  node->dir = dir;
}

/* Uninstall the node from the node tree; remove a reference from the
   lnode containing NODE.  */
void
lnode_uninstall (lnode_t *node)
{
  lnode_ref_remove (node->dir);
  *node->prevp = node->next;
  if (node->next)
    node->next->prevp = &node->next;
}

/* Add a reference to NODE, which must be locked.  */
void
lnode_ref_add (lnode_t *node)
{
  node->references++;
}

/* Remove a reference to NODE, which must be locked.  If that was the
   last reference, destroy the node, otherwise simply unlock NODE.  */
void
lnode_ref_remove (lnode_t *node)
{
  assert (node->references);
  if (! --node->references)
    {
      lnode_uninstall (node);
      lnode_destroy (node);
    }
  else
    pthread_mutex_unlock (&node->lock);
}

/* Get a light node by it's name.  The looked up node is locked and
   got one reference added.  */
error_t
lnode_get (lnode_t *dir, char *name,
	   lnode_t **node)
{
  error_t err = 0;
  lnode_t *n;

  for (n = dir->entries; n && strcmp (n->name, name); n = n->next);
  if (n)
    {
      pthread_mutex_lock (&n->lock);
      lnode_ref_add (n);
      *node = n;
    }
  else
    err = ENOENT;

  return err;
}

/* Construct the full path for a given light node.  */
error_t
lnode_path_construct (lnode_t *node,
		      char **path)
{
  error_t err = 0;
  int p_len = 1;
  lnode_t *n;
  char *p;

  for (n = node; n && n->dir; n = n->dir)
    p_len += n->name_len + (n->dir->dir ? 1 : 0);

  p = malloc (p_len);
  if (! p)
    err = ENOMEM;
  else
    {
      *(p + --p_len) = 0;
      for (n = node; n && n->dir; n = n->dir)
	{
	  p_len -= n->name_len;
	  strncpy (p + p_len, n->name, n->name_len);
	  if (n->dir->dir)
	    *(p + --p_len) = '/';
	}
      *path = p;
    }
  return err;
}