GRASS 8 Programmer's Manual 8.6.0dev(2026)-1878fdfec5
Loading...
Searching...
No Matches
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 * SPDX-FileCopyrightText: 2001-2009 GRASS Development Team
7 * SPDX-License-Identifier: GPL-2.0-or-later
8 *
9 * \author Original author CERL
10 */
11
12#include <grass/config.h>
13#include <stdio.h>
14#include <string.h>
15#include <unistd.h>
16#include <stdlib.h>
17#include <sys/types.h>
18#include <sys/stat.h>
19#include <dirent.h>
20#include <grass/gis.h>
21
22static int G__remove(int misc, const char *dir, const char *element,
23 const char *name);
24
25/*!
26 * \brief Remove a database file.
27 *
28 * The file or directory <i>name</i> under the database <i>element</i>
29 * directory in the current mapset is removed.
30 *
31 * If <i>name</i> is a directory, everything within the directory is
32 * removed as well.
33 *
34 * \param element element name
35 * \param name file name
36 *
37 * \return 0 if <i>name</i> does not exist
38 * \return 1 if successful
39 * \return -1 on error
40 */
41int G_remove(const char *element, const char *name)
42{
43 return G__remove(0, NULL, element, name);
44}
45
46/*!
47 * \brief Remove a database misc file.
48 *
49 * The file or directory <i>name</i> under the database <i>element</i>
50 * directory in the current mapset is removed.
51 *
52 * If <i>name</i> is a directory, everything within the directory is
53 * removed as well.
54 *
55 * \param element element name
56 * \param name file name
57 *
58 * \return 0 if <i>name</i> does not exist
59 * \return 1 if successful
60 * \return -1 on error
61 */
62int G_remove_misc(const char *dir, const char *element, const char *name)
63{
64 return G__remove(1, dir, element, name);
65}
66
67static int G__remove(int misc, const char *dir, const char *element,
68 const char *name)
69{
70 char path[GPATH_MAX];
71 const char *mapset;
73
74 /* name in mapset legal only if mapset is current mapset */
75 mapset = G_mapset();
77 if (strcmp(mapset, xmapset) != 0)
78 return -1;
79 name = xname;
80 }
81
82 if (G_legal_filename(name) < 0)
83 return -1;
84
85 if (misc)
86 G_file_name_misc(path, dir, element, name, mapset);
87 else
88 G_file_name(path, element, name, mapset);
89
90 /* if file does not exist, return 0 */
91 if (access(path, 0) != 0)
92 return 0;
93
94 if (G_recursive_remove(path) == 0)
95 return 1;
96
97 return -1;
98}
99
100/*!
101 \brief Recursively remove all files in given directory
102
103 Equivalent to rm -rf path.
104
105 \param path path to the directory which should be removed
106
107 \return 0 on success
108 \return -1 on error
109 */
110int G_recursive_remove(const char *path)
111{
112 DIR *dirp;
113 struct dirent *dp;
114 struct stat sb;
115 char path2[GPATH_MAX];
116
117 if (G_lstat(path, &sb))
118 return -1;
119 if (!S_ISDIR(sb.st_mode))
120 return remove(path) == 0 ? 0 : -1;
121
122 if ((dirp = opendir(path)) == NULL)
123 return -1;
124 while ((dp = readdir(dirp)) != NULL) {
125 if (dp->d_name[0] == '.')
126 continue;
127 if (strlen(path) + strlen(dp->d_name) + 2 > sizeof(path2))
128 continue;
129 snprintf(path2, sizeof(path2), "%s/%s", path, dp->d_name);
130 G_recursive_remove(path2);
131 }
132 closedir(dirp);
133
134 return rmdir(path) == 0 ? 0 : -1;
135}
#define NULL
Definition ccmath.h:32
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition nme_in_mps.c:34
int G_legal_filename(const char *)
Check for legal database file name.
Definition legal_name.c:32
char * G_file_name_misc(char *, const char *, const char *, const char *, const char *)
Builds full path names to GIS misc data files.
Definition file_name.c:99
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:59
int G_lstat(const char *, struct stat *)
Get file status.
Definition paths.c:145
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
struct DIR DIR
Definition dirent.h:18
#define GMAPSET_MAX
Definition gis.h:194
#define GPATH_MAX
Definition gis.h:196
#define GNAME_MAX
Definition gis.h:193
DIR * opendir(const char *name)
Definition msvc/dirent.c:15
struct dirent * readdir(DIR *dir)
Definition msvc/dirent.c:75
int closedir(DIR *dir)
Definition msvc/dirent.c:54
const char * name
Definition named_colr.c:6
int G_remove(const char *element, const char *name)
Remove a database file.
Definition remove.c:41
int G_recursive_remove(const char *path)
Recursively remove all files in given directory.
Definition remove.c:110
int G_remove_misc(const char *dir, const char *element, const char *name)
Remove a database misc file.
Definition remove.c:62
#define S_ISDIR(mode)
Definition stat.h:6
Definition path.h:15
#define access
Definition unistd.h:7
#define rmdir
Definition unistd.h:15