open.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*!
  2. * \file lib/gis/open.c
  3. *
  4. * \brief GIS Library - Open file functions
  5. *
  6. * (C) 1999-2009 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. char path[GPATH_MAX];
  46. char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
  47. G__check_gisinit();
  48. /* READ */
  49. if (mode == 0) {
  50. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  51. if (*mapset && strcmp(xmapset, mapset) != 0) {
  52. G_warning(_("G__open(read): mapset <%s> doesn't match xmapset <%s>"),
  53. mapset, xmapset);
  54. return -1;
  55. }
  56. name = xname;
  57. mapset = xmapset;
  58. }
  59. mapset = G_find_file2(element, name, mapset);
  60. if (!mapset)
  61. return -1;
  62. G_file_name(path, element, name, mapset);
  63. if ((fd = open(path, 0)) < 0)
  64. G_warning(_("G__open(read): Unable to open '%s': %s"),
  65. path, strerror(errno));
  66. return fd;
  67. }
  68. /* WRITE */
  69. if (mode == 1 || mode == 2) {
  70. mapset = G_mapset();
  71. if (G_name_is_fully_qualified(name, xname, xmapset)) {
  72. if (strcmp(xmapset, mapset) != 0) {
  73. G_warning(_("G__open(write): xmapset <%s> != G_mapset() <%s>"),
  74. xmapset, mapset);
  75. return -1;
  76. }
  77. name = xname;
  78. }
  79. if (*name && G_legal_filename(name) == -1)
  80. return -1;
  81. G_file_name(path, element, name, mapset);
  82. if (mode == 1 || access(path, 0) != 0) {
  83. G_make_mapset_element(element);
  84. close(open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666));
  85. }
  86. if ((fd = open(path, mode)) < 0)
  87. G_warning(_("G__open(write): Unable to open '%s': %s"),
  88. path, strerror(errno));
  89. return fd;
  90. }
  91. return -1;
  92. }
  93. /*!
  94. \brief Open a new database file
  95. Creates <i>name</i> in the current mapset and opens it
  96. for write only.
  97. The database file <i>name</i> under the <i>element</i> in the
  98. current mapset is created and opened for writing (but not reading).
  99. The UNIX open() routine is used to open the file. If the file does
  100. not exist, -1 is returned. Otherwise the file is positioned at the
  101. end of the file and the file descriptor from the open() is returned.
  102. \param element database element name
  103. \param name map file name
  104. \return open file descriptor (int)
  105. \return -1 could not open
  106. */
  107. int G_open_new(const char *element, const char *name)
  108. {
  109. return G__open(element, name, G_mapset(), 1);
  110. }
  111. /*!
  112. \brief Open a database file for reading
  113. The database file <i>name</i> under the <i>element</i> in the
  114. specified <i>mapset</i> is opened for reading (but not for writing).
  115. The UNIX open() routine is used to open the file. If the file does
  116. not exist, -1 is returned. Otherwise the file descriptor from the
  117. open() is returned.
  118. \param element database element name
  119. \param name map file name
  120. \param mapset mapset containing map <i>name</i>
  121. \return open file descriptor (int)
  122. \return -1 could not open
  123. */
  124. int G_open_old(const char *element, const char *name, const char *mapset)
  125. {
  126. return G__open(element, name, mapset, 0);
  127. }
  128. /*!
  129. \brief Open a database file for update
  130. The database file <i>name</i> under the <i>element</i> in the
  131. current mapset is opened for reading and writing. The UNIX open()
  132. routine is used to open the file. If the file does not exist, -1 is
  133. returned. Otherwise the file is positioned at the end of the file
  134. and the file descriptor from the open() is returned.
  135. \param element database element name
  136. \param name map file name
  137. \return open file descriptor (int)
  138. \return -1 could not open
  139. */
  140. int G_open_update(const char *element, const char *name)
  141. {
  142. int fd;
  143. fd = G__open(element, name, G_mapset(), 2);
  144. if (fd >= 0)
  145. lseek(fd, 0L, SEEK_END);
  146. return fd;
  147. }
  148. /*!
  149. \brief Open a new database file
  150. The database file <i>name</i> under the <i>element</i> in the
  151. current mapset is created and opened for writing (but not reading).
  152. The UNIX fopen() routine, with "w" write mode, is used to open the
  153. file. If the file does not exist, the NULL pointer is
  154. returned. Otherwise the file is positioned at the end of the file
  155. and the file descriptor from the fopen() is returned.
  156. \param element database element name
  157. \param name map file name
  158. \return open file descriptor (FILE *)
  159. \return NULL on error
  160. */
  161. FILE *G_fopen_new(const char *element, const char *name)
  162. {
  163. int fd;
  164. fd = G__open(element, name, G_mapset(), 1);
  165. if (fd < 0) {
  166. G_debug(1, "G_fopen_new(): element = %s, name = %s : NULL",
  167. element, name);
  168. return (FILE *) 0;
  169. }
  170. G_debug(2, "\tfile open: new (mode = w)");
  171. return fdopen(fd, "w");
  172. }
  173. /*!
  174. \brief Open a database file for reading
  175. The database file <i>name</i> under the <i>element</i> in the
  176. specified <i>mapset</i> is opened for reading (but not for writing).
  177. The UNIX fopen() routine, with "r" read mode, is used to open the
  178. file. If the file does not exist, the NULL pointer is
  179. returned. Otherwise the file descriptor from the fopen() is
  180. returned.
  181. \param element database element name
  182. \param name map file name
  183. \param mapset mapset name containing map <i>name</i>
  184. \return open file descriptor (FILE *)
  185. \return NULL on error
  186. */
  187. FILE *G_fopen_old(const char *element, const char *name, const char *mapset)
  188. {
  189. int fd;
  190. fd = G__open(element, name, mapset, 0);
  191. if (fd < 0)
  192. return (FILE *) NULL;
  193. G_debug(2, "\tfile open: read (mode = r)");
  194. return fdopen(fd, "r");
  195. }
  196. /*!
  197. \brief Open a database file for update (append mode)
  198. The database file <i>name</i> under the <i>element</i> in the
  199. current mapset is opened for for writing. The UNIX fopen() routine,
  200. with "a" append mode, is used to open the file. If the file does not
  201. exist, the NULL pointer is returned. Otherwise the file descriptor
  202. from the fopen() is returned.
  203. \param element database element name
  204. \param name map file name
  205. \return open file descriptor (FILE *)
  206. \return NULL on error
  207. */
  208. FILE *G_fopen_append(const char *element, const char *name)
  209. {
  210. int fd;
  211. fd = G__open(element, name, G_mapset(), 2);
  212. if (fd < 0)
  213. return (FILE *) 0;
  214. lseek(fd, 0L, SEEK_END);
  215. G_debug(2, "\tfile open: append (mode = a)");
  216. return fdopen(fd, "a");
  217. }
  218. /*!
  219. \brief Open a database file for update (r+ mode)
  220. The database file <i>name</i> under the <i>element</i> in the
  221. current mapset is opened for for writing. The UNIX fopen() routine,
  222. with "r+" append mode, is used to open the file. If the file does not
  223. exist, the NULL pointer is returned. Otherwise the file descriptor
  224. from the fopen() is returned.
  225. \param element database element name
  226. \param name map file name
  227. \return open file descriptor (FILE *)
  228. \return NULL on error
  229. */
  230. FILE *G_fopen_modify(const char *element, const char *name)
  231. {
  232. int fd;
  233. fd = G__open(element, name, G_mapset(), 2);
  234. if (fd < 0)
  235. return (FILE *) 0;
  236. lseek(fd, 0L, SEEK_END);
  237. G_debug(2, "\tfile open: modify (mode = r+)");
  238. return fdopen(fd, "r+");
  239. }