Statistics
| Branch: | Revision:

grapes / src / Chunkiser / input-stream-dumb.c @ 04dd3295

History | View | Annotate | Download (2.1 KB)

1 fa87ad46 Luca Abeni
/*
2
 *  Copyright (c) 2010 Luca Abeni
3
 *
4
 *  This is free software; see gpl-3.0.txt
5
 */
6
7
#include <sys/types.h>
8
#include <sys/stat.h>
9
#include <fcntl.h>
10
#include <unistd.h>
11 4f4dbedb Luca Abeni
#include <errno.h>
12 fa87ad46 Luca Abeni
#include <stdint.h>
13
#include <stdlib.h>
14 04dd3295 Luca Abeni
#include <stdio.h>
15 fa87ad46 Luca Abeni
#include <string.h>
16
17
#include "chunkiser_iface.h"
18
#include "config.h"
19
20
struct chunkiser_ctx {
21 53abf41e Luca Abeni
  int loop;        //loop on input file infinitely
22 fa87ad46 Luca Abeni
  int chunk_size;
23 4cdd16a7 Luca Abeni
  int fds[2];
24 fa87ad46 Luca Abeni
};
25
#define DEFAULT_CHUNK_SIZE 2 * 1024
26
27
static struct chunkiser_ctx *dumb_open(const char *fname, int *period, const char *config)
28
{
29
  struct tag *cfg_tags;
30
  struct chunkiser_ctx *res;
31
32
  res = malloc(sizeof(struct chunkiser_ctx));
33
  if (res == NULL) {
34
    return NULL;
35
  }
36
37 53abf41e Luca Abeni
  res->loop = 0;
38 4cdd16a7 Luca Abeni
  res->fds[0] = open(fname, O_RDONLY);
39
  if (res->fds[0] < 0) {
40 fa87ad46 Luca Abeni
    free(res);
41
42
    return NULL;
43
  }
44 4cdd16a7 Luca Abeni
  res->fds[1] = -1;
45 fa87ad46 Luca Abeni
46
  *period = 0;
47
  res->chunk_size = DEFAULT_CHUNK_SIZE;
48
  cfg_tags = config_parse(config);
49
  if (cfg_tags) {
50
    const char *access_mode;
51
52 53abf41e Luca Abeni
    config_value_int(cfg_tags, "loop", &res->loop);
53 fa87ad46 Luca Abeni
    config_value_int(cfg_tags, "chunk_size", &res->chunk_size);
54
    access_mode = config_value_str(cfg_tags, "mode");
55 71ce3f9c Luca Abeni
    if (access_mode && !strcmp(access_mode, "nonblock")) {
56 448fba6a Csaba Kiraly
#ifndef _WIN32
57 4cdd16a7 Luca Abeni
      fcntl(res->fds[0], F_SETFL, O_NONBLOCK);
58 448fba6a Csaba Kiraly
#else
59 2146c711 Luca Abeni
      fprintf(stderr, "nonblock is not implemented yet\n");
60 448fba6a Csaba Kiraly
#endif
61 fa87ad46 Luca Abeni
    }
62
  }
63
  free(cfg_tags);
64
65
  return res;
66
}
67
68
static void dumb_close(struct chunkiser_ctx *s)
69
{
70 4cdd16a7 Luca Abeni
  close(s->fds[0]);
71 fa87ad46 Luca Abeni
  free(s);
72
}
73
74
static uint8_t *dumb_chunkise(struct chunkiser_ctx *s, int id, int *size, uint64_t *ts)
75
{
76
  uint8_t *res;
77
78
  res = malloc(s->chunk_size);
79
  if (res == NULL) {
80
    *size = -1;
81
82
    return NULL;
83
  }
84
  *ts = 0;
85 4cdd16a7 Luca Abeni
  *size = read(s->fds[0], res, s->chunk_size);
86 4f4dbedb Luca Abeni
  if ((*size == 0) && (errno != EAGAIN)) {
87
    *size = -1;
88 53abf41e Luca Abeni
    if (s->loop) {
89
      if (lseek(s->fds[0], 0, SEEK_SET) == 0) {
90
        *size = 0;
91
      }
92
    }
93 4f4dbedb Luca Abeni
    free(res);
94
    res = NULL;
95
  }
96 fa87ad46 Luca Abeni
97
  return res;
98
}
99
100 4cdd16a7 Luca Abeni
const int *dumb_get_fds(const struct chunkiser_ctx *s)
101
{
102
  return s->fds;
103
}
104
105 fa87ad46 Luca Abeni
struct chunkiser_iface in_dumb = {
106
  .open = dumb_open,
107
  .close = dumb_close,
108
  .chunkise = dumb_chunkise,
109 4cdd16a7 Luca Abeni
  .get_fds = dumb_get_fds,
110 fa87ad46 Luca Abeni
};
111