GRASS 8 Programmer's Manual 8.6.0dev(2026)-4bb960b182
Loading...
Searching...
No Matches
do_copy.c
Go to the documentation of this file.
1/*!
2 \file lib/manage/do_copy.c
3
4 \brief Manage Library - Copy element
5
6 SPDX-FileCopyrightText: 2001-2011 GRASS Development Team
7 SPDX-License-Identifier: GPL-2.0-or-later
8
9 \author Original author CERL
10 */
11
12#include <stdio.h>
13#include <string.h>
14#include <unistd.h>
15
16#include <grass/gis.h>
17#include <grass/glocale.h>
18#include <grass/vector.h>
19
20#include "manage_local_proto.h"
21
22/*!
23 \brief Copy element
24
25 \param n element id
26 \param old source name
27 \param mapset name of source mapset
28 \param new destination name
29
30 \return 0 on success
31 \return 1 on error
32 */
33int M_do_copy(int n, const char *old, const char *mapset, const char *new)
34{
35 int i, ret;
36 char path[GPATH_MAX], path2[GPATH_MAX];
37 int result = 0;
38 char *mname = G_fully_qualified_name(old, mapset);
39
40 G_debug(3, "Copy %s", list[n].alias);
41
42 G_message(_("Copying %s <%s> to current mapset as <%s>"), list[n].maindesc,
43 mname, new);
44
46 if (G_strcasecmp(list[n].alias, "vector") == 0) {
47 ret = Vect_copy(old, mapset, new);
48 if (ret == -1) {
49 G_warning(_("Unable to copy <%s> to current mapset as <%s>"), mname,
50 new);
51 result = 1;
52 }
53 }
54 else {
55 for (i = 0; i < list[n].nelem; i++) {
57 G_file_name(path, list[n].element[i], old, mapset);
58 if (access(path, 0) != 0) {
59 G_remove(list[n].element[i], new);
60 G_verbose_message(_("%s is missing"), list[n].desc[i]);
61
62 continue;
63 }
64 G_file_name(path2, list[n].element[i], new, G_mapset());
65 if (G_recursive_copy(path, path2) == 1) {
66 G_warning(_("Unable to copy <%s> to current mapset as <%s>"),
67 mname, new);
68 result = 1;
69 }
70 else {
71 G_verbose_message(_("%s copied"), list[n].desc[i]);
72 }
73 }
74 }
75
76 /* special case: remove (yes, remove) the secondary color table, if it
77 * exists */
78 if (G_strcasecmp(list[n].element[0], "cell") == 0) {
79 char colr2[6 + GMAPSET_MAX];
80
81 if (snprintf(colr2, 6 + GMAPSET_MAX, "colr2/%s", G_mapset()) >=
82 6 + GMAPSET_MAX)
83 G_warning(_("String for secondary color table has been truncated"));
84 G_remove(colr2, new);
85 }
88
89 return result;
90}
int G_recursive_copy(const char *, const char *)
Copy recursively source directory to destination directory.
Definition copy_dir.c:67
void G_free(void *)
Free allocated memory.
Definition gis/alloc.c:145
void G_warning(const char *,...) __attribute__((format(printf
int G_make_mapset_object_group(const char *)
Create directory for group of elements of a given type.
Definition mapset_msc.c:73
void void G_verbose_message(const char *,...) __attribute__((format(printf
char * G_file_name(char *, const char *, const char *, const char *)
Builds full path names to GIS data files.
Definition file_name.c:59
char * G_fully_qualified_name(const char *, const char *)
Get fully qualified element name.
Definition nme_in_mps.c:99
int G_remove(const char *, const char *)
Remove a database file.
Definition remove.c:41
int int G_strcasecmp(const char *, const char *)
String compare ignoring case (upper or lower)
Definition strings.c:45
void G_message(const char *,...) __attribute__((format(printf
int G_debug(int, const char *,...) __attribute__((format(printf
const char * G_mapset(void)
Get current mapset name.
Definition gis/mapset.c:31
int M__hold_signals(int)
Hold signals.
Definition sighold.c:22
int Vect_copy(const char *, const char *, const char *)
Copy vector map including attribute tables.
Definition map.c:123
int M_do_copy(int n, const char *old, const char *mapset, const char *new)
Copy element.
Definition do_copy.c:33
#define GMAPSET_MAX
Definition gis.h:194
#define GPATH_MAX
Definition gis.h:196
#define _(str)
Definition glocale.h:10
Definition manage.h:4
int nelem
Definition manage.h:9
Definition path.h:15
#define access
Definition unistd.h:7