format.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /**
  2. * \file format.c
  3. *
  4. * \brief Segment formatting routines.
  5. *
  6. * This program is free software under the GNU General Public License
  7. * (>=v2). Read the file COPYING that comes with GRASS for details.
  8. *
  9. * \author GRASS GIS Development Team
  10. *
  11. * \date 2005-2009
  12. */
  13. #include <stdio.h>
  14. #include <string.h>
  15. #include <errno.h>
  16. #include <unistd.h>
  17. #include <limits.h>
  18. #include <grass/gis.h>
  19. #include <grass/glocale.h>
  20. #include <grass/segment.h>
  21. static int _segment_format(int, int, int, int, int, int, int);
  22. static int write_int(int, int);
  23. static int zero_fill(int, off_t);
  24. /* fd must be open for write */
  25. /**
  26. * \fn int segment_format (int fd, int nrows, int ncols, int srows, int scols, int len)
  27. *
  28. * \brief Format a segment file.
  29. *
  30. * The segmentation routines require a disk file to be used for paging
  31. * segments in and out of memory. This routine formats the file open for
  32. * write on file descriptor <b>fd</b> for use as a segment file.
  33. *
  34. * A segment file must be formatted before it can be processed by other
  35. * segment routines. The configuration parameters <b>nrows</b>,
  36. * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
  37. * to the beginning of the segment file which is then filled with zeros.
  38. *
  39. * The corresponding nonsegmented data matrix, which is to be
  40. * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
  41. * segment file is to be formed of segments which are <b>srows</b> by
  42. * <b>scols</b>. The data items have length <b>len</b> bytes. For
  43. * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
  44. *
  45. * \param[in] fd file descriptor
  46. * \param[in] nrows number of non-segmented rows
  47. * \param[in] ncols number of non-segmented columns
  48. * \param[in] srows segment rows
  49. * \param[in] scols segment columns
  50. * \param[in] len length of data type
  51. * \return 1 of successful
  52. * \return -1 if unable to seek or write <b>fd</b>
  53. * \return -3 if illegal parameters are passed
  54. */
  55. int segment_format(int fd, int nrows, int ncols, int srows, int scols,
  56. int len)
  57. {
  58. return _segment_format(fd, nrows, ncols, srows, scols, len, 1);
  59. }
  60. /**
  61. * \fn int segment_format_nofill (int fd, int nrows, int ncols, int srows, int scols, int len)
  62. *
  63. * \brief Format a segment file.
  64. *
  65. * The segmentation routines require a disk file to be used for paging
  66. * segments in and out of memory. This routine formats the file open for
  67. * write on file descriptor <b>fd</b> for use as a segment file.
  68. *
  69. * A segment file must be formatted before it can be processed by other
  70. * segment routines. The configuration parameters <b>nrows</b>,
  71. * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
  72. * to the beginning of the segment file which is then filled with zeros.
  73. *
  74. * The corresponding nonsegmented data matrix, which is to be
  75. * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
  76. * segment file is to be formed of segments which are <b>srows</b> by
  77. * <b>scols</b>. The data items have length <b>len</b> bytes. For
  78. * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
  79. *
  80. * <b>Note:</b> This version of the function does <b>not</b> fill in the
  81. * initialized data structures with zeros.
  82. *
  83. * \param[in] fd file descriptor
  84. * \param[in] nrows number of non-segmented rows
  85. * \param[in] ncols number of non-segmented columns
  86. * \param[in] srows segment rows
  87. * \param[in] scols segment columns
  88. * \param[in] len length of data type
  89. * \return 1 of successful
  90. * \return -1 if unable to seek or write <b>fd</b>
  91. * \return -3 if illegal parameters are passed
  92. */
  93. int segment_format_nofill(int fd, int nrows, int ncols, int srows, int scols,
  94. int len)
  95. {
  96. return _segment_format(fd, nrows, ncols, srows, scols, len, 0);
  97. }
  98. static int _segment_format(int fd,
  99. int nrows, int ncols,
  100. int srows, int scols, int len, int fill)
  101. {
  102. off_t nbytes;
  103. int spr, size;
  104. if (nrows <= 0 || ncols <= 0 || len <= 0 || srows <= 0 || scols <= 0) {
  105. G_warning("segment_format(fd,%d,%d,%d,%d,%d): illegal value(s)",
  106. nrows, ncols, srows, scols, len);
  107. return -3;
  108. }
  109. if (sizeof(off_t) == 4 && sizeof(double) >= 8) {
  110. double d_size;
  111. off_t o_size;
  112. spr = ncols / scols;
  113. if (ncols % scols)
  114. spr++;
  115. /* calculate total number of segments */
  116. d_size = (double) spr * ((nrows + srows - 1) / srows);
  117. /* multiply with segment size */
  118. d_size *= srows * scols * len;
  119. /* add header */
  120. d_size += 5 * sizeof(int);
  121. o_size = (off_t) d_size;
  122. /* this test assumes that all off_t values can be exactly
  123. * represented as double if sizeof(off_t) = 4 and sizeof(double) >= 8 */
  124. if ((double) o_size != d_size) {
  125. G_warning(_("Segment format: file size too large"));
  126. G_warning(_("Please recompile with Large File Support (LFS)"));
  127. return -1;
  128. }
  129. }
  130. if (lseek(fd, 0L, SEEK_SET) == (off_t) -1) {
  131. G_warning("segment_format(): Unable to seek (%s)", strerror(errno));
  132. return -1;
  133. }
  134. if (!write_int(fd, nrows) || !write_int(fd, ncols)
  135. || !write_int(fd, srows) || !write_int(fd, scols)
  136. || !write_int(fd, len))
  137. return -1;
  138. if (!fill)
  139. return 1;
  140. spr = ncols / scols;
  141. if (ncols % scols)
  142. spr++;
  143. size = srows * scols * len;
  144. /* calculate total number of segments */
  145. nbytes = spr * ((nrows + srows - 1) / srows);
  146. nbytes *= size;
  147. /* fill segment file with zeros */
  148. /* NOTE: this could be done faster using lseek() by seeking
  149. * ahead nbytes and then writing a single byte of 0,
  150. * provided lseek() on all version of UNIX will create a file
  151. * with holes that read as zeros.
  152. */
  153. if (zero_fill(fd, nbytes) < 0)
  154. return -1;
  155. return 1;
  156. }
  157. static int write_int(int fd, int n)
  158. {
  159. if (write(fd, &n, sizeof(int)) != sizeof(int)) {
  160. G_warning("segment_format(): Unable to write (%s)", strerror(errno));
  161. return 0;
  162. }
  163. return 1;
  164. }
  165. static int zero_fill(int fd, off_t nbytes)
  166. {
  167. #ifndef USE_LSEEK
  168. char buf[16384];
  169. register char *b;
  170. register int n;
  171. /* zero buf */
  172. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  173. b = buf;
  174. while (n-- > 0)
  175. *b++ = 0;
  176. while (nbytes > 0) {
  177. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  178. if (write(fd, buf, n) != n) {
  179. G_warning("segment zero_fill(): Unable to write (%s)", strerror(errno));
  180. return -1;
  181. }
  182. nbytes -= n;
  183. }
  184. return 1;
  185. #else
  186. /* Using lseek (faster upon initialization).
  187. NOTE: This version doesn't allocate disk storage for the file; storage will
  188. be allocated dynamically as blocks are actually written. This could
  189. result in zero_fill() succeeding but a subsequent call to write() failing
  190. with ENOSPC ("No space left on device").
  191. */
  192. static const char buf[10];
  193. G_debug(3, "Using new segmentation code...");
  194. if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
  195. G_warning("segment zero_fill(): Unable to seek (%s)", strerror(errno));
  196. return -1;
  197. }
  198. if (write(fd, buf, 1) != 1) {
  199. G_warning("segment zero_fill(): Unable to write (%s)", strerror(errno));
  200. return -1;
  201. }
  202. return 1;
  203. #endif
  204. }