GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
segment/format.c
Go to the documentation of this file.
1 
15 #include <stdio.h>
16 #include <string.h>
17 #include <errno.h>
18 #include <unistd.h>
19 #include <limits.h>
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);
26 static int write_int(int, int);
27 static int write_off_t(int, off_t);
28 static int zero_fill(int, off_t);
29 
30 /* fd must be open for write */
31 
32 
62 int Segment_format(int fd, off_t nrows, off_t ncols, int srows, int scols,
63  int len)
64 {
65  return seg_format(fd, nrows, ncols, srows, scols, len, 1);
66 }
67 
100 int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols,
101  int len)
102 {
103  return seg_format(fd, nrows, ncols, srows, scols, len, 0);
104 }
105 
106 
107 static int seg_format(int fd,
108  off_t nrows, off_t ncols,
109  int srows, int scols, int len, int fill)
110 {
111  off_t nbytes;
112  int spr, size;
113 
114  if (nrows <= 0 || ncols <= 0 || len <= 0 || srows <= 0 || scols <= 0) {
115  G_warning("Segment_format(fd,%lld,%lld,%d,%d,%d): illegal value(s)",
116  nrows, ncols, srows, scols, len);
117  return -3;
118  }
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 
149  if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {
150  int err = errno;
151 
152  G_warning("Segment_format(): Unable to seek (%s)", strerror(err));
153  return -1;
154  }
155 
156  if (!write_off_t(fd, nrows) || !write_off_t(fd, ncols)
157  || !write_int(fd, srows) || !write_int(fd, scols)
158  || !write_int(fd, len))
159  return -1;
160 
161  if (!fill)
162  return 1;
163 
164  /* calculate total number of segments */
165  nbytes = spr * ((nrows + srows - 1) / srows);
166  nbytes *= size;
167 
168  /* fill segment file with zeros */
169  /* NOTE: this could be done faster using lseek() by seeking
170  * ahead nbytes and then writing a single byte of 0,
171  * provided lseek() on all version of UNIX will create a file
172  * with holes that read as zeros.
173  */
174  if (zero_fill(fd, nbytes) < 0)
175  return -1;
176 
177  return 1;
178 }
179 
180 
181 static int write_int(int fd, int n)
182 {
183  errno = 0;
184  if (write(fd, &n, sizeof(int)) != sizeof(int)) {
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 }
212 
213 static int zero_fill(int fd, off_t nbytes)
214 {
215 #ifndef USE_LSEEK
216  char buf[16384];
217  register char *b;
218  register int n;
219 
220  /* zero buf */
221  n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
222  b = buf;
223  while (n-- > 0)
224  *b++ = 0;
225 
226  while (nbytes > 0) {
227  n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
228  errno = 0;
229  if (write(fd, buf, n) != n) {
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?)");
236  return -1;
237  }
238  nbytes -= n;
239  }
240  return 1;
241 #else
242  /* Using lseek (faster upon initialization).
243  NOTE: This version doesn't allocate disk storage for the file; storage will
244  be allocated dynamically as blocks are actually written. This could
245  result in zero_fill() succeeding but a subsequent call to write() failing
246  with ENOSPC ("No space left on device").
247  */
248 
249  static const char buf[10];
250 
251  G_debug(3, "Using new segmentation code...");
252  errno = 0;
253  if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
254  int err = errno;
255 
256  G_warning("segment zero_fill(): Unable to seek (%s)", strerror(err));
257  return -1;
258  }
259  errno = 0;
260  if (write(fd, buf, 1) != 1) {
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?)");
267  return -1;
268  }
269 
270  return 1;
271 #endif
272 }
int Segment_format(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition: symbol/read.c:220
double b
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:203