readdir was rewritten in Ruby just before the end of the hackfest
[hurd/trovefs.git] / fs.c
1 /*
2  * Copyright (C) 1997, 2001, 2003 Free Software Foundation, Inc.
3  * Copyright (C) 2013  Steven McDonald <steven@steven-mcdonald.id.au>
4  *
5  * ftpfs translator written by Miles Bader <miles@gnu.org>
6  * Modified for trovefs by Steven McDonald <steven@steven-mcdonald.id.au>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2, or (at your option)
11  * any later version.
12  *
13  * This software is distributed in the hope that it will be useful, but
14  * WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
21  *
22  */
23
24 #include <stddef.h>
25 #include <hurd/netfs.h>
26
27 #include "trovefs.h"
28
29 error_t
30 trovefs_create (char *hostname, char *bucket, char *access_key_id, char *secret_access_key, int fsid, struct trovefs **fs)
31 {
32         error_t err;
33         S3Status s3err;
34         struct trovefs_dir *super_root_dir;
35         struct node *super_root;
36         struct trovefs *new = malloc (sizeof (struct trovefs));
37
38         if (! new)
39                 return ENOMEM;
40
41         new->fsid = fsid;
42         new->next_inode = 2;
43
44         s3err = S3_initialize (TROVEFS_USER_AGENT, S3_INIT_ALL, hostname);
45         switch (s3err)
46         {
47                 case S3StatusOK:
48                         break;
49                 case S3StatusUriTooLong:
50                         return EINVAL;
51                 case S3StatusInternalError:
52                         return EGRATUITOUS;
53                 case S3StatusOutOfMemory:
54                         return ENOMEM;
55                 default:
56                         return EGRATUITOUS;
57         }
58
59         new->s3_ctx = malloc (sizeof (S3BucketContext));
60         new->s3_ctx->hostName = hostname;
61         new->s3_ctx->bucketName = bucket;
62         new->s3_ctx->protocol = S3ProtocolHTTPS;
63         new->s3_ctx->uriStyle = S3UriStyleVirtualHost;
64         new->s3_ctx->accessKeyId = access_key_id;
65         new->s3_ctx->secretAccessKey = secret_access_key;
66
67         hurd_ihash_init (&new->inode_mappings, offsetof (struct trovefs_dir_entry, inode_locp));
68         pthread_spin_init (&new->inode_mappings_lock, PTHREAD_PROCESS_PRIVATE);
69
70         super_root = netfs_make_node (0);
71         if (! super_root)
72                 err = ENOMEM;
73         else {
74                 err = trovefs_dir_create (new, super_root, "/", &super_root_dir);
75                 if (! err)
76                         err = trovefs_dir_null_lookup (super_root_dir, &new->root);
77         }
78
79         if (err) {
80                 hurd_ihash_destroy (&new->inode_mappings);
81                 free (new);
82         }
83         else
84                 *fs = new;
85
86         return err;
87 }