GRASS Programmer's Manual  6.4.3(2013)-r
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Macros Pages
mapset_msc.c
Go to the documentation of this file.
1 
12 #include <string.h>
13 #include <unistd.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <sys/types.h>
17 #include <sys/stat.h>
18 #include <grass/gis.h>
19 #include <grass/glocale.h>
20 
34 int G__make_mapset_element(const char *p_element)
35 {
36  char path[GPATH_MAX];
37  char *p;
38  const char *element;
39 
40  element = p_element;
41  if (*element == 0)
42  return 0;
43 
44  G__file_name(p = path, "", "", G_mapset());
45  while (*p)
46  p++;
47  /* add trailing slash if missing */
48  --p;
49  if (*p++ != '/') {
50  *p++ = '/';
51  *p = 0;
52  }
53 
54  /* now append element, one directory at a time, to path */
55  while (1) {
56  if (*element == '/' || *element == 0) {
57  *p = 0;
58  if (access(path, 0) != 0) { /* directory not yet created */
59  if (G_mkdir(path) != 0)
60  G_fatal_error(_("Unable to make mapset element %s (%s): %s"),
61  p_element, path, strerror(errno));
62  }
63  if (access(path, 0) != 0) /* directory not accessible */
64  G_fatal_error(_("Unable to access mapset element %s (%s): %s"),
65  p_element, path, strerror(errno));
66  if (*element == 0)
67  return 1;
68  }
69  *p++ = *element++;
70  }
71 }
72 
82 int G__make_mapset_element_misc(const char *dir, const char *name)
83 {
84  char buf[GNAME_MAX * 2 + 1];
85 
86  sprintf(buf, "%s/%s", dir, name);
87  return G__make_mapset_element(buf);
88 }
89 
90 static int check_owner(const struct stat *info)
91 {
92 #if defined(__MINGW32__) || defined(SKIP_MAPSET_OWN_CHK)
93  return 1;
94 #else
95  const char *check = getenv("GRASS_SKIP_MAPSET_OWNER_CHECK");
96  if (check && *check)
97  return 1;
98  if (info->st_uid != getuid())
99  return 0;
100  if (info->st_uid != geteuid())
101  return 0;
102  return 1;
103 #endif
104 }
105 
115 int G__mapset_permissions(const char *mapset)
116 {
117  char path[GPATH_MAX];
118  struct stat info;
119 
120  G__file_name(path, "", "", mapset);
121 
122  if (G_stat(path, &info) != 0)
123  return -1;
124  if (!S_ISDIR(info.st_mode))
125  return -1;
126 
127  if (!check_owner(&info))
128  return 0;
129 
130  return 1;
131 }
132 
144 int G__mapset_permissions2(const char *gisdbase, const char *location,
145  const char *mapset)
146 {
147  char path[GPATH_MAX];
148  struct stat info;
149 
150  sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
151 
152  if (G_stat(path, &info) != 0)
153  return -1;
154  if (!S_ISDIR(info.st_mode))
155  return -1;
156 
157  if (!check_owner(&info))
158  return 0;
159 
160  return 1;
161 }