GRASS GIS 8 Programmer's Manual  8.5.0dev(2024)-36359e2344
rename.c
Go to the documentation of this file.
1 /*!
2  * \file lib/gis/rename.c
3  *
4  * \brief GIS Library - Rename file functions.
5  *
6  * (C) 2001-2015 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 <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <unistd.h>
18 #include <grass/gis.h>
19 
20 /*!
21  \brief Rename a file or a directory in the filesystem.
22 
23  The file or directory <i>oldname</i> is renamed to <i>newname</i>.
24 
25  \param oldname current name
26  \param newname new name
27 
28  \return 0 if successful
29  \return -1 on error
30  */
31 int G_rename_file(const char *oldname, const char *newname)
32 {
33  int ret;
34 
35 #ifdef __MINGW32__
36  remove(newname);
37 #endif
38 
39  ret = rename(oldname, newname);
40 
41  if (ret == -1) {
42  /* if fails, try to copy file and then remove */
43  if (1 == G_copy_file(oldname, newname)) {
44  if (remove(oldname) != -1)
45  ret = 0;
46  }
47  }
48 
49  return ret;
50 }
51 
52 /*!
53  \brief Rename a database file.
54 
55  The file or directory <i>oldname</i> under the database <i>element</i>
56  directory in the current mapset is renamed to <i>newname</i>.
57 
58  \bug This routine does not check to see if the <i>newname</i>
59  name is a valid database file name.
60 
61  \param element element name
62  \param oldname current name
63  \param newname new name
64 
65  \return 0 if <i>oldname</i> does not exist
66  \return 1 if successful
67  \return -1 on error
68  */
69 int G_rename(const char *element, const char *oldname, const char *newname)
70 {
71  const char *mapset;
72  char xname[GNAME_MAX], xmapset[GMAPSET_MAX];
73  char from[GPATH_MAX], to[GPATH_MAX];
74 
75  /* name in mapset legal only if mapset is current mapset */
76  mapset = G_mapset();
77  if (G_name_is_fully_qualified(oldname, xname, xmapset) &&
78  strcmp(mapset, xmapset))
79  return -1;
80  if (G_name_is_fully_qualified(newname, xname, xmapset) &&
81  strcmp(mapset, xmapset))
82  return -1;
83 
84  /* if file does not exist return 0 */
85  if (access(G_file_name(from, element, oldname, mapset), 0) != 0)
86  return 0;
87 
88  G_file_name(to, element, newname, mapset);
89 
90  /* return result of rename */
91  return G_rename_file(from, to) == 0 ? 1 : -1;
92 }
int G_name_is_fully_qualified(const char *, char *, char *)
Check if map name is fully qualified (map @ mapset)
Definition: nme_in_mps.c:36
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition: file_name.c:61
const char * G_mapset(void)
Get current mapset name.
Definition: gis/mapset.c:33
int G_copy_file(const char *, const char *)
Copies one file to another.
Definition: copy_file.c:33
#define GMAPSET_MAX
Definition: gis.h:192
#define GPATH_MAX
Definition: gis.h:194
#define GNAME_MAX
Definition: gis.h:191
int G_rename_file(const char *oldname, const char *newname)
Rename a file or a directory in the filesystem.
Definition: rename.c:31
int G_rename(const char *element, const char *oldname, const char *newname)
Rename a database file.
Definition: rename.c:69
Definition: lidar.h:85