summaryrefslogtreecommitdiffstats
path: root/lib/libftp/utils/mirror.c
blob: babd840e682073b9d48cb3f219ecbb240805955d (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

/* Mirror directrory structure to another host */


#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/stat.h>
#include <syslog.h>
#include <FtpLibrary.h>

/* Usage: mirror <local_dir> <host> <user> <passwd> <remote_dir> */ 
FTP *ftp;

main(int a,char **b)
{

#define LOCAL_DIR  b[1]
#define HOST       b[2]
#define USER       b[3]
#define PASSWD     b[4]
#define REMOTE_DIR b[5]

  if ( a < 5 )
    quit("Usage: mirror <local_dir> <host> <user> <passwd> <remote_dir>");

  
  FtplibDebug(yes);
  FtpLogin(&ftp,HOST,USER,PASSWD,NULL);
  FtpChdir(ftp,REMOTE_DIR);
  FtpBinary(ftp);
  doit(LOCAL_DIR);
  exit(0);
}

doit(char *dirname)
{
  DIR *dp;
  struct direct *de;
  char n[256],fn[256];
  struct stat st;


  if ( (dp=opendir(dirname)) == NULL )
    {
      log(dirname);
      return;
    }

  while ( (de = readdir(dp)) != NULL )
    {
      if ( de -> d_name[0] == '.' )
	continue;

      sprintf(fn,"%s/%s",dirname,de->d_name);
      
      if ( stat(fn,&st) != 0 ) {
	log(fn);
	continue;
      }

      if ( S_ISDIR (st.st_mode) )
	{
	  FtpCommand(ftp,"MKD %s",fn,0,EOF); /* Ignore errors (0,EOF) */
	  doit(fn);
	  continue;
	}

      if ( st.st_size != FtpSize(ftp,fn))

      FtpPut(ftp,fn,fn);
    }

  closedir(dp);
  
}



quit(char *s)
{
  log(s);
  exit(1);
}

log(char *s)
{
  perror(s);
}



OpenPOWER on IntegriCloud