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