format.c 6.5 KB

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