GRASS GIS 7 Programmer's Manual  7.0.3(2016)-r00000
mapset_msc.c
Go to the documentation of this file.
1 
12 #include <grass/config.h>
13 #include <string.h>
14 #include <unistd.h>
15 #include <stdlib.h>
16 #include <errno.h>
17 #include <sys/types.h>
18 #include <sys/stat.h>
19 #include <grass/gis.h>
20 #include <grass/glocale.h>
21 
35 int G_make_mapset_element(const char *p_element)
36 {
37  char path[GPATH_MAX];
38  char *p;
39  const char *element;
40 
41  element = p_element;
42  if (*element == 0)
43  return 0;
44 
45  G_file_name(p = path, NULL, NULL, G_mapset());
46  while (*p)
47  p++;
48  /* add trailing slash if missing */
49  --p;
50  if (*p++ != '/') {
51  *p++ = '/';
52  *p = 0;
53  }
54 
55  /* now append element, one directory at a time, to path */
56  while (1) {
57  if (*element == '/' || *element == 0) {
58  *p = 0;
59  if (access(path, 0) != 0) { /* directory not yet created */
60  if (G_mkdir(path) != 0)
61  G_fatal_error(_("Unable to make mapset element %s (%s): %s"),
62  p_element, path, strerror(errno));
63  }
64  if (access(path, 0) != 0) /* directory not accessible */
65  G_fatal_error(_("Unable to access mapset element %s (%s): %s"),
66  p_element, path, strerror(errno));
67  if (*element == 0)
68  return 1;
69  }
70  *p++ = *element++;
71  }
72 }
73 
83 int G__make_mapset_element_misc(const char *dir, const char *name)
84 {
85  char buf[GNAME_MAX * 2 + 1];
86 
87  sprintf(buf, "%s/%s", dir, name);
88  return G_make_mapset_element(buf);
89 }
90 
91 static int check_owner(const struct stat *info)
92 {
93 #if defined(__MINGW32__) || defined(SKIP_MAPSET_OWN_CHK)
94  return 1;
95 #else
96  const char *check = getenv("GRASS_SKIP_MAPSET_OWNER_CHECK");
97  if (check && *check)
98  return 1;
99  if (info->st_uid != getuid())
100  return 0;
101  if (info->st_uid != geteuid())
102  return 0;
103  return 1;
104 #endif
105 }
106 
116 int G_mapset_permissions(const char *mapset)
117 {
118  char path[GPATH_MAX];
119  struct stat info;
120 
121  G_file_name(path, "", "", mapset);
122 
123  if (G_stat(path, &info) != 0)
124  return -1;
125  if (!S_ISDIR(info.st_mode))
126  return -1;
127 
128  if (!check_owner(&info))
129  return 0;
130 
131  return 1;
132 }
133 
145 int G_mapset_permissions2(const char *gisdbase, const char *location,
146  const char *mapset)
147 {
148  char path[GPATH_MAX];
149  struct stat info;
150 
151  sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
152 
153  if (G_stat(path, &info) != 0)
154  return -1;
155  if (!S_ISDIR(info.st_mode))
156  return -1;
157 
158  if (!check_owner(&info))
159  return 0;
160 
161  return 1;
162 }
int G_make_mapset_element(const char *p_element)
Create element in the current mapset.
Definition: mapset_msc.c:35
int G_stat(const char *file_name, struct stat *buf)
Get file status.
Definition: paths.c:128
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:27
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
int G_mapset_permissions(const char *mapset)
Check for user mapset permission.
Definition: mapset_msc.c:116
#define NULL
Definition: ccmath.h:32
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
Definition: lidar.h:89
char * G_file_name(char *path, const char *element, const char *name, const char *mapset)
Builds full path names to GIS data files.
Definition: file_name.c:33
int G_mapset_permissions2(const char *gisdbase, const char *location, const char *mapset)
Check for user mapset permission.
Definition: mapset_msc.c:145
int G__make_mapset_element_misc(const char *dir, const char *name)
Create misc element in the current mapset.
Definition: mapset_msc.c:83
Definition: path.h:16
const char * name
Definition: named_colr.c:7