-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands-file-transfer.c
853 lines (649 loc) · 22.1 KB
/
commands-file-transfer.c
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
#include "commands-file-transfer.h"
//need this for CMD_ IDs
#include "commands-parse.h"
#include "common.h"
#include <sys/ioctl.h>
#define SKIP_EXISTS -1
#define SKIP_AGE -2
#define SKIP_FILETYPE -3
typedef struct
{
TFileStore *SrcFS;
TFileStore *DestFS;
int Command;
int CmdFlags;
int Throttle;
int CompareLevel;
int IntegrityLevel;
unsigned long Transferred;
unsigned long Failed;
unsigned long Total;
double TotalBytes;
char *ActionName;
char *IncludePattern;
char *ExcludePattern;
char *PreCopyHook;
char *PostCopyHook;
ListNode *Vars;
} TTransferContext;
TTransferContext *TransferContextCreate(TFileStore *SrcFS, TFileStore *DestFS, int Command, int CmdFlags, ListNode *Vars)
{
TTransferContext *Ctx;
char *ptr;
Ctx=(TTransferContext *) calloc(1,sizeof(TTransferContext));
Ctx->SrcFS=SrcFS;
Ctx->DestFS=DestFS;
Ctx->Command=Command;
Ctx->CmdFlags=CmdFlags;
Ctx->Vars=Vars;
ptr=GetVar(Vars,"Throttle");
if (ptr) Ctx->Throttle=atoi(ptr);
Ctx->IncludePattern=CopyStr(Ctx->IncludePattern,GetVar(Vars,"IncludePattern"));
Ctx->ExcludePattern=CopyStr(Ctx->ExcludePattern,GetVar(Vars,"ExcludePattern"));
Ctx->PostCopyHook=CopyStr(Ctx->PostCopyHook,GetVar(Vars,"PostCopyHook"));
Ctx->PreCopyHook=CopyStr(Ctx->PreCopyHook,GetVar(Vars,"PreCopyHook"));
if (Command == CMD_PUT) Ctx->ActionName=CopyStr(Ctx->ActionName,"put");
else if (Command == CMD_CREATE) Ctx->ActionName=CopyStr(Ctx->ActionName,"create");
else Ctx->ActionName=CopyStr(Ctx->ActionName,"get");
ptr=GetVar(Vars,"CompareLevel");
Ctx->CompareLevel=FileStoreSelectCompareLevel(SrcFS,DestFS,ptr);
ptr=GetVar(Vars,"IntegrityLevel");
if (StrLen(ptr)) Ctx->IntegrityLevel=FileStoreSelectCompareLevel(SrcFS,DestFS,ptr);
return(Ctx);
}
void TransferContextDestroy(TTransferContext *Ctx)
{
DestroyString(Ctx->IncludePattern);
DestroyString(Ctx->ExcludePattern);
DestroyString(Ctx->ActionName);
free(Ctx);
}
void DisplayTransferStatus(char *Line, unsigned int transferred, unsigned int total, unsigned int *percent, unsigned int secs,int CmdFlags, int Throttle)
{
int result=0, cols=80, bps=0;
char *Tempstr=NULL, *TimeStr=NULL, *ptr=NULL;
static int ThrotCount=0;
struct winsize w;
if (Settings.Flags & FLAG_QUIET) return;
if (CmdFlags & FLAG_QUIET) return;
if (! isatty(1)) return;
ptr=getenv("COLUMNS");
if (ptr) cols=atoi(ptr);
printf("\r");
if (total==0) Tempstr=FormatStr(Tempstr,"%s bytes sent ",GetHumanReadableDataQty(transferred,0));
else
{
result=(transferred * 100) / total;
// if (result != *percent)
{
Tempstr=FormatStr(Tempstr,"%d%% %s of ",result,GetHumanReadableDataQty(transferred,0));
Tempstr=CatStr(Tempstr,GetHumanReadableDataQty(total,0));
}
*percent=result;
}
bps=transferred / secs;
TimeStr=FormatStr(TimeStr," in %d secs %s Bps ",secs,GetHumanReadableDataQty(bps,0) );
if ((Throttle > 0) && (bps > Throttle))
{
ThrotCount++;
usleep(ThrotCount * 250000);
}
else if (ThrotCount > 0) ThrotCount--;
if (ThrotCount > 0) Tempstr=MCatStr(Tempstr,TimeStr, Line, " (throttling) ",NULL);
else Tempstr=MCatStr(Tempstr,TimeStr, Line, " ",NULL);
ioctl(0, TIOCGWINSZ, &w);
if (StrLen(Tempstr) > w.ws_col) Tempstr[w.ws_col]='\0';
printf("%s",Tempstr);
fflush(NULL);
DestroyString(TimeStr);
DestroyString(Tempstr);
}
int AlterFileExtn(TFileStore *FS, int CmdFlags, char *FName, char *NewExtn)
{
char *NewName=NULL;
int result;
NewName=PathChangeExtn(NewName, FName, NewExtn);
result=FileStoreRename(FS,FName,NewName);
printf("ALTER: %s %s\n",FName,NewName);
if (! result) HandleEventMessage(Settings.Flags,"FAIL: Cannot rename %s -> %s",FName,NewName);
else if (CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"OKAY: Renamed %s -> %s",FName,NewName);
DestroyString(NewName);
return(result);
}
int ProcessRename(TFileStore *FS, char *From, char *To, int CmdFlags, ListNode *Vars)
{
ListNode *Items, *Curr;
TFileInfo *FI, *DestFI=NULL;
int result, RetVal=ERR_NOFILES;
char *Tempstr=NULL, *ptr;
if (! FS->RenameFile)
{
printf("Filestore does not support renaming files\n");
return(FALSE);
}
Items=ListCreate();
/*
FileStoreLoadDir(FS,To,Items,0);
Curr=ListGetNext(Items);
if (Curr) DestFI=(TFileInfo *) Curr->Item;
ListClear(Items,NULL);
*/
FileStoreLoadDir(FS,From,Items,0);
Curr=ListGetNext(Items);
//If we don't know about these items, we have to hope that
//the server we are talking to does
if (Curr==NULL)
{
if (FileStoreRename(FS,From,To))
{
FileStoreLoadDir(FS,"",NULL, LIST_REFRESH|LIST_INCLUDE_DIRS);
RetVal=TRANSFER_OKAY;
}
else RetVal=ERR_FAIL;
}
else
{
while (Curr)
{
FI=(TFileInfo *) Curr->Item;
if (DestFI && (DestFI->Type==FTYPE_DIR))
{
//Tempstr=MCopyStr(Tempstr,DestFI->Path,"/",FI->Name,NULL);
Tempstr=MCopyStr(Tempstr,DestFI->Path,NULL);
}
else Tempstr=CopyStr(Tempstr,To);
if (CmdFlags) result=AlterFileExtn(FS,CmdFlags,FI->Path,To);
else result=FileStoreRename(FS,FI->Path,Tempstr);
if (result)
{
if ((RetVal==ERR_NOFILES) ||(RetVal==TRANSFER_OKAY )) RetVal=TRANSFER_OKAY;
else RetVal=ERR_PARTIAL;
}
else
{
if (RetVal==TRANSFER_OKAY) RetVal=ERR_PARTIAL;
else RetVal=ERR_FAIL;
}
Curr=ListGetNext(Curr);
}
}
ListDestroy(Items,FileInfoDestroy);
FileInfoDestroy(DestFI);
DestroyString(Tempstr);
return(RetVal);
}
char *TransferDecidePermissions(TFileInfo *SrcFI)
{
if (StrLen(SrcFI->Permissions)) return(SrcFI->Permissions);
if (SrcFI->Type==FTYPE_DIR) return(GetVar(Settings.Vars,"dirmode"));
if (SrcFI->Type==FTYPE_EXE) return(GetVar(Settings.Vars,"exemode"));
return(GetVar(Settings.Vars,"filemode"));
}
STREAM *InternalCopyOpenSource(TTransferContext *Ctx, TFileInfo *SrcFI)
{
STREAM *S;
char *ptr;
int val=0;
//Specify the DESTINATION size in the source, for use in 'resume' transfers
if (Ctx->DestFS->GetFileSize) SrcFI->ResumePoint=Ctx->DestFS->GetFileSize(Ctx->DestFS, SrcFI->Name);
//Set version info if getting a particular version of file
ptr=GetVar(Ctx->Vars,"Version");
SetVar(SrcFI->Vars,"Version,",ptr);
if (
(Ctx->CmdFlags & FLAG_CMD_RESUME) &&
(Ctx->SrcFS->Features & FS_RESUME_TRANSFERS) &&
(Ctx->DestFS->Features & FS_RESUME_TRANSFERS)
) val |= OPEN_RESUME;
S=Ctx->SrcFS->OpenFile(Ctx->SrcFS,SrcFI, val);
//OPEN SOURCE DONE
//Construct 'write' args. THESE ARE ARGS TO OPEN THE DEST FILE
if (S)
{
ptr=STREAMGetValue(S,"filesize");
if (StrLen(ptr)) SrcFI->Size=atoi(ptr);
else if (Ctx->SrcFS->GetFileSize) SrcFI->Size=Ctx->SrcFS->GetFileSize(Ctx->SrcFS, SrcFI->Path);
}
return(S);
}
STREAM *InternalCopyOpenDest(TTransferContext *Ctx, TFileInfo *SrcFI, TFileInfo *DestFI)
{
STREAM *S;
char *ptr;
int val=0;
//For the destination we are writing the file into the current directory, so never use a full path
//use the name of the source file as the path
if (Ctx->CmdFlags & FLAG_CMD_EXTN_TEMP) DestFI->Path=PathChangeExtn(DestFI->Path, SrcFI->Name, GetVar(Ctx->Vars,"TransferTempExtn"));
else DestFI->Path=CopyStr(DestFI->Path, SrcFI->Name);
DestFI->Permissions=CopyStr(DestFI->Permissions,TransferDecidePermissions(SrcFI));
ptr=GetVar(Ctx->Vars,"ContentType");
if (StrLen(ptr)) DestFI->MediaType=CopyStr(DestFI->MediaType,ptr);
else FileStoreGetFileType(Ctx->SrcFS, DestFI);
val=OPEN_WRITE;
if (Ctx->CmdFlags & FLAG_CMD_PUBLIC) val |= OPEN_PUBLIC;
if (
(Ctx->CmdFlags & FLAG_CMD_RESUME) &&
(Ctx->SrcFS->Features & FS_RESUME_TRANSFERS) &&
(Ctx->DestFS->Features & FS_RESUME_TRANSFERS)
) val |= OPEN_RESUME;
S=Ctx->DestFS->OpenFile(Ctx->DestFS,DestFI, val);
return(S);
}
TProcessingModule *ActivateCompression(TFileStore *FS1, TFileStore *FS2)
{
char *p_CompTypes1, *p_CompTypes2;
char *Token1=NULL, *Token2=NULL, *Selection=NULL;
TProcessingModule *CompressionMod=NULL;
p_CompTypes1=GetVar(FS1->Vars,"CompressionTypes");
p_CompTypes2=GetVar(FS2->Vars,"CompressionTypes");
p_CompTypes1=GetToken(p_CompTypes1," ",&Token1,0);
while (p_CompTypes1)
{
p_CompTypes2=GetToken(p_CompTypes2," ",&Token2,0);
while (p_CompTypes2)
{
if (strcmp(Token1,Token2)==0)
{
Selection=CopyStr(Selection,Token1);
break;
}
if (StrLen(Selection)) break;
p_CompTypes2=GetToken(p_CompTypes2," ",&Token2,0);
}
p_CompTypes1=GetToken(p_CompTypes1," ",&Token1,0);
}
printf("CompSelect: %s\n",Selection);
if (StrLen(Selection)) CompressionMod=StandardDataProcessorCreate("Compression",Selection,"");
DestroyString(Token1);
DestroyString(Token2);
DestroyString(Selection);
return(CompressionMod);
}
int HandleCompression(TProcessingModule *CompressionMod, char *Buffer, int Bytes)
{
}
int InternalCopyFile(TTransferContext *Ctx, TFileInfo *iSrcFI)
{
STREAM *Src=NULL, *Dest=NULL;
TFileInfo *SrcFI=NULL, *DestFI=NULL;
char *Buffer=NULL, *Tempstr=NULL, *ptr;
int BuffSize=4096;
int result, bytes=0, towrite=0;
unsigned int TotalBytes=0, BytesSent=0, percent=0, secs=0, val;
double bigval;
//TFileInfo *FI;
int RetVal=TRANSFER_OKAY;
struct timeval StartTime, Now;
TProcessingModule *CompressionMod=NULL;
if (! Ctx->SrcFS->ReadBytes) return(ERR_READ_NOTSUPPORTED);
if (Ctx->DestFS && (! Ctx->DestFS->WriteBytes)) return(ERR_WRITE_NOTSUPPORTED);
gettimeofday(&StartTime,NULL);
if (Ctx->DestFS && Ctx->DestFS->BuffSize) BuffSize=Ctx->DestFS->BuffSize;
Buffer=SetStrLen(Buffer,BuffSize*2+1);
SrcFI=FileInfoClone(iSrcFI);
DestFI=FileInfoClone(iSrcFI);
DestFI->Path=FileStoreFormatPath(DestFI->Path, Ctx->DestFS, iSrcFI->Name);
//if a PreCopyHook script is specified, then run it
if (StrLen(Ctx->PreCopyHook))
{
Tempstr=FormatStr(Tempstr,"%s '%s' '%s' '%s'",Ctx->PreCopyHook,DestFI->Name,DestFI->Path,"");
system(Tempstr);
}
Src=InternalCopyOpenSource(Ctx, SrcFI);
TotalBytes=SrcFI->Size;
if (Ctx->CmdFlags & FLAG_CMD_RESUME) BytesSent=SrcFI->ResumePoint;
if (Src)
{
if (Ctx->SrcFS->Settings & FS_COMPRESSION) CompressionMod=ActivateCompression(Ctx->DestFS,Ctx->SrcFS);
if (Ctx->DestFS) Dest=InternalCopyOpenDest(Ctx, SrcFI, DestFI);
else Dest=STREAMFromFD(1);
if (Dest)
{
result=Ctx->SrcFS->ReadBytes(Ctx->SrcFS,Src,Buffer,BuffSize*2);
while ((bytes > 0) || (result != EOF))
{
if (CompressionMod) result=HandleCompression(CompressionMod,Buffer,result);
if (result > 0) bytes+=result;
if (bytes > BuffSize) towrite=BuffSize;
else towrite=bytes;
if (Ctx->DestFS) result=Ctx->DestFS->WriteBytes(Ctx->DestFS,Dest,Buffer,towrite);
else result=STREAMWriteBytes(Dest,Buffer,towrite);
if (result==-1)
{
RetVal=ERR_INTERRUPTED;
break;
}
if (Settings.Flags & FLAG_INTERRUPT)
{
Settings.Flags &= ~FLAG_INTERRUPT;
RetVal=ERR_CANCEL;
break;
}
BytesSent+=result;
bytes-=result;
if (bytes > 0) memmove(Buffer,Buffer+result,bytes);
gettimeofday(&Now,NULL);
val=Now.tv_sec - StartTime.tv_sec;
if (val > secs)
{
secs=val;
if (secs > 0) DisplayTransferStatus(SrcFI->Name,BytesSent,TotalBytes,&percent,secs,Ctx->CmdFlags,Ctx->Throttle);
}
result=Ctx->SrcFS->ReadBytes(Ctx->SrcFS,Src,Buffer+bytes,BuffSize*2 -bytes);
}
if (Ctx->SrcFS->CloseFile) Ctx->SrcFS->CloseFile(Ctx->SrcFS,Src);
if (Ctx->DestFS && Ctx->DestFS->CloseFile)
{
result=Ctx->DestFS->CloseFile(Ctx->DestFS,Dest);
if (result !=TRUE)
{
//Handle Errors returned by Close file
if (RetVal >= FALSE) RetVal=result;
SetVar(Ctx->Vars,"Error",GetVar(Ctx->DestFS->Vars,"Error"));
SetVar(Ctx->DestFS->Vars,"Error","");
}
else ListAddNamedItem(Ctx->DestFS->DirListing,DestFI->Name,FileInfoClone(DestFI));
}
else STREAMFlush(Dest);
if ((TotalBytes > 0) && (BytesSent < TotalBytes)) RetVal=ERR_INTERRUPTED;
}
else RetVal=ERR_DESTFILE;
secs=Now.tv_sec - StartTime.tv_sec;
if (secs < 1) secs=1;
DisplayTransferStatus(SrcFI->Name,BytesSent,TotalBytes,&percent,secs,0,0);
//if (! (CmdFlags & FLAG_QUIET))
printf("\n");
}
else RetVal=ERR_SOURCEFILE;
Ctx->TotalBytes+=BytesSent;
FileInfoDestroy(DestFI);
FileInfoDestroy(SrcFI);
DestroyString(Buffer);
DestroyString(Tempstr);
return(RetVal);
}
void HandleTransferResult(TTransferContext *Ctx, TFileInfo *FI, int result)
{
TFileInfo *NewFI;
char *Tempstr=NULL, *ResultStr=NULL;
int val;
//A 'show' command might result in this being called with no 'Ctx->DestFS'
if (! Ctx->DestFS) return;
switch (result)
{
case TRANSFER_OKAY:
NewFI=FileInfoClone(FI);
NewFI->Path=MCopyStr(NewFI->Path,Ctx->DestFS->CurrDir,NewFI->Name,NULL);
ListAddNamedItem(Ctx->DestFS->DirListing,FI->Name,NewFI);
if (FI->Type==FTYPE_DIR)
{
if (Ctx->CmdFlags & FLAG_CMD_QUIET) ResultStr=CopyStr(ResultStr,"");
else ResultStr=FormatStr(ResultStr,"OKAY: %s directory '%s'",Ctx->ActionName,FI->Name);
}
else
{
if (Settings.Flags & FLAG_INTEGRITY_CHECK) val=FileStoreCompareFile(Ctx->SrcFS,Ctx->DestFS,FI,NewFI,Ctx->IntegrityLevel);
else val=CMP_EXISTS;
if (val==CMP_FAIL)
{
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' integrity check failed",Ctx->ActionName,FI->Name);
result=FALSE;
}
else if (val==CMP_MD5) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with MD5",Ctx->ActionName,FI->Name);
else if (val==CMP_SHA1) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with SHA1",Ctx->ActionName,FI->Name);
else if (val==CMP_SIZE) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with filesize",Ctx->ActionName,FI->Name);
else if (val==CMP_SIZE_TIME) ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' confirmed with filesize & time",Ctx->ActionName,FI->Name);
else ResultStr=FormatStr(ResultStr,"OKAY: %s '%s' complete",Ctx->ActionName, FI->Name);
}
if (Ctx->CmdFlags & FLAG_CMD_EXTN_TEMP)
{
ResultStr=PathChangeExtn(ResultStr, FI->Name, GetVar(Ctx->Vars,"TransferTempExtn"));
//Different sense than AlterFileExtn! We rename to FI->Name, not from it
FileStoreRename(Ctx->DestFS,ResultStr,FI->Name);
if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"Renaming: %s -> %s",ResultStr,FI->Name);
}
if (Ctx->CmdFlags & FLAG_CMD_EXTN_DEST) AlterFileExtn(Ctx->DestFS,Ctx->CmdFlags,FI->Name,GetVar(Ctx->Vars,"TransferDestExtn"));
//Use FI->Path here, because local files can be referred to by full paths
if (Ctx->CmdFlags & FLAG_CMD_EXTN_SOURCE) AlterFileExtn(Ctx->SrcFS,Ctx->CmdFlags,FI->Path,GetVar(Ctx->Vars,"TransferSourceExtn"));
/*
if (Ctx->CmdFlags & FLAG_CMD_CHMOD)
{
if (FI->Type==FTYPE_DIR) FileStoreChMod(Ctx->DestFS, FI, GetVar(Ctx->Vars,"DChMod"));
else FileStoreChMod(Ctx->DestFS, FI, GetVar(Ctx->Vars,"FChMod"));
}
*/
break;
case ERR_FORBID:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' FORBIDDEN. Insufficient Permissions.",Ctx->ActionName,FI->Name);
break;
case ERR_LOCKED:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' LOCKED. File locked.",Ctx->ActionName, FI->Name);
break;
case ERR_EXISTS:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' CONFLICT. Cannot overwrite.",Ctx->ActionName,FI->Name);
break;
case ERR_TOOBIG:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' TOO BIG. File too big.",Ctx->ActionName,FI->Name);
break;
case ERR_FULL:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' FULL. Insufficient storage.",Ctx->ActionName,FI->Name);
break;
case ERR_RATELIMIT:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' REFUSED. Rate Limiting in effect. Try later.",Ctx->ActionName,FI->Name);
break;
case ERR_BADNAME:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Bad filename.",Ctx->ActionName,FI->Name);
break;
case ERR_SOURCEFILE:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Cannot open source file for upload",Ctx->ActionName,FI->Name);
break;
case ERR_DESTFILE:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Cannot open destination file for writing",Ctx->ActionName,FI->Name);
break;
case ERR_INTERRUPTED:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Transfer interrupted.",Ctx->ActionName,FI->Name);
break;
case ERR_READ_NOTSUPPORTED:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Filestore does not support read/get.",Ctx->ActionName,FI->Name);
break;
case ERR_WRITE_NOTSUPPORTED:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Filestore does not support write/put.",Ctx->ActionName,FI->Name);
break;
case ERR_CUSTOM:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' %s.",Ctx->ActionName,FI->Name,GetVar(Ctx->Vars,"Error"));
break;
default:
ResultStr=FormatStr(ResultStr,"FAIL: %s '%s' Unknown error.",Ctx->ActionName,FI->Name);
break;
}
if (StrLen(ResultStr)) HandleEventMessage(Settings.Flags,ResultStr);
if (result==TRANSFER_OKAY)
{
if (StrLen(Ctx->PostCopyHook))
{
Tempstr=FormatStr(Tempstr,"%s '%s' '%s' '%s'",Ctx->PostCopyHook,FI->Name,FI->Path,ResultStr);
system(Tempstr);
}
}
DestroyString(ResultStr);
DestroyString(Tempstr);
}
int FileNeedsTransfer(TTransferContext *Ctx, TFileInfo *SrcFI, TFileInfo *DestFI)
{
switch (SrcFI->Type)
{
case FTYPE_CHARDEV:
case FTYPE_BLKDEV:
case FTYPE_SOCKET:
case FTYPE_LINK:
return(SKIP_FILETYPE);
break;
case FTYPE_DIR:
if (Ctx->CmdFlags & FLAG_CMD_RECURSE) return(TRUE);
break;
}
//Things that can't be 'switched'
if ((SrcFI->Type != FTYPE_DIR) && (! CheckFileMtime(SrcFI, Ctx->CmdFlags, Ctx->Vars))) return(SKIP_AGE);
if (! (Ctx->CmdFlags & FLAG_CMD_SYNC)) return(TRUE);
if (FileStoreCompareFile(Ctx->SrcFS,Ctx->DestFS,SrcFI,DestFI,Ctx->CompareLevel) < Ctx->CompareLevel) return(TRUE);
return(SKIP_EXISTS);
}
int InternalTransferDir(TTransferContext *Ctx, char *Path, char *Pattern)
{
int result=ERR_SOURCEFILE;
char *Dir=NULL, *DestRoot=NULL, *SrcRoot=NULL, *ptr;
if (Ctx->DestFS) DestRoot=CopyStr(DestRoot,Ctx->DestFS->CurrDir);
SrcRoot=CopyStr(SrcRoot,Ctx->SrcFS->CurrDir);
ptr=GetToken(Path,"/",&Dir,NULL);
while (ptr)
{
if (Ctx->SrcFS->ChDir(Ctx->SrcFS,Dir))
{
if (Ctx->DestFS)
{
Ctx->DestFS->MkDir(Ctx->DestFS,Dir);
if (! Ctx->DestFS->ChDir(Ctx->DestFS,Dir)) break;
}
result=TRANSFER_OKAY; //set this so we know we entered dir okay
}
else break;
ptr=GetToken(ptr,"/",&Dir,NULL);
}
if (result==TRANSFER_OKAY)
{
//if ChDir successed then CurrDir will hold the path of the directory we entered
HandleEventMessage(Settings.Flags,"INFO: Entering Directory: %s",Ctx->SrcFS->CurrDir);
result=InternalTransferFiles(Ctx, Pattern);
}
else HandleEventMessage(Settings.Flags,"ERROR: Failed to Enter Directory: '%s' in %s",Dir,Path);
if (Ctx->DestFS) Ctx->DestFS->ChDir(Ctx->DestFS,DestRoot);
Ctx->SrcFS->ChDir(Ctx->SrcFS,SrcRoot);
DestroyString(DestRoot);
DestroyString(SrcRoot);
DestroyString(Dir);
return(result);
}
//This accepts a single pattern to match against. If that includes a space, it is thought to be a filename
//with a space in. To supply multiple patterns look at "ProcessPutFiles" below.
int InternalTransferFiles(TTransferContext *Ctx, char *Pattern)
{
ListNode *SrcItems=NULL, *DestItems=NULL, *Curr, *Node;
TFileInfo *FI, *NewFI, *DestFI;
int result=FALSE, IncludeResult=0;
char *Tempstr=NULL, *ptr;
int val;
//if we've been given a path to something a few directories down, then do this
if (Ctx->CmdFlags & FLAG_CMD_FULLPATH)
{
ptr=strrchr(Pattern,'/');
if (StrLen(ptr) > 1)
{
Tempstr=CopyStrLen(Tempstr,Pattern,ptr-Pattern);
ptr++;
result=InternalTransferDir(Ctx, Tempstr, ptr);
DestroyString(Tempstr);
return(result);
}
}
SrcItems=ListCreate();
SrcItems->Flags |= LIST_FLAG_CASE;
if (Ctx->CmdFlags & FLAG_CMD_SYNC)
{
DestItems=ListCreate();
DestItems->Flags |= LIST_FLAG_CASE;
FileStoreLoadDir(Ctx->DestFS,"*",DestItems, LIST_REFRESH | Ctx->CompareLevel);
FileStoreLoadDir(Ctx->SrcFS,Pattern,SrcItems, LIST_REFRESH | Ctx->CompareLevel);
}
else FileStoreLoadDir(Ctx->SrcFS,Pattern,SrcItems, LIST_REFRESH);
Curr=ListGetNext(SrcItems);
if (! Curr) result=FALSE;
while (Curr)
{
FI=(TFileInfo *) Curr->Item;
Node=ListFindNamedItem(DestItems,FI->Name);
if (Node) DestFI=(TFileInfo *) Node->Item;
else DestFI=NULL;
if (FileIncluded(FI,Ctx->IncludePattern,Ctx->ExcludePattern,Ctx->CmdFlags, Ctx->Vars))
{
IncludeResult=FileNeedsTransfer(Ctx, FI, DestFI);
switch (IncludeResult)
{
case TRUE:
if (FI->Type==FTYPE_DIR)
{
result=InternalTransferDir(Ctx, FI->Name, "*");
if (result != TRANSFER_OKAY) Ctx->Failed++;
}
else
{
if ((FI->Type==FTYPE_LINK))
{
if (Ctx->DestFS->Settings & FS_SYMLINKS) result=Ctx->DestFS->Symlink(Ctx->DestFS, FI->Name, FI->EditPath);
else result=InternalTransferDir(Ctx, FI->Name, "*");
}
else result=InternalCopyFile(Ctx, FI);
HandleTransferResult(Ctx, FI, result);
if (result >= TRANSFER_OKAY) Ctx->Transferred++;
else Ctx->Failed++;
}
break;
case SKIP_EXISTS:
if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"INFO: '%s' exists, skipping.",FI->Name);
break;
case SKIP_AGE:
if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"INFO: '%s' doesn't match required age, skipping.",FI->Name);
break;
case SKIP_FILETYPE:
if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"INFO: '%s' unsupported filetype, skipping.",FI->Name);
break;
}
Ctx->Total++;
}
if (result==ERR_CANCEL) break;
Curr=ListGetNext(Curr);
}
ListDestroy(DestItems,FileInfoDestroy);
ListDestroy(SrcItems,FileInfoDestroy);
DestroyString(Tempstr);
return(result);
}
int ProcessTransferFiles(TFileStore *SrcFS, TFileStore *DestFS, int Command, char *Pattern, int CmdFlags, ListNode *Vars)
{
TTransferContext *Ctx;
char *Tempstr=NULL;
int result;
Ctx=TransferContextCreate(SrcFS, DestFS, Command, CmdFlags, Vars);
result=InternalTransferFiles(Ctx, Pattern);
Tempstr=FormatStr(Tempstr,"%d",Ctx->Transferred);
SetVar(Vars,"Transferred",Tempstr);
Tempstr=FormatStr(Tempstr,"%d",Ctx->Failed);
SetVar(Vars,"Failed",Tempstr);
Tempstr=FormatStr(Tempstr,"%d",Ctx->Total);
SetVar(Vars,"Total",Tempstr);
DestroyString(Tempstr);
TransferContextDestroy(Ctx);
return(result);
}
//This accepts a single pattern to match against. If that includes a space, it is thought to be a filename
//with a space in. To supply multiple patterns look at "ProcessPutFiles" below.
int ProcessCreateFile(TFileStore *FS, char *Name, ListNode *Vars)
{
TFileInfo *FI;
int result;
char *ptr;
STREAM *S;
TTransferContext *Ctx;
FI=FileInfoCreate(Name, Name, FTYPE_FILE, 0, 0);
ptr=GetVar(Vars,"ContentType");
if (StrLen(ptr)) FI->MediaType=CopyStr(FI->MediaType,ptr);
else FileStoreGetFileType(FS, FI);
S=FS->OpenFile(FS, FI, OPEN_WRITE);
result=FS->CloseFile(FS,S);
if (result != TRANSFER_OKAY)
{
Ctx=TransferContextCreate(NULL, FS, 0, 0, Vars);
HandleTransferResult(Ctx,FI, result);
TransferContextDestroy(Ctx);
}
FileInfoDestroy(FI);
return(result);
}