aboutsummaryrefslogtreecommitdiffstats
path: root/src/backup.c
blob: 3c59a9d586e76917ad138486c84af06d04e1e626 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*  
   Copyright (C)  2008 Renaissance Technologies Corp.
                  main developer: HP Wei <hp@rentec.com>
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; see the file COPYING.
   If not, write to the Free Software Foundation,
   59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  
*/

#include "main.h"
#include <regex.h>   /* use POSIX in order to be portable to linux */

extern int verbose;
int backup = FALSE;
char *pattern_baseDir = NULL;

/* --------- for syncing all files with/without backup. */
int nPattern=0;   /* number of regular expression for backup files */
regex_t ** fPatterns;/* array of pointers to regular expression */

int set_nPattern(char * fpat_file) 
{
  FILE *fd;
  char pat[PATH_MAX]; 
  int count = 0;
  if ((fd = fopen(fpat_file, "r"))==NULL) {
    fprintf(stderr, "Cannot open file -- %s\n", fpat_file);
    return FAIL;
  }
  while (!feof(fd)) {
    if (fscanf(fd, "%s", pat) == 1) {
      ++count;
    }
  }
  nPattern = count;
  fclose(fd);
  return SUCCESS;
}
 
int read_backup_pattern(char * fpat_file)
{
  FILE *fd;
  char pat[PATH_MAX]; 
  int i = 0;

  if (!set_nPattern(fpat_file)) return FAIL;

  fPatterns = malloc(sizeof(void *)*nPattern);
  for(i = 0; i< nPattern; ++i) {
    fPatterns[i] = (regex_t *) malloc(sizeof(regex_t));
  }

  fd = fopen(fpat_file, "r");

  /* 
     if we don't prepend ^,
     then pattern 'file' intended for files under srcBase
     will select files with pattern = subdir/file.* 
     which is not our intention.
  */
  i = 0;
  while (!feof(fd)) {
    if (fscanf(fd, "%s", pat) == 1) {
      char fullpat[PATH_MAX];
      sprintf(fullpat, "^%s", pat);

      regcomp(fPatterns[i], fullpat, REG_EXTENDED|REG_NOSUB);
      ++i;
    }
  };
  fclose(fd);
  return SUCCESS;
}

int needBackup(char * filename) /* fullpath */
{
  /* we reach this point when the backup flag is true
        if no_pattern -> backup all of them
        if pattern 
           if match   -> backup
              nomatch -> no-backup
  */
  int i;
  char *p;
  if (!backup) return FALSE;
  if (nPattern==0) return TRUE;

  p = filename + strlen(pattern_baseDir) + 1; /* +1 to get pass the / after basedir */
  /* fprintf(stderr, "nPattern = %d file= %s\n", nPattern, p); ********/
  for(i=0; i<nPattern; ++i) {
    if (regexec(fPatterns[i], p, (size_t)0, NULL, 0)!=0) 
      continue; /* no match */    
    return TRUE;
  }
  return FALSE;
}