GRASS GIS 7 Programmer's Manual  7.5.svn(2017)-r71933
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
remove.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/remove.c
3  *
4  * \brief GIS Library - File remove functions.
5  *
6  * (C) 2001-2009 by the GRASS Development Team
7  *
8  * This program is free software under the GNU General Public License
9  * (>=v2). Read the file COPYING that comes with GRASS for details.
10  *
11  * \author Original author CERL
12  */
13 
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 G__remove(int misc, const char *dir, const char *element,
25  const char *name);
26 
27 /*!
28  * \brief Remove a database file.
29  *
30  * The file or directory <i>name</i> under the database <i>element</i>
31  * directory in the current mapset is removed.
32  *
33  * If <i>name</i> is a directory, everything within the directory is
34  * removed as well.
35  *
36  * \param element element name
37  * \param name file name
38  *
39  * \return 0 if <i>name</i> does not exist
40  * \return 1 if successful
41  * \return -1 on error
42  */
43 
44 int G_remove(const char *element, const char *name)
45 {
46  return G__remove(0, NULL, element, name);
47 }
48 
49 /*!
50  * \brief Remove a database misc file.
51  *
52  * The file or directory <i>name</i> under the database <i>element</i>
53  * directory in the current mapset is removed.
54  *
55  * If <i>name</i> is a directory, everything within the directory is
56  * removed as well.
57  *
58  * \param element element name
59  * \param name file name
60  *
61  * \return 0 if <i>name</i> does not exist
62  * \return 1 if successful
63  * \return -1 on error
64  */
65 int G_remove_misc(const char *dir, const char *element, const char *name)
66 {
67  return G__remove(1, dir, element, name);
68 }
69 
70 static int G__remove(int misc, const char *dir, const char *element,
71  const char *name)
72 {
73  char path[GPATH_MAX];
74  const char *mapset;
75  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
76 
77  /* name in mapset legal only if mapset is current mapset */
78  mapset = G_mapset();
79  if (G_name_is_fully_qualified(name, xname, xmapset)) {
80  if (strcmp(mapset, xmapset) != 0)
81  return -1;
82  name = xname;
83  }
84 
85  if (G_legal_filename(name) < 0)
86  return -1;
87 
88  if (misc)
89  G_file_name_misc(path, dir, element, name, mapset);
90  else
91  G_file_name(path, element, name, mapset);
92 
93  /* if file does not exist, return 0 */
94  if (access(path, 0) != 0)
95  return 0;
96 
97  if (G_recursive_remove(path) == 0)
98  return 1;
99 
100  return -1;
101 }
102 
103 /*!
104  \brief Recursively remove all files in given directory
105 
106  Equivalent to rm -rf path.
107 
108  \param path path to the directory which should be removed
109 
110  \return 0 on success
111  \return -1 on error
112 */
113 int G_recursive_remove(const char *path)
114 {
115  DIR *dirp;
116  struct dirent *dp;
117  struct stat sb;
118  char path2[GPATH_MAX];
119 
120  if (G_lstat(path, &sb))
121  return -1;
122  if (!S_ISDIR(sb.st_mode))
123  return remove(path) == 0 ? 0 : -1;
124 
125  if ((dirp = opendir(path)) == NULL)
126  return -1;
127  while ((dp = readdir(dirp)) != NULL) {
128  if (dp->d_name[0] == '.')
129  continue;
130  if (strlen(path) + strlen(dp->d_name) + 2 > sizeof(path2))
131  continue;
132  sprintf(path2, "%s/%s", path, dp->d_name);
133  G_recursive_remove(path2);
134  }
135  closedir(dirp);
136 
137  return rmdir(path) == 0 ? 0 : -1;
138 }
DIR * opendir()
#define GMAPSET_MAX
Definition: gis.h:149
#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
dir_entry * readdir()
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:38
#define GPATH_MAX
Definition: gis.h:151
int G_recursive_remove(const char *path)
Recursively remove all files in given directory.
Definition: remove.c:113
int G_remove(const char *element, const char *name)
Remove a database file.
Definition: remove.c:44
#define GNAME_MAX
Definition: gis.h:148
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
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
int G_remove_misc(const char *dir, const char *element, const char *name)
Remove a database misc file.
Definition: remove.c:65
char * G_file_name_misc(char *path, const char *dir, const char *element, const char *name, const char *mapset)
Builds full path names to GIS misc data files.
Definition: file_name.c:55
const char * name
Definition: named_colr.c:7