Loading...
Searching...
No Matches
remove.c
Go to the documentation of this file.
/*!
* \file lib/gis/remove.c
*
* \brief GIS Library - File remove functions.
*
* (C) 2001-2009 by the GRASS Development Team
*
* This program is free software under the GNU General Public License
* (>=v2). Read the file COPYING that comes with GRASS for details.
*
* \author Original author CERL
*/
#include <grass/config.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <grass/gis.h>
static int G__remove(int misc, const char *dir, const char *element,
const char *name);
/*!
* \brief Remove a database file.
*
* The file or directory <i>name</i> under the database <i>element</i>
* directory in the current mapset is removed.
*
* If <i>name</i> is a directory, everything within the directory is
* removed as well.
*
* \param element element name
* \param name file name
*
* \return 0 if <i>name</i> does not exist
* \return 1 if successful
* \return -1 on error
*/
int G_remove(const char *element, const char *name)
{
return G__remove(0, NULL, element, name);
}
/*!
* \brief Remove a database misc file.
*
* The file or directory <i>name</i> under the database <i>element</i>
* directory in the current mapset is removed.
*
* If <i>name</i> is a directory, everything within the directory is
* removed as well.
*
* \param element element name
* \param name file name
*
* \return 0 if <i>name</i> does not exist
* \return 1 if successful
* \return -1 on error
*/
int G_remove_misc(const char *dir, const char *element, const char *name)
{
return G__remove(1, dir, element, name);
}
static int G__remove(int misc, const char *dir, const char *element,
const char *name)
{
char path[GPATH_MAX];
const char *mapset;
char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
/* name in mapset legal only if mapset is current mapset */
mapset = G_mapset();
if (G_name_is_fully_qualified(name, xname, xmapset)) {
if (strcmp(mapset, xmapset) != 0)
return -1;
name = xname;
}
if (G_legal_filename(name) < 0)
return -1;
if (misc)
G_file_name_misc(path, dir, element, name, mapset);
else
G_file_name(path, element, name, mapset);
/* if file does not exist, return 0 */
if (access(path, 0) != 0)
return 0;
if (G_recursive_remove(path) == 0)
return 1;
return -1;
}
/*!
\brief Recursively remove all files in given directory
Equivalent to rm -rf path.
\param path path to the directory which should be removed
\return 0 on success
\return -1 on error
*/
int G_recursive_remove(const char *path)
{
DIR *dirp;
struct dirent *dp;
struct stat sb;
char path2[GPATH_MAX];
if (G_lstat(path, &sb))
return -1;
if (!S_ISDIR(sb.st_mode))
return remove(path) == 0 ? 0 : -1;
if ((dirp = opendir(path)) == NULL)
return -1;
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_name[0] == '.')
continue;
if (strlen(path) + strlen(dp->d_name) + 2 > sizeof(path2))
continue;
snprintf(path2, sizeof(path2), "%s/%s", path, dp->d_name);
G_recursive_remove(path2);
}
closedir(dirp);
return rmdir(path) == 0 ? 0 : -1;
}
NULL
#define NULL
Definition ccmath.h:32
G_file_name
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:61
G_file_name_misc
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:101
G_legal_filename
int G_legal_filename(const char *s)
Check for legal database file name.
Definition legal_name.c:34
G_mapset
const char * G_mapset(void)
Get current mapset name.
Definition mapset.c:33
name
const char * name
Definition named_colr.c:6
G_name_is_fully_qualified
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
G_lstat
int G_lstat(const char *file_name, struct stat *buf)
Get file status.
Definition paths.c:145
G_remove
int G_remove(const char *element, const char *name)
Remove a database file.
Definition remove.c:43
G_recursive_remove
int G_recursive_remove(const char *path)
Recursively remove all files in given directory.
Definition remove.c:112
G_remove_misc
int G_remove_misc(const char *dir, const char *element, const char *name)
Remove a database misc file.
Definition remove.c:64
element
Definition lidar.h:85
path
Definition path.h:15