summaryrefslogtreecommitdiff
path: root/client/afpuri.c
blob: 7fa0f327b617d47afe2a4605d9daed1d53232cd3 (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 * Copyright (C) Carl-Anton Ingmarsson 2011 <ca.ingmarsson@gmail.com>.
 *
 * 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., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 *
 * Author: Alexander Larsson <alexl@redhat.com>
 *         Carl-Anton Ingmarsson <ca.ingmarsson@gmail.com>
 */

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

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

typedef struct _GVfsUriMapperAfp GVfsUriMapperAfp;
typedef struct _GVfsUriMapperAfpClass GVfsUriMapperAfpClass;

struct _GVfsUriMapperAfp
{
  GVfsUriMapper parent;
};

struct _GVfsUriMapperAfpClass
{
  GVfsUriMapperClass parent_class;
};

GType g_vfs_uri_mapper_afp_get_type (void);
void g_vfs_uri_mapper_afp_register (GIOModule *module);

G_DEFINE_DYNAMIC_TYPE (GVfsUriMapperAfp, g_vfs_uri_mapper_afp, G_VFS_TYPE_URI_MAPPER)

static void
g_vfs_uri_mapper_afp_init (GVfsUriMapperAfp *vfs)
{
}

static const char * const *
afp_get_handled_schemes (GVfsUriMapper *mapper)
{
  static const char *schemes[] = {
    "afp",
    NULL
  };
  return schemes;
}

static GMountSpec *
afp_from_uri (GVfsUriMapper  *mapper,
              const char     *uri_str,
              char          **path)
{
  const char *p;
  GDecodedUri *uri;
  GMountSpec *spec;

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

  if (uri->host == NULL || strlen (uri->host) == 0)
  {
    g_vfs_decoded_uri_free (uri);
    return NULL;
  }
  else
  {
    /* host set */
    p = uri->path;
    while (p && *p == '/')
      p++;

    if (p == NULL || *p == 0)
    {
      /* uri form: afp://$host/ */
       spec = g_mount_spec_new ("afp-server");

       g_mount_spec_set (spec, "host", uri->host);
       *path = g_strdup ("/");
    }
    else
    {
      const char *volume, *volume_end;

      volume = p;
      volume_end = strchr (volume, '/');
      if (volume_end == NULL)
        volume_end = volume + strlen (volume);

      p = volume_end;

      while (*p == '/')
        p++;

      if (*p == 0)
      {
        /* uri form: afp://$host/$volume/
         * Here we special case afp-server files by adding "._" to the names in the uri */
        if (volume[0] == '.' && volume[1] == '_')
        {
          char *tmp;

          spec = g_mount_spec_new ("afp-server");
          g_mount_spec_set (spec, "host", uri->host);

          tmp = g_strndup (volume + 2, volume_end - (volume + 2));
          *path = g_strconcat ("/", tmp, NULL);
          g_free (tmp);
        }
        else
        {
          char *tmp;

          spec = g_mount_spec_new ("afp-volume");
          g_mount_spec_set (spec, "host", uri->host);

          tmp = g_strndup (volume, volume_end - volume);
          g_mount_spec_take (spec, "volume", tmp);

          *path = g_strdup ("/");
        }
      }
      else
      {
        char *tmp;

        spec = g_mount_spec_new ("afp-volume");
        g_mount_spec_set (spec, "host", uri->host);

        tmp = g_strndup (volume, volume_end - volume);
        g_mount_spec_take (spec, "volume", tmp);

        *path = g_strconcat ("/", p, NULL);
      }
    }
  }

  if (uri->userinfo)
    g_mount_spec_set (spec, "user", uri->userinfo);

  g_vfs_decoded_uri_free (uri);

  return spec;
}

static const char * const *
afp_get_handled_mount_types (GVfsUriMapper *mapper)
{
  static const char *types[] = {
    "afp-server",
    "afp-volume",
    NULL
  };
  return types;
}

static char *
afp_to_uri (GVfsUriMapper *mapper,
            GMountSpec *spec,
            const char *path,
            gboolean allow_utf8)
{
  const char *type;
  const char *host;
  const char *port;
  const char *user;
  char *s;
  GDecodedUri *uri;

  uri = g_new0 (GDecodedUri, 1);

  type = g_mount_spec_get (spec, "type");

  uri->scheme = g_strdup ("afp");

  host = g_mount_spec_get (spec, "host");
  uri->host = g_strdup (host);

  port = g_mount_spec_get (spec, "port");
  if (port)
    uri->port = atoi (port);
  else
    uri->port = -1;

  user = g_mount_spec_get (spec, "user");
  uri->userinfo = g_strdup (user);

  if (strcmp (type, "afp-server") == 0)
  {
    /* Map the mountables in server to ._share because the actual share mount maps to afp://host/share */
     if (path && path[0] == '/' && path[1] != 0)
     uri->path = g_strconcat ("/._", path + 1, NULL);
     else
     uri->path = g_strdup ("/");
  }
  else if (strcmp (type, "afp-volume") == 0)
  {
    const char *volume;

    volume = g_mount_spec_get (spec, "volume");
    if (path[0] == '/')
      uri->path = g_strconcat ("/", volume, path, NULL);
    else
      uri->path = g_strconcat ("/", volume, "/", path, NULL);
  }

  s = g_vfs_encode_uri (uri, allow_utf8);
  g_vfs_decoded_uri_free (uri);
  return s;
}

static const char *
afp_to_uri_scheme (GVfsUriMapper *mapper,
                   GMountSpec *spec)
{
  const gchar *type = g_mount_spec_get (spec, "type");

  if (strcmp ("afp-server", type) == 0 ||
      strcmp ("afp-volume", type) == 0)
    return "afp";
  else
    return NULL;
}

static void
g_vfs_uri_mapper_afp_class_finalize (GVfsUriMapperAfpClass *klass)
{
}

static void
g_vfs_uri_mapper_afp_class_init (GVfsUriMapperAfpClass *class)
{
  GVfsUriMapperClass *mapper_class;

  mapper_class = G_VFS_URI_MAPPER_CLASS (class);
  mapper_class->get_handled_schemes = afp_get_handled_schemes;
  mapper_class->from_uri = afp_from_uri;
  mapper_class->get_handled_mount_types = afp_get_handled_mount_types;
  mapper_class->to_uri = afp_to_uri;
  mapper_class->to_uri_scheme = afp_to_uri_scheme;
}

void
g_vfs_uri_mapper_afp_register (GIOModule *module)
{
  g_vfs_uri_mapper_afp_register_type (G_TYPE_MODULE (module));
}