summaryrefslogtreecommitdiff
path: root/node.c
blob: ad1f74569612a98693955187249f49330f167a5f (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
/* Gopherfs node handling routines

   Copyright (C) 2002 James A. Morrison <ja2morri@uwaterloo.ca>
   Copyright (C) 2000 Igor Khavkine <igor@twu.net>
   This file is part of the Gopherfs translator.

   Gopherfs 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, or (at
   your option) any later version.

   Gopherfs 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, USA. */

#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include "gopherfs.h"

#include <hurd/hurd_types.h>
#include <hurd/netfs.h>

/* make an instance of `struct netnode' with the specified parameters,
   return NULL on error */
struct netnode *
gopherfs_make_netnode (char type, char *name, char *selector,
		       char *server, unsigned short port)
{
  struct netnode *nn;
  int err;

  nn = (struct netnode *) malloc (sizeof (struct netnode));
  if (!nn)
    return NULL;
  memset (nn, 0, sizeof (struct netnode));
  nn->type = type;
  nn->name = strdup (name);
  if (! nn->name)
    err = 1;
  nn->selector = strdup (selector);
  if (! nn->selector)
    err = 2;
  nn->server = strdup (server);
  if (! nn->server)
    err = 3;
  nn->port = port;
  nn->ents = NULL;
  nn->noents = FALSE;
  /* XXX init cache references */

  switch (err)
    {
    case 4:
      free (nn->server);
    case 3:
      free (nn->selector);
    case 2:
      free (nn->name);
    case 1:
      free (nn);
    case 0:
      return NULL;
    default:
      return nn;
    }

}

/* free an instance of `struct netnode' */
void
free_netnode (struct netnode *node)
{
  struct node *nd;

  free (node->server);
  free (node->selector);
  free (node->name);
  for (nd = node->ents; nd; nd = nd->next)
    free (nd);
  free (node);
}

/* make an instance of `struct node' with the specified parameters,
   return NULL on error */
struct node *
gopherfs_make_node (char type, char *name, char *selector,
		    char *server, unsigned short port)
{
  struct netnode *nn;
  struct node *nd;

  nn = gopherfs_make_netnode (type, name, selector, server, port);
  if (!nn)
    return NULL;

  nd = netfs_make_node (nn);
  if (!nd)
    {
      free (nn);
      return NULL;
    }
  nd->next = NULL;
  nd->prevp = NULL;
  nd->owner = gopherfs->uid;

  /* XXX Hold a reference to the new dir's node.  */
  spin_lock (&netfs_node_refcnt_lock);
  nd->references++;
  spin_unlock (&netfs_node_refcnt_lock);

  /* fill in stat info for the node */
  nd->nn_stat.st_mode = (S_IRUSR | S_IRGRP | S_IROTH) & ~gopherfs->umask;
  nd->nn_stat.st_mode |= type == GPHR_DIR ? S_IFDIR : S_IFREG;
  nd->nn_stat.st_nlink = 1;
  nd->nn_stat.st_uid = gopherfs->uid;
  nd->nn_stat.st_gid = gopherfs->gid;
  nd->nn_stat.st_rdev = 0;
  nd->nn_stat.st_size = 0;
  nd->nn_stat.st_blksize = 0;
  nd->nn_stat.st_blocks = 0;
  nd->nn_stat.st_ino = gopherfs->next_inode++;
  fshelp_touch (&nd->nn_stat, TOUCH_ATIME | TOUCH_MTIME | TOUCH_CTIME,
		gopherfs_maptime);

  return nd;
}

/* free an instance of `struct node' */
void
free_node (struct node *node)
{
  /* XXX possibly take care of cache references */
  free_netnode (node->nn);
  free (node);
}