File: slim.c

package info (click to toggle)
libgetdata 0.10.0-5%2Bdeb10u1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 16,260 kB
  • sloc: ansic: 99,855; cpp: 4,820; fortran: 4,524; sh: 4,178; f90: 2,543; python: 2,378; perl: 1,893; php: 1,447; makefile: 1,418
file content (231 lines) | stat: -rw-r--r-- 5,442 bytes parent folder | download | duplicates (6)
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
/* Copyright (C) 2008, 2010-2015 D. V. Wiebe
 *
 ***************************************************************************
 *
 * This file is part of the GetData project.
 *
 * GetData 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.1 of the License, or (at your
 * option) any later version.
 *
 * GetData 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 GetData; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
#ifdef ZZSLIM
#define GD_SLIM(x) _GD_Zzslim ## x
#else
#define GD_SLIM(x) _GD_Slim ## x
#include "internal.h"
#endif

#ifdef HAVE_SLIMLIB_H
#include <slimlib.h>
#endif

struct gd_slimdata {
  SLIMFILE *f;
  char *filepath;
};

static struct gd_slimdata *GD_SLIM(DoOpen)(int dirfd, struct gd_raw_file_* file)
{
  struct gd_slimdata *gdsl;

  dtrace("%i, %p", dirfd, file);

  /* slimdopen in slimlib-2.6.7 and earlier contains a bug resulting in a SEGV,
   * so disable this for now */
#if 0
  /* #ifdef HAVE_SLIMDOPEN */
  {
    int fd = gd_OpenAt(file->D, dirfd, file->name, O_RDONLY | O_BINARY, 0666);
    if (fd < 0) {
      dreturn("%i", -1);
      return -1;
    }

    file->edata = slimdopen(fd, "r");

    if (file->edata == NULL) {
      close(fd);
      dreturn("%i", 1);
      return 1;
    }
  }
#else
  {
    gdsl = malloc(sizeof *gdsl);
    if (gdsl == NULL) {
      file->error = ENOMEM;
      dreturn("%p", NULL);
      return NULL;
    }

    /* this is easily broken, but the best we can do in this case */
    gdsl->filepath = gd_MakeFullPathOnly(file->D, dirfd, file->name);
    if (gdsl->filepath == NULL) {
      file->error = errno;
      free(gdsl);
      dreturn("%p", NULL);
      return NULL;
    }

    gdsl->f = slimopen(gdsl->filepath, "r");
  }
#endif

  if (gdsl->f == NULL) {
    free(gdsl->filepath);
    free(gdsl);
    dreturn("%p", NULL);
    return NULL;
  }

  dreturn("%p", gdsl);
  return gdsl;
}

int GD_SLIM(Open)(int dirfd, struct gd_raw_file_* file,
    gd_type_t data_type gd_unused_, int swap gd_unused_,
    unsigned int mode gd_unused_)
{
  dtrace("%i, %p, <unused>, <unused>, <unused>", dirfd, file);

  file->edata = GD_SLIM(DoOpen)(dirfd, file);

  if (file->edata == NULL) {
    dreturn("%i", 1);
    return 1;
  }

  file->mode = GD_RDONLY;
  file->idata = 0;
  dreturn("%i", 0);
  return 0;
}

off64_t GD_SLIM(Seek)(struct gd_raw_file_* file, off64_t count,
    gd_type_t data_type, unsigned int mode gd_unused_)
{
  struct gd_slimdata *gdsl = file->edata;

  dtrace("%p, %" PRId64 ", 0x%X, <unused>", file, (int64_t)count, data_type);

  /* slimlib appears to do a rewind before every SEEK_SET ! */
  if (slimseek(gdsl->f, (long)count * GD_SIZE(data_type), SEEK_SET))
  {
    /* Handle seeks past the EOF */
    off64_t size = slimrawsize(gdsl->filepath) / GD_SIZE(data_type);
    if (count < size || slimseek(gdsl->f, size, SEEK_SET)) {
      dreturn("%i", -1);
      return -1;
    }
    count = size;
  }

  file->pos = count;
  dreturn("%" PRId64, (int64_t)count);
  return count;
}

ssize_t GD_SLIM(Read)(struct gd_raw_file_ *restrict file, void *restrict ptr,
    gd_type_t data_type, size_t nmemb)
{
  ssize_t n;
  struct gd_slimdata *gdsl = file->edata;

  dtrace("%p, %p, 0x%X, %" PRIuSIZE, file, ptr, data_type, nmemb);

  n = slimread(ptr, GD_SIZE(data_type), nmemb, gdsl->f);

  dreturn("%" PRIuSIZE, n);
  return n;
}

int GD_SLIM(Close)(struct gd_raw_file_ *file)
{
  int ret;
  struct gd_slimdata *gdsl = file->edata;

  dtrace("%p", file);

  ret = slimclose(gdsl->f);
  if (!ret) {
    file->idata = -1;
    free(gdsl->filepath);
    free(file->edata);
    file->edata = NULL;
    file->mode = 0;
  }

  dreturn("%i", ret);
  return ret;
}

off64_t GD_SLIM(Size)(int dirfd, struct gd_raw_file_ *file, gd_type_t data_type,
    int swap gd_unused_)
{
  off64_t size;

  dtrace("%i, %p, 0x%X", dirfd, file, data_type);

  /* slimdrawsize in slimlib-2.6.7 and earlier contains a bug resulting in a
   * SEGV, so disable this for now */
#if 0
  /* #ifdef HAVE_SLIMDRAWSIZE */
  {
    int fd = gd_OpenAt(file->D, dirfd, file->name, O_RDONLY | O_BINARY, 0666);
    if (fd < 0) {
      dreturn("%i", -1);
      return -1;
    }

    size = slimdrawsize(fd);
    close(fd);
  }
#else
  {
    /* this is easily broken, but the best we can do in this case */
    char *filepath = gd_MakeFullPathOnly(file->D, dirfd, file->name);
    if (filepath == NULL) {
      dreturn("%i", -1);
      return -1;
    }

    size = slimrawsize(filepath);
    free(filepath);
  }
#endif

  if (size < 0) {
    dreturn("%i", -1);
    return -1;
  }

  size /= GD_SIZE(data_type);

  dreturn("%" PRId64, (int64_t)size);
  return size;
}

int GD_SLIM(Strerr)(const struct gd_raw_file_ *file, char *buf, size_t buflen)
{
  int r = 0;

  dtrace("%p, %p, %" PRIuSIZE, file, buf, buflen);

  if (file->error)
    r = gd_StrError(file->error, buf, buflen);
  else /* the slimlib C API has no error reporting */
    strncpy(buf, "SLIMLIB: Unspecified error", buflen);

  dreturn("%i", r);
  return r;
}