File: sites.c

package info (click to toggle)
screem 0.2.1-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 3,748 kB
  • ctags: 2,270
  • sloc: ansic: 26,366; sh: 7,453; makefile: 512; sed: 93; perl: 18
file content (1638 lines) | stat: -rw-r--r-- 48,934 bytes parent folder | download
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
/* 
   sitecopy, for managing remote web sites.
   Copyright (C) 1998-99, Joe Orton <joe@orton.demon.co.uk>
                                                                     
   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 of the License, 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; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   $Id: sites.c,v 1.1.1.1 1999/11/21 19:47:24 davek Exp $
*/

/* This is the core functionality of sitecopy, performing updates
 * and checking files etc. */

#include <config.h>

#include <sys/types.h>
#include <sys/stat.h>

#include <errno.h>
#include <dirent.h>
#include <fnmatch.h>
#include <fcntl.h>
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif /* HAVE_STDLIB_H */
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
#include <time.h>
#include <utime.h>

#ifndef HAVE_SNPRINTF
#include <snprintf.h>
#endif /* !HAVE_SNPRINTF */

#include <basename.h>

#include "common.h"
#include "dirname.h"
#include "frontend.h"
#include "protocol.h"
#include "socket.h"
#include "sites.h"

#include "ftp.h"

/* The protocol drivers */
const struct proto_driver ftp_driver = {
    ftp_init,
    ftp_finish,
    ftp_move,
    ftp_put,
    ftp_get,
    ftp_delete,
    ftp_chmod,
    ftp_mkdir,
    ftp_rmdir,
    NULL, /* create link */
    NULL, /* change link target */
    NULL, /* delete link */
    ftp_fetch,
    "ftp",
    "FTP",
    ftp_error
};

#ifdef USE_DAV
#include "httpdav.h"

const struct proto_driver dav_driver = {
    http_init, /* Connect */
    http_finish, /* Disconnect */
    dav_move, /* File move */
    http_put, /* File upload - HTTP PUT */
    http_get, /* File download */
    http_delete, /* File delete */
    NULL, /* File chmod... this could be done using either
	   *   a) A special live property
	   *   b) The ACL extensions: draft-ietf-webdav-acl-??.txt
	   */
    dav_mkcol, /* Dir create */
    dav_rmdir, /* Dir remove, same as file remove */
    dav_mkref, /* Create link */
    dav_chref, /* Change link */
    dav_rmref, /* Remove link */
    /* fetch listing */
#ifdef HAVE_LIBEXPAT
    dav_fetch, 
#else
    NULL, 
#endif
    "http", /* Service name */
    "WebDAV", /* User-visible protocol name */
    http_error
};
#endif

/* This is the maximum number of directories which can be held in the queue 
 * at one time - each entry is only a pointer, so the size is not too
 * much of a problem.  */
#define MAXDIRS 500

/* This is the string used in the storage files to indicate the
 * line is a directory rather than a file */
#define DIRWORD "dir"
/* And this is used for links */
#define LINKWORD "link"

/* Shorthand for protocol driver methods */
#define CALL( a ) (*the_site->driver->a)

/* This holds ALL the sites defined in the rcfile */
struct site_t *all_sites;
 
bool fe_prompting;
bool site_keepgoing;

/* Prototypes */
void site_checkmoved( struct site_t *any_site );

void site_destroyfile( struct site_file_t *file );
int site_synch_create_directories( struct site_t *the_site );
int site_synch_files( struct site_t *the_site );
int site_synch_remove_directories( struct site_t *the_site );

int site_update_create_directories( struct site_t *the_site, bool onlymarked );
int site_update_delete_directories( struct site_t *the_site, bool onlymarked );
int site_update_delete_files( struct site_t *the_site, bool onlymarked );
int site_update_files( struct site_t *the_site, bool onlymarked );
int site_update_remove_directories( struct site_t *the_site, bool onlymarked );
int site_update_links( struct site_t *the_site, bool onlymarked );

void site_fetch_walk( struct site_t *the_site,
		      struct proto_file_t *files );

int site_readlocalfiles( struct site_t * );
int site_readremotefiles( struct site_t * );

void site_flatlist_items( FILE *f, struct site_t *the_site, 
			  const enum file_diff diff, const char *name );

/* Assigns the _local and _remote filenames to the site file */
void site_assignnames( struct site_file_t *the_file, struct site_t *the_site );

/* Functions for manipulating the doubly linked files list */
void file_delete( struct site_t *site, struct site_file_t *item );
struct site_file_t *file_prepend( struct site_t *site );
struct site_file_t *file_append( struct site_t *site );
struct site_file_t *file_create( void );

/* Whether a file */
bool file_isexcluded( const char *filename, const char *fullpath, 
		      struct site_t *site );
/* Whether a file is ASCII text or binary */
bool file_isascii( char *filename, struct site_t *site );

/* Creates and initializes a file.
 * Returns NULL if out-of-memory */
inline struct site_file_t *file_create( void ) {
    struct site_file_t *file;
    file = malloc( sizeof(struct site_file_t) );
    if( file!=NULL ) {
	/* Initialize */
	memset( file, 0, sizeof(struct site_file_t) );
    }
    return file;
}

/* Creates a file prepended on to the beginning of the site files list.
 * Returns NULL if the file could not be malloc'ed. */
struct site_file_t *file_prepend( struct site_t *site ) {
    struct site_file_t *file;
    file = file_create();
    if( file == NULL ) return NULL;
    if( site->files == NULL ) {
	/* Empty list */
	site->files = file;
	site->files_tail = file;
    } else {
	/* Non-empty list... update pointers */
	site->files->prev = file;
	file->next = site->files;
	site->files = file;
    }
    return file;
}

/* Deletes the given file from the given site */
void file_delete( struct site_t *site, struct site_file_t *item ) {
    if( item->prev ) {
	/* Not first in list */
	item->prev->next = item->next;
    } else {
	/* Not last in list */
	site->files = item->next;
    }
    if( item->next ) {
	/* Not last in list */
	item->next->prev = item->prev;
    } else {
	/* Last in list */
	site->files_tail = item->prev;
    }
    /* Safety */
    item->prev = NULL;
    item->next = NULL;
}

/* Creates a file added on to the end of the site files list.
 * Returns NULL if the file could not be malloc'ed. */
struct site_file_t *file_append( struct site_t *site ) {
    struct site_file_t *file;
    file = file_create();
    if( file == NULL ) return NULL;
    if( site->files_tail == NULL ) {
	/* Empty list */
	site->files = file;
	site->files_tail = file;
    } else {
	/* Non-empty list... update pointers */
	site->files_tail->next = file;
	file->prev = site->files_tail;
	site->files_tail = file;
    }
    return file;
}

/* Returns whether the given filename is excluded from the
 * given site
 */
bool file_isexcluded( const char *filename, const char *fullpath, 
		      struct site_t *site ) {
    struct exclude_t *excl;

    DEBUG( DEBUG_FILES, "\nChecking excludes:\n" );
    for( excl=site->excludes; excl != NULL; excl=excl->next ) {
	DEBUG( DEBUG_FILES, "%s ", excl->pattern );
	if( excl->haspath ) {
	    if( fnmatch( excl->pattern, fullpath, FNM_PATHNAME ) == 0 )
		break;
	} else {
	    if( fnmatch( excl->pattern, filename, 0 ) == 0 )
		break;
	}
    }
    if( excl != NULL ) { /* excluded */
	DEBUG( DEBUG_FILES, "- matched\n" );
	return true;
    } else {
	DEBUG( DEBUG_FILES, "... not matched.\n" );
	return false;
    }
}

bool file_isascii( char *filename, struct site_t *site ) {
    int n;

    DEBUG( DEBUG_FILES, "\nChecking ASCII list:\n" );
    for( n=0; n != site->numascii; n++ ) {
	DEBUG( DEBUG_FILES, "%s ", site->ascii[n] );
	if( fnmatch( site->ascii[n], filename, 0 ) == 0 )
	    break;
    }
    if( n < site->numascii ) { 
	DEBUG( DEBUG_FILES, "- matched\n" );
	return true;
    } else {
	DEBUG( DEBUG_FILES, "... not matched.\n" );
	return false;
    }
}


struct site_t *site_find( const char *sitename ) {
    struct site_t *current;

    for( current = all_sites; current!=NULL; current=current->next ) {
	if( strcmp( current->name, sitename ) == 0 ) {
	    /* We found it */
	    return current;
	}
    }

    return NULL;
}

int site_synch_create_directories( struct site_t *the_site ) {
    struct site_file_t *current;
    int ret;
    
    ret = 0;
    
    for( current=the_site->files; current!=NULL; current=current->next ) {
	if( current->dir && current->diff==file_deleted ) {
	    fe_synching( current );
	    if( mkdir( current->full_local, 0755 ) == 0 ) {
		fe_synched( current, true, NULL );
	    } else {
		ret = 1;
		fe_synched( current, false, strerror(errno) );
	    }
	}
    }
    return ret;
}

int site_synch_files( struct site_t *the_site ) {
    struct site_file_t *current;
    struct utimbuf times;
    int ret;

    ret = 0;

    for( current = the_site->files; current!=NULL; current=current->next ) {
	if( current->dir ) continue;
	switch( current->diff ) {
	case file_changed:
	case file_deleted:
	    fe_synching( current );
	    if( CALL(file_download)( current->full_local,current->full_remote,
				     current->remotesize, current->isascii )
		!= PROTO_OK ) {
		fe_synched( current, false, the_site->driver->last_error );
		ret = 2;
	    } else {
		/* Change the modtime of the local file so it doesn't look
		 * like it's changed already */
		times.actime = current->remotetime;
		times.modtime = current->remotetime;
		if( utime( current->full_local, &times ) < 0 ) {
		    fe_synched( current, false, strerror(errno) );
		    ret = 2;
		} else {
		    fe_synched( current, true, NULL );
		}
	    }
	    break;
	case file_new:
	    fe_synching( current );
	    if( unlink( current->full_local ) != 0 ) {
		fe_synched( current, false, strerror(errno) );
		ret = 2;
	    } else {
		fe_synched( current, true, NULL );
	    }
	    break;
	case file_moved:
	    fe_synching( current );
	    if( rename( current->full_local, 
			current->old->full_local ) == 0 ) {
		fe_synched( current, true, NULL );
	    } else {
		fe_synched( current, false, strerror(errno) );
		ret = 2;
	    }
	default:
	    break;	    
	}

    }

    return ret;

}

int site_synch_remove_directories( struct site_t *the_site ) {
    struct site_file_t *current;
    int ret;

    ret = 0;

    for( current=the_site->files_tail; current!=NULL; current=current->prev ) {
	if( current->dir && (current->diff==file_new) ) {
	    fe_synching( current );
	    if( rmdir( current->full_local ) == -1 ) {
		fe_synched( current, false, strerror(errno) );
		ret = 3;
	    } else {
		fe_synched( current, true, NULL );
	    }
	}
    }

    return ret;
}

/* Resyncs the LOCAL site with the REMOTE site.
 * This is site_update backwards, and is essentially the same in structure,
 * except with the logic reversed.
 */
int site_synch( struct site_t *the_site ) {
    int ret;

    ftp_use_passive = the_site->ftp_pasv_mode;

    ret = CALL(init)( the_site->remote_root, the_site->server, the_site->port,
		      the_site->username, the_site->password );
    switch( ret ) {
    case PROTO_LOOKUP:
	return SITE_LOOKUP;
    case PROTO_CONNECT:
	return SITE_CONNECT;
    case PROTO_AUTH:
	return SITE_AUTH;
    default:
	break;
    }

    ret = site_synch_create_directories( the_site );
    if( ret == 0 ) {
	ret = site_synch_files( the_site );
	if( ret == 0 ) {
	    ret = site_synch_remove_directories( the_site );
	}
    }
    
    CALL(finish)( );

    if( ret == 0 ) {
	return SITE_OK;
    } else {
	return SITE_ERRORS;
    }

}

int site_update_create_directories( struct site_t *the_site, bool onlymarked) {
    struct site_file_t *current;
    int ret = 0;

    for( current=the_site->files; current!=NULL; current=current->next ) {
	if( onlymarked && !current->marked ) continue;
	if( current->dir && (current->diff == file_new ) ) {
	    /* New dir! */
	    if( fe_prompting ) if( !fe_can_update( current ) ) continue;
	    fe_updating( current );
	    if( (*the_site->driver->dir_create)( current->full_remote ) != PROTO_OK ) {
		fe_updated( current, false, the_site->driver->last_error );
		ret = 1;
	    } else {
		fe_updated( current, true, NULL );
		current->updated = true;
	    }
	}
    }

    return ret;
}

int site_update_delete_files( struct site_t *the_site, bool onlymarked ) {
    struct site_file_t *current;
    int ret = 0;

    for( current=the_site->files; current!=NULL; current=current->next ) {
	if( onlymarked && !current->marked ) continue;
	/* Skip directories and links, and only do deleted files on
	 * this pass */
	if( (current->diff == file_deleted ) &&
	    !( current->dir || current->link ) ) {
	    if( fe_prompting ) if( !fe_can_update( current ) ) break;
	    fe_updating( current );
	    if( (*the_site->driver->file_delete)( current->full_remote ) != PROTO_OK ) {
		fe_updated( current, false, the_site->driver->last_error  );
		ret = 1;
	    } else {
		/* Successful update - file was deleted */
		fe_updated( current, true, NULL );
		current->updated = true;
	    }
	}
	    
    }
    
    return ret;

}

/* Does everything but file deletes */
int site_update_files( struct site_t *the_site, bool onlymarked ) {
    struct site_file_t *current;
    char *driver_error = the_site->driver->last_error;
    int ret = 0;

    for( current=the_site->files; current!=NULL; current=current->next ) {
	if( current->dir || current->link || 
	    (onlymarked && !current->marked) ) continue;
	switch( current->diff ) {
	case file_new: /* File is new, upload it */
	    if( fe_prompting ) if( !fe_can_update( current ) ) break;
	    fe_updating( current );
	    if( CALL(file_upload)( current->full_local, current->full_remote, current->isascii ) != PROTO_OK ) {
		fe_updated( current, false, driver_error );
		ret = 1;
	    } else { 
		/* Successful upload... now we need to chmod it? */
		if( (the_site->perms == sitep_all) ||
		    ( (current->mode & S_IXUSR) &&
		      (the_site->perms == sitep_exec) ) ) {
		    /* We need to chmod the file ... */
		    if( CALL(file_chmod)( current->full_remote, current->mode ) != PROTO_OK ) {
			fe_updated( current, false, the_site->driver->last_error );
			ret = 1;
		    } else {
			fe_updated( current, true, NULL );
			current->updated = true;
		    }
		} else {
		    fe_updated( current, true, NULL );
		    current->updated = true;
		}
	    }
	    break;
	    
	case file_changed: /* File has changed, upload it */
	    if( fe_prompting ) if( !fe_can_update( current ) ) continue;
	    if( the_site->nooverwrite ) {
		/* Must delete remote file before uploading new copy.
		 * FIXME: Icky hack to convince the FE we are about to
		 * delete the file */
		current->diff = file_deleted;
		fe_updating( current );
		if( CALL(file_delete)( current->full_remote ) != PROTO_OK ) {
		    fe_updated( current, false, driver_error );
		    ret = 1;
		    current->diff = file_changed;
		    /* Don't upload it! */
		    break;
		} else {
		    fe_updated( current, true, NULL );
		    current->diff = file_changed;
		}
	    }
	    fe_updating( current );
	    /* Now, upload it */
	    if( CALL(file_upload)( current->full_local, current->full_remote, current->isascii ) != PROTO_OK ) {
		fe_updated( current, false, driver_error );
		ret = 1;
	    } else {
		/* Successful upload... now we need to chmod it? */
		if( (the_site->perms == sitep_all) ||
		    ( (current->mode & S_IXUSR) &&
		      (the_site->perms == sitep_exec) ) ) {
		    /* We need to chmod the file ... */
		    if( CALL(file_chmod)( current->full_remote, current->mode ) != PROTO_OK ) {
			fe_updated( current, false, the_site->driver->last_error );
			ret = 1;
		    } else {
			fe_updated( current, true, NULL );
			current->updated = true;
		    }
		} else {
		    fe_updated( current, true, NULL );
		    current->updated = true;
		}
	    }
	    break;
		
	case file_moved:
	    /* The file has been moved */
	    if( fe_prompting ) {
		if( !fe_can_update( current ) ) break;
	    } else {
		fe_updating( current );
	    }
	    if( CALL(file_move)( current->old->full_remote, 
				current->full_remote ) != PROTO_OK ) {
		ret = 1;
		fe_updated( current, false, driver_error );
	    } else {
		/* Successful update - file was moved */
		current->updated = true;
		fe_updated( current, true, NULL );
	    }
	    break;
	    
	default: /* Ignore everything else */
	    break;
	}
    }

    return ret;
    
}

int site_update_delete_directories( struct site_t *the_site, bool onlymarked ){
    struct site_file_t *current;
    int ret = 0;

    /* This one must iterate through the list BACKWARDS, so
     * directories are deleted bottom up */
    for( current=the_site->files_tail; current!=NULL; current=current->prev ) {
	if( onlymarked && !current->marked ) continue;
	if( current->dir && (current->diff == file_deleted ) ) {
	    if( fe_prompting ) if( !fe_can_update( current ) ) continue;
	    fe_updating( current );
	    if( (*the_site->driver->dir_remove)( current->full_remote ) != PROTO_OK ) {
		ret = 1;
		fe_updated( current, false, the_site->driver->last_error );
	    } else {
		/* Successful delete */
		current->updated = true;
		fe_updated( current, true, NULL );
	    }
	}
    }
    return ret;
}

int site_update_links( struct site_t *the_site, bool onlymarked ) {
    struct site_file_t *current;
    int ret = 0;

    for( current=the_site->files; current!=NULL; current=current->next ) {
	if( onlymarked && !current->marked ) continue;
	if( current->link ) {
	    switch( current->diff ) {
	    case file_new:
		fe_updating( current );
		if( (*the_site->driver->link_create)( current->full_remote,
				      current->locallink ) != PROTO_OK ) {
		    fe_updated( current, false, the_site->driver->last_error );
		    ret = 1;
		} else {
		    fe_updated( current, true, NULL );
		    current->updated = true;
		}
		break;
	    case file_changed:
		fe_updating( current );
		if( (*the_site->driver->link_change)( current->full_remote,
				      current->locallink ) != PROTO_OK ) {
		    fe_updated( current, false, the_site->driver->last_error );
		    ret = 1;
		} else {
		    fe_updated( current, true, NULL );
		    current->updated = true;
		}
		break;
	    case file_deleted:
		fe_updating( current );
		if( (*the_site->driver->link_delete)( current->full_remote ) !=
		    PROTO_OK ) {
		    fe_updated( current, false, the_site->driver->last_error );
		    ret = 1;
		} else {
		    fe_updated( current, true, NULL );
		    current->updated = true;
		}
	    default:
		break;
	    }
	}
    }
    return ret;

}


/* Updates the remote site with the local copy.
 * This is the core functionality of sitecopy.
 * All we do is compare the local files list with the remote files
 * list, and upload any changed or new files.
 * Directories are treated differently - any new ones must be created BEFORE
 * any files are uploaded, and any old ones must be deleted AFTER any files
 * are deleted, since most FTP servers will not allow you to DELE a 
 * non-empty directory.
 */
int site_update( struct site_t *the_site, bool onlymarked )  {
    int ret = 0;
    /* FIXME: screeech... i don't like this at all */
    ftp_use_passive = the_site->ftp_pasv_mode;

    ret = CALL(init)( the_site->remote_root, the_site->server, the_site->port,
		      the_site->username, the_site->password );
    switch( ret ) {
    case PROTO_LOOKUP:
	return SITE_LOOKUP;
    case PROTO_CONNECT:
	return SITE_CONNECT;
    case PROTO_AUTH:
	return SITE_AUTH;
    default:
	break;
    }

    ret = site_update_create_directories( the_site, onlymarked );
    if( ret == 0 || site_keepgoing ) {
	/* Delete files first, since they might be quota-limited
	 * on the remote site */
	if( !the_site->nodelete ) {
	    ret += site_update_delete_files( the_site, onlymarked );
	}
	if( ret == 0 || site_keepgoing ) {
	    ret += site_update_files( the_site, onlymarked );
	    if( (ret == 0 || site_keepgoing) && 
		(the_site->symlinks == sitesym_maintain ) ) {
		ret += site_update_links( the_site, onlymarked );
	    }
	    if( (ret == 0 || site_keepgoing) && !the_site->nodelete ) {
		ret += site_update_delete_directories( the_site, onlymarked );
	    }
	}
    }

    CALL(finish)( );

    if( ret == 0 ) {
	/* Site updated successfully.
	 * Mark the site as 'unchanged' so, in a GUI FE, they
	 * don't try and update it again. Hopefully.
	 */
	the_site->is_different = false;
	return SITE_OK;
    } else {
	/* Update not totally successfull */
	return SITE_ERRORS;
    }

}

/* This populates the additional names in the site_file_t struct given,
 * using information in the given site */
void site_assignnames( struct site_file_t *the_file, struct site_t *the_site ){

    the_file->full_remote = malloc( strlen(the_site->remote_root) + 
				    strlen(the_file->directory) +
				    strlen(the_file->filename) + 1 );
    the_file->full_local = malloc( strlen(the_site->local_root) + 
				   strlen(the_file->directory) +
				   strlen(the_file->filename) + 1 );
    the_file->rel_local = malloc( strlen(the_file->directory) +
				  strlen(the_file->filename) + 2 );
    the_file->rel_remote = malloc( strlen(the_file->directory) +
				   strlen(the_file->filename) + 2 );
    
    strcpy( the_file->full_remote, the_site->remote_root );
    strcat( the_file->full_remote, the_file->directory );
    strcat( the_file->full_remote, the_file->filename );

    strcpy( the_file->full_local, the_site->local_root );
    strcat( the_file->full_local, the_file->directory );
    strcat( the_file->full_local, the_file->filename );

    strcpy( the_file->rel_local, "/" );
    strcat( the_file->rel_local, the_file->directory );
    strcat( the_file->rel_local, the_file->filename );

    strcpy( the_file->rel_remote, "/" );
    strcat( the_file->rel_remote, the_file->directory );
    strcat( the_file->rel_remote, the_file->filename );

}

/* This reads off the remote files and the local files */
int site_readfiles( struct site_t *the_site ) {
    if( site_readremotefiles( the_site ) < 0 )
	return -1;
    if( site_readlocalfiles( the_site ) )
	return -2;
    /* Check for moved files - compare new files with deleted ones */
    if( the_site->checkmoved ) {
	site_checkmoved( the_site );
    }
    /* Summing up... */
    if( (the_site->numchanged +
	 the_site->nummoved + 
	 the_site->numnew +
	 (the_site->nodelete?0:the_site->numdeleted) ) > 0 ) {
	the_site->is_different = true;
    } else {
	the_site->is_different = false;
    }
    return 0;
}

int site_readremotefiles( struct site_t *the_site ) {
    FILE *fp;
    char buf[BUFSIZ], /* for the line */
	tmp[BUFSIZ], /* for the current field */
	*pos, /* for the current position within buf */
	*point, /* for the curpos within tmp */
	*this_file = NULL;
    struct site_file_t *current;
    size_t dir_length;
    int state;
    /* Get the remote files from the storage file */
    DEBUG( DEBUG_FILES, "Reading info file: %s\n", the_site->infofile );
    fp = fopen( the_site->infofile, "r" );
    if( fp == NULL ) {
	/* This is not an error condition, since the info file WILL NOT 
	 * exist until the site has been created, which is okay, just means
	 * there are 'no' files on the remote site */
	return 1;
    }
    /* The file exists, so read it.
     * Format: one item / line, tab-separated fields.
     * First field is filename of item.
     * Second field is 'dir' for directory items, or mtime for files
     * Third field (files only) is size of file */
    current = NULL;
    while( fgets( buf, BUFSIZ, fp) != NULL ) {
	/* Create a new file item and lob it on the end of the linked 
	 * list */
	current = file_append( the_site );
	/* Make sure we have an end-of-buffer */
	buf[BUFSIZ-1] = '\0';
	this_file = NULL;
	/* Now parse the line. Simple DFA, states are:
	 *  0: Reading filename, field 1
	 *  1: Reading date/time stamp or DIRWORD, field 2
	 *  2: Reading file size, field 3 (if file)
	 *  3: Junk state - consume-all-crud-till-end-of-line.
	 * 
	 * We read the current field into tmp char by char.
	 * point is the current point within tmp. */
	state = 0;
	point = tmp;
	for( pos = buf; *pos!='\0'; pos++ ) {
	    if( *pos < 0 ) state = 5;
	    switch( state ) {
	    case 0:
		if( *pos == '\t' ) {
		    /* End of the filename */
		    *point = '\0';
		    this_file = strdup( tmp );
		    point = tmp;
		    state = 1;
		} else {
		    /* Still in the filename */
		    *(point++) = *pos;
		}
		break;
	    case 1:
		if( *pos == '\t' || *pos == '\n' ) {
		    /* End of the second field */
		    *point = '\0';
		    if( strlen( tmp ) > 0 ) {
			if( strcmp( tmp, DIRWORD ) == 0 ) {
			    /* It's a directory! */
			    current->dir = true;
			    state = 3; /* that's all we need */
			} else if( strcmp( tmp, LINKWORD ) == 0 ) {
			    current->link = true;
			    point = tmp;
			    state = 4; /* read the link target */
			} else {
			    /* It's a file! - field 2 is the mtime */
			    current->dir = false;
			    current->remotetime = atol( tmp );
			    point = tmp;
			    state = 2;
			}
		    } else {
			/* Corrupt line, we need at least two fields */
			/* FIXME: Report this to the user. */
			state = 5;
		    }			
		} else {
		    /* Within the second field */
		    *(point++) = *pos;
		}
		break;
	    case 2:
		if( *pos == '\n' ) {
		    /* End of the size field */
		    *point = '\0';
		    current->remotesize = atol( tmp );
		    state = 3;
		} else {
		    /* Within the file size field */
		    *(point++) = *pos;
		}
		break;
	    case 3: /* junk state */
		break;
	    case 4: /* read the link name */
		if( *pos == '\n' ) {
		    /* End of the field */
		    *point = '\0';
		    current->remotelink = strdup( tmp );
		    state = 3;
		} else {
		    *(point++) = *pos;
		}
	    case 5: /* error state */
		break;
	    }
	}
	if( this_file==NULL || state == 5 ) {
	    /* FIXME: Report this to the user */
	    DEBUG( DEBUG_FILES, "Corrupt line.\n" );
	    file_delete( the_site, current );
	    continue;
	}
	current->diff = file_deleted;
	current->filename = strdup( base_name( this_file ) );
	/* we are going to chop the leading / of this_file */
	if( strlen(current->filename) > strlen(this_file) ) {
	    /* FIXME: Handle these errors properly */
	    DEBUG( DEBUG_FILES, "Very corrupt line.\n" );
	    free( current->filename );
	    file_delete( the_site, current );
	    continue;
	}
	dir_length = strlen(this_file) - strlen(current->filename ) - 1;
	current->directory = malloc( dir_length + 1 );
	strncpy( current->directory, this_file + 1, dir_length );
	*(current->directory+dir_length) = '\0'; /* null-term */
	DEBUG( DEBUG_FILES, "Split %s is: Dir [%s], name [%s]\n",
	       this_file, current->directory, current->filename );
	the_site->numdeleted++;
	site_assignnames( current, the_site );
	DEBUG( DEBUG_FILES, "Remote: [%s] - file: size: %ld  time: %ld\n",
	       current->filename, current->remotesize, current->remotetime );
	DEBUG( DEBUG_FILES, 
	       "Directory: [%s]  Filename: %s\n"
	       "rel_local: %s  rel_remote: %s\n"
	       "full_local: %s  full_remote: %s\n",
	       current->directory, current->filename,
	       current->rel_local, current->rel_remote, 
	       current->full_local, current->full_remote );
	free( this_file );
    }
    fclose( fp );
    return 0;
}

/* Read the local site files... 
 * A stack is used for directories within the site - this is not recursive.
 * Each item on the stack is a FULL PATH to the directory, i.e., including
 * the local site root. */

int site_readlocalfiles( struct site_t *the_site ) {
    char *dirstack[MAXDIRS], *this;
    char *full = NULL, *fname, *temp;
    struct stat item;
    DIR *curdir;
    struct dirent *ent;
    int dirtop = 0;
    size_t dir_length;
    struct site_file_t *current;

    /* Push the root directory on to the stack */
    dirstack[dirtop++] = strdup( the_site->local_root );
    
    /* Now, for all items in the stack, process all the files, and
     * add the dirs to the stack. Everything we put on the stack is
     * temporary and gets freed eventually. */

    while( dirtop > 0 ) {
	/* Pop the stack */
	this = dirstack[--dirtop];
	
	DEBUG( DEBUG_FILES, "Dir: %s\n", this );
	curdir = opendir( this );
	if( curdir == NULL ) {
	    free( this );
	    continue;
	}

	/* Now read all the directory entries */

	while( (ent = readdir( curdir )) != NULL ) {
	    if( full != NULL ) 
		free( NULL );
	    full = malloc( strlen( this ) + strlen( ent->d_name ) + 1 );
	    strcpy( full, this );
	    strcat( full, ent->d_name );
	    DEBUG( DEBUG_FILES, "Item: %s - ", ent->d_name );
#ifndef __EMX__
 	    if( lstat( full, &item ) == -1 ) {
#else
	    /* There are no symlinks under OS/2, use stat() instead */
	    if( stat( full, &item ) == -1 ) {
#endif
		/* FIXME: FE warning here */
		continue;
	    }
#ifndef __EMX__
	    /* Is this a symlink? */
	    if( S_ISLNK( item.st_mode ) ) {
		DEBUG( DEBUG_FILES, "symlink - " );
		if( the_site->symlinks == sitesym_ignore ) {
		    /* Just skip it */
		    DEBUG( DEBUG_FILES, "ignoring.\n" );
		    continue;
		} else if( the_site->symlinks == sitesym_follow ) {
		    DEBUG( DEBUG_FILES, "followed - " );
		    /* Else, carry on as normal, stat the real file */
		    if( stat( full, &item ) == -1 ) {
			/* It's probably a broken link */
			DEBUG( DEBUG_FILES, "broken.\n" );
			continue;
		    }
		} else {
		    DEBUG( DEBUG_FILES, "maintained:\n" );
		}
	    }
#endif /* __EMX__ */
	    /* Now process it */

	    /* This is the rel_local of this file - i.e., everything
	     * apart from the local root */
	    fname = (char *)full+strlen(the_site->local_root)-1;

	    /* Check for excludes */
	    if( file_isexcluded( ent->d_name, fname, the_site ) )
		continue;

   
	    for( current=the_site->files; current!=NULL; current=current->next ) {
		if( strcmp( current->rel_local, fname ) == 0 )
		    /* Match found! */
		    break;
/*		last = current; */
	    }
	    
	    if( S_ISREG( item.st_mode ) ) {
		DEBUG( DEBUG_FILES, "file - " );

		if( current == NULL ) {
		    /* This is a new local file... */

		    DEBUG( DEBUG_FILES, "new - " );
		    the_site->numnew++;
		    /* Add a new file to the beginning of the list */
		    current = file_prepend( the_site );
		    current->filename = strdup( base_name( fname ) );
		    /* This is the length of the directory name...
		     * fname = "/full/file/name.ext", filename = "name.ext"
		     * we want directory = "full/file/"... do the maths */
		    dir_length = strlen(fname) - strlen(current->filename)-1;
		    DEBUG( DEBUG_FILES, "dir_length = %ld\n", (long) dir_length );
		    current->directory = malloc( dir_length + 1 );
		    strncpy( current->directory, fname+1, dir_length );
		    *(current->directory+dir_length) = '\0'; /* nullterm */
		    DEBUG( DEBUG_FILES, "directory = %s\n", current->directory );
		    site_assignnames( current, the_site );
		    current->localtime = item.st_mtime;
		    current->localsize = item.st_size;
		    current->diff = file_new;
		    current->mode = item.st_mode;
		    current->dir = false;
		    the_site->totalnew += item.st_size;
/*		    DEBUG( DEBUG_FILES, "new; size: %ld, time: %ld\n", item.st_size, item.st_mtime );  */
		} else {
		    /* Match found... compare it */
		    current->localtime = item.st_mtime;
		    current->localsize = item.st_size;
		    the_site->numdeleted--;
		    if( (current->localtime > current->remotetime) ||
			(current->localsize != current->remotesize) ) {
			/* Changed file */
			current->diff = file_changed;
			current->mode = item.st_mode;
			the_site->numchanged++;
			the_site->totalchanged += item.st_size;
			DEBUG( DEBUG_FILES, "changed (%ld -> %ld)\n", current->localtime, current->remotetime );
		    } else {
			current->diff = file_unchanged;
			current->updated = true;
			the_site->numunchanged++;
			DEBUG( DEBUG_FILES, "unchanged.\n" );
		    }
		    /* The names will already have been assigned */
		}
				    
		/* Assign the ASCII type value */
		current->isascii = file_isascii( fname, the_site );

	    } else if( S_ISDIR( item.st_mode ) ) {
		/* THIS IS A DIRECTORY! */
		if( strcmp( ent->d_name, "." )==0 ||
		    strcmp( ent->d_name, ".." )==0 ) {
		    DEBUG( DEBUG_FILES, "ignored.\n" );
		    continue;
		}
		
		DEBUG( DEBUG_FILES, "directory - " );
		if( dirtop < MAXDIRS ) {
		    temp = malloc( strlen( full ) + 2 );
		    strcpy( temp, full );
		    strcat( temp, "/" );
		    dirstack[dirtop++] = temp;
		} else {
		    /* No more room in stack
		     * FIXME: Report this to the user.*/
		}
		if( current == NULL ) {
		    /* New Directory */
		    the_site->numnew++;
		    current = file_append( the_site );
		    current->filename = strdup( base_name( fname ) );
		    /* Work out the directory name */
		    dir_length = strlen(fname) - strlen(current->filename )-1;
		    current->directory = malloc( dir_length + 1 );
		    strncpy( current->directory, fname+1, dir_length );
		    *(current->directory+dir_length) = '\0'; /* null-term */
		    site_assignnames( current, the_site );
		    current->dir = true;
		    current->diff = file_new;
		    current->mode = item.st_mode;
		    current->localsize = item.st_size;
		    DEBUG( DEBUG_FILES, "new.\n" ); 
		} else {
		    /* It's an existing directory */
		    the_site->numdeleted--;
		    the_site->numunchanged++;
		    current->diff = file_unchanged;
		    current->updated = true;
		    DEBUG( DEBUG_FILES, "existing.\n" ); 
		}
	    } 
#ifndef __EMX__
	    else if( S_ISLNK( item.st_mode ) ) {
		char tmp[BUFSIZ];
		memset( tmp, 0, BUFSIZ );
		DEBUG( DEBUG_FILES, "symlink being maintained.\n" );
		if( readlink( full, tmp, BUFSIZ ) == -1 ) {
		    DEBUG( DEBUG_FILES, "readlink failed: %s\n", 
			   strerror(errno) );
		    continue;
		}
		DEBUG( DEBUG_FILES, "Link target: %s\n", tmp );
		if( current == NULL ) {
		    /* New link */
		    the_site->numnew++;
		    current = file_append( the_site );
		    current->filename = strdup( base_name( fname ) );
		    dir_length = strlen(fname) - strlen(current->filename)-1;
		    DEBUG( DEBUG_FILES, "dir_length = %ld\n", (long) dir_length );
		    current->directory = malloc( dir_length + 1 );
		    strncpy( current->directory, fname+1, dir_length );
		    *(current->directory+dir_length) = '\0'; /* nullterm */
		    DEBUG( DEBUG_FILES, "directory = %s\n", current->directory );
		    current->locallink = strdup( tmp );
		    current->diff = file_new;
		    current->link = true;
		    site_assignnames( current, the_site );
		} else {
		    /* Existing link... compare */
		    the_site->numdeleted--;
		    current->locallink = strdup( tmp );
		    if( strcmp( current->remotelink, tmp ) == 0 ) {
			/* Links match */
			current->updated = true;
			current->diff = file_unchanged;
			the_site->numunchanged++;
		    } else {
			current->diff = file_changed;
			the_site->numchanged++;
		    }
		}
	    }
#endif /* __EMX__ */
	    else {
		DEBUG( DEBUG_FILES, "something else.\n" );
	    }
	    DEBUG( DEBUG_FILES, 
		   "Directory: [%s]  Filename: %s\n"
		   "rel_local: %s  rel_remote: %s\n"
		   "full_local: %s  full_remote: %s\n",
		   current->directory, current->filename,
		   current->rel_local, current->rel_remote,
		   current->full_local, current->full_remote );
	}
	/* Close the open directory */
	closedir( curdir );
	/* And we're finished with this */
	free( this );
    }
    return 0;
}

void site_checkmoved( struct site_t *the_site ) {
    /* Have any files been moved?
     * We look for new files, then see if we can find a deleted file
     * which matches filename, mod time, and size exactly.
     * 
     * This is simplistic, and it is possible to get it wrong in some
     * (unusual) circumstances:
     *    Old: /bar/file1.zip, /foo/file1.zip 
     *    New: /new/file1.zip.
     * If all the file1.zip's have the same size and mod time, it
     * IS NOT POSSIBLE to know which one has become /new/file1.zip without 
     * storing remembering the contents of /bar/file1.zip somehow and 
     * comparing, or using a filesystem which remembers stuff. In any case
     * it's pretty silly to try - so the whole shabang is optional.
     *
     * It would probably be better to search for DELETED files, then 
     * check them against NEW files, since files are more often added
     * to sites then they are deleted. But hey, this is C, who'll notice?
     */
    struct site_file_t *search, *current;
    /*  char *bname, *tmp; */
    /* First off, find the new files... */
    DEBUG( DEBUG_FILES, "Checking for moved files...\n" );
    for( search=the_site->files; search!=NULL; search=search->next ) {
	if( search->diff != file_new )
	    continue;
	/* OK, we have us a new file... now iterate through the deleted
	 * files and look for a match */
	DEBUG( DEBUG_FILES, "- New file: %s\nComparing:", search->rel_local );
	for( current=the_site->files; current!=NULL ; current=current->next ) {
	    if( (current->diff != file_deleted) || current->dir ) {
		/* Ignore files which aren't deleted, and dirs, completely.
		 * (search->diff==file_new, so we DON'T need to check
		 * that current != search as well) */
		continue;
	    }
	    DEBUG( DEBUG_FILES, " %s", current->rel_local );
	    if( (strcmp( search->filename, current->filename ) == 0) &&
		current->remotesize == search->localsize &&
		current->remotetime == search->localtime ) {
		DEBUG( DEBUG_FILES, " - matched!\n" );
		break;
	    }
	}
	if( current != NULL ) {
	    /* OK, current == the deleted file, search == the new file.
	     * First make sure search knows where it WAS before the move... */
	    search->old = current;
	    /* Remove the deleted file from the list so it doesn't
	     * actually get deleted by site_update */
	    file_delete( the_site, current );
	    /* Diddle the totals a bit */
	    search->diff = file_moved;
	    the_site->numdeleted--;
	    the_site->numnew--;
	    the_site->nummoved++;
	} else {
	    DEBUG( DEBUG_FILES, " - No match found.\n" );
	}
    }
    DEBUG( DEBUG_FILES, "Finished checking for moved files.\n" );
}

/* This writes the remote files list back to the file
 * and has some hairy logic in the middle, but is otherwise
 * pretty simple.
 * TODO: This is crap. What we should really do is only 
 * write out the remote file info, regardless... that is what the 
 * info file is for. This would GREATLY simplify this entire function.
 * Would mean simply that in site_update, copying over the
 * modtimes + sizes.
 */

#define WRITE_REMOTE_FILE( f ) \
fprintf( fp, "%s\t%ld\t%ld\n", f->rel_remote, f->remotetime, f->remotesize );
#define WRITE_LOCAL_FILE( f ) \
fprintf( fp, "%s\t%ld\t%ld\n", f->rel_local, f->localtime, f->localsize );
#define WRITE_REMOTE_LINK( f ) \
fprintf( fp, "%s\t%s\t%s\n", f->rel_remote, LINKWORD, f->remotelink );
#define WRITE_LOCAL_LINK( f ) \
fprintf( fp, "%s\t%s\t%s\n", f->rel_local, LINKWORD, f->locallink );

int site_writefiles( struct site_t *the_site ) {
    FILE *fp;
    struct site_file_t *current;
    fp = fopen( the_site->infofile, "w" );
    if( fp == NULL ) {
	return -1; 
    }
    for( current = the_site->files; current!=NULL; current=current->next ) {
	if( current->dir ) {
	    if( current->updated ) {
		/* DIR: Updated */
		switch( current->diff ) {
		case file_new:
		case file_unchanged:
		    /* It's new, or it's already there... */
		    fprintf( fp, "%s\t%s\n", current->rel_local, DIRWORD );
		    break;
		case file_moved: /* invalid */
		case file_changed: /* invalid */
		case file_deleted:/* it's not there now */
		default:
		    break;
		} 
	    } else {
		/* DIR: Not Updated */
		switch( current->diff ) {
		case file_unchanged: /* ... it was there anyway */
		case file_deleted: /* ... but it wasn't deleted */
		    /* So the directory exists remotely */
		    fprintf( fp, "%s\t%s\n", current->rel_remote, DIRWORD );
		    break;
		case file_new: /* ... but it wasn't created */
		default:
		    /* So the directory does not exist remotely */
		    break;
		}
	    }
	} else if( current->link ) {
	    if( current->updated ) {
		/* LINK: Updated */
		switch( current->diff ) {
		case file_new: /* it's there now */
		case file_unchanged: /* it's still there now */
		case file_changed: /* it's there now but differently */
		    WRITE_LOCAL_LINK( current );
		    break;
		case file_deleted: /* it's not there now */
		default: 
		    break;
		}
	    } else {
		/* LINK: Not Updated */
		switch( current->diff ) {
		case file_unchanged: /* it's still there anyway  */
		case file_changed:   /* it's still there anyway */
		case file_deleted: /* but actually still there */
		    WRITE_REMOTE_LINK( current );
		    break;
		case file_new: /* it's not there */
		default:
		    break;
		}
	    }
	} else {
	    if( current->updated ) {
		/* FILE: Updated */
		switch( current->diff ) {
		case file_unchanged:
		    /* Write state of remote file */
		    WRITE_REMOTE_FILE( current );
		    break;
		case file_moved:
		case file_changed:
		case file_new:
		    /* Write state of local file */
		    WRITE_LOCAL_FILE( current );
		    break;
		case file_deleted:
		    /* So don't write anything out */
		    break;
		}
	    } else {
		/* FILE: Not Updated */
		switch( current->diff ) {
		case file_changed: /* ... but it hasn't been updated */
		case file_unchanged: 
		case file_deleted: /* ... but it hasn't been deleted */
		    /* Write out the remote time */
		    WRITE_REMOTE_FILE( current );
		    break;
		case file_moved: 
		    /* But it hasn't actually been moved, so write out
		     * the 'deleted' file (where the moved file WAS), and 
		     * ignore the 'new' file (where it was moved TO) */
		    WRITE_REMOTE_FILE( current->old );
		    break;
		case file_new: /* So ignore it... */
		    break;
		}
	    }
	}
    }
    fclose( fp );
    return 0;
}

#undef WRITE_REMOTE_FILE
#undef WRITE_LOCAL_FILE

/* Simply marks all the files in the given site as 'updated'.
 * For 'dynamic updating', this will do, by diff==
 *   file_deleted: Remove the file from the list
 *   file_changed, file_new: Set diff=file_unchanged
 *   file_moved: Remove the ->old, set diff=file_unchanged
 *   file_unchanged: Do nothing.
 */
void site_catchup( struct site_t *the_site ) {
    struct site_file_t *current;
    for( current=the_site->files; current!=NULL; current=current->next ) {
	current->updated = true;
    }
    /* Mark it as mirrored */
    the_site->is_different = false;
}

/* Reinitializes the site - clears any remote files
 * from the list, and marks all other files as 'new locally'.
 */
void site_initialize( struct site_t *the_site ) {
    struct site_file_t *current, *next;
    
    current = the_site->files;

    while( current!= NULL ) {
	next = current->next;
	switch( current->diff ) {
	case file_deleted:
	    file_delete( the_site, current );
	    the_site->numdeleted--;
	    break;
	case file_moved:
	    /* Current is the NEW file, current->old is the DELETED
	     * file. So, simply nuke current->old. */
	    current->old = NULL;
	    current->updated = false;
	    the_site->nummoved--;
	    the_site->numnew++;
	    current->diff = file_new;
	    break;
	case file_changed:
	    current->updated = false;
	    the_site->numchanged--;
	    the_site->numnew++;
	    current->diff = file_new;
	    break;
	case file_unchanged:
	    current->updated = false;
	    the_site->numunchanged--;
	    the_site->numnew++;
	    current->diff = file_new;
	    break;
	case file_new:
	    current->updated = false;
	    break;
	}
	current = next;
    }
    
    if( the_site->files == NULL ) {
	/* There are no files remotely OR locally, so the
	 * sites are the same */
	the_site->is_different = false;
    } else {
	/* Otherwise, we KNOW that there are no files remotely
	 * since we just removed them from memory, so any 
	 * remaining files must be local ones. Hence, the sites
	 * ARE different */
	the_site->is_different = true;
    }

}

/* This walks the files list as returned by the protocol driver,
 * and converts it into a 'real' files list. */
void site_fetch_walk( struct site_t *the_site,
		      struct proto_file_t *files ) {
    struct proto_file_t *this_file, *next_file;
    struct site_file_t *file;
    char rel_local[BUFSIZ]; /* temporary */

    site_destroy( the_site );

    this_file = files;
    while( this_file != NULL ) {
	/* Check whether the filename is excluded.  Mock up a rel_local
	 * for it. Bit noddy */
	/* rel_local starts with a / */
	rel_local[0] = '/'; rel_local[1] = '\0';
	/* FIXME: Buffer ovverrun here. */
	strcat( rel_local, this_file->directory );
	strcat( rel_local, this_file->filename );
	
	if( !file_isexcluded( this_file->filename, rel_local, the_site ) ) {
	    /* Add the file to the list */
	    if( this_file->isdir ) {
		file = file_append( the_site );
	    } else {
		file = file_prepend( the_site );
	    }
	    
	    /* Pointer copy the filename and directory strings...  nothing
	     * to free out of this_file now. */
	    file->filename = this_file->filename;
	    file->directory = this_file->directory;
	    file->dir = this_file->isdir;
	    /* Sort out the names */
	    site_assignnames( file, the_site );
	    
	    file->diff = file_deleted;
	    file->updated = false;
	    /* ... so, it exists remotely only and hasn't been deleted.
	     * bit of a hack, but okay for the moment... */
	    
	    if( ! this_file->isdir ) {
		file->remotesize = this_file->size;
		file->remotetime = this_file->modtime;
	    }
	    
	    fe_fetch_found( file );
	    
	}

	next_file = this_file->next;
	free( this_file );
	this_file = next_file;
    }

}

/* Updates the remote file list... site_fetch_callback is called for
 * every remote file found.
 */
int site_fetch( struct site_t *the_site ) {
    struct proto_file_t *files = NULL;
    int ret;

    if( CALL(fetch_list) == NULL ) {
	return SITE_UNIMPL;
    }

    ftp_use_passive = the_site->ftp_pasv_mode;

    ret = CALL(init)( the_site->remote_root, the_site->server, the_site->port,
		      the_site->username, the_site->password );
    switch( ret ) {
    case PROTO_LOOKUP:
	return SITE_LOOKUP;
    case PROTO_CONNECT:
	return SITE_CONNECT;
    case PROTO_AUTH:
	return SITE_AUTH;
    default:
	break;
    }

    ret = CALL(fetch_list)( the_site->remote_root, &files );

    CALL(finish)( );
    
    if( ret == PROTO_OK ) {
	/* Copy over the list of files */
	site_fetch_walk( the_site, files );
	return SITE_OK;
    } else {
	return SITE_FAILED;
    }

}

/* Destroys a file */
void site_destroyfile( struct site_file_t *file ) {
    
    free( file->directory );
    free( file->filename );

    free( file->full_local );
    free( file->full_remote );

    free( file->rel_local );
    free( file->rel_remote );

    free( file );

}

/* Called to delete all the files associated with the site */
void site_destroy( struct site_t *the_site ) {
    struct site_file_t *current, *next;

    current = the_site->files;

    while( current != NULL ) {
	next = current->next;
	if( current->old != NULL ) {
	    site_destroyfile( current->old );
	}
	site_destroyfile( current );
	current = next;
    }

    the_site->files = NULL;
    the_site->files_tail = NULL;

}


/* Produces a section of the flat listing output, of all the items
 * with the given diff type in the given site, using the given section
 * name. */
void site_flatlist_items( FILE *f, struct site_t *the_site, 
			  const enum file_diff diff, const char *name ) {
    struct site_file_t *current;
    fprintf( f, "sectstart|%s", name );
    putc( '\n', f );
    for( current = the_site->files; current!=NULL; current=current->next) {
	if( current->diff == diff ) {
	    fprintf( f, "item|%s%s", current->rel_remote,
		    current->dir?"/":"" );
	    if( current->old !=NULL ) {
		fprintf( f, "|%s\n", current->old->rel_remote );
	    } else {
		putc( '\n', f );
	    }	    
	}
    }
    fprintf( f, "sectend|%s\n", name );
}

/* Produce the flat listing output for the given site */
void site_flatlist( FILE *f, struct site_t *the_site ) {
    fprintf( f, "sitestart|%s", the_site->name );
    if( the_site->url )	fprintf( f, "|%s", the_site->url );
    putc( '\n', f );
    if( the_site->numnew > 0 )
	site_flatlist_items( f, the_site, file_new, "added" );
    if( the_site->numchanged > 0 )
	site_flatlist_items( f, the_site, file_changed, "changed" );
    if( the_site->numdeleted > 0 )
	site_flatlist_items( f, the_site, file_deleted, "deleted" );
    if( the_site->nummoved > 0 )
	site_flatlist_items( f, the_site, file_moved, "moved" );
    fprintf( f, "siteend|%s\n", the_site->is_different?"changed":"unchanged" );
}

const char *site_pseudourl( struct site_t *the_site ) {
    static char urlbuf[256];
    snprintf( urlbuf, 256, 
	      "%s://%s%s",
	      the_site->driver->service_name, the_site->server, 
	      the_site->remote_root_user +
	      ( the_site->remote_isrel ? 1 : 0 ) );
    return urlbuf;
}