Make idev, hlink and file_struct + strings use allocation
[rsync/rsync.git] / exclude.c
index 1bb042b..d7fe924 100644 (file)
--- a/exclude.c
+++ b/exclude.c
@@ -40,10 +40,10 @@ static struct exclude_struct *make_exclude(const char *pattern, int include)
        char *cp;
        int pat_len;
 
-       ret = (struct exclude_struct *)malloc(sizeof(*ret));
+       ret = new(struct exclude_struct);
        if (!ret) out_of_memory("make_exclude");
 
-       memset(ret, 0, sizeof(*ret));
+       memset(ret, 0, sizeof ret[0]);
 
        if (strncmp(pattern,"- ",2) == 0) {
                pattern += 2;
@@ -57,8 +57,8 @@ static struct exclude_struct *make_exclude(const char *pattern, int include)
        if (exclude_path_prefix)
                ret->match_flags |= MATCHFLG_ABS_PATH;
        if (exclude_path_prefix && *pattern == '/') {
-               ret->pattern = malloc(strlen(exclude_path_prefix)
-                               + strlen(pattern) + 1);
+               ret->pattern = new_array(char,
+                       strlen(exclude_path_prefix) + strlen(pattern) + 1);
                if (!ret->pattern) out_of_memory("make_exclude");
                sprintf(ret->pattern, "%s%s", exclude_path_prefix, pattern);
        }
@@ -92,7 +92,7 @@ static struct exclude_struct *make_exclude(const char *pattern, int include)
 static void free_exclude(struct exclude_struct *ex)
 {
        free(ex->pattern);
-       memset(ex,0,sizeof(*ex));
+       memset(ex, 0, sizeof ex[0]);
        free(ex);
 }
 
@@ -102,7 +102,7 @@ void free_exclude_list(struct exclude_struct ***listp)
        struct exclude_struct **list = *listp;
 
        if (verbose > 2)
-               rprintf(FINFO,"clearing exclude list\n");
+               rprintf(FINFO, "[%s] clearing exclude list\n", who_am_i());
 
        if (!list)
                return;
@@ -132,8 +132,7 @@ static int check_one_exclude(char *name, struct exclude_struct *ex,
                static char full_name[MAXPATHLEN];
                extern char curr_dir[];
                int plus = curr_dir[1] == '\0'? 1 : 0;
-               snprintf(full_name, sizeof full_name,
-                        "%s/%s", curr_dir+plus, name);
+               pathjoin(full_name, sizeof full_name, curr_dir+plus, name);
                name = full_name;
        }
 
@@ -204,12 +203,14 @@ static void report_exclude_result(char const *name,
         * then it is stripped out by make_exclude.  So as a special
         * case we add it back in here. */
 
-       if (verbose >= 2)
-               rprintf(FINFO, "%s %s %s because of pattern %s%s\n",
+       if (verbose >= 2) {
+               rprintf(FINFO, "[%s] %s %s %s because of pattern %s%s\n",
+                       who_am_i(),
                        ent->include ? "including" : "excluding",
                        name_is_dir ? "directory" : "file",
                        name, ent->pattern,
                        ent->directory ? "/" : "");
+       }
 }
 
 
@@ -238,20 +239,21 @@ void add_exclude(struct exclude_struct ***listp, const char *pattern, int includ
        int len = 0;
 
        if (*pattern == '!' && !pattern[1]) {
-           free_exclude_list(listp);
-           return;
+               free_exclude_list(listp);
+               return;
        }
 
        if (list)
                for (; list[len]; len++) {}
 
-       list = *listp = (struct exclude_struct **)Realloc(list,sizeof(struct exclude_struct *)*(len+2));
+       list = *listp = realloc_array(list, struct exclude_struct *, len+2);
 
        if (!list || !(list[len] = make_exclude(pattern, include)))
                out_of_memory("add_exclude");
 
        if (verbose > 2) {
-               rprintf(FINFO,"add_exclude(%s,%s)\n",pattern,
+               rprintf(FINFO, "[%s] add_exclude(%s,%s)\n",
+                       who_am_i(), pattern,
                        include ? "include" : "exclude");
        }
 
@@ -262,7 +264,7 @@ void add_exclude(struct exclude_struct ***listp, const char *pattern, int includ
 void add_exclude_file(struct exclude_struct ***listp, const char *fname,
                      int fatal, int include)
 {
-       int fd;
+       FILE *fp;
        char line[MAXPATHLEN];
        char *eob = line + MAXPATHLEN - 1;
        extern int eol_nulls;
@@ -271,10 +273,10 @@ void add_exclude_file(struct exclude_struct ***listp, const char *fname,
                return;
 
        if (*fname != '-' || fname[1])
-               fd = open(fname, O_RDONLY|O_BINARY);
+               fp = fopen(fname, "rb");
        else
-               fd = 0;
-       if (fd < 0) {
+               fp = stdin;
+       if (!fp) {
                if (fatal) {
                        rsyserr(FERROR, errno,
                                "failed to open %s file %s",
@@ -286,11 +288,11 @@ void add_exclude_file(struct exclude_struct ***listp, const char *fname,
        }
 
        while (1) {
-               char ch, *s = line;
-               int cnt;
+               char *s = line;
+               int ch;
                while (1) {
-                       if ((cnt = read(fd, &ch, 1)) <= 0) {
-                               if (cnt < 0 && errno == EINTR)
+                       if ((ch = getc(fp)) == EOF) {
+                               if (ferror(fp) && errno == EINTR)
                                        continue;
                                break;
                        }
@@ -306,17 +308,16 @@ void add_exclude_file(struct exclude_struct ***listp, const char *fname,
                         * them but there's no need to save them. */
                        add_exclude(listp, line, include);
                }
-               if (cnt <= 0)
+               if (ch == EOF)
                        break;
        }
-       close(fd);
+       fclose(fp);
 }
 
 
 void send_exclude_list(int f)
 {
        int i;
-       extern int protocol_version;
        extern int list_only, recurse;
 
        /* This is a complete hack - blame Rusty.
@@ -331,20 +332,19 @@ void send_exclude_list(int f)
                return;
        }
 
-       for (i=0;exclude_list[i];i++) {
-               int l;
-               char pattern[MAXPATHLEN];
+       for (i = 0; exclude_list[i]; i++) {
+               unsigned int l;
+               char pattern[MAXPATHLEN+1];
 
-               strlcpy(pattern,exclude_list[i]->pattern,sizeof(pattern));
-               if (exclude_list[i]->directory) strlcat(pattern,"/", sizeof(pattern));
+               l = strlcpy(pattern, exclude_list[i]->pattern, sizeof pattern);
+               if (l == 0 || l >= MAXPATHLEN)
+                       continue;
+               if (exclude_list[i]->directory) {
+                       pattern[l++] = '/';
+                       pattern[l] = '\0';
+               }
 
-               l = strlen(pattern);
-               if (l == 0) continue;
                if (exclude_list[i]->include) {
-                       if (protocol_version < 19) {
-                               rprintf(FERROR,"remote rsync does not support include syntax - aborting\n");
-                               exit_cleanup(RERR_UNSUPPORTED);
-                       }
                        write_int(f,l+2);
                        write_buf(f,"+ ",2);
                } else {
@@ -359,12 +359,13 @@ void send_exclude_list(int f)
 
 void recv_exclude_list(int f)
 {
-       char line[MAXPATHLEN];
+       char line[MAXPATHLEN+1]; /* Allows a trailing slash on a max-len dir */
        unsigned int l;
 
-       while ((l=read_int(f))) {
-               if (l >= MAXPATHLEN) overflow("recv_exclude_list");
-               read_sbuf(f,line,l);
+       while ((l = read_int(f)) != 0) {
+               if (l >= sizeof line)
+                       overflow("recv_exclude_list");
+               read_sbuf(f, line, l);
                add_exclude(&exclude_list, line, ADD_EXCLUDE);
        }
 }
@@ -448,10 +449,9 @@ void add_cvs_excludes(void)
        for (i=0; cvs_ignore_list[i]; i++)
                add_exclude(&exclude_list, cvs_ignore_list[i], ADD_EXCLUDE);
 
-       if ((p=getenv("HOME")) && strlen(p) < (MAXPATHLEN-12)) {
-               snprintf(fname,sizeof(fname), "%s/.cvsignore",p);
-               add_exclude_file(&exclude_list,fname,MISSING_OK,ADD_EXCLUDE);
-       }
+       if ((p = getenv("HOME"))
+           && pathjoin(fname, sizeof fname, p, ".cvsignore") < sizeof fname)
+               add_exclude_file(&exclude_list, fname, MISSING_OK, ADD_EXCLUDE);
 
        add_exclude_line(&exclude_list, getenv("CVSIGNORE"), ADD_EXCLUDE);
 }