open.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /**
  2. * \file lib/segment/open.c
  3. *
  4. * \brief Segment creation routine.
  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 2018
  12. */
  13. #include <unistd.h>
  14. #include <fcntl.h>
  15. #include <grass/gis.h>
  16. #include <grass/glocale.h>
  17. #include "local_proto.h"
  18. /**
  19. * \brief Initialize segment structure and open segment file.
  20. *
  21. * Initializes the <b>seg</b> structure and prepares a temporary file.
  22. * This fn is a wrapper for Segment_format() and Segment_init()
  23. *
  24. * <b>Note:</b> The file with name fname will be created anew.
  25. *
  26. * \param[in,out] SEG segment
  27. * \param[in] fname file name
  28. * \param[in] nrows number of non-segmented rows
  29. * \param[in] ncols number of non-segmented columns
  30. * \param[in] srows segment rows
  31. * \param[in] scols segment columns
  32. * \param[in] len length of data type
  33. * \param[in] nseg number of segments to remain in memory
  34. * \return 1 if successful
  35. * \return -1 if file name is invalid
  36. * \return -2 if file write error
  37. * \return -3 if illegal parameters are passed
  38. * \return -4 if file could not be re-opened
  39. * \return -5 if prepared file could not be read
  40. * \return -6 if out of memory
  41. */
  42. int
  43. Segment_open(SEGMENT *SEG, char *fname, off_t nrows, off_t ncols,
  44. int srows, int scols, int len, int nseg)
  45. {
  46. int ret;
  47. int nseg_total;
  48. nseg_total = ((nrows + srows - 1) / srows) *
  49. ((ncols + scols - 1) / scols);
  50. if (nseg >= nseg_total) {
  51. G_verbose_message(_("Using memory cache"));
  52. SEG->nrows = nrows;
  53. SEG->ncols = ncols;
  54. SEG->len = len;
  55. SEG->nseg = nseg;
  56. SEG->cache = G_calloc(sizeof(char) * SEG->nrows * SEG->ncols, SEG->len);
  57. SEG->scb = NULL;
  58. SEG->open = 1;
  59. return 1;
  60. }
  61. G_verbose_message(_("Using disk cache"));
  62. if (!fname) {
  63. G_warning(_("Segment file name is NULL"));
  64. return -1;
  65. }
  66. /* file exists? */
  67. if (access(fname, F_OK) == 0) {
  68. G_warning(_("Segment file exists already"));
  69. return -1;
  70. }
  71. SEG->fname = G_store(fname);
  72. SEG->fd = -1;
  73. if (-1 == (SEG->fd = creat(SEG->fname, 0666))) {
  74. G_warning(_("Unable to create segment file"));
  75. return -1;
  76. }
  77. if (0 > (ret = Segment_format_nofill(SEG->fd, nrows, ncols, srows,
  78. scols, len))) {
  79. close(SEG->fd);
  80. unlink(SEG->fname);
  81. if (ret == -1) {
  82. G_warning(_("Could not write segment file"));
  83. return -2;
  84. }
  85. else { /* ret = -3 */
  86. G_warning(_("Illegal segment configuration parameter(s)"));
  87. return ret;
  88. }
  89. }
  90. /* re-open for read and write */
  91. close(SEG->fd);
  92. SEG->fd = -1;
  93. if (-1 == (SEG->fd = open(SEG->fname, 2))) {
  94. unlink(SEG->fname);
  95. G_warning(_("Unable to re-open segment file"));
  96. return -4;
  97. }
  98. if (0 > (ret = Segment_init(SEG, SEG->fd, nseg))) {
  99. close(SEG->fd);
  100. unlink(SEG->fname);
  101. if (ret == -1) {
  102. G_warning(_("Could not read segment file"));
  103. return -5;
  104. }
  105. else {
  106. G_warning(_("Out of memory"));
  107. return -6;
  108. }
  109. }
  110. return 1;
  111. }