summaryrefslogtreecommitdiff
path: root/ftpfs/conn.c
blob: 930e2eb72b7ed10587c51b27d0472af757c34f49 (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
/* Ftp connection management

   Copyright (C) 1997 Free Software Foundation, Inc.
   Written by Miles Bader <miles@gnu.ai.mit.edu>
   This file is part of the GNU Hurd.

   The GNU Hurd 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.

   The GNU Hurd 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 <assert.h>

#include "ftpfs.h"

/* A particular connection.  */
struct ftpfs_conn 
{
  struct ftp_conn *conn;
  struct ftpfs_conn *next;
};

/* For debugging purposes, give each connection a unique integer id.  */
static unsigned conn_id = 0;

/* Get an ftp connection to use for an operation. */
error_t
ftpfs_get_ftp_conn (struct ftpfs *fs, struct ftp_conn **conn)
{
  struct ftpfs_conn *fsc;

  spin_lock (&fs->conn_lock);
  fsc = fs->free_conns;
  if (fsc)
    fs->free_conns = fsc->next;
  spin_unlock (&fs->conn_lock);

  if (! fsc)
    {
      error_t err;

      fsc = malloc (sizeof (struct ftpfs_conn));
      if (! fsc)
	return ENOMEM;

      err = ftp_conn_create (fs->ftp_params, fs->ftp_hooks, &fsc->conn);

      if (! err)
	{
	  /* Set connection type to binary.  */
	  err = ftp_conn_set_type (fsc->conn, "I");
	  if (err)
	    ftp_conn_free (fsc->conn);
	}

      if (err)
	{
	  free (fsc);
	  return err;
	}

      /* For debugging purposes, give each connection a unique integer id.  */
      fsc->conn->hook = (void *)conn_id++;
    }

  spin_lock (&fs->conn_lock);
  fsc->next = fs->conns;
  fs->conns = fsc;
  spin_unlock (&fs->conn_lock);

  *conn = fsc->conn;

  return 0;
}

/* Return CONN to the pool of free connections in FS.  */
void
ftpfs_release_ftp_conn (struct ftpfs *fs, struct ftp_conn *conn)
{
  struct ftpfs_conn *fsc, *pfsc;

  spin_lock (&fs->conn_lock);
  for (pfsc = 0, fsc = fs->conns; fsc; pfsc = fsc, fsc = fsc->next)
    if (fsc->conn == conn)
      {
	if (pfsc)
	  pfsc->next = fsc->next;
	else
	  fs->conns = fsc->next;
	fsc->next = fs->free_conns;
	fs->free_conns = fsc;
	break;
      }
  assert (fsc);
  spin_unlock (&fs->conn_lock);
}