GRASS Programmer's Manual 6.4.1(2011)
|
00001 00012 #include <string.h> 00013 #include <unistd.h> 00014 #include <stdlib.h> 00015 #include <errno.h> 00016 #include <sys/types.h> 00017 #include <sys/stat.h> 00018 #include <grass/gis.h> 00019 #include <grass/glocale.h> 00020 00034 int G__make_mapset_element(const char *p_element) 00035 { 00036 char path[GPATH_MAX]; 00037 char *p; 00038 const char *element; 00039 00040 element = p_element; 00041 if (*element == 0) 00042 return 0; 00043 00044 G__file_name(p = path, "", "", G_mapset()); 00045 while (*p) 00046 p++; 00047 /* add trailing slash if missing */ 00048 --p; 00049 if (*p++ != '/') { 00050 *p++ = '/'; 00051 *p = 0; 00052 } 00053 00054 /* now append element, one directory at a time, to path */ 00055 while (1) { 00056 if (*element == '/' || *element == 0) { 00057 *p = 0; 00058 if (access(path, 0) != 0) { /* directory not yet created */ 00059 if (G_mkdir(path) != 0) 00060 G_fatal_error(_("Unable to make mapset element %s (%s): %s"), 00061 p_element, path, strerror(errno)); 00062 } 00063 if (access(path, 0) != 0) /* directory not accessible */ 00064 G_fatal_error(_("Unable to access mapset element %s (%s): %s"), 00065 p_element, path, strerror(errno)); 00066 if (*element == 0) 00067 return 1; 00068 } 00069 *p++ = *element++; 00070 } 00071 } 00072 00082 int G__make_mapset_element_misc(const char *dir, const char *name) 00083 { 00084 char buf[GNAME_MAX * 2 + 1]; 00085 00086 sprintf(buf, "%s/%s", dir, name); 00087 return G__make_mapset_element(buf); 00088 } 00089 00099 int G__mapset_permissions(const char *mapset) 00100 { 00101 char path[GPATH_MAX]; 00102 struct stat info; 00103 00104 G__file_name(path, "", "", mapset); 00105 00106 if (G_stat(path, &info) != 0) 00107 return -1; 00108 if (!S_ISDIR(info.st_mode)) 00109 return -1; 00110 00111 #ifndef __MINGW32__ 00112 if (info.st_uid != getuid()) 00113 return 0; 00114 if (info.st_uid != geteuid()) 00115 return 0; 00116 #endif 00117 00118 return 1; 00119 } 00120 00132 int G__mapset_permissions2(const char *gisdbase, const char *location, 00133 const char *mapset) 00134 { 00135 char path[GPATH_MAX]; 00136 struct stat info; 00137 00138 sprintf(path, "%s/%s/%s", gisdbase, location, mapset); 00139 00140 if (G_stat(path, &info) != 0) 00141 return -1; 00142 if (!S_ISDIR(info.st_mode)) 00143 return -1; 00144 00145 #ifndef __MINGW32__ 00146 if (info.st_uid != getuid()) 00147 return 0; 00148 if (info.st_uid != geteuid()) 00149 return 0; 00150 #endif 00151 00152 return 1; 00153 }