GRASS GIS 7 Programmer's Manual  7.0.5(2016)-r00000
paths.c
Go to the documentation of this file.
1 #include <grass/config.h>
2 #include <sys/types.h>
3 #include <sys/stat.h>
4 #include <unistd.h>
5 
6 #ifndef __MINGW32__
7 #include <pwd.h>
8 #else
9 #include <windows.h>
10 #include "aclapi.h"
11 #endif
12 
13 #include <grass/gis.h>
14 #include <grass/glocale.h>
15 
27 int G_mkdir(const char *path)
28 {
29 #ifdef __MINGW32__
30  return mkdir(path);
31 #else
32  return mkdir(path, 0777);
33 #endif
34 }
35 
45 int G_is_dirsep(char c)
46 {
47  if (c == GRASS_DIRSEP || c == HOST_DIRSEP)
48  return 1;
49  else
50  return 0;
51 }
52 
62 int G_is_absolute_path(const char *path)
63 {
64  if (G_is_dirsep(path[0])
65 #ifdef __MINGW32__
66  || (isalpha(path[0]) && (path[1] == ':') && G_is_dirsep(path[2]))
67 #endif
68  )
69  return 1;
70  else
71  return 0;
72 }
73 
84 {
85  char *i;
86 
87  for (i = path; *i; i++) {
88  if (*i == GRASS_DIRSEP)
89  *i = HOST_DIRSEP;
90  }
91 
92  return path;
93 }
94 
106 {
107  char *i;
108 
109  for (i = path; *i; i++) {
110  if (*i == HOST_DIRSEP)
111  *i = GRASS_DIRSEP;
112  }
113 
114  return path;
115 }
116 
128 int G_stat(const char *file_name, struct stat *buf)
129 {
130  return stat(file_name, buf);
131 }
132 
145 int G_lstat(const char *file_name, struct stat *buf)
146 {
147 #ifdef __MINGW32__
148  return stat(file_name, buf);
149 #else
150  return lstat(file_name, buf);
151 #endif
152 }
153 
164 int G_owner(const char *path)
165 {
166 
167 #ifndef __MINGW32__
168  struct stat info;
169 
170  G_stat(path, &info);
171 
172  return (int)info.st_uid;
173 #else
174 
175  /* this code is taken from the official example to
176  * find the owner of a file object from
177  * http://msdn.microsoft.com/en-us/library/windows/desktop/aa446629%28v=vs.85%29.aspx */
178 
179  DWORD dwRtnCode = 0;
180  PSID pSidOwner = NULL;
181  BOOL bRtnBool = TRUE;
182  LPTSTR AcctName = NULL;
183  LPTSTR DomainName = NULL;
184  DWORD dwAcctName = 1, dwDomainName = 1;
185  SID_NAME_USE eUse = SidTypeUnknown;
186  HANDLE hFile;
187  PSECURITY_DESCRIPTOR pSD = NULL;
188 
189  /* Get the handle of the file object. */
190  hFile = CreateFile(
191  TEXT(path), /* lpFileName */
192  GENERIC_READ, /* dwDesiredAccess */
193  FILE_SHARE_READ, /* dwShareMode */
194  NULL, /* lpSecurityAttributes */
195  OPEN_EXISTING, /* dwCreationDisposition */
196  FILE_ATTRIBUTE_NORMAL, /* dwFlagsAndAttributes */
197  NULL /* hTemplateFile */
198  );
199 
200  if (hFile == INVALID_HANDLE_VALUE) {
201  G_fatal_error(_("Unable to open file <%s> for reading"), path);
202  }
203 
204  /* Get the owner SID of the file. */
205  dwRtnCode = GetSecurityInfo(
206  hFile, /* handle */
207  SE_FILE_OBJECT, /* ObjectType */
208  OWNER_SECURITY_INFORMATION, /* SecurityInfo */
209  &pSidOwner, /* ppsidOwner */
210  NULL, /* ppsidGroup */
211  NULL, /* ppDacl */
212  NULL, /* ppSacl */
213  &pSD /* ppSecurityDescriptor */
214  );
215 
216  if (dwRtnCode != ERROR_SUCCESS) {
217  G_fatal_error(_("Unable to fetch security info for <%s>"), path);
218  }
219  CloseHandle(hFile);
220 
221  return (int)pSidOwner;
222 #endif
223 }
int G_stat(const char *file_name, struct stat *buf)
Get file status.
Definition: paths.c:128
int G_owner(const char *path)
Get owner id of path.
Definition: paths.c:164
int G_mkdir(const char *path)
Creates a new directory.
Definition: paths.c:27
char * G_convert_dirseps_to_host(char *path)
Converts directory separator characters in a string to the native host separator character (/ on Unix...
Definition: paths.c:83
#define NULL
Definition: ccmath.h:32
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
Definition: paths.c:145
void G_fatal_error(const char *msg,...)
Print a fatal error message to stderr.
Definition: gis/error.c:159
#define TRUE
Definition: dbfopen.c:118
Definition: path.h:16
int G_is_absolute_path(const char *path)
Checks if a specified path looks like an absolute path on the host system.
Definition: paths.c:62
int G_is_dirsep(char c)
Checks if a specified character is a valid directory separator character on the host system...
Definition: paths.c:45
char * G_convert_dirseps_from_host(char *path)
Converts directory separator characters in a string from the native host character to the GRASS separ...
Definition: paths.c:105