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