mapset_msc.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*!
  2. \file mapset_msc.c
  3. \brief GIS library - Mapset user permission routines.
  4. (C) 1999-2008 The GRASS development team
  5. This program is free software under the GNU General Public License
  6. (>=v2). Read the file COPYING that comes with GRASS for details.
  7. */
  8. #include <string.h>
  9. #include <unistd.h>
  10. #include <stdlib.h>
  11. #include <errno.h>
  12. #include <sys/types.h>
  13. #include <sys/stat.h>
  14. #include <grass/gis.h>
  15. #include <grass/glocale.h>
  16. /*!
  17. \brief Create element in the current mapset.
  18. Make the specified element in the current mapset
  19. will check for the existence of the element and
  20. do nothing if it is found so this routine
  21. can be called even if the element already exists.
  22. \param element element to be created in mapset
  23. \return 0 ?
  24. \return ?
  25. */
  26. int G__make_mapset_element(const char *p_element)
  27. {
  28. char path[GPATH_MAX];
  29. char *p;
  30. const char *element;
  31. element = p_element;
  32. if (*element == 0)
  33. return 0;
  34. G__file_name(p = path, "", "", G_mapset());
  35. while (*p)
  36. p++;
  37. /* add trailing slash if missing */
  38. --p;
  39. if (*p++ != '/') {
  40. *p++ = '/';
  41. *p = 0;
  42. }
  43. /* now append element, one directory at a time, to path */
  44. while (1) {
  45. if (*element == '/' || *element == 0) {
  46. *p = 0;
  47. if (access(path, 0) != 0) { /* directory not yet created */
  48. if (G_mkdir(path) != 0)
  49. G_fatal_error(_("Unable to make mapset element %s (%s): %s"),
  50. p_element, path, strerror(errno));
  51. }
  52. if (access(path, 0) != 0) /* directory not accessible */
  53. G_fatal_error(_("Unable to access mapset element %s (%s): %s"),
  54. p_element, path, strerror(errno));
  55. if (*element == 0)
  56. return 1;
  57. }
  58. *p++ = *element++;
  59. }
  60. }
  61. /*!
  62. \brief Create misc element in the current mapset.
  63. \param dir directory path
  64. \param name element name
  65. \return 0 ?
  66. \return ?
  67. */
  68. int G__make_mapset_element_misc(const char *dir, const char *name)
  69. {
  70. char buf[GNAME_MAX * 2 + 1];
  71. sprintf(buf, "%s/%s", dir, name);
  72. return G__make_mapset_element(buf);
  73. }
  74. /*!
  75. \brief Check for user mapset permission
  76. \param mapset mapset name
  77. \return 1 mapset exists, and user has permission
  78. \return 0 mapset exists, BUT user denied permission
  79. \return -1 mapset does not exist
  80. */
  81. int G__mapset_permissions(const char *mapset)
  82. {
  83. char path[GPATH_MAX];
  84. struct stat info;
  85. G__file_name(path, "", "", mapset);
  86. if (G_stat(path, &info) != 0)
  87. return -1;
  88. if (!S_ISDIR(info.st_mode))
  89. return -1;
  90. #ifndef __MINGW32__
  91. if (info.st_uid != getuid())
  92. return 0;
  93. if (info.st_uid != geteuid())
  94. return 0;
  95. #endif
  96. return 1;
  97. }
  98. /*!
  99. \brief Check for user mapset permission
  100. \param gisdbase full path to GISDBASE
  101. \param location location name
  102. \param mapset mapset name
  103. \return 1 mapset exists, and user has permission
  104. \return 0 mapset exists, BUT user denied permission
  105. \return -1 mapset does not exist
  106. */
  107. int G__mapset_permissions2(const char *gisdbase, const char *location,
  108. const char *mapset)
  109. {
  110. char path[GPATH_MAX];
  111. struct stat info;
  112. sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
  113. if (G_stat(path, &info) != 0)
  114. return -1;
  115. if (!S_ISDIR(info.st_mode))
  116. return -1;
  117. #ifndef SKIP_MAPSET_OWN_CHK
  118. #ifndef __MINGW32__
  119. if (info.st_uid != getuid())
  120. return 0;
  121. if (info.st_uid != geteuid())
  122. return 0;
  123. #endif
  124. #endif
  125. return 1;
  126. }