format.c 6.9 KB

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