GRASS GIS 7 Programmer's Manual  7.5.svn(2018)-r72645
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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-2009
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,
109  off_t nrows, off_t ncols,
110  int srows, int scols, int len, int fill)
111 {
112  off_t nbytes;
113  int spr, size;
114 
115  if (nrows <= 0 || ncols <= 0 || len <= 0 || srows <= 0 || scols <= 0) {
116  G_warning("Segment_format(fd,%lld,%lld,%d,%d,%d): illegal value(s)",
117  nrows, ncols, srows, scols, len);
118  return -3;
119  }
120 
121  spr = ncols / scols;
122  if (ncols % scols)
123  spr++;
124 
125  size = srows * scols * len;
126 
127  if (sizeof(off_t) == 4 && sizeof(double) >= 8) {
128  double d_size;
129  off_t o_size;
130 
131  /* calculate total number of segments */
132  d_size = (double) spr * ((nrows + srows - 1) / srows);
133  /* multiply with segment size */
134  d_size *= size;
135 
136  /* add header */
137  d_size += 2 * sizeof(off_t) + 3 * sizeof(int);
138 
139  o_size = (off_t) d_size;
140 
141  /* this test assumes that all off_t values can be exactly
142  * represented as double if sizeof(off_t) = 4 and sizeof(double) >= 8 */
143  if ((double) o_size != d_size) {
144  G_warning(_("Segment format: file size too large"));
145  G_warning(_("Please recompile with Large File Support (LFS)"));
146  return -1;
147  }
148  }
149 
150  if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {
151  int err = errno;
152 
153  G_warning("Segment_format(): Unable to seek (%s)", strerror(err));
154  return -1;
155  }
156 
157  if (!write_off_t(fd, nrows) || !write_off_t(fd, ncols)
158  || !write_int(fd, srows) || !write_int(fd, scols)
159  || !write_int(fd, len))
160  return -1;
161 
162  /* calculate total number of segments */
163  nbytes = spr * ((nrows + srows - 1) / srows);
164  nbytes *= size;
165 
166  if (!fill) {
167  /* only seek and write a zero byte to the end */
168  if (seek_only(fd, nbytes) < 0)
169  return -1;
170  return 1;
171  }
172 
173  /* fill segment file with zeros */
174  /* NOTE: this could be done faster using lseek() by seeking
175  * ahead nbytes and then writing a single byte of 0,
176  * provided lseek() on all version of UNIX will create a file
177  * with holes that read as zeros.
178  */
179  if (zero_fill(fd, nbytes) < 0)
180  return -1;
181 
182  return 1;
183 }
184 
185 
186 static int write_int(int fd, int n)
187 {
188  errno = 0;
189  if (write(fd, &n, sizeof(int)) != sizeof(int)) {
190  int err = errno;
191 
192  if (err)
193  G_warning("Segment format: Unable to write (%s)", strerror(err));
194  else
195  G_warning("Segment format: Unable to write (insufficient disk space?)");
196  return 0;
197  }
198 
199  return 1;
200 }
201 
202 static int write_off_t(int fd, off_t n)
203 {
204  errno = 0;
205  if (write(fd, &n, sizeof(off_t)) != sizeof(off_t)) {
206  int err = errno;
207 
208  if (err)
209  G_warning("Segment format: Unable to write (%s)", strerror(err));
210  else
211  G_warning("Segment format: Unable to write (insufficient disk space?)");
212  return 0;
213  }
214 
215  return 1;
216 }
217 
218 static int zero_fill(int fd, off_t nbytes)
219 {
220 #ifndef USE_LSEEK
221  char buf[16384];
222  register char *b;
223  register int n;
224 
225  /* zero buf */
226  n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
227  b = buf;
228  while (n-- > 0)
229  *b++ = 0;
230 
231  while (nbytes > 0) {
232  n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
233  errno = 0;
234  if (write(fd, buf, n) != n) {
235  int err = errno;
236 
237  if (err)
238  G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
239  else
240  G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
241  return -1;
242  }
243  nbytes -= n;
244  }
245  return 1;
246 #else
247  return seek_only(fd, nbytes);
248 #endif
249 }
250 
251 static int seek_only(int fd, off_t nbytes)
252 {
253  /* Using lseek (faster upon initialization).
254  NOTE: This version doesn't allocate disk storage for the file; storage will
255  be allocated dynamically as blocks are actually written. This could
256  result in seek_only() succeeding but a subsequent call to write() failing
257  with ENOSPC ("No space left on device").
258  */
259 
260  static const char buf[10];
261 
262  G_debug(3, "Using new segmentation code...");
263  errno = 0;
264  if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
265  int err = errno;
266 
267  G_warning("segment zero_fill(): Unable to seek (%s)", strerror(err));
268  return -1;
269  }
270  errno = 0;
271  if (write(fd, buf, 1) != 1) {
272  int err = errno;
273 
274  if (err)
275  G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
276  else
277  G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
278  return -1;
279  }
280 
281  return 1;
282 }
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:58
int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols, int len)
Format a segment file.
fd
Definition: d/range.c:69
SYMBOL * err(FILE *fp, SYMBOL *s, char *msg)
Definition: symbol/read.c:220
double b
Definition: r_raster.c:39
int G_debug(int level, const char *msg,...)
Print debugging message.
Definition: debug.c:65
#define _(str)
Definition: glocale.h:13
void G_warning(const char *msg,...)
Print a warning message to stderr.
Definition: gis/error.c:204