GRASS GIS 7 Programmer's Manual  7.9.dev(2021)-e5379bbd7
segment/format.c
Go to the documentation of this file.
1 
2 /**
3  * \file lib/segment/format.c
4  *
5  * \brief Segment formatting routines.
6  *
7  * This program is free software under the GNU General Public License
8  * (>=v2). Read the file COPYING that comes with GRASS for details.
9  *
10  * \author GRASS GIS Development Team
11  *
12  * \date 2005-2018
13  */
14 
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 static int seek_only(int, off_t);
30 
31 /* fd must be open for write */
32 
33 
34 /**
35  * \brief Format a segment file.
36  *
37  * The segmentation routines require a disk file to be used for paging
38  * segments in and out of memory. This routine formats the file open for
39  * write on file descriptor <b>fd</b> for use as a segment file.
40  *
41  * A segment file must be formatted before it can be processed by other
42  * segment routines. The configuration parameters <b>nrows</b>,
43  * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
44  * to the beginning of the segment file which is then filled with zeros.
45  *
46  * The corresponding nonsegmented data matrix, which is to be
47  * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
48  * segment file is to be formed of segments which are <b>srows</b> by
49  * <b>scols</b>. The data items have length <b>len</b> bytes. For
50  * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
51  *
52  * \param[in] fd file descriptor
53  * \param[in] nrows number of non-segmented rows
54  * \param[in] ncols number of non-segmented columns
55  * \param[in] srows segment rows
56  * \param[in] scols segment columns
57  * \param[in] len length of data type
58  * \return 1 of successful
59  * \return -1 if unable to seek or write <b>fd</b>
60  * \return -3 if illegal parameters are passed
61  */
62 
63 int Segment_format(int fd, off_t nrows, off_t ncols, int srows, int scols,
64  int len)
65 {
66  return seg_format(fd, nrows, ncols, srows, scols, len, 1);
67 }
68 
69 /**
70  * \brief Format a segment file.
71  *
72  * The segmentation routines require a disk file to be used for paging
73  * segments in and out of memory. This routine formats the file open for
74  * write on file descriptor <b>fd</b> for use as a segment file.
75  *
76  * A segment file must be formatted before it can be processed by other
77  * segment routines. The configuration parameters <b>nrows</b>,
78  * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
79  * to the beginning of the segment file which is then filled with zeros.
80  *
81  * The corresponding nonsegmented data matrix, which is to be
82  * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
83  * segment file is to be formed of segments which are <b>srows</b> by
84  * <b>scols</b>. The data items have length <b>len</b> bytes. For
85  * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
86  *
87  * <b>Note:</b> This version of the function does <b>not</b> fill in the
88  * initialized data structures with zeros.
89  *
90  * \param[in] fd file descriptor
91  * \param[in] nrows number of non-segmented rows
92  * \param[in] ncols number of non-segmented columns
93  * \param[in] srows segment rows
94  * \param[in] scols segment columns
95  * \param[in] len length of data type
96  * \return 1 of successful
97  * \return -1 if unable to seek or write <b>fd</b>
98  * \return -3 if illegal parameters are passed
99  */
100 
101 int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols,
102  int len)
103 {
104  return seg_format(fd, nrows, ncols, srows, scols, len, 0);
105 }
106 
107 
108 static int seg_format(int fd, 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,%"PRI_OFF_T",%"PRI_OFF_T",%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  /* calculate total number of segments */
162  nbytes = spr * ((nrows + srows - 1) / srows);
163  nbytes *= size;
164 
165  if (!fill) {
166  /* only seek and write a zero byte to the end */
167  if (seek_only(fd, nbytes) < 0)
168  return -1;
169  return 1;
170  }
171 
172  /* fill segment file with zeros */
173  /* NOTE: this could be done faster using lseek() by seeking
174  * ahead nbytes and then writing a single byte of 0,
175  * provided lseek() on all version of UNIX will create a file
176  * with holes that read as zeros.
177  */
178  if (zero_fill(fd, nbytes) < 0)
179  return -1;
180 
181  return 1;
182 }
183 
184 
185 static int write_int(int fd, int n)
186 {
187  errno = 0;
188  if (write(fd, &n, sizeof(int)) != sizeof(int)) {
189  int err = errno;
190 
191  if (err)
192  G_warning("Segment format: Unable to write (%s)", strerror(err));
193  else
194  G_warning("Segment format: Unable to write (insufficient disk space?)");
195  return 0;
196  }
197 
198  return 1;
199 }
200 
201 static int write_off_t(int fd, off_t n)
202 {
203  errno = 0;
204  if (write(fd, &n, sizeof(off_t)) != sizeof(off_t)) {
205  int err = errno;
206 
207  if (err)
208  G_warning("Segment format: Unable to write (%s)", strerror(err));
209  else
210  G_warning("Segment format: Unable to write (insufficient disk space?)");
211  return 0;
212  }
213 
214  return 1;
215 }
216 
217 static int zero_fill(int fd, off_t nbytes)
218 {
219 #ifndef USE_LSEEK
220  char buf[16384];
221  register char *b;
222  register int n;
223 
224  /* zero buf */
225  n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
226  b = buf;
227  while (n-- > 0)
228  *b++ = 0;
229 
230  while (nbytes > 0) {
231  n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
232  errno = 0;
233  if (write(fd, buf, n) != n) {
234  int err = errno;
235 
236  if (err)
237  G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
238  else
239  G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
240  return -1;
241  }
242  nbytes -= n;
243  }
244  return 1;
245 #else
246  return seek_only(fd, nbytes);
247 #endif
248 }
249 
250 static int seek_only(int fd, off_t nbytes)
251 {
252  /* Using lseek (faster upon initialization).
253  NOTE: This version doesn't allocate disk storage for the file; storage will
254  be allocated dynamically as blocks are actually written. This could
255  result in seek_only() succeeding but a subsequent call to write() failing
256  with ENOSPC ("No space left on device").
257  */
258 
259  static const char buf[10];
260 
261  G_debug(3, "Using new segmentation code...");
262  errno = 0;
263  if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
264  int err = errno;
265 
266  G_warning("segment zero_fill(): Unable to seek (%s)", strerror(err));
267  return -1;
268  }
269  errno = 0;
270  if (write(fd, buf, 1) != 1) {
271  int err = errno;
272 
273  if (err)
274  G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
275  else
276  G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
277  return -1;
278  }
279 
280  return 1;
281 }
int Segment_format(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
int nbytes
Definition: R.h:73
int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
#define PRI_OFF_T
Definition: gis.h:69
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition: symbol/read.c:220
double b
Definition: r_raster.c:39
void G_warning(const char *,...) __attribute__((format(printf
#define _(str)
Definition: glocale.h:10
int G_debug(int, const char *,...) __attribute__((format(printf