summaryrefslogtreecommitdiff
path: root/node.c
blob: 5dff209f1cab568bd6c3cd935f67ce78af726587 (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
/* 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;

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

  if (!(nn->server && nn->selector && nn->name))
    { /* We are allowed to free NULL pointers */
      free (nn->server);
      free (nn->selector);
      free (nn->name);
      free (nn);
      return NULL;
    }
  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);
}