format.c 7.1 KB

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