open.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*!
  2. * \file lib/gis/open.c
  3. *
  4. * \brief GIS Library - Open file functions
  5. *
  6. * (C) 1999-2015 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public
  9. * License (>=v2). Read the file COPYING that comes with GRASS
  10. * for details.
  11. *
  12. * \author USACERL and many others
  13. */
  14. #include <grass/config.h>
  15. #include <errno.h>
  16. #include <string.h>
  17. #include <unistd.h>
  18. #include <fcntl.h>
  19. #include <grass/gis.h>
  20. #include <grass/glocale.h>
  21. #include "gis_local_proto.h"
  22. /*!
  23. \brief Lowest level open routine.
  24. Opens the file <i>name</i> in <i>element</i> ("cell", etc.) in mapset <i>mapset</i>
  25. according to the i/o <i>mode</i>.
  26. - mode = 0 (read) will look for <i>name</i> in <i>mapset</i> and
  27. open the file for read only the file must exist
  28. - mode = 1 (write) will create an empty file <i>name</i> in the
  29. current mapset and open the file for write only
  30. <i>mapset</i> ignored
  31. - mode = 2 (read and write) will open a file in the current mapset
  32. for reading and writing creating a new file if
  33. necessary <i>mapset</i> ignored
  34. \param element database element name
  35. \param name map file name
  36. \param mapset mapset containing map <i>name</i>
  37. \param mode r/w mode 0=read, 1=write, 2=read/write
  38. \return open file descriptor (int)
  39. \return -1 could not open
  40. */
  41. static int G__open(const char *element,
  42. const char *name, const char *mapset, int mode)
  43. {
  44. int fd;
  45. int is_tmp;
  46. char path[GPATH_MAX];
  47. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  48. G__check_gisinit();
  49. is_tmp = (element && strncmp(element, ".tmp", 3) == 0);
  50. if (is_tmp)
  51. G_file_name_tmp(path, element, name, mapset);
  52. else
  53. G_file_name(path, element, name, mapset);
  54. /* READ */
  55. if (mode == 0) {
  56. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  57. if (*mapset && strcmp(xmapset, mapset) != 0) {
  58. G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"),
  59. mapset, xmapset);
  60. return -1;
  61. }
  62. name = xname;
  63. mapset = xmapset;
  64. }
  65. if (!is_tmp) {
  66. mapset = G_find_file2(element, name, mapset);
  67. if (!mapset)
  68. return -1;
  69. G_file_name(path, element, name, mapset);
  70. }
  71. if ((fd = open(path, 0)) < 0)
  72. G_warning(_("G__open(read): Unable to open '%s': %s"),
  73. path, strerror(errno));
  74. return fd;
  75. }
  76. /* WRITE */
  77. if (mode == 1 || mode == 2) {
  78. mapset = G_mapset();
  79. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  80. if (strcmp(xmapset, mapset) != 0) {
  81. G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"),
  82. xmapset, mapset);
  83. return -1;
  84. }
  85. name = xname;
  86. }
  87. if (*name && G_legal_filename(name) == -1)
  88. return -1;
  89. if (mode == 1 || access(path, 0) != 0) {
  90. if (is_tmp)
  91. G_make_mapset_element_tmp(element);
  92. else
  93. G_make_mapset_element(element);
  94. close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666));
  95. }
  96. if ((fd = open(path, mode)) < 0)
  97. G_warning(_("G__open(write): Unable to open '%s': %s"),
  98. path, strerror(errno));
  99. return fd;
  100. }
  101. return -1;
  102. }
  103. /*!
  104. \brief Open a new database file
  105. Creates <i>name</i> in the current mapset and opens it
  106. for write only.
  107. The database file <i>name</i> under the <i>element</i> in the
  108. current mapset is created and opened for writing (but not reading).
  109. The UNIX open() routine is used to open the file. If the file does
  110. not exist, -1 is returned. Otherwise the file is positioned at the
  111. end of the file and the file descriptor from the open() is returned.
  112. \param element database element name
  113. \param name map file name
  114. \return open file descriptor (int)
  115. \return -1 could not open
  116. */
  117. int G_open_new(const char *element, const char *name)
  118. {
  119. return G__open(element, name, G_mapset(), 1);
  120. }
  121. /*!
  122. \brief Open a database file for reading
  123. The database file <i>name</i> under the <i>element</i> in the
  124. specified <i>mapset</i> is opened for reading (but not for writing).
  125. The UNIX open() routine is used to open the file. If the file does
  126. not exist, -1 is returned. Otherwise the file descriptor from the
  127. open() is returned.
  128. \param element database element name
  129. \param name map file name
  130. \param mapset mapset containing map <i>name</i>
  131. \return open file descriptor (int)
  132. \return -1 could not open
  133. */
  134. int G_open_old(const char *element, const char *name, const char *mapset)
  135. {
  136. return G__open(element, name, mapset, 0);
  137. }
  138. /*!
  139. \brief Open a database file for update
  140. The database file <i>name</i> under the <i>element</i> in the
  141. current mapset is opened for reading and writing. The UNIX open()
  142. routine is used to open the file. If the file does not exist, -1 is
  143. returned. Otherwise the file is positioned at the end of the file
  144. and the file descriptor from the open() is returned.
  145. \param element database element name
  146. \param name map file name
  147. \return open file descriptor (int)
  148. \return -1 could not open
  149. */
  150. int G_open_update(const char *element, const char *name)
  151. {
  152. int fd;
  153. fd = G__open(element, name, G_mapset(), 2);
  154. if (fd >= 0)
  155. lseek(fd, 0L, SEEK_END);
  156. return fd;
  157. }
  158. /*!
  159. \brief Open a new database file
  160. The database file <i>name</i> under the <i>element</i> in the
  161. current mapset is created and opened for writing (but not reading).
  162. The UNIX fopen() routine, with "w" write mode, is used to open the
  163. file. If the file does not exist, the NULL pointer is
  164. returned. Otherwise the file is positioned at the end of the file
  165. and the file descriptor from the fopen() is returned.
  166. \param element database element name
  167. \param name map file name
  168. \return open file descriptor (FILE *)
  169. \return NULL on error
  170. */
  171. FILE *G_fopen_new(const char *element, const char *name)
  172. {
  173. int fd;
  174. fd = G__open(element, name, G_mapset(), 1);
  175. if (fd < 0) {
  176. G_debug(1, "G_fopen_new(): element = %s, name = %s : NULL",
  177. element, name);
  178. return (FILE *) 0;
  179. }
  180. G_debug(2, "\tfile open: new (mode = w)");
  181. return fdopen(fd, "w");
  182. }
  183. /*!
  184. \brief Open a database file for reading
  185. The database file <i>name</i> under the <i>element</i> in the
  186. specified <i>mapset</i> is opened for reading (but not for writing).
  187. The UNIX fopen() routine, with "r" read mode, is used to open the
  188. file. If the file does not exist, the NULL pointer is
  189. returned. Otherwise the file descriptor from the fopen() is
  190. returned.
  191. \param element database element name
  192. \param name map file name
  193. \param mapset mapset name containing map <i>name</i>
  194. \return open file descriptor (FILE *)
  195. \return NULL on error
  196. */
  197. FILE *G_fopen_old(const char *element, const char *name, const char *mapset)
  198. {
  199. int fd;
  200. fd = G__open(element, name, mapset, 0);
  201. if (fd < 0)
  202. return (FILE *) NULL;
  203. G_debug(2, "\tfile open: read (mode = r)");
  204. return fdopen(fd, "r");
  205. }
  206. /*!
  207. \brief Open a database file for update (append mode)
  208. The database file <i>name</i> under the <i>element</i> in the
  209. current mapset is opened for for writing. The UNIX fopen() routine,
  210. with "a" append mode, is used to open the file. If the file does not
  211. exist, the NULL pointer is returned. Otherwise the file descriptor
  212. from the fopen() is returned.
  213. \param element database element name
  214. \param name map file name
  215. \return open file descriptor (FILE *)
  216. \return NULL on error
  217. */
  218. FILE *G_fopen_append(const char *element, const char *name)
  219. {
  220. int fd;
  221. fd = G__open(element, name, G_mapset(), 2);
  222. if (fd < 0)
  223. return (FILE *) 0;
  224. lseek(fd, 0L, SEEK_END);
  225. G_debug(2, "\tfile open: append (mode = a)");
  226. return fdopen(fd, "a");
  227. }
  228. /*!
  229. \brief Open a database file for update (r+ mode)
  230. The database file <i>name</i> under the <i>element</i> in the
  231. current mapset is opened for for writing. The UNIX fopen() routine,
  232. with "r+" append mode, is used to open the file. If the file does not
  233. exist, the NULL pointer is returned. Otherwise the file descriptor
  234. from the fopen() is returned.
  235. \param element database element name
  236. \param name map file name
  237. \return open file descriptor (FILE *)
  238. \return NULL on error
  239. */
  240. FILE *G_fopen_modify(const char *element, const char *name)
  241. {
  242. int fd;
  243. fd = G__open(element, name, G_mapset(), 2);
  244. if (fd < 0)
  245. return (FILE *) 0;
  246. lseek(fd, 0L, SEEK_END);
  247. G_debug(2, "\tfile open: modify (mode = r+)");
  248. return fdopen(fd, "r+");
  249. }