tempfile.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*!
  2. * \file lib/gis/tempfile.c
  3. *
  4. * \brief GIS Library - Temporary file functions.
  5. *
  6. * (C) 2001-2015 by the GRASS Development Team
  7. *
  8. * This program is free software under the GNU General Public License
  9. * (>=v2). Read the file COPYING that comes with GRASS for details.
  10. *
  11. * \author Original author CERL
  12. */
  13. #include <string.h>
  14. #include <unistd.h>
  15. #include <sys/stat.h>
  16. #include <stdlib.h>
  17. #include <grass/gis.h>
  18. #include "gis_local_proto.h"
  19. static struct Counter unique;
  20. static int initialized;
  21. /*!
  22. \brief Initialize environment for creating tempfiles.
  23. */
  24. void G_init_tempfile(void)
  25. {
  26. if (G_is_initialized(&initialized))
  27. return;
  28. G_init_counter(&unique, 0);
  29. G_initialize_done(&initialized);
  30. }
  31. /*!
  32. * \brief Returns a temporary file name.
  33. *
  34. * This routine returns a pointer to a string containing a unique
  35. * temporary file name that can be used as a temporary file within the
  36. * module. Successive calls to G_tempfile() will generate new
  37. * names. Only the file name is generated. The file itself is not
  38. * created. To create the file, the module must use standard UNIX
  39. * functions which create and open files, e.g., <i>creat()</i> or
  40. * <i>fopen()</i>.
  41. *
  42. * Successive calls will generate different names the names are of the
  43. * form pid.n where pid is the programs process id number and n is a
  44. * unique identifier.
  45. *
  46. * <b>Note:</b> It is recommended to <i>unlink()</i> (remove) the
  47. * temp file on exit/error. Only if GRASS is left with 'exit', the GIS
  48. * mapset management will clean up the temp directory (ETC/clean_temp).
  49. *
  50. * \return pointer to a character string containing the name. The name
  51. * is copied to allocated memory and may be released by the unix free()
  52. * routine.
  53. */
  54. char *G_tempfile(void)
  55. {
  56. return G_tempfile_pid(getpid());
  57. }
  58. /*!
  59. * \brief Create tempfile from process id.
  60. *
  61. * See G_tempfile().
  62. *
  63. * \param pid
  64. * \return pointer to string path
  65. */
  66. char *G_tempfile_pid(int pid)
  67. {
  68. char path[GPATH_MAX];
  69. char name[GNAME_MAX];
  70. char element[100];
  71. if (pid <= 0)
  72. pid = getpid();
  73. G_temp_element(element);
  74. G_init_tempfile();
  75. do {
  76. int uniq = G_counter_next(&unique);
  77. sprintf(name, "%d.%d", pid, uniq);
  78. G_file_name(path, element, name, G_mapset());
  79. }
  80. while (access(path, F_OK) == 0);
  81. G_debug(2, "G_tempfile_pid(): %s", path);
  82. return G_store(path);
  83. }
  84. /*!
  85. * \brief Populates element with a path string.
  86. *
  87. * \param[out] element element name
  88. */
  89. void G_temp_element(char *element)
  90. {
  91. G__temp_element(element, FALSE);
  92. }
  93. /*!
  94. * \brief Populates element with a path string (internal use only!)
  95. *
  96. * \param[out] element element name
  97. * \param tmp TRUE to use G_make_mapset_element_tmp() instead of G_make_mapset_element()
  98. */
  99. void G__temp_element(char *element, int tmp)
  100. {
  101. const char *machine;
  102. strcpy(element, ".tmp");
  103. machine = G__machine_name();
  104. if (machine != NULL && *machine != 0) {
  105. strcat(element, "/");
  106. strcat(element, machine);
  107. }
  108. if (!tmp)
  109. G_make_mapset_object_group(element);
  110. else
  111. G_make_mapset_object_group_tmp(element);
  112. G_debug(2, "G__temp_element(): %s (tmp=%d)", element, tmp);
  113. }