aboutsummaryrefslogtreecommitdiff
path: root/src/storage/get.c
blob: 92e050f3988ba359c2647ee0b4f89c8564340db1 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/* -*- Mode: C; indent-tabs-mode: nil; c-basic-offset: 2; tab-width: 2 -*- */
/* vim: set expandtab ts=2 sw=2: */
/*
 * get.c
 * Copyright (C) David Barksdale 2011 <amatus.amongus@gmail.com>
 * 
 * foofs 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 3 of the License, or
 * (at your option) any later version.
 * 
 * foofs 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, see <http://www.gnu.org/licenses/>.
 */
#include <errno.h>
#include <fcntl.h>
#include <microhttpd.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include "storage.h"

struct c_block_list {
  struct c_block_list *next;
  char *path;
  char *c_index;
  off_t size;
  int skip;
};

static struct c_block_list *free_c_block_list_head(struct c_block_list *block)
{
  struct c_block_list *next = block->next;
  
  if( NULL != block->path ) {
    free(block->path);
  }
  if( NULL != block->c_index ) {
    free(block->c_index);
  }
  free(block);
  return next;
}

struct read_blocks_state {
  char *e_hnk;
  char *c_index;
  char *path;
  int fd;
  struct c_block_list *blocks;
};

static void free_read_blocks_state(void *cls)
{
  struct read_blocks_state *s = cls;

  if( NULL != s->e_hnk ) {
    free(s->e_hnk);
  }
  if( NULL != s->c_index ) {
    free(s->c_index);
  }
  if( NULL != s->path ) {
    free(s->path);
  }
  if( -1 != s->fd ) {
    close(s->fd);
  }
  while( NULL != s->blocks ) {
    s->blocks = free_c_block_list_head(s->blocks);
  }
  free(s);
}

static ssize_t read_blocks(void *cls, uint64_t pos, char *buf, size_t max)
{
  struct read_blocks_state *s = cls;
  ssize_t len;

again:
  if( -1 != s->fd ) {
    len = read(s->fd, buf, max);
    if( -1 == len ) {
      if( EINTR == errno ) {
        goto again;
      }
      return MHD_CONTENT_READER_END_OF_STREAM;
    }
    if( len > 0 ) {
      // TODO: don't return just yet if we have more buffer we can fill
      return len;
    }
    close(s->fd);
    // TODO: handle EINTR
    s->fd = -1;
  }
  // open the next file
  if( NULL != s->blocks ) {
    s->fd = open(s->blocks->path, O_RDONLY);
    if( -1 == s->fd ) {
      return MHD_CONTENT_READER_END_OF_STREAM;
    }
    s->blocks = free_c_block_list_head(s->blocks);
    goto again;
  }
  return MHD_CONTENT_READER_END_OF_STREAM;
}

static int get_arg_visitor(void *cls, enum MHD_ValueKind kind, const char *key,
 const char *value)
{
  struct c_block_list *block = cls;

  if(0 != strcmp(key, "skip")) {
    return MHD_YES;
  }
  if(0 != strcmp(value, block->c_index)) {
    return MHD_YES;
  }
  block->skip = 1;
  return MHD_NO;
}

static int foreach_c_block(void *cls, const char *name)
{
  struct read_blocks_state *s = cls;
  struct stat st;
  struct c_block_list *block;
  char *path;
  int ret;

  if( '.' == name[0] ) {
    return 0;
  }
  path = strprintf("%s/%s", s->path, name);
  if( NULL == path ) {
    return 1;
  }
  ret = stat(path, &st);
  if( -1 == ret || !S_ISREG(st.st_mode)) {
    free(path);
    return 0;
  }
  block = malloc(sizeof(*block));
  if( NULL == block ) {
    free(path);
    return 1;
  }
  block->next = s->blocks;
  block->path = path;
  block->c_index = strdup(name);
  block->size = st.st_size;
  block->skip = 0;
  s->blocks = block;
  return 0;
}

static int foreach_gen(void *cls, const char *name)
{
  struct read_blocks_state *s = cls;
  DIR *dir;

  if( '.' == name[0] ) {
    return 0;
  }
  s->path = strprintf("%s/%s/%c%c/%s", g_storage_root, name,
      s->e_hnk[0], s->e_hnk[1], &s->e_hnk[2]);
  if( NULL == s->path ) {
    return 1;
  }
  if( NULL != s->c_index ) {
    int ret;

    ret = foreach_c_block(s, s->c_index);
    free(s->path);
    s->path = NULL;
    return ret;
  }
  dir = opendir(s->path);
  if( NULL == dir ) {
    free(s->path);
    s->path = NULL;
    return 0;
  }
  dir_foreach(dir, foreach_c_block, s);
  free(s->path);
  s->path = NULL;
  return 0;
}

int handle_get(void *cls, struct MHD_Connection *connection,
 const char *url)
{
  struct MHD_Response *response;
  DIR *server_dir;
  struct read_blocks_state *s;
  struct c_block_list *block, **prev;
  char *e_hnk, *c_index;
  int ret;
  int status_code;
  int count;

  s = malloc(sizeof(*s));
  if( NULL == s ) {
    status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
    goto err;
  }
  memset(s, 0, sizeof(*s));
  s->fd = -1;
  url = copy_path_elem(url, &s->e_hnk);
  if( NULL == s->e_hnk || strlen(s->e_hnk) < 3 || '.' == s->e_hnk[0]
      || '.' == s->e_hnk[2]) {
    free_read_blocks_state(s);
    status_code = MHD_HTTP_BAD_REQUEST;
    goto err;
  }
  if( 0 != url[0] ) {
    copy_path_elem(url, &s->c_index);
    if( NULL == s->c_index || 0 == s->c_index[0] || '.' == s->c_index[0] ) {
      free_read_blocks_state(s);
      status_code = MHD_HTTP_BAD_REQUEST;
      goto err;
    }
  }
  server_dir = opendir(g_storage_root);
  if( NULL == server_dir ) {
    free_read_blocks_state(s);
    status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
    goto err;
  }
  ret = dir_foreach(server_dir, foreach_gen, s);
  closedir(server_dir);
  if( ret < 1 ) {
    free_read_blocks_state(s);
    status_code = MHD_HTTP_INTERNAL_SERVER_ERROR;
    goto err;
  }
  response = MHD_create_response_from_callback(
    /*       size */ MHD_SIZE_UNKNOWN, 
    /* block_size */ 64 * 1024,
    /*        crc */ &read_blocks,
    /*    crc_cls */ s,
    /*       crfc */ &free_read_blocks_state);
  prev = &s->blocks;
  block = *prev;
  count = 0;
  while( NULL != block ) {
    char *header, *content;

    MHD_get_connection_values(connection, MHD_GET_ARGUMENT_KIND,
      get_arg_visitor, block);
    if( block->skip ) {
      *prev = free_c_block_list_head(block);
      block = *prev;
      continue;
    }
    header = strprintf("X-Block-%u", count