GRASS GIS 7 Programmer's Manual  7.0.2(2015)-r00000
remove.c
Go to the documentation of this file.
1 
14 #include <grass/config.h>
15 #include <stdio.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <stdlib.h>
19 #include <sys/types.h>
20 #include <sys/stat.h>
21 #include <dirent.h>
22 #include <grass/gis.h>
23 
24 static int recursive_remove(const char *path);
25 static int G__remove(int misc, const char *dir, const char *element,
26  const char *name);
27 
45 int G_remove(const char *element, const char *name)
46 {
47  return G__remove(0, NULL, element, name);
48 }
49 
66 int G_remove_misc(const char *dir, const char *element, const char *name)
67 {
68  return G__remove(1, dir, element, name);
69 }
70 
71 static int G__remove(int misc, const char *dir, const char *element,
72  const char *name)
73 {
74  char path[GPATH_MAX];
75  const char *mapset;
76  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
77 
78  /* name in mapset legal only if mapset is current mapset */
79  mapset = G_mapset();
80  if (G_name_is_fully_qualified(name, xname, xmapset)) {
81  if (strcmp(mapset, xmapset) != 0)
82  return -1;
83  name = xname;
84  }
85 
86  if (G_legal_filename(name) < 0)
87  return -1;
88 
89  if (misc)
90  G_file_name_misc(path, dir, element, name, mapset);
91  else
92  G_file_name(path, element, name, mapset);
93 
94  /* if file does not exist, return 0 */
95  if (access(path, 0) != 0)
96  return 0;
97 
98  if (recursive_remove(path) == 0)
99  return 1;
100 
101  return -1;
102 }
103 
104 /* equivalent to rm -rf path */
105 static int recursive_remove(const char *path)
106 {
107  DIR *dirp;
108  struct dirent *dp;
109  struct stat sb;
110  char path2[GPATH_MAX];
111 
112  if (G_lstat(path, &sb))
113  return 1;
114  if (!S_ISDIR(sb.st_mode))
115  return remove(path) == 0 ? 0 : 1;
116 
117  if ((dirp = opendir(path)) == NULL)
118  return 1;
119  while ((dp = readdir(dirp)) != NULL) {
120  if (dp->d_name[0] == '.')
121  continue;
122  if (strlen(path) + strlen(dp->d_name) + 2 > sizeof(path2))
123  continue;
124  sprintf(path2, "%s/%s", path, dp->d_name);
125  recursive_remove(path2);
126  }
127  closedir(dirp);
128 
129  return rmdir(path) == 0 ? 0 : 1;
130 }
const char * G_mapset(void)
Get current mapset name.
Definition: mapset.c:33
#define NULL
Definition: ccmath.h:32
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
Definition: paths.c:145
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_remove(const char *element, const char *name)
Remove a database file.
Definition: remove.c:45
Definition: path.h:16
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:36
int G_remove_misc(const char *dir, const char *element, const char *name)
Remove a database misc file.
Definition: remove.c:66
char * G_file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Definition: file_name.c:75
const char * name
Definition: named_colr.c:7