summaryrefslogtreecommitdiff
path: root/node.c
blob: 9389103e84b9c8216a2a2c2f96bcb749b8fa5152 (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
/* 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>

/* free an instance of `struct netnode' */
void
free_netnode (struct netnode *nn)
{
  free (nn->e->name);
  free (nn->e->selector);
  free (nn->e->server);
  free (nn->e);
  for (struct node *nd = nn->ents; nd; nd = nd->next)
    free (nd);
  free (nn);
}

/* 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);
}

/* Normalize filename: replace '/' by '-'. */
char *
normalize_filename (char *name)
{
  char *s = strdup (name);

  for (char *tmp = s; *tmp != '\0'; tmp++)
    {
      if (*tmp == '/')
	*tmp = '-';
    }

  return s;
}

/* make an instance of `struct netnode' with the specified parameters,
   return NULL on error */
static struct netnode *
gopherfs_make_netnode (struct gopher_entry *entry,
		       struct node *dir)
{
  struct netnode *nn;

  if (!entry)
    return NULL;

  nn = malloc (sizeof (struct netnode));
  if (!nn)
    return NULL;
  memset (nn, 0, sizeof (struct netnode));

  nn->dir = dir;
  nn->e = malloc (sizeof (struct gopher_entry));
  if (!nn->e)
    {
      free (nn);
      return NULL;
    }

  debug ("Creating entry %s (selector %s, server %s, port %hu)",
	 entry->name,
	 entry->selector, entry->server, entry->port);
  nn->e->name = normalize_filename (entry->name);
  nn->e->type = entry->type;
  nn->e->selector = strdup (entry->selector);
  nn->e->server = strdup (entry->server);
  nn->e->port = entry->port;

  /* XXX init cache references */

  if (!(nn->e->name && nn->e->selector && nn->e->server))
    { 
      /* We are allowed to free NULL pointers */
      free_netnode (nn);
      return NULL;
    }
  return nn;

}

/* make an instance of `struct node' in DIR with the parameters in ENTRY.
   return NULL on error */
struct node *
gopherfs_make_node (struct gopher_entry *entry,
		    struct node *dir)
{
  struct netnode *nn;
  struct node *nd;

  nn = gopherfs_make_netnode (entry, dir);
  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 |= entry->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;
}