Changeset 31018


Ignore:
Timestamp:
Apr 17, 2008, 12:55:07 AM (16 years ago)
Author:
neteler
Message:

Move recursive_copy() into libgis (G_recursive_copy()) to prevent LFS issues [bug #50] (backport from head, c30997 and c31002)

Location:
grass/branches/releasebranch_6_3
Files:
1 added
1 deleted
3 edited

Legend:

Unmodified
Added
Removed
  • grass/branches/releasebranch_6_3/general/manage/lib/Makefile

    r24848 r31018  
    44
    55LIB_NAME = $(MANAGE_LIBNAME)
    6 
    7 LIB_OBJS = add_elem.o ask.o copyfile.o do_copy.o do_list.o do_remove.o \
    8            do_rename.o empty.o find.o get_len.o menu.o read_list.o \
    9            show_elem.o sighold.o
    106
    117LOCAL_HEADERS = $(wildcard *.h) ../list.h
  • grass/branches/releasebranch_6_3/general/manage/lib/do_copy.c

    r23024 r31018  
    11#include <stdio.h>
    22#include <string.h>
    3 #include <fcntl.h>
    43#include <unistd.h>
    5 #include <dirent.h>
    6 #include <sys/types.h>
    7 #include <sys/stat.h>
    84#include <grass/gis.h>
    95#include <grass/Vect.h>
    106#include "list.h"
    11 
    12 static int recursive_copy(const char *src, const char *dst);
    137
    148/*
     
    1913{
    2014    int i, ret, len;
    21     char path[1024], path2[1024];
     15    char path[GPATH_MAX], path2[GPATH_MAX];
    2216    int result = 0;
    2317
     
    5145            }
    5246            G__file_name (path2, list[n].element[i], new, G_mapset());
    53             if ( recursive_copy(path, path2) == 1 )
     47            if ( G_recursive_copy(path, path2) == 1 )
    5448            {
    5549                result = 1;
     
    6660    if (G_strcasecmp (list[n].element[0], "cell") == 0)
    6761    {
    68         char colr2[50];
     62        char colr2[GNAME_MAX];
    6963
    7064        sprintf (colr2, "colr2/%s", G_mapset());
     
    7670}
    7771
    78 /* RULE:
    79  * 1. If destination does not exist, copy source to destination as expected.
    80  * 2. If destination already exists and it's a file, destination will be
    81  *    deleted first and apply RULE 1.
    82  * 3. If destination already exists which is a directory and source is a file,
    83  *    try to copy source to destination directory.
    84  * 4. If destination already exists which is a directory and source is also a
    85  *    directory, try to copy all contents in source to destination directory.
    86  *
    87  * This rule is designed according to general/manage/lib/copy.sh.
    88  *
    89  * POSSIBLE CASES:
    90  * if src is a file:
    91  *      if dst does not exist:
    92  *              copy src to dst                         RULE 1
    93  *      if dst is a file:
    94  *              delete dst and copy src to dst          RULE 2
    95  *      if dst is a directory:
    96  *              try recursive_copy(src, dst/src)        RULE 3
    97  * if src is a directory:
    98  *      if dst does not exist:
    99  *              copy src to dst                         RULE 1
    100  *      if dst is a file:
    101  *              delete dst and copy src to dst          RULE 2
    102  *      if dst is a directory:
    103  *              try                                     RULE 4
    104  *              for i in `ls src`
    105  *              do
    106  *                      recursive_copy(src/$i, dst/$i)
    107  *              done
    108  *
    109  * RETURN: 0 if successful, otherwise 1
    110  */
    111 static int
    112 recursive_copy(const char *src, const char *dst)
    113 {
    114         DIR *dirp;
    115         struct dirent *dp;
    116         struct stat sb;
    117         char buf[1024], buf2[1024];
    118         int fd, fd2;
    119         size_t len, len2;
    120         mode_t mode;
    121 
    122         if(G_lstat(src, &sb))
    123                 return 1;
    124 
    125         /* src is a file */
    126         if(!S_ISDIR((mode = sb.st_mode)))
    127         {
    128                 if(!G_lstat(dst, &sb) && S_ISDIR(sb.st_mode))
    129                 {
    130                         const char *p = strrchr(src, '/');
    131                         /* src => dst/src */
    132                         sprintf(buf, "%s/%s", dst, (p?p+1:src));
    133 
    134                         return recursive_copy(src, buf);
    135                 }
    136 
    137                 /* src => dst */
    138                 if((fd = open(src, O_RDONLY)) < 0)
    139                         return 1;
    140 
    141                 if((fd2 = open(dst, O_CREAT|O_TRUNC|O_WRONLY, mode & 0777)) < 0)
    142                 {
    143                         close(fd);
    144                         return 1;
    145                 }
    146                 while((len = read(fd, buf, 1024)) > 0)
    147                 {
    148                         while(len && (len2 = write(fd2, buf, len)) >= 0)
    149                                 len -= len2;
    150                 }
    151                 close(fd);
    152                 close(fd2);
    153 
    154                 return 0;
    155         }
    156 
    157         /* src is a directory */
    158 
    159         if(G_lstat(dst, &sb))
    160         {
    161                 if(G_mkdir(dst))
    162                         return 1;
    163         }else
    164         /* if dst already exists and it's a file, try to remove it */
    165         if(!S_ISDIR(sb.st_mode))
    166         {
    167                 if(remove(dst) || G_mkdir(dst))
    168                         return 1;
    169         }
    170 
    171         if((dirp = opendir(src)) == NULL)
    172                 return 1;
    173         while((dp = readdir(dirp)) != NULL)
    174         {
    175                 /* do not copy hidden files */
    176                 if(dp->d_name[0] == '.')
    177                         continue;
    178                 sprintf(buf, "%s/%s", src, dp->d_name);
    179                 sprintf(buf2, "%s/%s", dst, dp->d_name);
    180                 if(recursive_copy(buf, buf2))
    181                         return 1;
    182         }
    183         closedir(dirp);
    184 
    185         return 0;
    186 }
  • grass/branches/releasebranch_6_3/include/gisdefs.h

    r25396 r31018  
    373373
    374374/* copy_file.c */
    375 int G_copy_file(const char *infile, const char *outfile);
     375int G_copy_file(const char *, const char *);
     376int G_recursive_copy(const char *, const char *);
    376377
    377378/* dalloc.c */
Note: See TracChangeset for help on using the changeset viewer.