10
10
* \author GRASS GIS Development Team
15
#include <grass/config.h>
17
16
#include <string.h>
19
18
#include <unistd.h>
20
19
#include <limits.h>
21
#include <grass/segment.h>
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"
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);
28
30
/* fd must be open for write */
32
* \fn int segment_format (int fd, int nrows, int ncols, int srows, int scols, int len)
34
34
* \brief Format a segment file.
36
36
* The segmentation routines require a disk file to be used for paging
59
59
* \return -3 if illegal parameters are passed
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,
65
return _segment_format(fd, nrows, ncols, srows, scols, len, 1);
65
return seg_format(fd, nrows, ncols, srows, scols, len, 1);
69
* \fn int segment_format_nofill (int fd, int nrows, int ncols, int srows, int scols, int len)
71
69
* \brief Format a segment file.
73
71
* The segmentation routines require a disk file to be used for paging
99
97
* \return -3 if illegal parameters are passed
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,
105
return _segment_format(fd, nrows, ncols, srows, scols, len, 0);
103
return seg_format(fd, nrows, ncols, srows, scols, len, 0);
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)
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);
124
size = srows * scols * len;
126
if (sizeof(off_t) == 4 && sizeof(double) >= 8) {
130
/* calculate total number of segments */
131
d_size = (double) spr * ((nrows + srows - 1) / srows);
132
/* multiply with segment size */
136
d_size += 2 * sizeof(off_t) + 3 * sizeof(int);
138
o_size = (off_t) d_size;
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)"));
122
149
if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {
123
G_warning("segment_format(): Unable to seek (%s)", strerror(errno));
152
G_warning("Segment_format(): Unable to seek (%s)", strerror(err));
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))
158
181
static int write_int(int fd, int n)
160
184
if (write(fd, &n, sizeof(int)) != sizeof(int)) {
161
G_warning("segment_format(): Unable to write (%s)", strerror(errno));
188
G_warning("Segment format: Unable to write (%s)", strerror(err));
190
G_warning("Segment format: Unable to write (insufficient disk space?)");
197
static int write_off_t(int fd, off_t n)
200
if (write(fd, &n, sizeof(off_t)) != sizeof(off_t)) {
204
G_warning("Segment format: Unable to write (%s)", strerror(err));
206
G_warning("Segment format: Unable to write (insufficient disk space?)");
169
213
static int zero_fill(int fd, off_t nbytes)
171
215
#ifndef USE_LSEEK
173
217
register char *b;
199
249
static const char buf[10];
201
251
G_debug(3, "Using new segmentation code...");
202
253
if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
203
G_warning("segment zero_fill(): Unable to seek (%s)", strerror(errno));
256
G_warning("segment zero_fill(): Unable to seek (%s)", strerror(err));
206
260
if (write(fd, buf, 1) != 1) {
207
G_warning("segment zero_fill(): Unable to write (%s)", strerror(errno));
264
G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
266
G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");