-
-
Notifications
You must be signed in to change notification settings - Fork 288
/
mapset_msc.c
191 lines (152 loc) · 4.25 KB
/
mapset_msc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*!
\file lib/gis/mapset_msc.c
\brief GIS library - Mapset user permission routines.
(C) 1999-2014 The GRASS development team
This program is free software under the GNU General Public License
(>=v2). Read the file COPYING that comes with GRASS for details.
*/
#include <grass/config.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <grass/gis.h>
#include <grass/glocale.h>
static int make_mapset_element(const char *, const char *);
/*!
\brief Create element in the current mapset.
Make the specified element in the current mapset will check for the
existence of the element and do nothing if it is found so this
routine can be called even if the element already exists.
Calls G_fatal_error() on failure.
\param p_element element to be created in mapset
\return 0 no element defined
\return 1 on success
*/
int G_make_mapset_element(const char *p_element)
{
char path[GPATH_MAX];
G_file_name(path, NULL, NULL, G_mapset());
return make_mapset_element(path, p_element);
}
/*!
\brief Create element in the temporary directory.
See G_file_name_tmp() for details.
\param p_element element to be created in mapset
\return 0 no element defined
\return 1 on success
*/
int G_make_mapset_element_tmp(const char *p_element)
{
char path[GPATH_MAX];
G_file_name_tmp(path, NULL, NULL, G_mapset());
return make_mapset_element(path, p_element);
}
int make_mapset_element(const char *p_path, const char *p_element)
{
char path[GPATH_MAX], *p;
const char *element;
element = p_element;
if (*element == 0)
return 0;
strncpy(path, p_path, GPATH_MAX);
p = path;
while (*p)
p++;
/* add trailing slash if missing */
--p;
if (*p++ != '/') {
*p++ = '/';
*p = 0;
}
/* now append element, one directory at a time, to path */
while (1) {
if (*element == '/' || *element == 0) {
*p = 0;
if (access(path, 0) != 0) { /* directory not yet created */
if (G_mkdir(path) != 0)
G_fatal_error(_("Unable to make mapset element %s (%s): %s"),
p_element, path, strerror(errno));
}
if (access(path, 0) != 0) /* directory not accessible */
G_fatal_error(_("Unable to access mapset element %s (%s): %s"),
p_element, path, strerror(errno));
if (*element == 0)
return 1;
}
*p++ = *element++;
}
}
/*!
\brief Create misc element in the current mapset.
\param dir directory path
\param name element to be created in mapset
\return 0 no element defined
\return 1 on success
*/
int G__make_mapset_element_misc(const char *dir, const char *name)
{
char buf[GNAME_MAX * 2 + 1];
sprintf(buf, "%s/%s", dir, name);
return G_make_mapset_element(buf);
}
static int check_owner(const struct stat *info)
{
#if defined(__MINGW32__) || defined(SKIP_MAPSET_OWN_CHK)
return 1;
#else
const char *check = getenv("GRASS_SKIP_MAPSET_OWNER_CHECK");
if (check && *check)
return 1;
if (info->st_uid != getuid())
return 0;
if (info->st_uid != geteuid())
return 0;
return 1;
#endif
}
/*!
\brief Check for user mapset permission
\param mapset mapset name
\return 1 mapset exists, and user has permission
\return 0 mapset exists, BUT user denied permission
\return -1 mapset does not exist
*/
int G_mapset_permissions(const char *mapset)
{
char path[GPATH_MAX];
struct stat info;
G_file_name(path, "", "", mapset);
if (G_stat(path, &info) != 0)
return -1;
if (!S_ISDIR(info.st_mode))
return -1;
if (!check_owner(&info))
return 0;
return 1;
}
/*!
\brief Check for user mapset permission
\param gisdbase full path to GISDBASE
\param location location name
\param mapset mapset name
\return 1 mapset exists, and user has permission
\return 0 mapset exists, BUT user denied permission
\return -1 mapset does not exist
*/
int G_mapset_permissions2(const char *gisdbase, const char *location,
const char *mapset)
{
char path[GPATH_MAX];
struct stat info;
sprintf(path, "%s/%s/%s", gisdbase, location, mapset);
if (G_stat(path, &info) != 0)
return -1;
if (!S_ISDIR(info.st_mode))
return -1;
if (!check_owner(&info))
return 0;
return 1;
}