mapset_msc.c 3.1 KB

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