GRASS Programmer's Manual  6.4.2(2012)
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator 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 
99 int G__mapset_permissions(const char *mapset)
100 {
101  char path[GPATH_MAX];
102  struct stat info;
103 
104  G__file_name(path, "", "", mapset);
105 
106  if (G_stat(path, &info) != 0)
107  return -1;
108  if (!S_ISDIR(info.st_mode))
109  return -1;
110 
111 #ifndef __MINGW32__
112  if (info.st_uid != getuid())
113  return 0;
114  if (info.st_uid != geteuid())
115  return 0;
116 #endif
117 
118  return 1;
119 }
120 
132 int G__mapset_permissions2(const char *gisdbase, const char *location,
133  const char *mapset)
134 {
135  char path[GPATH_MAX];
136  struct stat info;
137 
138  sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
139 
140  if (G_stat(path, &info) != 0)
141  return -1;
142  if (!S_ISDIR(info.st_mode))
143  return -1;
144 
145 #ifndef __MINGW32__
146  if (info.st_uid != getuid())
147  return 0;
148  if (info.st_uid != geteuid())
149  return 0;
150 #endif
151 
152  return 1;
153 }