summaryrefslogtreecommitdiff
path: root/client/sftpuri.c
blob: 65ed6cbdc7b4e976b5e4caf92640f8b9b3b93baf (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
156
157
158
159
160
161
162
163
164
165
166
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 */

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

#include <gio/gio.h>
#include <gvfsurimapper.h>
#include <gvfsuriutils.h>

typedef struct _GVfsUriMapperSftp GVfsUriMapperSftp;
typedef struct _GVfsUriMapperSftpClass GVfsUriMapperSftpClass;

struct _GVfsUriMapperSftp
{
  GVfsUriMapper parent;
};

struct _GVfsUriMapperSftpClass
{
  GVfsUriMapperClass parent_class;
};

GType g_vfs_uri_mapper_sftp_get_type (void);
void g_vfs_uri_mapper_sftp_register (GIOModule *module);

G_DEFINE_DYNAMIC_TYPE (GVfsUriMapperSftp, g_vfs_uri_mapper_sftp, G_VFS_TYPE_URI_MAPPER)

static void
g_vfs_uri_mapper_sftp_init (GVfsUriMapperSftp *vfs)
{
}

static const char * const *
sftp_get_handled_schemes (GVfsUriMapper *mapper)
{
  static const char *schemes[] = {
    "sftp",
    "ssh",
    NULL
  };
  return schemes;
}

static GVfsUriMountInfo *
sftp_from_uri (GVfsUriMapper *mapper,
	       const char *uri_str)
{
  GDecodedUri *uri;
  GVfsUriMountInfo *info;

  uri = g_vfs_decode_uri (uri_str);
  if (uri == NULL)
    return NULL;

  info = g_vfs_uri_mount_info_new ("sftp");
  
  if (uri->host && *uri->host)
    g_vfs_uri_mount_info_set (info, "host", uri->host);
  
  if (uri->userinfo && *uri->userinfo)
    g_vfs_uri_mount_info_set (info, "user", uri->userinfo);
  
  if (uri->port != -1)
    {
      char *port = g_strdup_printf ("%d", uri->port);
      g_vfs_uri_mount_info_set (info, "port", port);
      g_free (port);
    }
  
  info->path = g_strdup (uri->path);
  
  g_vfs_decoded_uri_free (uri);

  return info;
}

static const char * const *
sftp_get_handled_mount_types (GVfsUriMapper *mapper)
{
  static const char *types[] = {
    "sftp", 
    NULL
  };
  return types;
}

static char *
sftp_to_uri (GVfsUriMapper *mapper,
	     GVfsUriMountInfo *info,
	     gboolean allow_utf8)
{
  GDecodedUri uri;
  const char *port;

  memset (&uri, 0, sizeof (uri));
  uri.port = -1;

  uri.scheme = "sftp";
  uri.host = (char *)g_vfs_uri_mount_info_get (info, "host");
  uri.userinfo = (char *)g_vfs_uri_mount_info_get (info, "user");
  
  port = g_vfs_uri_mount_info_get (info, "port");
  if (port != NULL)
    uri.port = atoi (port);

  if (info->path == NULL)
    uri.path = "/";
  else
    uri.path = info->path;
  
  return g_vfs_encode_uri (&uri, allow_utf8);
}

static const char *
sftp_to_uri_scheme (GVfsUriMapper *mapper,
		    GVfsUriMountInfo *info)
{
  return "sftp";
}

static void
g_vfs_uri_mapper_sftp_class_finalize (GVfsUriMapperSftpClass *klass)
{
}

static void
g_vfs_uri_mapper_sftp_class_init (GVfsUriMapperSftpClass *class)
{
  GObjectClass *object_class;
  GVfsUriMapperClass *mapper_class;
  
  object_class = (GObjectClass *) class;
  mapper_class = G_VFS_URI_MAPPER_CLASS (class);
  mapper_class->get_handled_schemes = sftp_get_handled_schemes;
  mapper_class->from_uri = sftp_from_uri;
  mapper_class->get_handled_mount_types = sftp_get_handled_mount_types;
  mapper_class->to_uri = sftp_to_uri;
  mapper_class->to_uri_scheme = sftp_to_uri_scheme;
}

void
g_vfs_uri_mapper_sftp_register (GIOModule *module)
{
  g_vfs_uri_mapper_sftp_register_type (G_TYPE_MODULE (module));
}