format.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  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 seg_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 seg_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 seg_format(fd, nrows, ncols, srows, scols, len, 0);
  94. }
  95. static int seg_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. int err = errno;
  130. G_warning("Segment_format(): Unable to seek (%s)", strerror(err));
  131. return -1;
  132. }
  133. if (!write_off_t(fd, nrows) || !write_off_t(fd, ncols)
  134. || !write_int(fd, srows) || !write_int(fd, scols)
  135. || !write_int(fd, len))
  136. return -1;
  137. if (!fill)
  138. return 1;
  139. /* calculate total number of segments */
  140. nbytes = spr * ((nrows + srows - 1) / srows);
  141. nbytes *= size;
  142. /* fill segment file with zeros */
  143. /* NOTE: this could be done faster using lseek() by seeking
  144. * ahead nbytes and then writing a single byte of 0,
  145. * provided lseek() on all version of UNIX will create a file
  146. * with holes that read as zeros.
  147. */
  148. if (zero_fill(fd, nbytes) < 0)
  149. return -1;
  150. return 1;
  151. }
  152. static int write_int(int fd, int n)
  153. {
  154. errno = 0;
  155. if (write(fd, &n, sizeof(int)) != sizeof(int)) {
  156. int err = errno;
  157. if (err)
  158. G_warning("Segment format: Unable to write (%s)", strerror(err));
  159. else
  160. G_warning("Segment format: Unable to write (insufficient disk space?)");
  161. return 0;
  162. }
  163. return 1;
  164. }
  165. static int write_off_t(int fd, off_t n)
  166. {
  167. errno = 0;
  168. if (write(fd, &n, sizeof(off_t)) != sizeof(off_t)) {
  169. int err = errno;
  170. if (err)
  171. G_warning("Segment format: Unable to write (%s)", strerror(err));
  172. else
  173. G_warning("Segment format: Unable to write (insufficient disk space?)");
  174. return 0;
  175. }
  176. return 1;
  177. }
  178. static int zero_fill(int fd, off_t nbytes)
  179. {
  180. #ifndef USE_LSEEK
  181. char buf[16384];
  182. register char *b;
  183. register int n;
  184. /* zero buf */
  185. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  186. b = buf;
  187. while (n-- > 0)
  188. *b++ = 0;
  189. while (nbytes > 0) {
  190. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  191. errno = 0;
  192. if (write(fd, buf, n) != n) {
  193. int err = errno;
  194. if (err)
  195. G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
  196. else
  197. G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
  198. return -1;
  199. }
  200. nbytes -= n;
  201. }
  202. return 1;
  203. #else
  204. /* Using lseek (faster upon initialization).
  205. NOTE: This version doesn't allocate disk storage for the file; storage will
  206. be allocated dynamically as blocks are actually written. This could
  207. result in zero_fill() succeeding but a subsequent call to write() failing
  208. with ENOSPC ("No space left on device").
  209. */
  210. static const char buf[10];
  211. G_debug(3, "Using new segmentation code...");
  212. errno = 0;
  213. if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
  214. int err = errno;
  215. G_warning("segment zero_fill(): Unable to seek (%s)", strerror(err));
  216. return -1;
  217. }
  218. errno = 0;
  219. if (write(fd, buf, 1) != 1) {
  220. int err = errno;
  221. if (err)
  222. G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
  223. else
  224. G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
  225. return -1;
  226. }
  227. return 1;
  228. #endif
  229. }