[GRASS-SVN] r30997 - in grass/trunk: general/manage/lib include
lib/gis
svn_grass at osgeo.org
svn_grass at osgeo.org
Mon Apr 14 21:56:04 EDT 2008
Author: glynn
Date: 2008-04-14 21:56:04 -0400 (Mon, 14 Apr 2008)
New Revision: 30997
Added:
grass/trunk/lib/gis/copy_dir.c
Removed:
grass/trunk/general/manage/lib/copyfile.c
Modified:
grass/trunk/general/manage/lib/Makefile
grass/trunk/general/manage/lib/do_copy.c
grass/trunk/include/gisdefs.h
Log:
Move recursive_copy() into libgis (G_recursive_copy()) to prevent LFS issues [bug #50]
Modified: grass/trunk/general/manage/lib/Makefile
===================================================================
--- grass/trunk/general/manage/lib/Makefile 2008-04-15 01:25:48 UTC (rev 30996)
+++ grass/trunk/general/manage/lib/Makefile 2008-04-15 01:56:04 UTC (rev 30997)
@@ -4,10 +4,6 @@
LIB_NAME = $(MANAGE_LIBNAME)
-LIB_OBJS = add_elem.o ask.o copyfile.o do_copy.o do_list.o do_remove.o \
- do_rename.o empty.o find.o get_len.o menu.o read_list.o \
- show_elem.o sighold.o
-
LOCAL_HEADERS = $(wildcard *.h) ../list.h
EXTRA_INC = -I.. $(VECT_INC)
Deleted: grass/trunk/general/manage/lib/copyfile.c
===================================================================
--- grass/trunk/general/manage/lib/copyfile.c 2008-04-15 01:25:48 UTC (rev 30996)
+++ grass/trunk/general/manage/lib/copyfile.c 2008-04-15 01:56:04 UTC (rev 30997)
@@ -1,31 +0,0 @@
-#include <grass/gis.h>
-#include <unistd.h>
-int
-copyfile (char *element, char *old, char *mapset, char *new)
-{
- char buf[1024];
- int in, out;
- int ix, ox = 0;
-
- in = G_open_old (element, old, mapset);
- if (in < 0)
- return -1;
- out = G_open_new (element, new);
- if (out < 0)
- {
- close (in);
- return -2;
- }
- while ((ix = read (in, buf, sizeof buf)) > 0)
- if ((ox = write (out, buf, (size_t)ix)) != ix)
- break;
-
- close (in);
- close (out);
-
- if (ix < 0)
- return -3;
- if (ix != 0 && ix != ox)
- return -4;
- return 0;
-}
Modified: grass/trunk/general/manage/lib/do_copy.c
===================================================================
--- grass/trunk/general/manage/lib/do_copy.c 2008-04-15 01:25:48 UTC (rev 30996)
+++ grass/trunk/general/manage/lib/do_copy.c 2008-04-15 01:56:04 UTC (rev 30997)
@@ -1,16 +1,9 @@
#include <stdio.h>
#include <string.h>
-#include <fcntl.h>
-#include <unistd.h>
-#include <dirent.h>
-#include <sys/types.h>
-#include <sys/stat.h>
#include <grass/gis.h>
#include <grass/Vect.h>
#include "list.h"
-static int recursive_copy(const char *src, const char *dst);
-
/*
* returns 0 - success
* 1 - error
@@ -18,7 +11,7 @@
int do_copy (int n, char *old, char *mapset, char *new)
{
int i, ret, len;
- char path[1024], path2[1024];
+ char path[GPATH_MAX], path2[GPATH_MAX];
int result = 0;
G_debug (3, "Copy %s", list[n].alias );
@@ -50,7 +43,7 @@
continue;
}
G__file_name (path2, list[n].element[i], new, G_mapset());
- if ( recursive_copy(path, path2) == 1 )
+ if ( G_recursive_copy(path, path2) == 1 )
{
result = 1;
}
@@ -65,7 +58,7 @@
/* special case: remove (yes, remove) the secondary color table, if it exists */
if (G_strcasecmp (list[n].element[0], "cell") == 0)
{
- char colr2[50];
+ char colr2[GNAME_MAX];
sprintf (colr2, "colr2/%s", G_mapset());
G_remove (colr2, new);
@@ -75,112 +68,3 @@
return result;
}
-/* RULE:
- * 1. If destination does not exist, copy source to destination as expected.
- * 2. If destination already exists and it's a file, destination will be
- * deleted first and apply RULE 1.
- * 3. If destination already exists which is a directory and source is a file,
- * try to copy source to destination directory.
- * 4. If destination already exists which is a directory and source is also a
- * directory, try to copy all contents in source to destination directory.
- *
- * This rule is designed according to general/manage/lib/copy.sh.
- *
- * POSSIBLE CASES:
- * if src is a file:
- * if dst does not exist:
- * copy src to dst RULE 1
- * if dst is a file:
- * delete dst and copy src to dst RULE 2
- * if dst is a directory:
- * try recursive_copy(src, dst/src) RULE 3
- * if src is a directory:
- * if dst does not exist:
- * copy src to dst RULE 1
- * if dst is a file:
- * delete dst and copy src to dst RULE 2
- * if dst is a directory:
- * try RULE 4
- * for i in `ls src`
- * do
- * recursive_copy(src/$i, dst/$i)
- * done
- *
- * RETURN: 0 if successful, otherwise 1
- */
-static int
-recursive_copy(const char *src, const char *dst)
-{
- DIR *dirp;
- struct dirent *dp;
- struct stat sb;
- char buf[1024], buf2[1024];
- int fd, fd2;
- size_t len, len2;
- mode_t mode;
-
- if(G_lstat(src, &sb))
- return 1;
-
- /* src is a file */
- if(!S_ISDIR((mode = sb.st_mode)))
- {
- if(!G_lstat(dst, &sb) && S_ISDIR(sb.st_mode))
- {
- const char *p = strrchr(src, '/');
- /* src => dst/src */
- sprintf(buf, "%s/%s", dst, (p?p+1:src));
-
- return recursive_copy(src, buf);
- }
-
- /* src => dst */
- if((fd = open(src, O_RDONLY)) < 0)
- return 1;
-
- if((fd2 = open(dst, O_CREAT|O_TRUNC|O_WRONLY, mode & 0777)) < 0)
- {
- close(fd);
- return 1;
- }
- while((len = read(fd, buf, 1024)) > 0)
- {
- while(len && (len2 = write(fd2, buf, len)) >= 0)
- len -= len2;
- }
- close(fd);
- close(fd2);
-
- return 0;
- }
-
- /* src is a directory */
-
- if(G_lstat(dst, &sb))
- {
- if(G_mkdir(dst))
- return 1;
- }else
- /* if dst already exists and it's a file, try to remove it */
- if(!S_ISDIR(sb.st_mode))
- {
- if(remove(dst) || G_mkdir(dst))
- return 1;
- }
-
- if((dirp = opendir(src)) == NULL)
- return 1;
- while((dp = readdir(dirp)) != NULL)
- {
- /* do not copy hidden files */
- if(dp->d_name[0] == '.')
- continue;
- sprintf(buf, "%s/%s", src, dp->d_name);
- sprintf(buf2, "%s/%s", dst, dp->d_name);
- if(recursive_copy(buf, buf2))
- return 1;
- }
- closedir(dirp);
-
- return 0;
-}
Modified: grass/trunk/include/gisdefs.h
===================================================================
--- grass/trunk/include/gisdefs.h 2008-04-15 01:25:48 UTC (rev 30996)
+++ grass/trunk/include/gisdefs.h 2008-04-15 01:56:04 UTC (rev 30997)
@@ -372,7 +372,8 @@
int G_copy(void *, const void *, int);
/* copy_file.c */
-int G_copy_file(const char *infile, const char *outfile);
+int G_copy_file(const char *, const char *);
+int G_recursive_copy(const char *, const char *);
/* dalloc.c */
double *G_alloc_vector(size_t);
Added: grass/trunk/lib/gis/copy_dir.c
===================================================================
--- grass/trunk/lib/gis/copy_dir.c (rev 0)
+++ grass/trunk/lib/gis/copy_dir.c 2008-04-15 01:56:04 UTC (rev 30997)
@@ -0,0 +1,149 @@
+/****************************************************************************
+ *
+ * MODULE: GRASS GIS library - copy_dir.c
+ * AUTHOR(S): Huidae Cho
+ * PURPOSE: Function to recursively copy a directory
+ * COPYRIGHT: (C) 2008 by the GRASS Development Team
+ *
+ * NOTE: Extracted from general/manage/lib/do_copy.c
+ *
+ * This program is free software under the GNU General Public
+ * License (>=v2). Read the file COPYING that comes with GRASS
+ * for details.
+ *
+ *****************************************************************************/
+
+#include <stdio.h>
+#include <errno.h>
+#include <string.h>
+
+#include <grass/gis.h>
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <dirent.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+
+/* RULE:
+ * 1. If destination does not exist, copy source to destination as expected.
+ * 2. If destination already exists and it's a file, destination will be
+ * deleted first and apply RULE 1.
+ * 3. If destination already exists which is a directory and source is a file,
+ * try to copy source to destination directory.
+ * 4. If destination already exists which is a directory and source is also a
+ * directory, try to copy all contents in source to destination directory.
+ *
+ * This rule is designed according to general/manage/lib/copy.sh.
+ *
+ * POSSIBLE CASES:
+ * if src is a file:
+ * if dst does not exist:
+ * copy src to dst RULE 1
+ * if dst is a file:
+ * delete dst and copy src to dst RULE 2
+ * if dst is a directory:
+ * try recursive_copy(src, dst/src) RULE 3
+ * if src is a directory:
+ * if dst does not exist:
+ * copy src to dst RULE 1
+ * if dst is a file:
+ * delete dst and copy src to dst RULE 2
+ * if dst is a directory:
+ * try RULE 4
+ * for i in `ls src`
+ * do
+ * recursive_copy(src/$i, dst/$i)
+ * done
+ *
+ * RETURN: 0 if successful, otherwise 1
+ */
+
+int G_recursive_copy(const char *src, const char *dst)
+{
+ DIR *dirp;
+ struct stat sb;
+
+ if (G_lstat(src, &sb) < 0)
+ return 1;
+
+ /* src is a file */
+ if (!S_ISDIR(sb.st_mode))
+ {
+ char buf[4096];
+ int fd, fd2;
+ size_t len, len2;
+
+ if (G_lstat(dst, &sb) == 0 && S_ISDIR(sb.st_mode))
+ {
+ char path[GPATH_MAX];
+ const char *p = strrchr(src, '/');
+
+ /* src => dst/src */
+ sprintf(path, "%s/%s", dst, (p?p+1:src));
+ return G_recursive_copy(src, path);
+ }
+
+ /* src => dst */
+ if ((fd = open(src, O_RDONLY)) < 0)
+ return 1;
+
+ if ((fd2 = open(dst, O_CREAT|O_TRUNC|O_WRONLY, sb.st_mode & 0777)) < 0)
+ {
+ close(fd);
+ return 1;
+ }
+
+ while ((len = read(fd, buf, sizeof(buf))) > 0)
+ {
+ while(len && (len2 = write(fd2, buf, len)) >= 0)
+ len -= len2;
+ }
+
+ close(fd);
+ close(fd2);
+
+ return 0;
+ }
+
+ /* src is a directory */
+ if (G_lstat(dst, &sb) < 0)
+ {
+ if(G_mkdir(dst))
+ return 1;
+ }
+ else
+ /* if dst already exists and it's a file, try to remove it */
+ if (!S_ISDIR(sb.st_mode))
+ {
+ if (remove(dst) < 0 || G_mkdir(dst) < 0)
+ return 1;
+ }
+
+ dirp = opendir(src);
+ if (!dirp)
+ return 1;
+
+ for (;;)
+ {
+ char path[GPATH_MAX], path2[GPATH_MAX];
+ struct dirent *dp = readdir(dirp);
+
+ if (!dp)
+ break;
+
+ /* do not copy hidden files */
+ if (dp->d_name[0] == '.')
+ continue;
+
+ sprintf(path, "%s/%s", src, dp->d_name);
+ sprintf(path2, "%s/%s", dst, dp->d_name);
+
+ if (G_recursive_copy(path, path2) != 0)
+ return 1;
+ }
+
+ closedir(dirp);
+
+ return 0;
+}
More information about the grass-commit
mailing list