~ubuntu-branches/ubuntu/vivid/grass/vivid-proposed

« back to all changes in this revision

Viewing changes to lib/segment/format.c

  • Committer: Package Import Robot
  • Author(s): Bas Couwenberg
  • Date: 2015-02-20 23:12:08 UTC
  • mfrom: (8.2.6 experimental)
  • Revision ID: package-import@ubuntu.com-20150220231208-1u6qvqm84v430b10
Tags: 7.0.0-1~exp1
* New upstream release.
* Update python-ctypes-ternary.patch to use if/else instead of and/or.
* Drop check4dev patch, rely on upstream check.
* Add build dependency on libpq-dev to grass-dev for libpq-fe.h.
* Drop patches applied upstream, refresh remaining patches.
* Update symlinks for images switched from jpg to png.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
 
2
2
/**
3
 
 * \file format.c
 
3
 * \file lib/segment/format.c
4
4
 *
5
5
 * \brief Segment formatting routines.
6
6
 *
9
9
 *
10
10
 * \author GRASS GIS Development Team
11
11
 *
12
 
 * \date 2005-2006
 
12
 * \date 2005-2009
13
13
 */
14
14
 
15
 
#include <grass/config.h>
16
15
#include <stdio.h>
17
16
#include <string.h>
18
17
#include <errno.h>
19
18
#include <unistd.h>
20
19
#include <limits.h>
21
 
#include <grass/segment.h>
22
 
 
23
 
 
24
 
static int _segment_format(int, int, int, int, int, int, int);
 
20
#include <grass/gis.h>
 
21
#include <grass/glocale.h>
 
22
#include "local_proto.h"
 
23
 
 
24
 
 
25
static int seg_format(int, off_t, off_t, int, int, int, int);
25
26
static int write_int(int, int);
 
27
static int write_off_t(int, off_t);
26
28
static int zero_fill(int, off_t);
27
29
 
28
30
/* fd must be open for write */
29
31
 
30
32
 
31
33
/**
32
 
 * \fn int segment_format (int fd, int nrows, int ncols, int srows, int scols, int len)
33
 
 *
34
34
 * \brief Format a segment file.
35
35
 *
36
36
 * The segmentation routines require a disk file to be used for paging 
59
59
 * \return -3 if illegal parameters are passed
60
60
 */
61
61
 
62
 
int segment_format(int fd, int nrows, int ncols, int srows, int scols,
 
62
int Segment_format(int fd, off_t nrows, off_t ncols, int srows, int scols,
63
63
                   int len)
64
64
{
65
 
    return _segment_format(fd, nrows, ncols, srows, scols, len, 1);
 
65
    return seg_format(fd, nrows, ncols, srows, scols, len, 1);
66
66
}
67
67
 
68
68
/**
69
 
 * \fn int segment_format_nofill (int fd, int nrows, int ncols, int srows, int scols, int len)
70
 
 *
71
69
 * \brief Format a segment file.
72
70
 *
73
71
 * The segmentation routines require a disk file to be used for paging 
99
97
 * \return -3 if illegal parameters are passed
100
98
 */
101
99
 
102
 
int segment_format_nofill(int fd, int nrows, int ncols, int srows, int scols,
 
100
int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols,
103
101
                          int len)
104
102
{
105
 
    return _segment_format(fd, nrows, ncols, srows, scols, len, 0);
 
103
    return seg_format(fd, nrows, ncols, srows, scols, len, 0);
106
104
}
107
105
 
108
106
 
109
 
static int _segment_format(int fd,
110
 
                           int nrows, int ncols,
 
107
static int seg_format(int fd,
 
108
                           off_t nrows, off_t ncols,
111
109
                           int srows, int scols, int len, int fill)
112
110
{
113
111
    off_t nbytes;
114
112
    int spr, size;
115
113
 
116
114
    if (nrows <= 0 || ncols <= 0 || len <= 0 || srows <= 0 || scols <= 0) {
117
 
        G_warning("segment_format(fd,%d,%d,%d,%d,%d): illegal value(s)",
 
115
        G_warning("Segment_format(fd,%lld,%lld,%d,%d,%d): illegal value(s)",
118
116
                  nrows, ncols, srows, scols, len);
119
117
        return -3;
120
118
    }
121
119
 
 
120
    spr = ncols / scols;
 
121
    if (ncols % scols)
 
122
        spr++;
 
123
 
 
124
    size = srows * scols * len;
 
125
 
 
126
    if (sizeof(off_t) == 4 && sizeof(double) >= 8) {
 
127
        double d_size;
 
128
        off_t o_size;
 
129
 
 
130
        /* calculate total number of segments */
 
131
        d_size = (double) spr * ((nrows + srows - 1) / srows);
 
132
        /* multiply with segment size */
 
133
        d_size *= size;
 
134
 
 
135
        /* add header */
 
136
        d_size += 2 * sizeof(off_t) + 3 * sizeof(int);
 
137
 
 
138
        o_size = (off_t) d_size;
 
139
 
 
140
        /* this test assumes that all off_t values can be exactly 
 
141
         * represented as double if sizeof(off_t) = 4 and sizeof(double) >= 8 */
 
142
        if ((double) o_size != d_size) {
 
143
            G_warning(_("Segment format: file size too large"));
 
144
            G_warning(_("Please recompile with Large File Support (LFS)"));
 
145
            return -1;
 
146
        }
 
147
    }
 
148
 
122
149
    if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {
123
 
        G_warning("segment_format(): Unable to seek (%s)", strerror(errno));
 
150
        int err = errno;
 
151
 
 
152
        G_warning("Segment_format(): Unable to seek (%s)", strerror(err));
124
153
        return -1;
125
154
    }
126
155
 
127
 
    if (!write_int(fd, nrows) || !write_int(fd, ncols)
 
156
    if (!write_off_t(fd, nrows) || !write_off_t(fd, ncols)
128
157
        || !write_int(fd, srows) || !write_int(fd, scols)
129
158
        || !write_int(fd, len))
130
159
        return -1;
132
161
    if (!fill)
133
162
        return 1;
134
163
 
135
 
    spr = ncols / scols;
136
 
    if (ncols % scols)
137
 
        spr++;
138
 
 
139
 
    size = srows * scols * len;
140
 
 
141
164
    /* calculate total number of segments */
142
165
    nbytes = spr * ((nrows + srows - 1) / srows);
143
166
    nbytes *= size;
157
180
 
158
181
static int write_int(int fd, int n)
159
182
{
 
183
    errno = 0;
160
184
    if (write(fd, &n, sizeof(int)) != sizeof(int)) {
161
 
        G_warning("segment_format(): Unable to write (%s)", strerror(errno));
162
 
        return 0;
163
 
    }
164
 
 
165
 
    return 1;
166
 
}
167
 
 
 
185
        int err = errno;
 
186
 
 
187
        if (err)
 
188
            G_warning("Segment format: Unable to write (%s)", strerror(err));
 
189
        else
 
190
            G_warning("Segment format: Unable to write (insufficient disk space?)");
 
191
        return 0;
 
192
    }
 
193
 
 
194
    return 1;
 
195
}
 
196
 
 
197
static int write_off_t(int fd, off_t n)
 
198
{
 
199
    errno = 0;
 
200
    if (write(fd, &n, sizeof(off_t)) != sizeof(off_t)) {
 
201
        int err = errno;
 
202
 
 
203
        if (err)
 
204
            G_warning("Segment format: Unable to write (%s)", strerror(err));
 
205
        else
 
206
            G_warning("Segment format: Unable to write (insufficient disk space?)");
 
207
        return 0;
 
208
    }
 
209
 
 
210
    return 1;
 
211
}
168
212
 
169
213
static int zero_fill(int fd, off_t nbytes)
170
214
{
171
215
#ifndef USE_LSEEK
172
 
    char buf[10240];
 
216
    char buf[16384];
173
217
    register char *b;
174
218
    register int n;
175
219
 
181
225
 
182
226
    while (nbytes > 0) {
183
227
        n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
 
228
        errno = 0;
184
229
        if (write(fd, buf, n) != n) {
185
 
            G_warning("segment zero_fill(): Unable to write (%s)", strerror(errno));
 
230
            int err = errno;
 
231
 
 
232
            if (err)
 
233
                G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
 
234
            else
 
235
                G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
186
236
            return -1;
187
237
        }
188
238
        nbytes -= n;
199
249
    static const char buf[10];
200
250
 
201
251
    G_debug(3, "Using new segmentation code...");
 
252
    errno = 0;
202
253
    if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
203
 
        G_warning("segment zero_fill(): Unable to seek (%s)", strerror(errno));
 
254
        int err = errno;
 
255
 
 
256
        G_warning("segment zero_fill(): Unable to seek (%s)", strerror(err));
204
257
        return -1;
205
258
    }
 
259
    errno = 0;
206
260
    if (write(fd, buf, 1) != 1) {
207
 
        G_warning("segment zero_fill(): Unable to write (%s)", strerror(errno));
 
261
        int err = errno;
 
262
 
 
263
        if (err)
 
264
            G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
 
265
        else
 
266
            G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
208
267
        return -1;
209
268
    }
210
269