summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog27
-rw-r--r--Makefile6
-rw-r--r--TODO13
-rw-r--r--args.c68
-rw-r--r--debug.c45
-rw-r--r--debug.h31
-rw-r--r--dir.c69
-rw-r--r--fs.c62
-rw-r--r--fs.h28
-rw-r--r--gopher.c203
-rw-r--r--gopher.h62
-rw-r--r--gopherfs.c75
-rw-r--r--gopherfs.h65
-rw-r--r--netfs.c555
-rw-r--r--node.c114
15 files changed, 954 insertions, 469 deletions
diff --git a/ChangeLog b/ChangeLog
index 06b2ff16b..8c2f2ff4e 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,30 @@
+2010-10-09 Manuel Menal <mmenal@hurdfr.org>
+
+ * args.c: modify -D option to take a filename.
+ (parse_opt): don't hardcode port number, use getservbyname().
+ (parse_opt): check if server address resolves.
+ * debug.c: new file with debugging output functions.
+ * debug.h: new file.
+ * dir.c: new file: separate gopher-specific parts from directory handling.
+ * fs.h: new file.
+ * gopher.c (lookup_host): use getaddrinfo.
+ (open_selector): rename to `gopher_open', changed to use struct gopher_entry.
+ (gopher_list_entries): new function.
+ * gopher.h: new file: gopher client functions and declarations.
+ * gopherfs.c: use a gopherfs object instead of multiple global variables.
+ * gopherfs.h: use only one global variable. Use `struct gopher_entry'.
+ * netfs.c: change to use GNU Coding Standards, like all the other files.
+ (netfs_get_dirents): modify to new `fill_dirnode' syntax.
+ (netfs_attempt_lookup): check for . and .. and fix reference counting.
+ (netfs_attempt_read): use `read' instead of `pread' which won't work on sockets.
+ (netfs_node_norefs): fix mutex/spinlock handling.
+ * node.c: (free_netnode): adapt to new structure.
+ (free_node): ditto.
+ (normalize_filename): new function.
+ (gopherfs_make_netnode): use `struct gopher_entry'.
+ (gopherfs_make_node): ditto.
+ * TODO: new file.
+
2005-12-31 Manuel Menal <mmenal@hurdfr.org>
* Makefile (CFLAGS): Add -D_FILE_OFFSET_BITS=64
diff --git a/Makefile b/Makefile
index a977892a3..a0625203e 100644
--- a/Makefile
+++ b/Makefile
@@ -23,10 +23,10 @@ makemode := server
target = gopherfs
CC = gcc
-CFLAGS = -Wall -g -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64
+CFLAGS = -Wall -g -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -std=gnu99
INCLUDES = -I.
-SRCS = gopherfs.c args.c netfs.c gopher.c node.c
-LCLHDRS = gopherfs.h
+SRCS = gopherfs.c args.c netfs.c gopher.c node.c fs.c debug.c dir.c
+LCLHDRS = gopherfs.h fs.h debug.h
OBJS = $(SRCS:.c=.o)
HURDLIBS = -lnetfs -lfshelp -liohelp -lports
diff --git a/TODO b/TODO
new file mode 100644
index 000000000..cb102ff8e
--- /dev/null
+++ b/TODO
@@ -0,0 +1,13 @@
+* Add caching for contents.
+* Add caching for DNS lookups (no need to call getaddrinfo() for every access).
+* Add support for search servers
+
+Search servers could be implemented with quite simple semantics:
+
+$ echo "keyword" > node
+$ ls node
+keyword
+$ cd node/keyword
+$ ls
+<search results>
+
diff --git a/args.c b/args.c
index b0cf8f1ae..55aaab9ee 100644
--- a/args.c
+++ b/args.c
@@ -21,9 +21,14 @@
#include <limits.h>
#include <unistd.h>
#include <error.h>
+#include <stdio.h>
#include <argp.h>
#include <argz.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netdb.h>
+
#include <hurd/netfs.h>
#include "gopherfs.h"
@@ -34,8 +39,8 @@ const char *argp_program_bug_address = "ja2morri@uwaterloo.ca";
char args_doc[] = "SERVER [REMOTE_FS]";
char doc[] = "Hurd gopher filesystem translator";
static const struct argp_option options[] = {
- {"debug", 'D', 0, 0, "enable debug output"},
{"port", 'P', "NUMBER", 0, "Specify a non-standard port"},
+ {"debug", 'D', "FILE", 0, "Print debug output to FILE"},
{0}
};
/* the function that groks the arguments and fills
@@ -44,38 +49,82 @@ static const struct argp_option options[] = {
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
{
+ struct gopherfs_params *params = state->input;
+ error_t err = 0;
+
+ FILE *debug_stream = NULL;
char *tail;
switch (key)
{
case 'D':
- debug_flag = 1;
+ if (arg)
+ {
+ debug_stream = fopen (arg, "w+");
+ if (! debug_stream)
+ {
+ err = errno;
+ argp_failure (state, 0, errno, "Cannot open debugging file %s", arg);
+ }
+ }
+ else
+ debug_stream = stderr;
+
+ if (!err)
+ debug_init (debug_stream);
+
break;
+
case 'P':
- gopherfs_root_port = (unsigned short) strtol (arg, &tail, 10);
- if (tail == arg || gopherfs_root_port > USHRT_MAX)
+ params->port = (unsigned short) strtol (arg, &tail, 10);
+ if (tail == arg || params->port > USHRT_MAX)
{
/* XXX bad integer conversion */
error (1, errno, "bad port number");
}
break;
+
case ARGP_KEY_ARG:
if (state->arg_num == 0)
{
- gopherfs_root_port = 70;
- gopherfs_root_server = arg;
+ params->dir = "";
+ params->server = arg;
}
else if (state->arg_num == 1)
{
- gopherfs_server_dir = arg;
+ params->dir = arg;
}
else
return ARGP_ERR_UNKNOWN;
break;
+
+ case ARGP_KEY_SUCCESS:
+ if (state->arg_num == 0)
+ argp_error (state, "No remote filesystem specified");
+ else
+ {
+ /* No port was provided. Get gopher default port. */
+ if (params->port == 0)
+ {
+ struct servent *se = getservbyname ("gopher", "tcp");
+ if (! se)
+ argp_error (state, "Couldn't get gopher port");
+
+ params->port = ntohs(se->s_port);
+ }
+
+ /* Check if the given address resolves. */
+ error_t err;
+ struct addrinfo *addr;
+ err = lookup_host (params->server, params->port, &addr);
+ if (err)
+ argp_failure (state, 10, 0, "%s: %s", params->server, gai_strerror (err));
+ }
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
+
static struct argp_child argp_children[] = { {&netfs_std_startup_argp}, {0} };
static struct argp parser =
{ options, parse_opt, args_doc, doc, argp_children };
@@ -88,8 +137,7 @@ error_t netfs_append_args (char **argz, size_t * argz_len);
/* handle all initial parameter parsing */
error_t
-gopherfs_parse_args (int argc, char **argv)
+gopherfs_parse_args (int argc, char **argv, struct gopherfs_params *params)
{
- /* XXX: handle command line arguments properly */
- return argp_parse (&parser, argc, argv, 0, 0, /*&conf */ 0);
+ return argp_parse (&parser, argc, argv, 0, 0, params);
}
diff --git a/debug.c b/debug.c
new file mode 100644
index 000000000..0cffe668b
--- /dev/null
+++ b/debug.c
@@ -0,0 +1,45 @@
+/* Gopher filesystem
+
+ Copyright (C) 2010 Manuel Menal <mmenal@hurdfr.org>
+ 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 <stdio.h>
+#include <stdarg.h>
+
+static FILE *debug_stream = NULL;
+
+void
+debug_init (FILE *stream)
+{
+ setbuf (stream, NULL);
+ debug_stream = stream;
+}
+
+void
+_debug (const char *format, ...)
+{
+ if (!debug_stream)
+ return;
+
+ va_list args;
+ fprintf (debug_stream, "D: ");
+ va_start (args, format);
+ vfprintf (debug_stream, format, args);
+ va_end (args);
+ fprintf (debug_stream, "\n");
+}
+
diff --git a/debug.h b/debug.h
new file mode 100644
index 000000000..64633969a
--- /dev/null
+++ b/debug.h
@@ -0,0 +1,31 @@
+/* Gopher filesystem
+
+ Copyright (C) 2010 Manuel Menal <mmenal@hurdfr.org>
+ 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. */
+
+#ifndef __DEBUG_H__
+#define __DEBUG_H__
+
+#include <stdio.h>
+
+#define debug(format, args...) _debug ("%s: " format, __func__, ##args)
+
+void debug_init (FILE *stream);
+void _debug (const char *format, ...);
+
+#endif /* __FS_H__ */
+
diff --git a/dir.c b/dir.c
new file mode 100644
index 000000000..be842e3ee
--- /dev/null
+++ b/dir.c
@@ -0,0 +1,69 @@
+/* Gopher filesystem
+
+ Copyright (C) 2010 Manuel Menal <mmenal@hurdfr.org>
+ 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 <errno.h>
+
+#include "gopherfs.h"
+
+#include <hurd/hurd_types.h>
+#include <hurd/netfs.h>
+
+/* fetch a directory node from the gopher server
+ DIR should already be locked */
+error_t
+fill_dirnode (struct node *dir)
+{
+ error_t err = 0;
+ struct node *nd, **prevp;
+ struct gopher_entry *map = NULL;
+
+ /* Get entries from DIR. */
+ err = gopher_list_entries (dir->nn->e, &map);
+ if (err)
+ return err;
+
+ dir->nn->noents = TRUE;
+ dir->nn->num_ents = 0;
+
+ /* Fill DIR. */
+ prevp = &dir->nn->ents;
+ for (struct gopher_entry *ent = map; ent; ent = ent->next)
+ {
+ /* Don't make node from informational messages: they don't even have a valid selector. */
+ if (ent->type == GPHR_INFO)
+ continue;
+
+ nd = gopherfs_make_node (ent, dir);
+ if (!nd)
+ {
+ err = ENOMEM;
+ break;
+ }
+
+ *prevp = nd;
+ nd->prevp = prevp;
+ prevp = &nd->next;
+
+ dir->nn->num_ents++;
+ if (dir->nn->noents)
+ dir->nn->noents = FALSE;
+ }
+
+ return err;
+}
diff --git a/fs.c b/fs.c
new file mode 100644
index 000000000..d2d878cee
--- /dev/null
+++ b/fs.c
@@ -0,0 +1,62 @@
+/* Gopher filesystem
+
+ Copyright (C) 2010 Manuel Menal <mmenal@hurdfr.org>
+ 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 <stdlib.h>
+#include <unistd.h>
+#include <sys/types.h>
+
+#include <stdio.h>
+
+#include "fs.h"
+
+error_t
+gopherfs_create (struct gopherfs_params *params)
+{
+ error_t err = 0;
+ struct gopher_entry *root;
+
+ gopherfs = malloc (sizeof (struct gopherfs));
+ if (!gopherfs)
+ return ENOMEM;
+
+ gopherfs->umask = 0;
+ gopherfs->uid = getuid();
+ gopherfs->gid = getgid();
+ gopherfs->next_inode = 0;
+
+ root = malloc (sizeof (struct gopher_entry));
+ if (!root)
+ return ENOMEM;
+ root->type = GPHR_DIR;
+ root->name = "dir";
+ root->selector = "";
+ root->server = params->server;
+ root->port = params->port;
+
+ debug ("creating root node for %s:%hu%s", root->server, root->port);
+ gopherfs->root = gopherfs_make_node (root, NULL);
+ if (!gopherfs->root)
+ err = ENOMEM;
+ free (root);
+
+ gopherfs->params = params;
+
+ return err;
+}
+
diff --git a/fs.h b/fs.h
new file mode 100644
index 000000000..0b635c4be
--- /dev/null
+++ b/fs.h
@@ -0,0 +1,28 @@
+/* Gopher filesystem
+
+ Copyright (C) 2010 Manuel Menal <mmenal@hurdfr.org>
+ 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. */
+
+#ifndef __FS_H__
+#define __FS_H__
+
+#include "gopherfs.h"
+
+error_t gopherfs_create (struct gopherfs_params *params);
+
+#endif /* __FS_H__ */
+
diff --git a/gopher.c b/gopher.c
index a112150ea..28caab032 100644
--- a/gopher.c
+++ b/gopher.c
@@ -23,6 +23,7 @@
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
+#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
@@ -33,70 +34,82 @@
#include <hurd/hurd_types.h>
#include <hurd/netfs.h>
-/* do a DNS lookup for NAME and store result in *ENT */
+/* do a DNS lookup for NAME:PORT and store results in ENT and error in H_ERR */
error_t
-lookup_host (char *name, struct hostent **ent)
+lookup_host (char *name, unsigned short port, struct addrinfo **ai)
{
+ /* XXX: cache host lookups. */
+ struct addrinfo hints;
+ char *service;
error_t err;
- struct hostent hentbuf;
- int herr = 0;
- char *tmpbuf;
- size_t tmpbuflen = 512;
- tmpbuf = (char *) malloc (tmpbuflen);
- if (!tmpbuf)
+ memset (&hints, 0, sizeof(hints));
+ hints.ai_family = PF_INET;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_protocol = IPPROTO_IP;
+ hints.ai_flags = AI_NUMERICSERV;
+
+ err = asprintf (&service, "%hu", port);
+ if (err == -1)
return ENOMEM;
- /* XXX: use getaddrinfo */
- while ((err = gethostbyname_r (name, &hentbuf, tmpbuf,
- tmpbuflen, ent, &herr)) == ERANGE)
- {
- tmpbuflen *= 2;
- tmpbuf = (char *) realloc (tmpbuf, tmpbuflen);
- if (!tmpbuf)
- return ENOMEM;
- }
- free (tmpbuf);
+ err = getaddrinfo (name, service, &hints, ai);
- if (!herr)
- return EINVAL;
+ free (service);
- return 0;
+ return err;
}
-/* store the remote socket in *FD after writing a gopher selector
- to it */
+/* open ENTRY and store the remote socket in *FD */
error_t
-open_selector (struct netnode * node, int *fd)
+gopher_open (struct gopher_entry *entry, int *fd)
{
+ struct addrinfo *addr;
error_t err;
- struct hostent *server_ent;
- struct sockaddr_in server;
ssize_t written;
size_t towrite;
+
+ *fd = socket (PF_INET, SOCK_STREAM, 0);
+ if (*fd < 0)
+ {
+ debug ("failed to open socket (errno: %s)", strerror(errno));
+ return errno;
+ }
- err = lookup_host (node->server, &server_ent);
- if (!err)
- return err;
- if (debug_flag)
- fprintf (stderr, "trying to open %s:%d/%s\n", node->server,
- node->port, node->selector);
+ err = lookup_host (entry->server, entry->port, &addr);
+ if (err)
+ {
+ debug ("looking up host failed: %s", gai_strerror(err));
+ return err;
+ }
- server.sin_family = AF_INET;
- server.sin_port = htons (node->port);
- server.sin_addr = *(struct in_addr *) server_ent->h_addr;
+ /* XXX: cache host lookups. */
+ do
+ {
+ debug ("connecting to %s:%hu...",
+ inet_ntoa(((struct sockaddr_in *)addr->ai_addr)->sin_addr),
+ ntohs(((struct sockaddr_in *)addr->ai_addr)->sin_port));
- *fd = socket (PF_INET, SOCK_STREAM, 0);
- if (*fd == -1)
- return errno;
+ err = connect (*fd, (struct sockaddr_in *) addr->ai_addr, addr->ai_addrlen);
+ if (err)
+ debug ("connect() failed! errno: %s", strerror(errno));
- err = connect (*fd, (struct sockaddr *) &server, sizeof (server));
- if (err == -1)
- return errno;
+ addr = addr->ai_next;
+ }
+ while (addr && err);
- towrite = strlen (node->selector);
+ freeaddrinfo(addr);
+ if (err)
+ {
+ close (*fd);
+ return errno;
+ }
+
+ /* Write selector to *FD. */
+
+ towrite = strlen (entry->selector);
/* guard against EINTR failures */
- written = TEMP_FAILURE_RETRY (write (*fd, node->selector, towrite));
+ written = TEMP_FAILURE_RETRY (write (*fd, entry->selector, towrite));
written += TEMP_FAILURE_RETRY (write (*fd, "\r\n", 2));
if (written == -1 || written < (towrite + 2))
return errno;
@@ -104,76 +117,68 @@ open_selector (struct netnode * node, int *fd)
return 0;
}
-/* fetch a directory node from the gopher server
- DIR should already be locked */
+/* List all entries from ENTRY and store them in *MAP. */
error_t
-fill_dirnode (struct netnode * dir)
+gopher_list_entries (struct gopher_entry *entry, struct gopher_entry **map)
{
- error_t err = 0;
FILE *sel;
- int sel_fd;
- char *line;
- size_t line_len;
- struct node *nd, **prevp;
-
- err = open_selector (dir, &sel_fd);
- if (err)
- return err;
- if (debug_flag)
- fprintf (stderr, "filling out dir %s\n", dir->name);
- errno = 0;
- sel = fdopen (sel_fd, "r");
- if (!sel)
- {
- close (sel_fd);
- return errno;
- }
+ char *line = NULL;
+ size_t line_len = 0;
+ struct gopher_entry *prev = NULL;
+ error_t err = 0;
- dir->noents = TRUE;
- dir->num_ents = 0;
- prevp = &dir->ents;
- line = NULL;
- line_len = 0;
+ {
+ int fd;
+ err = gopher_open (entry, &fd);
+ if (err)
+ return err;
+
+ sel = fdopen (fd, "r");
+ if (!sel)
+ {
+ close (fd);
+ return errno;
+ }
+ }
+
while (getline (&line, &line_len, sel) >= 0)
{
- char type, *name, *selector, *server;
- unsigned short port;
- char *tok, *endtok;
+ /* Parse gopher entry. */
+ struct gopher_entry *cur;
+ char *tmp;
+
+ debug ("%s", line);
- if (debug_flag)
- fprintf (stderr, "%s\n", line);
if (*line == '.' || err)
break;
- /* parse the gopher node description */
- type = *line;
- endtok = line + 1;
- name = strsep (&endtok, "\t");
- selector = strsep (&endtok, "\t");
- server = strsep (&endtok, "\t");
- port = (unsigned short) atoi (strsep (&endtok, "\t"));
-
- nd = gopherfs_make_node (type, name, selector, server, port);
- if (!nd)
+ cur = malloc (sizeof (struct gopher_entry));
+ if (!cur)
{
- err = ENOMEM;
- break;
+ err = ENOMEM;
+ break;
}
- *prevp = nd;
- nd->prevp = prevp;
- prevp = &nd->next;
- dir->num_ents++;
- if (dir->noents)
- dir->noents = FALSE;
+ cur->type = line[0];
+ tmp = line + 1;
+ cur->name = strdup(strsep (&tmp, "\t"));
+ cur->selector = strdup(strsep (&tmp, "\t"));
+ cur->server = strdup(strsep (&tmp, "\t"));
+ cur->port = (unsigned short) atoi (strsep (&tmp, "\t"));
+
+ if (!*map)
+ /* First item. */
+ *map = cur;
+ else
+ prev->next = cur;
+
+ cur->prev = prev;
+ cur->next = NULL;
+ prev = cur;
}
- free (line);
- if (err)
- {
- fclose (sel);
- return err;
- }
+ free (line);
+ fclose (sel);
- return 0;
+ return err;
}
diff --git a/gopher.h b/gopher.h
new file mode 100644
index 000000000..aab95b5c7
--- /dev/null
+++ b/gopher.h
@@ -0,0 +1,62 @@
+/* Gopher filesystem
+
+ Copyright (C) 2010 Manuel Menal <mmenal@hurdfr.org>
+ 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. */
+
+#ifndef __GOPHER_H__
+#define __GOPHER_H__
+
+#include "gopherfs.h"
+
+struct gopher_entry
+{
+ enum
+ {
+ GPHR_FILE = '0', /* Item is a file */
+ GPHR_DIR = '1', /* Item is a directory */
+ GPHR_CSOPH = '2', /* Item is a CSO phone-book server */
+ GPHR_ERROR = '3', /* Error */
+ GPHR_BINHEX = '4', /* Item is a BinHexed Macintosh file */
+ GPHR_DOSBIN = '5', /* Item is DOS binary archive of some sort */
+ GPHR_UUENC = '6', /* Item is a UNIX uuencoded file */
+ GPHR_SEARCH = '7', /* Item is an Index-Search server */
+ GPHR_TELNET = '8', /* Item points to a text-based telnet session */
+ GPHR_BIN = '9', /* Item is a binary file */
+ GPHR_GIF = 'g', /* Item is a GIF image */
+ GPHR_HTML = 'h', /* Item is a HTML file */
+ GPHR_INFO = 'i' /* Item is an informational message */
+ } type;
+
+ char *name;
+ char *selector;
+ char *server;
+ unsigned short port;
+
+ struct gopher_entry *prev, *next;
+};
+
+/* Do a DNS lookup for NAME:PORT and store result in PARAMS and error in H_ERR */
+error_t lookup_host (char *name, unsigned short port, struct addrinfo **addr);
+
+/* List all entries from ENTRY and store them in *MAP. */
+error_t gopher_list_entries (struct gopher_entry *entry, struct gopher_entry **map);
+
+/* open ENTRY and store the remote socket in *FD */
+error_t gopher_open (struct gopher_entry *entry, int *fd);
+
+#endif /* __GOPHER_H__ */
+
diff --git a/gopherfs.c b/gopherfs.c
index 3670290f4..cf9b54d8b 100644
--- a/gopherfs.c
+++ b/gopherfs.c
@@ -30,13 +30,8 @@
#include <hurd/netfs.h>
#include "gopherfs.h"
+#include "fs.h"
-/* definition of global config parapeters */
-char *gopherfs_root_server;
-unsigned short gopherfs_root_port;
-char *gopherfs_server_dir;
-
-int debug_flag;
char *netfs_server_name = GOPHER_SERVER_NAME;
char *netfs_server_version = GOPHER_SERVER_VERSION;
struct gopherfs *gopherfs; /* filesystem global pointer */
@@ -46,11 +41,16 @@ int
main (int argc, char **argv)
{
error_t err;
- mach_port_t bootstrap;
+ mach_port_t bootstrap, underlying_node;
+ struct stat underlying_stat;
+ struct gopherfs_params *gopherfs_params = malloc (sizeof (struct gopherfs_params));
+ memset (gopherfs_params, 0, sizeof (struct gopherfs_params));
+ if (!gopherfs_params)
+ error (1, errno, "Couldn't create params structure");
- gopherfs_parse_args (argc, argv);
- if (debug_flag)
- fprintf (stderr, "pid %d\n", getpid ());
+ // Parse arguments and fill GOPHERFS_PARAMS.
+ gopherfs_parse_args (argc, argv, gopherfs_params);
+ debug ("pid %d", getpid ());
task_get_bootstrap_port (mach_task_self (), &bootstrap);
if (bootstrap == MACH_PORT_NULL)
@@ -62,38 +62,41 @@ main (int argc, char **argv)
if (err)
error (1, err, "Error mapping time.");
+ // Create main gopherfs object.
+ err = gopherfs_create (gopherfs_params);
+ if (err)
+ error (1, err, "Couldn't create gopherfs structure");
- /* err = gopherfs_create (...); */
- /* XXX */
- gopherfs = (struct gopherfs *) malloc (sizeof (struct gopherfs));
- if (! gopherfs)
- error (1, errno, "Cannot allocate gopherfs.");
-
- gopherfs->umask = 0;
- gopherfs->uid = getuid ();
- gopherfs->gid = getgid ();
- gopherfs->next_inode = 0;
- gopherfs->root =
- gopherfs_make_node (GPHR_DIR, "dir", "", gopherfs_root_server,
- gopherfs_root_port);
- if (debug_flag)
- fprintf (stderr, "attaching to %s\n", gopherfs_root_server);
- /* XXX */
netfs_root_node = gopherfs->root;
- netfs_startup (bootstrap, 0);
- if (debug_flag)
- fprintf (stderr, "entering the main loop\n");
- for (;;)
- if (debug_flag)
- fprintf (stderr, "loop\n");
+ underlying_node = netfs_startup (bootstrap, 0);
- netfs_server_loop ();
+ err = io_stat (underlying_node, &underlying_stat);
+ if (err)
+ error (1, err, "cannot stat underlying node");
+
+ /* Initialize stat information of the root node. */
+ netfs_root_node->nn_stat = underlying_stat;
+ netfs_root_node->nn_stat.st_mode =
+ S_IFDIR | (underlying_stat.st_mode & ~S_IFMT & ~S_ITRANS);
+
+ /* If the underlying node isn't a directory, propagate read permission to
+ execute permission since we need that for lookups. */
+ if (! S_ISDIR (underlying_stat.st_mode))
+ {
+ if (underlying_stat.st_mode & S_IRUSR)
+ netfs_root_node->nn_stat.st_mode |= S_IXUSR;
+ if (underlying_stat.st_mode & S_IRGRP)
+ netfs_root_node->nn_stat.st_mode |= S_IXGRP;
+ if (underlying_stat.st_mode & S_IROTH)
+ netfs_root_node->nn_stat.st_mode |= S_IXOTH;
+ }
+
+ debug("entering the main loop");
- /*NOT REACHED */
- fprintf (stderr, "Reached, now it will die");
+ for (;;)
+ netfs_server_loop ();
- /* free (gopherfs); */
return 0;
}
diff --git a/gopherfs.h b/gopherfs.h
index d71037a1c..376bec482 100644
--- a/gopherfs.h
+++ b/gopherfs.h
@@ -32,44 +32,35 @@
#include <hurd/hurd_types.h>
#define GOPHER_SERVER_NAME "gopherfs"
#define GOPHER_SERVER_VERSION "0.1.2"
-/* declaration of global config parapeters */
-extern char *gopherfs_root_server;
-extern unsigned short gopherfs_root_port;
-extern char *gopherfs_server_dir;
-extern int debug_flag;
+#include "debug.h"
+
extern volatile struct mapped_time_value *gopherfs_maptime;
+/* gopherfs parameters. */
+struct gopherfs_params
+{
+ char *server; /* Server host name. */
+ unsigned short port; /* Port number. */
+ char *dir; /* Root directory. */
+};
-/* handle all initial parameter parsing */
-error_t gopherfs_parse_args (int argc, char **argv);
+#include "gopher.h"
/* private data per `struct node' */
struct netnode
{
- char *name;
- char *selector;
- char *server;
- unsigned short port;
- enum
- {
- GPHR_FILE = '0', /* Item is a file */
- GPHR_DIR = '1', /* Item is a directory */
- GPHR_CSOPH = '2', /* Item is a CSO phone-book server */
- GPHR_ERROR = '3', /* Error */
- GPHR_BINHEX = '4', /* Item is a BinHexed Macintosh file */
- GPHR_DOSBIN = '5', /* Item is DOS binary archive of some sort */
- GPHR_UUENC = '6', /* Item is a UNIX uuencoded file */
- GPHR_SEARCH = '7', /* Item is an Index-Search server */
- GPHR_TELNET = '8', /* Item points to a text-based telnet session */
- GPHR_BIN = '9' /* Item is a binary file */
- }
- type;
+ /* Gopher parameters (type, name, selector, server, port). */
+ struct gopher_entry *e;
+
+ /* The directory entry for this node. */
+ struct node *dir;
/* directory entries if this is a directory */
struct node *ents;
boolean_t noents;
unsigned int num_ents;
+
/* XXX cache reference ? */
};
@@ -83,33 +74,27 @@ struct gopherfs
gid_t gid;
ino_t next_inode;
/* some kind of cache thingy */
+
+ struct gopherfs_params *params;
};
+
/* global pointer to the filesystem */
extern struct gopherfs *gopherfs;
-/* do a DNS lookup for NAME and store result in *ENT */
-error_t lookup_host (char *name, struct hostent **ent);
-
-/* store the remote socket in *FD after writing a gopher selector
- to it */
-error_t open_selector (struct netnode *node, int *fd);
-
-/* 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);
+/* handle all initial parameter parsing */
+error_t gopherfs_parse_args (int argc, char **argv, struct gopherfs_params *params);
/* fetch a directory node from the gopher server
DIR should already be locked */
-error_t fill_dirnode (struct netnode *dir);
+error_t fill_dirnode (struct node *dir);
/* free an instance of `struct netnode' */
void free_netnode (struct netnode *node);
-/* make an instance of `struct node' with the specified parameters,
+/* make an instance of `struct node' in DIR with the parameters in ENTRY,
return NULL on error */
-struct node *gopherfs_make_node (char type, char *name, char *selector,
- char *server, unsigned short port);
+struct node *gopherfs_make_node (struct gopher_entry *entry,
+ struct node *dir);
/* free an instance of `struct node' */
void free_node (struct node *node);
diff --git a/netfs.c b/netfs.c
index d99375655..b4310647f 100644
--- a/netfs.c
+++ b/netfs.c
@@ -42,11 +42,11 @@
locked on success; no matter what, unlock DIR before returning. */
error_t
netfs_attempt_create_file (struct iouser *user, struct node *dir,
- char *name, mode_t mode, struct node **node)
+ char *name, mode_t mode, struct node **node)
{
- *node = NULL;
- mutex_unlock (&dir->lock);
- return EROFS;
+ *node = NULL;
+ mutex_unlock (&dir->lock);
+ return EROFS;
}
/* Node NODE is being opened by USER, with FLAGS. NEWNODE is nonzero if we
@@ -54,45 +54,49 @@ netfs_attempt_create_file (struct iouser *user, struct node *dir,
to complete because of a permission restriction. */
error_t
netfs_check_open_permissions (struct iouser *user, struct node *node,
- int flags, int newnode)
+ int flags, int newnodes)
{
- error_t err = 0;
- if (!err && (flags & O_READ))
- err = fshelp_access (&node->nn_stat, S_IREAD, user);
- if (!err && (flags & O_WRITE))
- err = fshelp_access (&node->nn_stat, S_IWRITE, user);
- if (!err && (flags & O_EXEC))
- err = fshelp_access (&node->nn_stat, S_IEXEC, user);
- return err;
+ debug("Checking permissions on %s", node->nn->e->name);
+ error_t err = 0;
+ if (!err && (flags & O_READ))
+ err = fshelp_access (&node->nn_stat, S_IREAD, user);
+ if (!err && (flags & O_WRITE))
+ err = fshelp_access (&node->nn_stat, S_IWRITE, user);
+ if (!err && (flags & O_EXEC))
+ err = fshelp_access (&node->nn_stat, S_IEXEC, user);
+ return err;
}
/* This should attempt a utimes call for the user specified by CRED on node
NODE, to change the atime to ATIME and the mtime to MTIME. */
error_t
netfs_attempt_utimes (struct iouser *cred, struct node *node,
- struct timespec *atime, struct timespec *mtime)
+ struct timespec *atime, struct timespec *mtime)
{
- error_t err = 0;
- int flags = TOUCH_CTIME;
+ error_t err = 0;
+ int flags = TOUCH_CTIME;
- if (!err)
- err = fshelp_isowner (&node->nn_stat, cred);
+ debug("Changing atime/mtime for file %s", node->nn->e->name);
- if (!err) {
- if (atime)
- node->nn_stat.st_atim = *atime;
- else
- flags |= TOUCH_ATIME;
+ if (!err)
+ err = fshelp_isowner (&node->nn_stat, cred);
- if (mtime)
- node->nn_stat.st_mtim = *mtime;
- else
- flags |= TOUCH_MTIME;
+ if (!err)
+ {
+ if (atime)
+ node->nn_stat.st_atim = *atime;
+ else
+ flags |= TOUCH_ATIME;
- fshelp_touch (&node->nn_stat, flags, gopherfs_maptime);
- }
+ if (mtime)
+ node->nn_stat.st_mtim = *mtime;
+ else
+ flags |= TOUCH_MTIME;
- return err;
+ fshelp_touch (&node->nn_stat, flags, gopherfs_maptime);
+ }
+
+ return err;
}
/* Return the valid access types (bitwise OR of O_READ, O_WRITE, and O_EXEC)
@@ -100,27 +104,30 @@ netfs_attempt_utimes (struct iouser *cred, struct node *node,
error_t
netfs_report_access (struct iouser *cred, struct node *node, int *types)
{
- *types = 0;
- if (fshelp_access (&node->nn_stat, S_IREAD, cred) == 0)
- *types |= O_READ;
- if (fshelp_access (&node->nn_stat, S_IWRITE, cred) == 0)
- *types |= O_WRITE;
- if (fshelp_access (&node->nn_stat, S_IEXEC, cred) == 0)
- *types |= O_EXEC;
-
- return 0;
+ debug("Reporting access types for file %s", node->nn->e->name);
+ *types = 0;
+ if (fshelp_access (&node->nn_stat, S_IREAD, cred) == 0)
+ *types |= O_READ;
+ if (fshelp_access (&node->nn_stat, S_IWRITE, cred) == 0)
+ *types |= O_WRITE;
+ if (fshelp_access (&node->nn_stat, S_IEXEC, cred) == 0)
+ *types |= O_EXEC;
+
+ return 0;
}
/* Trivial definitions. */
/* Make sure that NP->nn_stat is filled with current information. CRED
identifies the user responsible for the operation. */
-error_t netfs_validate_stat (struct node *node, struct iouser *cred)
+error_t
+netfs_validate_stat (struct node *node, struct iouser *cred)
{
- /* Assume that gopher data is static and will not change for however
- * long we keep the cached node. Hence nn_stat is always valid.
- * XXX: would be a good idea to introduce a forced refresh call */
- return 0;
+ debug("Validating stat infos for %s", node->nn->e->name);
+ /* Assume that gopher data is static and will not change for however
+ * long we keep the cached node. Hence nn_stat is always valid.
+ * XXX: would be a good idea to introduce a forced refresh call */
+ return 0;
}
/* This should sync the file NODE completely to disk, for the user CRED. If
@@ -128,7 +135,8 @@ error_t netfs_validate_stat (struct node *node, struct iouser *cred)
error_t
netfs_attempt_sync (struct iouser *cred, struct node *node, int wait)
{
- return 0;
+ debug("Attempting sync on %s", node->nn->e->name);
+ return 0;
}
#if 0
@@ -150,174 +158,213 @@ netfs_attempt_sync (struct iouser *cred, struct node *node, int wait)
/* Fetch a directory */
error_t
netfs_get_dirents (struct iouser *cred, struct node *dir,
- int first_entry, int max_entries, char **data,
- mach_msg_type_number_t *data_len,
- vm_size_t max_data_len, int *data_entries)
+ int first_entry, int max_entries, char **data,
+ mach_msg_type_number_t *data_len,
+ vm_size_t max_data_len, int *data_entries)
{
- error_t err = 0;
- struct node *nd;
- int count;
- size_t size;
- char *p;
-
- if (!dir || dir->nn->type != GPHR_DIR)
- return ENOTDIR;
-
- if (!dir->nn->ents && !dir->nn->noents) {
- mutex_lock (&dir->lock);
- err = fill_dirnode (dir->nn);
- mutex_unlock (&dir->lock);
- if (err)
- return err;
- }
+ debug("Listing directory entries for %s", dir->nn->e->name);
- for (nd = dir->nn->ents; first_entry > 0 && nd; first_entry--, nd=nd->next)
- ;
- if (!nd)
- max_entries = 0;
+ error_t err = 0;
+ struct node *nd;
+ int count;
+ size_t size;
+ char *p;
- if (max_entries == 0) {
- *data_len = 0;
- *data_entries = 0;
- *data = NULL;
- return 0;
- }
+ if (!dir || dir->nn->e->type != GPHR_DIR)
+ return ENOTDIR;
+
+ if (!dir->nn->ents && !dir->nn->noents)
+ {
+ err = fill_dirnode (dir);
+ if (err)
+ return err;
+ }
+
+ for (nd = dir->nn->ents; first_entry > 0 && nd; first_entry--, nd=nd->next)
+ ;
+ if (!nd)
+ max_entries = 0;
+
+ if (max_entries == 0)
+ {
+ *data_len = 0;
+ *data_entries = 0;
+ *data = NULL;
+ return 0;
+ }
#define DIRENTS_CHUNK_SIZE (8*1024)
- size = (max_data_len == 0 || max_data_len > DIRENTS_CHUNK_SIZE)
- ? DIRENTS_CHUNK_SIZE
- : max_data_len;
- errno = 0;
- *data = NULL;
- err = vm_allocate (mach_task_self (), (vm_address_t *)data,
- size, /*anywhere*/TRUE);
- if (err)
- return err;
- err = vm_protect (mach_task_self (), (vm_address_t)*data, size,
- /*set_maximum*/FALSE, VM_PROT_READ | VM_PROT_WRITE);
- if (err)
- return err;
-
- p = *data;
- for (count = 0; nd && (max_entries == -1 || count < max_entries); count++) {
+ size = (max_data_len == 0 || max_data_len > DIRENTS_CHUNK_SIZE)
+ ? DIRENTS_CHUNK_SIZE
+ : max_data_len;
+ errno = 0;
+ *data = NULL;
+ err = vm_allocate (mach_task_self (), (vm_address_t *)data,
+ size, /*anywhere*/TRUE);
+ if (err)
+ return err;
+ err = vm_protect (mach_task_self (), (vm_address_t)*data, size,
+ /*set_maximum*/FALSE, VM_PROT_READ | VM_PROT_WRITE);
+ if (err)
+ return err;
+
+ p = *data;
+ for (count = 0; nd && (max_entries == -1 || count < max_entries); count++)
+ {
#define DIRENT_NAME_OFFS offsetof (struct dirent, d_name)
- vm_address_t addr;
- struct dirent ent;
-
- ent.d_fileno = nd->nn_stat.st_ino;
- ent.d_type = IFTODT (nd->nn_stat.st_mode);
- ent.d_namlen = strlen (nd->nn->name);
- ent.d_reclen = DIRENT_NAME_OFFS + ent.d_namlen + 1;
- if (p - *data + ent.d_reclen > size) {
- size_t extra = ((p - *data + ent.d_reclen - size)/DIRENTS_CHUNK_SIZE + 1)*DIRENTS_CHUNK_SIZE;
- if (extra + size > max_data_len)
- break;
- addr = (vm_address_t) (*data + size);
- err = vm_allocate (mach_task_self (), &addr, extra, /*anywhere*/FALSE);
- if (err)
- break;
- err = vm_protect (mach_task_self (), (vm_address_t)*data, size,
- /*set_maximum*/FALSE, VM_PROT_READ | VM_PROT_WRITE);
- if (err)
- break;
- size += extra;
- }
- /* copy the dirent structure */
- memcpy (p, &ent, DIRENT_NAME_OFFS);
- /* copy ent.d_name */
- strncpy (p + DIRENT_NAME_OFFS, nd->nn->name, ent.d_namlen);
- p += ent.d_reclen;
-
- nd = nd->next;
- }
- if (err) {
- vm_deallocate (mach_task_self (), (vm_address_t)*data, size);
- *data_len = 0;
- *data_entries = 0;
- *data = NULL;
- return 0;
- } else {
- vm_address_t alloc_end = (vm_address_t) (*data + size);
- vm_address_t real_end = round_page (p);
-
- if (alloc_end > real_end)
- vm_deallocate (mach_task_self (), real_end, alloc_end - real_end);
- *data_len = p - *data;
- *data_entries = count;
+ vm_address_t addr;
+ struct dirent ent;
+
+ ent.d_fileno = nd->nn_stat.st_ino;
+ ent.d_type = IFTODT (nd->nn_stat.st_mode);
+ ent.d_namlen = strlen (nd->nn->e->name);
+ ent.d_reclen = DIRENT_NAME_OFFS + ent.d_namlen + 1;
+ if (p - *data + ent.d_reclen > size)
+ {
+ size_t extra = ((p - *data + ent.d_reclen - size)/DIRENTS_CHUNK_SIZE + 1)*DIRENTS_CHUNK_SIZE;
+ if (extra + size > max_data_len)
+ break;
+ addr = (vm_address_t) (*data + size);
+ err = vm_allocate (mach_task_self (), &addr, extra, /*anywhere*/FALSE);
+ if (err)
+ break;
+ err = vm_protect (mach_task_self (), (vm_address_t)*data, size,
+ /*set_maximum*/FALSE, VM_PROT_READ | VM_PROT_WRITE);
+ if (err)
+ break;
+ size += extra;
}
-
- return 0;
+ /* copy the dirent structure */
+ memcpy (p, &ent, DIRENT_NAME_OFFS);
+ /* copy ent.d_name */
+ strncpy (p + DIRENT_NAME_OFFS, nd->nn->e->name, ent.d_namlen);
+ p += ent.d_reclen;
+
+ nd = nd->next;
+ }
+
+ if (err)
+ {
+ vm_deallocate (mach_task_self (), (vm_address_t)*data, size);
+ *data_len = 0;
+ *data_entries = 0;
+ *data = NULL;
+ return 0;
+ }
+ else
+ {
+ vm_address_t alloc_end = (vm_address_t) (*data + size);
+ vm_address_t real_end = round_page (p);
+
+ if (alloc_end > real_end)
+ vm_deallocate (mach_task_self (), real_end, alloc_end - real_end);
+ *data_len = p - *data;
+ *data_entries = count;
+ }
+
+ return 0;
}
/* Lookup NAME in DIR for USER; set *NODE to the found name upon return. If
the name was not found, then return ENOENT. On any error, clear *NODE.
(*NODE, if found, should be locked, this call should unlock DIR no matter
what.) */
-error_t netfs_attempt_lookup (struct iouser *user, struct node *dir,
- char *name, struct node ** node)
+error_t
+netfs_attempt_lookup (struct iouser *user, struct node *dir,
+ char *name, struct node **node)
{
- error_t err;
- struct node *nd;
-
- if (dir->nn->type != GPHR_DIR)
- err = ENOTDIR;
- for (nd = dir->nn->ents; nd && strcmp (name, nd->nn->name); nd = nd->next)
- ;
- if (nd) {
- mutex_lock (&nd->lock);
- *node = nd;
- err = 0;
- } else
- err = ENOENT;
-
- mutex_unlock (&dir->lock);
- return 0;
+ error_t err = 0;
+ struct node *nd = NULL;
+
+ debug("Looking up %s in dir %s", name, dir->nn->e->name);
+
+ /* Make sure DIR is an actual directory. */
+ if (dir->nn->e->type != GPHR_DIR)
+ err = ENOTDIR;
+
+ if (strcmp (name, ".") == 0)
+ *node = dir;
+ else if (strcmp (name, "..") == 0)
+ {
+ if (dir->nn->dir)
+ *node = dir->nn->dir;
+ else
+ /* Root node. */
+ err = EAGAIN;
+ }
+ else
+ {
+ /* Lookup a regular node. */
+ for (nd = dir->nn->ents; nd && strcmp (name, nd->nn->e->name); nd = nd->next)
+ ;
+
+ if (nd)
+ mutex_lock (&nd->lock);
+ else
+ err = ENOENT;
+
+ *node = nd;
+ }
+
+ /* Add a new reference to *NODE. */
+ if (!err)
+ netfs_nref (*node);
+
+ mutex_unlock (&dir->lock);
+
+ return err;
}
/* Delete NAME in DIR for USER. */
-error_t netfs_attempt_unlink (struct iouser *user, struct node *dir,
- char *name)
+error_t
+netfs_attempt_unlink (struct iouser *user, struct node *dir,
+ char *name)
{
- return EROFS;
+ return EROFS;
}
/* Note that in this one call, neither of the specific nodes are locked. */
-error_t netfs_attempt_rename (struct iouser *user, struct node *fromdir,
- char *fromname, struct node *todir,
- char *toname, int excl)
+error_t
+netfs_attempt_rename (struct iouser *user, struct node *fromdir,
+ char *fromname, struct node *todir,
+ char *toname, int excl)
{
- return EROFS;
+ return EROFS;
}
/* Attempt to create a new directory named NAME in DIR for USER with mode
MODE. */
-error_t netfs_attempt_mkdir (struct iouser *user, struct node *dir,
- char *name, mode_t mode)
+error_t
+netfs_attempt_mkdir (struct iouser *user, struct node *dir,
+ char *name, mode_t mode)
{
- return EROFS;
+ return EROFS;
}
/* Attempt to remove directory named NAME in DIR for USER. */
-error_t netfs_attempt_rmdir (struct iouser *user,
- struct node *dir, char *name)
+error_t
+netfs_attempt_rmdir (struct iouser *user,
+ struct node *dir, char *name)
{
- return EROFS;
+ return EROFS;
}
/* This should attempt a chmod call for the user specified by CRED on node
NODE, to change the owner to UID and the group to GID. */
-error_t netfs_attempt_chown (struct iouser *cred, struct node *node,
- uid_t uid, uid_t gid)
+error_t
+netfs_attempt_chown (struct iouser *cred, struct node *node,
+ uid_t uid, uid_t gid)
{
- return EROFS;
+ return EROFS;
}
/* This should attempt a chauthor call for the user specified by CRED on node
NODE, to change the author to AUTHOR. */
-error_t netfs_attempt_chauthor (struct iouser *cred, struct node *node,
- uid_t author)
+error_t
+netfs_attempt_chauthor (struct iouser *cred, struct node *node,
+ uid_t author)
{
- return EROFS;
+ return EROFS;
}
/* This should attempt a chmod call for the user specified by CRED on node
@@ -325,33 +372,37 @@ error_t netfs_attempt_chauthor (struct iouser *cred, struct node *node,
of chmod, this function is also used to attempt to change files into other
types. If such a transition is attempted which is impossible, then return
EOPNOTSUPP. */
-error_t netfs_attempt_chmod (struct iouser *cred, struct node *node,
- mode_t mode)
+error_t
+netfs_attempt_chmod (struct iouser *cred, struct node *node,
+ mode_t mode)
{
- return EROFS;
+ return EROFS;
}
/* Attempt to turn NODE (user CRED) into a symlink with target NAME. */
-error_t netfs_attempt_mksymlink (struct iouser *cred, struct node *node,
- char *name)
+error_t
+netfs_attempt_mksymlink (struct iouser *cred, struct node *node,
+ char *name)
{
- return EROFS;
+ return EROFS;
}
/* Attempt to turn NODE (user CRED) into a device. TYPE is either S_IFBLK or
S_IFCHR. */
-error_t netfs_attempt_mkdev (struct iouser *cred, struct node *node,
- mode_t type, dev_t indexes)
+error_t
+netfs_attempt_mkdev (struct iouser *cred, struct node *node,
+ mode_t type, dev_t indexes)
{
- return EROFS;
+ return EROFS;
}
/* Attempt to set the passive translator record for FILE to ARGZ (of length
ARGZLEN) for user CRED. */
-error_t netfs_set_translator (struct iouser *cred, struct node *node,
- char *argz, size_t argzlen)
+error_t
+netfs_set_translator (struct iouser *cred, struct node *node,
+ char *argz, size_t argzlen)
{
- return EROFS;
+ return EROFS;
}
#if 0
@@ -360,121 +411,151 @@ error_t netfs_set_translator (struct iouser *cred, struct node *node,
mode, look up the name of its translator. Store the name into newly
malloced storage, and return it in *ARGZ; set *ARGZ_LEN to the total
length. */
-error_t netfs_get_translator (struct node *node, char **argz, size_t *argz_len)
+error_t
+netfs_get_translator (struct node *node, char **argz, size_t *argz_len)
{
}
#endif
/* This should attempt a chflags call for the user specified by CRED on node
NODE, to change the flags to FLAGS. */
-error_t netfs_attempt_chflags (struct iouser *cred, struct node *node,
- int flags)
+error_t
+netfs_attempt_chflags (struct iouser *cred, struct node *node,
+ int flags)
{
- return EROFS;
+ return EROFS;
}
/* This should attempt to set the size of the file NODE (for user CRED) to
SIZE bytes long. */
-error_t netfs_attempt_set_size (struct iouser *cred, struct node *node,
- off_t size)
+error_t
+netfs_attempt_set_size (struct iouser *cred, struct node *node,
+ off_t size)
{
- return EROFS;
+ return EROFS;
}
/* This should attempt to fetch filesystem status information for the remote
filesystem, for the user CRED. */
-error_t netfs_attempt_statfs (struct iouser *cred, struct node *node,
- struct statfs *st)
+error_t
+netfs_attempt_statfs (struct iouser *cred, struct node *node,
+ struct statfs *st)
{
- return EOPNOTSUPP;
+ return EOPNOTSUPP;
}
/* This should sync the entire remote filesystem. If WAIT is set, return
only after sync is completely finished. */
-error_t netfs_attempt_syncfs (struct iouser *cred, int wait)
+error_t
+netfs_attempt_syncfs (struct iouser *cred, int wait)
{
- return 0;
+ return 0;
}
/* Create a link in DIR with name NAME to FILE for USER. Note that neither
DIR nor FILE are locked. If EXCL is set, do not delete the target, but
return EEXIST if NAME is already found in DIR. */
-error_t netfs_attempt_link (struct iouser *user, struct node *dir,
- struct node *file, char *name, int excl)
+error_t
+netfs_attempt_link (struct iouser *user, struct node *dir,
+ struct node *file, char *name, int excl)
{
- return EROFS;
+ return EROFS;
}
/* Attempt to create an anonymous file related to DIR for USER with MODE.
Set *NODE to the returned file upon success. No matter what, unlock DIR. */
-error_t netfs_attempt_mkfile (struct iouser *user, struct node *dir,
- mode_t mode, struct node ** node)
+error_t
+netfs_attempt_mkfile (struct iouser *user, struct node *dir,
+ mode_t mode, struct node ** node)
{
- *node = NULL;
- mutex_unlock (&dir->lock);
- return EROFS;
+ *node = NULL;
+ mutex_unlock (&dir->lock);
+ return EROFS;
}
/* maximum numer of symlinks, does not really apply, so set to 0 */
int netfs_maxsymlinks = 0;
/* Read the contents of NODE (a symlink), for USER, into BUF. */
-error_t netfs_attempt_readlink (struct iouser *user, struct node *node,
- char *buf)
+error_t
+netfs_attempt_readlink (struct iouser *user, struct node *node,
+ char *buf)
{
- return EINVAL;
+ return EINVAL;
}
/* Read from the file NODE for user CRED starting at OFFSET and continuing for
up to *LEN bytes. Put the data at DATA. Set *LEN to the amount
successfully read upon return. */
-error_t netfs_attempt_read (struct iouser *cred, struct node *node,
- off_t offset, size_t *len, void *data)
+error_t
+netfs_attempt_read (struct iouser *cred, struct node *node,
+ off_t offset, size_t *len, void *data)
{
- error_t err;
- int remote_fd;
- ssize_t read_len;
-
- err = open_selector (node->nn, &remote_fd);
- if (err)
- return err;
-
- read_len = pread (remote_fd, data, *len, offset);
- if (read_len < 0)
- err = errno;
- else {
- *len = (size_t)read_len;
- err = 0;
- }
-
- return err;
+ int remote_fd;
+ void *buf;
+ ssize_t size;
+ error_t err;
+
+ debug("Reading contents of file %s (offset: %d, len: %d)",
+ node->nn->e->name, offset, *len);
+
+ err = gopher_open (node->nn->e, &remote_fd);
+ if (err)
+ return err;
+
+ /* We can't seek into a socket. Read offset + *len bytes and then
+ copy only the last *len bytes.*/
+ buf = malloc (offset + *len);
+
+ size = read (remote_fd, buf, *len + offset);
+ if (size < 0)
+ err = errno;
+ else
+ {
+ *len = size - offset;
+ memcpy (data, buf + offset, *len);
+ err = 0;
+ }
+
+ free (buf);
+
+ return err;
}
/* Write to the file NODE for user CRED starting at OFSET and continuing for up
to *LEN bytes from DATA. Set *LEN to the amount seccessfully written upon
return. */
-error_t netfs_attempt_write (struct iouser *cred, struct node *node,
- off_t offset, size_t *len, void *data)
+error_t
+netfs_attempt_write (struct iouser *cred, struct node *node,
+ off_t offset, size_t *len, void *data)
{
- return EROFS;
+ return EROFS;
}
/* XXX doesn't say anywhere what this must do */
#if 0
/* The user must define this function. Create a new user
from the specified UID and GID arrays. */
-struct iouser *netfs_make_user (uid_t *uids, int nuids,
- uid_t *gids, int ngids)
+struct iouser *
+netfs_make_user (uid_t *uids, int nuids,
+ uid_t *gids, int ngids)
{
}
#endif
/* The user must define this function. Node NP is all done; free
all its associated storage. */
-void netfs_node_norefs (struct node *np)
+void
+netfs_node_norefs (struct node *np)
{
- mutex_lock (&np->lock);
- *np->prevp = np->next;
- np->next->prevp = np->prevp;
- free_node (np);
- /* XXX: remove node from tree and delete the cache entry */
+ debug ("No more references for file %s", np->nn->e->name);
+
+ spin_unlock (&netfs_node_refcnt_lock);
+
+ *np->prevp = np->next;
+ np->next->prevp = np->prevp;
+
+ free_node (np);
+
+ /* XXX: remove node from tree and delete the cache entry */
+ spin_lock(&netfs_node_refcnt_lock);
}
diff --git a/node.c b/node.c
index 5dff209f1..9389103e8 100644
--- a/node.c
+++ b/node.c
@@ -27,63 +27,98 @@
#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 */
-struct netnode *
-gopherfs_make_netnode (char type, char *name, char *selector,
- char *server, unsigned short port)
+static struct netnode *
+gopherfs_make_netnode (struct gopher_entry *entry,
+ struct node *dir)
{
struct netnode *nn;
- nn = (struct netnode *) malloc (sizeof (struct netnode));
+ if (!entry)
+ return NULL;
+
+ nn = 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);
+ nn->dir = dir;
+ nn->e = malloc (sizeof (struct gopher_entry));
+ if (!nn->e)
+ {
free (nn);
return NULL;
}
- return nn;
-}
+ 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;
-/* free an instance of `struct netnode' */
-void
-free_netnode (struct netnode *node)
-{
- struct node *nd;
+ /* 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;
- 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,
+/* make an instance of `struct node' in DIR with the parameters in ENTRY.
return NULL on error */
struct node *
-gopherfs_make_node (char type, char *name, char *selector,
- char *server, unsigned short port)
+gopherfs_make_node (struct gopher_entry *entry,
+ struct node *dir)
{
struct netnode *nn;
struct node *nd;
- nn = gopherfs_make_netnode (type, name, selector, server, port);
+ nn = gopherfs_make_netnode (entry, dir);
if (!nn)
return NULL;
@@ -104,7 +139,7 @@ gopherfs_make_node (char type, char *name, char *selector,
/* 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_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;
@@ -118,12 +153,3 @@ gopherfs_make_node (char type, char *name, char *selector,
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);
-}