format.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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-2018
  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. static int seek_only(int, off_t);
  26. /* fd must be open for write */
  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, off_t nrows, off_t ncols, int srows, int scols,
  56. int len)
  57. {
  58. return seg_format(fd, nrows, ncols, srows, scols, len, 1);
  59. }
  60. /**
  61. * \brief Format a segment file.
  62. *
  63. * The segmentation routines require a disk file to be used for paging
  64. * segments in and out of memory. This routine formats the file open for
  65. * write on file descriptor <b>fd</b> for use as a segment file.
  66. *
  67. * A segment file must be formatted before it can be processed by other
  68. * segment routines. The configuration parameters <b>nrows</b>,
  69. * <b>ncols</b>, <b>srows</b>, <b>scols</b>, and <b>len</b> are written
  70. * to the beginning of the segment file which is then filled with zeros.
  71. *
  72. * The corresponding nonsegmented data matrix, which is to be
  73. * transferred to the segment file, is <b>nrows</b> by <b>ncols</b>. The
  74. * segment file is to be formed of segments which are <b>srows</b> by
  75. * <b>scols</b>. The data items have length <b>len</b> bytes. For
  76. * example, if the <em>data type is int</em>, <em>len is sizeof(int)</em>.
  77. *
  78. * <b>Note:</b> This version of the function does <b>not</b> fill in the
  79. * initialized data structures with zeros.
  80. *
  81. * \param[in] fd file descriptor
  82. * \param[in] nrows number of non-segmented rows
  83. * \param[in] ncols number of non-segmented columns
  84. * \param[in] srows segment rows
  85. * \param[in] scols segment columns
  86. * \param[in] len length of data type
  87. * \return 1 of successful
  88. * \return -1 if unable to seek or write <b>fd</b>
  89. * \return -3 if illegal parameters are passed
  90. */
  91. int Segment_format_nofill(int fd, off_t nrows, off_t ncols, int srows, int scols,
  92. int len)
  93. {
  94. return seg_format(fd, nrows, ncols, srows, scols, len, 0);
  95. }
  96. static int seg_format(int fd, 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,%"PRI_OFF_T",%"PRI_OFF_T",%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. /* calculate total number of segments */
  138. nbytes = spr * ((nrows + srows - 1) / srows);
  139. nbytes *= size;
  140. if (!fill) {
  141. /* only seek and write a zero byte to the end */
  142. if (seek_only(fd, nbytes) < 0)
  143. return -1;
  144. return 1;
  145. }
  146. /* fill segment file with zeros */
  147. /* NOTE: this could be done faster using lseek() by seeking
  148. * ahead nbytes and then writing a single byte of 0,
  149. * provided lseek() on all version of UNIX will create a file
  150. * with holes that read as zeros.
  151. */
  152. if (zero_fill(fd, nbytes) < 0)
  153. return -1;
  154. return 1;
  155. }
  156. static int write_int(int fd, int n)
  157. {
  158. errno = 0;
  159. if (write(fd, &n, sizeof(int)) != sizeof(int)) {
  160. int err = errno;
  161. if (err)
  162. G_warning("Segment format: Unable to write (%s)", strerror(err));
  163. else
  164. G_warning("Segment format: Unable to write (insufficient disk space?)");
  165. return 0;
  166. }
  167. return 1;
  168. }
  169. static int write_off_t(int fd, off_t n)
  170. {
  171. errno = 0;
  172. if (write(fd, &n, sizeof(off_t)) != sizeof(off_t)) {
  173. int err = errno;
  174. if (err)
  175. G_warning("Segment format: Unable to write (%s)", strerror(err));
  176. else
  177. G_warning("Segment format: Unable to write (insufficient disk space?)");
  178. return 0;
  179. }
  180. return 1;
  181. }
  182. static int zero_fill(int fd, off_t nbytes)
  183. {
  184. #ifndef USE_LSEEK
  185. char buf[16384];
  186. register char *b;
  187. register int n;
  188. /* zero buf */
  189. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  190. b = buf;
  191. while (n-- > 0)
  192. *b++ = 0;
  193. while (nbytes > 0) {
  194. n = nbytes > sizeof(buf) ? sizeof(buf) : nbytes;
  195. errno = 0;
  196. if (write(fd, buf, n) != n) {
  197. int err = errno;
  198. if (err)
  199. G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
  200. else
  201. G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
  202. return -1;
  203. }
  204. nbytes -= n;
  205. }
  206. return 1;
  207. #else
  208. return seek_only(fd, nbytes);
  209. #endif
  210. }
  211. static int seek_only(int fd, off_t nbytes)
  212. {
  213. /* Using lseek (faster upon initialization).
  214. NOTE: This version doesn't allocate disk storage for the file; storage will
  215. be allocated dynamically as blocks are actually written. This could
  216. result in seek_only() succeeding but a subsequent call to write() failing
  217. with ENOSPC ("No space left on device").
  218. */
  219. static const char buf[10];
  220. G_debug(3, "Using new segmentation code...");
  221. errno = 0;
  222. if (lseek(fd, nbytes - 1, SEEK_CUR) < 0) {
  223. int err = errno;
  224. G_warning("segment zero_fill(): Unable to seek (%s)", strerror(err));
  225. return -1;
  226. }
  227. errno = 0;
  228. if (write(fd, buf, 1) != 1) {
  229. int err = errno;
  230. if (err)
  231. G_warning("segment zero_fill(): Unable to write (%s)", strerror(err));
  232. else
  233. G_warning("segment zero_fill(): Unable to write (insufficient disk space?)");
  234. return -1;
  235. }
  236. return 1;
  237. }