GRASS Programmer's Manual  6.5.svn(2014)-r66266
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
remove.c
Go to the documentation of this file.
1 
17 #include <grass/config.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <unistd.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <dirent.h>
25 #include <grass/gis.h>
26 
27 static int recursive_remove(const char *path);
28 static int G__remove(int misc, const char *dir, const char *element,
29  const char *name);
30 
47 int G_remove(const char *element, const char *name)
48 {
49  return G__remove(0, NULL, element, name);
50 }
51 
67 int G_remove_misc(const char *dir, const char *element, const char *name)
68 {
69  return G__remove(1, dir, element, name);
70 }
71 
72 static int G__remove(int misc, const char *dir, const char *element,
73  const char *name)
74 {
75  char path[GPATH_MAX];
76  char *mapset;
77  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
78 
79  /* name in mapset legal only if mapset is current mapset */
80  mapset = G_mapset();
81  if (G__name_is_fully_qualified(name, xname, xmapset)) {
82  if (strcmp(mapset, xmapset) != 0)
83  return -1;
84  name = xname;
85  }
86 
87  if (G_legal_filename(name) < 0)
88  return -1;
89 
90  if (misc)
91  G__file_name_misc(path, dir, element, name, mapset);
92  else
93  G__file_name(path, element, name, mapset);
94 
95  /* if file does not exist, return 0 */
96  if (access(path, 0) != 0)
97  return 0;
98 
99  if (recursive_remove(path) == 0)
100  return 1;
101 
102  return -1;
103 }
104 
105 /* equivalent to rm -rf path */
106 static int recursive_remove(const char *path)
107 {
108  DIR *dirp;
109  struct dirent *dp;
110  struct stat sb;
111  char path2[GPATH_MAX];
112 
113  if (G_lstat(path, &sb))
114  return 1;
115  if (!S_ISDIR(sb.st_mode))
116  return remove(path) == 0 ? 0 : 1;
117 
118  if ((dirp = opendir(path)) == NULL)
119  return 1;
120  while ((dp = readdir(dirp)) != NULL) {
121  if (dp->d_name[0] == '.')
122  continue;
123  if (strlen(path) + strlen(dp->d_name) + 2 > sizeof(path2))
124  continue;
125  sprintf(path2, "%s/%s", path, dp->d_name);
126  recursive_remove(path2);
127  }
128  closedir(dirp);
129 
130  return rmdir(path) == 0 ? 0 : 1;
131 }
char * G_mapset(void)
current mapset name
Definition: mapset.c:31
sprintf(buf2,"%s", G3D_CATS_ELEMENT)
DIR * opendir()
char xmapset[512]
Definition: g3dcats.c:89
char * G__file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Definition: file_name.c:70
string name
Definition: render.py:1314
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
Definition: paths.c:135
dir_entry * readdir()
int stat
Definition: g3dcolor.c:369
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_remove(const char *element, const char *name)
Remove a database file.
Definition: remove.c:47
return NULL
Definition: dbfopen.c:1394
int G_remove_misc(const char *dir, const char *element, const char *name)
Remove a database misc file.
Definition: remove.c:67
char xname[512]
Definition: g3dcats.c:89
int G__name_is_fully_qualified(const char *fullname, char *name, char *mapset)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:57