Repository navigation
Expand file tree
/
Copy pathbackup.c
More file actions
1484 lines (1266 loc) · 30.6 KB
/
Copy pathbackup.c
File metadata and controls
1484 lines (1266 loc) · 30.6 KB
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
#include "common_msgcmd.h"
#include "update.h"
#include "backup.h"
#include "search.h"
#include "hddcmd.h"
#include "mydaemon.h"
#define BACKUP_AS_AVI
#ifdef BACKUP_AS_AVI
#include "avilib.h"
#include <time.h>
#endif
#include <sys/vfs.h>
#include "custommp4.h"
#include "Mod_syscomplex.h"
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#ifndef USE_AUDIO_PCMU
#define USE_AUDIO_PCMU
#endif
extern unsigned int nMaxSupportDiskNum;
// file description
//** macro
#define SOCKET_ERROR -1
#define USB_NOERROR 0
#define USB_ERROR_WRITE_ACCESS 1
#define USB_ERROR_WRITE_FULL 2
#define USB_ERROR_WRITE_CANCEL 3
//** typedef
typedef struct
{
u64 curpos;
u64 totalpos;
int cancel;
}PACK_NO_PADDING ifly_copy_progress_t;
//** local var
static ifly_copy_progress_t tCopy;
//** global var
extern SModSyscplx sModSysIns;
extern s32 modSysCmplx_UpdateNotify(
SProgress *sProg,
s32 evt,
s32 emRslt );
//** local functions
//** global functions
extern SDevInfo* modsys_GetDiskInfo( u8 nIdx );
extern int ifly_diskinfo(char *diskname,DiskInfo *inf);
int SendMsgToDaemon(my_daemon_param *pDaemon);
static int TimeZoneOffset[] =
{
-12*3600,
-11*3600,
-10*3600,
-9*3600,
-8*3600,
-7*3600,
-6*3600,
-5*3600,
-4*3600-1800,
-4*3600,
-3*3600-1800,
-3*3600,
-2*3600,
-1*3600,
0,
1*3600,
2*3600,
3*3600,
3*3600+1800,
4*3600,
4*3600+1800,
5*3600,
5*3600+1800,
5*3600+2700,
6*3600,
6*3600+1800,
7*3600,
8*3600,
9*3600,
9*3600+1800,
10*3600,
11*3600,
12*3600,
13*3600,
};
static int GetTZOffset(int index)
{
if(index < 0 || index >= sizeof(TimeZoneOffset)/sizeof(TimeZoneOffset[0]))
{
return 0;
}
return TimeZoneOffset[index];
}
//void* wait_backup(void* arg)
#define BAK_BUFSIZE (128 << 10)
#define SNAP_BUFSIZE (10 << 10)
s32 modSysCmplx_Backup_USB_JPEG( u32 nFileNum, SRecSnapInfo* pRecInfoList)
{
u32 open_offset;
u8 chn;
struct tm tm_time;
time_t t_time;
int filesize;
u8 errcode = 0;
u64 totalsize = 0;
char usb_device[32];
FILE *pFile1 = NULL;
FILE *pFile2 = NULL;
int index, i;
unsigned char buf[BAK_BUFSIZE];//zlbfix20110719 backup
char filename[64];
char ufilename[128];
char tmpfile[64];
BOOL haveerror = FALSE;
int ret = -1;
DiskInfo dinfo;
SEventPara sEventParaIns;
if(pRecInfoList==NULL)
{
printf("%s NULL file list!\n", __func__);
return -1;//
}
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_INIT, &sEventParaIns);
mkdir("myusb",1);
MODSYS_DEBUG_STR("before while.\n");
ret = get_usb_device(usb_device);
if(ret != 0)
{
sEventParaIns.emResult = EM_BACKUP_FAILED_UNKNOW;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
return 0;
}
ret = ifly_diskinfo(usb_device,&dinfo);
if(ret == 0)
{
printf("%s found usb disk:capability:%lld B,%dM\n",__func__, dinfo.capability,(int)(dinfo.capability/(1024*1024)));
}
for(index=0; index<nFileNum; index++)
{
get_snap_file_name((recsnapinfo_t *)&pRecInfoList[index],filename,&open_offset);
totalsize += (u64)pRecInfoList[index].nSize;
printf("%s filename %s size %d\n", __func__, filename, pRecInfoList[index].nSize);
}
printf("%s totalsize: %d\n", __func__, totalsize);
tCopy.cancel = 0;
tCopy.curpos = 0;
tCopy.totalpos = totalsize;
SDevInfo* pInfo = NULL;
for(i=0; i<nMaxSupportDiskNum; i++)
{
//printf("%s i: %d\n", __func__, i);
pInfo = modsys_GetDiskInfo(i);
if(pInfo && pInfo->nStatus==HDD_IDLE && pInfo->nType==1)
{
break;
}
}
if (NULL == pInfo || pInfo->nType !=1)
{
printf("%s not found USB disk\n", __func__);
return -1;
}
printf("%s: pInfo->nFree: %lld, totalsize: %lld\n", __func__, pInfo->nFree*(u64)KB, totalsize);
if((u64)pInfo->nFree*(u64)KB < totalsize)
{
//printf("%s yg 2\n", __func__);
umount_user("myusb");
sEventParaIns.emResult = EM_BACKUP_FAILED_NOSPACE;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
return -1; // too big files
}
memset(&sEventParaIns, 0, sizeof(sEventParaIns));
sEventParaIns.sProgress.lTotalSize = tCopy.totalpos;
for(index=0; index<nFileNum; index++)
{
get_snap_file_name((recsnapinfo_t *)&pRecInfoList[index],filename,&open_offset);
pFile1 = fopen(filename,"rb");
if(pFile1 == NULL)
{
printf("%s open recfile error\n", __func__);
haveerror = TRUE;
errcode = USB_NOERROR;
goto END;
}
fseek(pFile1,open_offset,SEEK_SET);
chn = pRecInfoList[index].nChn;
t_time = pRecInfoList[index].nStartTime;
//csp modify 20131213
t_time += GetTZOffset(sModSysIns.nTimeZone);
//memcpy(&tm_time,localtime(&t_time),sizeof(tm_time));
localtime_r(&t_time, &tm_time);
sprintf(tmpfile,"chn%02d_%04d%02d%02d_%02d%02d%02d",
chn+1,
tm_time.tm_year+1900,
tm_time.tm_mon+1,
tm_time.tm_mday,
tm_time.tm_hour,
tm_time.tm_min,
tm_time.tm_sec);
sprintf(ufilename, "myusb/%s.jpg", tmpfile); //08-01-18
printf("%s ufilename = %s\n", __func__, ufilename);
pFile2 = fopen(ufilename, "wb");
if(pFile2 == NULL)
{
printf("%s open usb file error\n", __func__);
haveerror = TRUE;
errcode = USB_ERROR_WRITE_ACCESS;
goto END;
}
int readlen = 0;
filesize = pRecInfoList[index].nSize;
readlen = fread(buf, 1, filesize, pFile1);
if (readlen != filesize)
{
haveerror = TRUE;
errcode = USB_NOERROR;
printf( "%s fread readlen: %d, filesize: %d\n", __func__, readlen, filesize);
printf( "%s fread error,errcode=%d,errstr=%s\n", __func__, errno, strerror(errno) );
break;
}
int writelen = 0;
writelen = fwrite(buf, 1, filesize,pFile2);
if (writelen != filesize)
{
haveerror = TRUE;
errcode = USB_ERROR_WRITE_FULL;
if(errno == 5)//Input/output error
{
errcode = USB_ERROR_WRITE_ACCESS;
}
printf( "%s Write error,errcode=%d,errstr=%s\n", __func__, errno, strerror(errno) );
break;
}
/**********************/
END:
if(pFile1 != NULL)
{
fclose(pFile1);
pFile1 = NULL;
}
if(pFile2 != NULL)
{
fflush(pFile2);
fsync(fileno(pFile2));//csp modify
fclose(pFile2);
pFile2 = NULL;
}
if(haveerror)
{
printf("copy failed!\n");
break;
}
tCopy.curpos += readlen;
sEventParaIns.sProgress.lCurSize= tCopy.curpos;
sEventParaIns.sProgress.nProgress = 100*tCopy.curpos/tCopy.totalpos;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_RUN, &sEventParaIns);
if(modsysCmplx_QueryBreakBackup())
{
printf("backup break!\n");
modsysCmplx_ClrBreakBackup();
umount_user("myusb");//cw_backup
return 0;
}
}
if(haveerror)
{
sEventParaIns.emResult = EM_BACKUP_FAILED_UNKNOW;
}
else
{
sEventParaIns.emResult = EM_BACKUP_SUCCESS;
}
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
umount_user("myusb");
return 0;
}
s32 modSysCmplx_Backup_USB( u32 nFileNum, SRecfileInfo* pRecInfoList, int justdo )
{
u32 open_offset;
u8 chn;
struct tm tm_time;
time_t t_time;
int filesize;
u8 errcode = 0;
u64 totalsize = 0;
char usb_device[32];
FILE *pFile1 = NULL;
FILE *pFile2 = NULL;
int index, i;
unsigned char buf[BAK_BUFSIZE];//zlbfix20110719 backup
char filename[64];
char ufilename[128];
char tmpfile[64];
BOOL haveerror = FALSE;
int ret = -1;
DiskInfo dinfo;
SEventPara sEventParaIns;
if(pRecInfoList==NULL)
{
printf("NULL file list!\n");
return -1;//
}
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_INIT, &sEventParaIns);
mkdir("myusb",1);
MODSYS_DEBUG_STR("before while.\n");
ret = get_usb_device(usb_device);
if(ret != 0)
{
sEventParaIns.emResult = EM_BACKUP_FAILED_UNKNOW;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
return 0;
}
ret = ifly_diskinfo(usb_device,&dinfo);
if(ret == 0)
{
printf("检测到u盘:capability:%lld B,%dM\n",dinfo.capability,(int)(dinfo.capability/(1024*1024)));
}
for(index=0; index<nFileNum; index++)
{
get_rec_file_name((recfileinfo_t *)&pRecInfoList[index],filename,&open_offset);
totalsize += (u64)pRecInfoList[index].nSize;
printf("filename %s size %d\n", filename, pRecInfoList[index].nSize);
}
tCopy.cancel = 0;
tCopy.curpos = 0;
tCopy.totalpos = totalsize;
#if 0
struct statfs s;
statfs("myusb", &s);
dinfo.freesize=s.f_bsize*s.f_bavail;
printf("u free size %lld B,%lld M,file %lld M\n",dinfo.freesize,dinfo.freesize/1024/1024,totalsize/1024/1024);
if(totalsize>dinfo.freesize)
{
umount_user("myusb");
sEventParaIns.emResult = EM_BACKUP_FAILED_UNKNOW;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
return -1; // too big files
}
#else
SDevInfo* pInfo = NULL;
for(i=0; i<nMaxSupportDiskNum; i++)
{
printf("%s i: %d\n", __func__, i);
pInfo = modsys_GetDiskInfo(i);
if(pInfo && pInfo->nStatus==HDD_IDLE && pInfo->nType==1)
{
break;
}
}
if (NULL == pInfo || pInfo->nType !=1)
{
printf("%s not found USB disk\n", __func__);
return -1;
}
printf("%s: pInfo->nFree: %lld, totalsize: %lld\n", __func__, pInfo->nFree*(u64)KB, totalsize);
if((u64)pInfo->nFree*(u64)KB < totalsize)
{
//printf("%s yg 2\n", __func__);
umount_user("myusb");
sEventParaIns.emResult = EM_BACKUP_FAILED_NOSPACE;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
return -1; // too big files
}
#endif
memset(&sEventParaIns, 0, sizeof(sEventParaIns));
sEventParaIns.sProgress.lTotalSize = tCopy.totalpos;
BOOL bBreakBackup = FALSE;
for(index=0; index<nFileNum; index++)
{
get_rec_file_name((recfileinfo_t *)&pRecInfoList[index],filename,&open_offset);
pFile1 = fopen(filename,"rb");
if(pFile1 == NULL)
{
printf("open recfile error\n");
haveerror = TRUE;
errcode = USB_NOERROR;
goto END;
}
fseek(pFile1,open_offset,SEEK_SET);
chn = pRecInfoList[index].nChn;
t_time = pRecInfoList[index].nStartTime;
//csp modify 20131213
t_time += GetTZOffset(sModSysIns.nTimeZone);
//memcpy(&tm_time,localtime(&t_time),sizeof(tm_time));
localtime_r(&t_time, &tm_time);
sprintf(tmpfile,"chn%d%04d%02d%02d%02d%02d%02d",
chn,
tm_time.tm_year+1900,
tm_time.tm_mon+1,
tm_time.tm_mday,
tm_time.tm_hour,
tm_time.tm_min,
tm_time.tm_sec);
sprintf(ufilename,"myusb/%s.ifv",tmpfile); //08-01-18
//printf("ufilename = %s\n", ufilename);
pFile2 = fopen(ufilename,"wb");
if(pFile2 == NULL)
{
printf("open usb file error\n");
haveerror = TRUE;
errcode = USB_ERROR_WRITE_ACCESS;
goto END;
}
filesize = pRecInfoList[index].nSize;
//printf("bakup filesize=%d,size=%d\n",filesize ,pRecInfoList[index].nSize);
int tmpsize = 0; //zlbfix20110719 backup
while((!feof(pFile1)) && (filesize > 0))
{
// check if need break backup
if(modsysCmplx_QueryBreakBackup())
{
bBreakBackup = TRUE;
break;
}
// if recv quit cmd, quit!!!
#if 1
if(tCopy.cancel)
{
haveerror = TRUE;
errcode = USB_ERROR_WRITE_CANCEL;
tCopy.cancel = 0;
break;
}
#endif
int readlen = 0;
int writelen = 0;
int rlen = min(sizeof(buf),filesize);
readlen = fread(buf,1,rlen,pFile1);
if( ferror(pFile1) )
{
haveerror = TRUE;
errcode = USB_NOERROR;
printf( "Read error" );
break;
}
writelen = fwrite(buf,readlen,1,pFile2);
if( ferror(pFile2) )
{
haveerror = TRUE;
errcode = USB_ERROR_WRITE_FULL;
if(errno == 5)//Input/output error
{
errcode = USB_ERROR_WRITE_ACCESS;
}
printf( "Write error,errcode=%d,errstr=%s\n", errno, strerror(errno) );
break;
}
filesize = filesize-readlen;
tCopy.curpos += readlen;//zlbfix20110719 backup
//csp modify
//if(tCopy.curpos > tmpsize + 4 * BAK_BUFSIZE)//zlbfix20110719 backup
if(tCopy.curpos >= tmpsize + 16 * BAK_BUFSIZE)//zlbfix20110719 backup
{
//printf("fflush %u\n",tmpsize);
//csp modify
//tmpsize += 4 * BAK_BUFSIZE;//zlbfix20110719 backup
tmpsize = tCopy.curpos;
fflush(pFile2);
fsync(fileno(pFile2));
}
sEventParaIns.sProgress.lCurSize= tCopy.curpos;
sEventParaIns.sProgress.nProgress = 100*tCopy.curpos/tCopy.totalpos;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_RUN, &sEventParaIns);
}
END:
if(pFile1 != NULL)
{
fclose(pFile1);
}
if(pFile2 != NULL)
{
fflush(pFile2);
fsync(fileno(pFile2));//csp modify
fclose(pFile2);
}
if(haveerror)
{
printf("copy failed!\n");
break;
}
if(bBreakBackup)
{
printf("backup break!\n");
modsysCmplx_ClrBreakBackup();
umount_user("myusb");//cw_backup
return 0;
// break;
}
}
if(haveerror)
{
sEventParaIns.emResult = EM_BACKUP_FAILED_UNKNOW;
}
else
{
sEventParaIns.emResult = EM_BACKUP_SUCCESS;
}
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
umount_user("myusb");
return 0;
}
s32 modSysCmplx_Backup_USB_Avi( u32 nFileNum, SRecfileInfo* pRecInfoList )
{
u32 open_offset;
u8 chn;
struct tm tm_time;
time_t t_time;
u8 errcode = 0;
u64 totalsize = 0, total_frames = 0;
char usb_device[32];
custommp4_t* pFile1 = NULL;
avi_t* pAviHandle = NULL;
int index;
//csp modify 20121116//这里需要验证//CHIP_HISI3531是否起作用?
#ifdef CHIP_HISI3531
unsigned char buf[512 << 10];
#else
//csp modify 20121120
//unsigned char buf[100 << 10];
unsigned char buf[512 << 10];//unsigned char buf[256 << 10];//csp modify 20140302
#endif
char filename[64];
char ufilename[128];
char tmpfile[64];
BOOL haveerror = FALSE;
int ret = -1, i;
DiskInfo dinfo;
u32 start_time, size;
u8 key;
SEventPara sEventParaIns;
if(pRecInfoList==NULL)
{
printf("NULL file list!\n");
return -1;//
}
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_INIT, &sEventParaIns);
mkdir("myusb",1);
MODSYS_DEBUG_STR("before while.\n");
ret = get_usb_device(usb_device);
if(ret != 0)
{
return 0;
}
ret = ifly_diskinfo(usb_device,&dinfo);
if(ret == 0)
{
printf("found disk : capability:%lld B,%dM\n",dinfo.capability,(int)(dinfo.capability/(1024*1024)));
}
for(index=0; index<nFileNum; index++)
{
get_rec_file_name((recfileinfo_t *)&pRecInfoList[index],filename,&open_offset);
printf("filename %s size %d\n", filename, pRecInfoList[index].nSize);
totalsize += (u64)pRecInfoList[index].nSize;
pFile1 = custommp4_open(filename, O_R, open_offset);
if (pFile1)
{
total_frames += custommp4_video_length(pFile1);
custommp4_close(pFile1);
pFile1 = NULL;
}
}
tCopy.cancel = 0;
tCopy.curpos = 0;
tCopy.totalpos = total_frames;
SDevInfo* pInfo;
for(i=0; i<nMaxSupportDiskNum; i++)
{
pInfo = modsys_GetDiskInfo(i);
if(pInfo && pInfo->nStatus==HDD_IDLE && pInfo->nType==1)
{
break;
}
}
if (NULL == pInfo || pInfo->nType !=1)
{
printf("%s not found USB disk\n", __func__);
return -1;
}
if((u64)pInfo->nFree*(u64)KB < totalsize)
{
umount_user("myusb");
sEventParaIns.emResult = EM_BACKUP_FAILED_NOSPACE;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
return -1; // too big files
}
memset(&sEventParaIns, 0, sizeof(sEventParaIns));
sEventParaIns.sProgress.lTotalSize = tCopy.totalpos;
BOOL bBreakBackup = FALSE;
for(index=0; index<nFileNum; index++)
{
get_rec_file_name((recfileinfo_t*)&pRecInfoList[index],filename,&open_offset);
pFile1 = custommp4_open(filename, O_R, open_offset);
if(pFile1 == NULL)
{
printf("avi custommp4_open recfile error\n");
haveerror = TRUE;
errcode = USB_NOERROR;
goto END;
}
chn = pRecInfoList[index].nChn;
t_time = pRecInfoList[index].nStartTime;
//csp modify 20131213
t_time += GetTZOffset(sModSysIns.nTimeZone);
//memcpy(&tm_time,localtime(&t_time),sizeof(tm_time));
localtime_r(&t_time, &tm_time);
sprintf(tmpfile,"chn%d%04d%02d%02d%02d%02d%02d",
chn,
tm_time.tm_year+1900,
tm_time.tm_mon+1,
tm_time.tm_mday,
tm_time.tm_hour,
tm_time.tm_min,
tm_time.tm_sec);
sprintf(ufilename,"myusb/%s.avi",tmpfile); //08-01-18
printf("ufilename = %s\n", ufilename);
pAviHandle = AVI_open_output_file(ufilename);
if (!pAviHandle)
{
printf("avi file open failed \n");
haveerror = TRUE;
errcode = USB_ERROR_WRITE_ACCESS;
goto END;
}
//USE_AUDIO_PCMU begin
u32 file_totaltime = abs(pRecInfoList[index].nEndTime - pRecInfoList[index].nStartTime);
if (0 == file_totaltime) file_totaltime = 1;
AVI_set_video(pAviHandle,
custommp4_video_width(pFile1),
custommp4_video_height(pFile1),
(float)custommp4_video_length(pFile1)/(float)file_totaltime/*custommp4_video_frame_rate(pFile1)*/,
"H264" );
AVI_set_audio(pAviHandle,
custommp4_audio_channels(pFile1),
custommp4_audio_sample_rate(pFile1),
custommp4_audio_bits(pFile1),
WAVE_FORMAT_PCM, 0 );
//USE_AUDIO_PCMU end
size = custommp4_video_length(pFile1);
#ifdef USE_AUDIO_PCMU
u8 media_type;
u64 pts = 0;
#endif
while(size > 0)
{
// check if need break backup
if(modsysCmplx_QueryBreakBackup())
{
bBreakBackup = TRUE;
break;
}
if(tCopy.cancel)
{
haveerror = TRUE;
errcode = USB_ERROR_WRITE_CANCEL;
tCopy.cancel = 0;
break;
}
#ifdef USE_AUDIO_PCMU
int readlen = custommp4_read_one_media_frame(pFile1, buf, sizeof(buf), &start_time, &key, &pts,&media_type);
#else
int readlen = custommp4_read_one_video_frame(pFile1, buf, sizeof(buf), &start_time, &duration, &key);
#endif
if (readlen <= 0)
{
tCopy.curpos += size;
haveerror = TRUE;
errcode = USB_NOERROR;
printf( "avi Read error 10,errcode=%d,errstr=%s\n", errno, strerror(errno));
break;
}
#ifdef USE_AUDIO_PCMU
if(0 == media_type){
#endif
int ret_avi = AVI_write_frame(pAviHandle, buf, readlen, key);
if (ret_avi)
{
haveerror = TRUE;
errcode = USB_ERROR_WRITE_FULL;
if(errno == 5)//Input/output error
{
errcode = USB_ERROR_WRITE_ACCESS;
}
printf( "avi Write error 1,errcode=%d,errstr=%s\n", errno, strerror(errno) );
break;
}
tCopy.curpos++;
size--;
#ifdef USE_AUDIO_PCMU
}
else
{
int ret_avi = AVI_write_audio(pAviHandle, buf, readlen);
if (ret_avi)
{
haveerror = TRUE;
errcode = USB_ERROR_WRITE_FULL;
if(errno == 5)//Input/output error
{
errcode = USB_ERROR_WRITE_ACCESS;
}
printf( "avi Write error 1,errcode=%d,errstr=%s\n", errno, strerror(errno) );
break;
}
}
#endif
sEventParaIns.sProgress.lCurSize = tCopy.curpos;
u8 nProg = 100*tCopy.curpos/tCopy.totalpos;
if(nProg != sEventParaIns.sProgress.nProgress)
{
sEventParaIns.sProgress.nProgress = nProg;
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_RUN, &sEventParaIns);
}
}
END:
if (pFile1)
{
custommp4_close(pFile1);
pFile1 = NULL;
}
if (pAviHandle)
{
AVI_close(pAviHandle);
pAviHandle = NULL;
}
if(haveerror)
{
printf("avi copy failed\n");
break;
}
if(bBreakBackup)
{
printf("backup break!\n");
modsysCmplx_ClrBreakBackup();
umount_user("myusb");//cw_backup
return 0;
// break;
}
}
if(haveerror)
{
sEventParaIns.emResult = EM_BACKUP_FAILED_UNKNOW;
}
else
{
sEventParaIns.emResult = EM_BACKUP_SUCCESS;
}
EvNotify(sModSysIns.pFNSysNotifyCB, EM_SYSEVENT_BACKUP_DONE, &sEventParaIns);
umount_user("myusb");
return 0;
}
s32 modSysCmplx_Backup_DVDR(
u32 nFileNum,
SRecfileInfo* pRecInfoList,
EMBACKUPDVDRWMODE mode
)
{
FILE *pFile1 = NULL;
my_daemon_param daemon;
int i;
int index;
unsigned char buf[1024*32];
char filename[64];
//char ufilename[128];
char tmpfilestat[128];
char tmpfile[64];
BOOL haveerror = FALSE;
u8 errcode = 0;
int ret = -1;
SEventPara sEventParaIns;
SProgress sProg;
#ifdef DVD_RW_DMA
#ifdef HISI_DVDR
MODSYS_ASSERT(NULL!=pRecInfoList);
printf("file number is %d\n",nFileNum);
for(i=0;i<MAX_HDD_NUM;i++)
{
char medianame[64];
//sprintf(medianame,"/dev/scsi/host%d/bus0/target0/lun0/cd",i);
sprintf(medianame,"/dev/sr0");
ret = open(medianame, O_RDONLY);
//FILE *fpidetype = fopen(medianame,"rb");
if(ret < 0)
{
if(errno==123) //无光盘
{
printf("~~~~~~ 无光盘!\n");
}
else //其他情况
{
printf("其他情况!\n");
}
continue;
}
else
{
close(ret);
break;
}
}
//printf("dvd index=%d\n",i);
memset(&sProg, 0, sizeof(sProg));
if(i>=MAX_HDD_NUM)
{
modSysCmplx_UpdateNotify(NULL, -1, -1); // dvdr no hdd
;// TEMP_DEBUG_DROP_IT //return -1;
}
if(EM_DVDRW_MODE_NEW==mode)
{
sprintf(daemon.arg2,"-Z /dev/sr0");
}
else if(EM_DVDRW_MODE_ADD==mode)
{
sprintf(daemon.arg2,"-M /dev/sr0");
}
else
{
modSysCmplx_UpdateNotify(NULL, -1, -1); // dvdr no hdd
return -1;
}
#else
printf("else HISI_DVDR!\n");
for(i=0;i<2;i++)
{
char medianame[64];
char dvdrwname[64];
char idetype[32];
sprintf(medianame,"/proc/ide/hd%c/media",'a'+i);
FILE *fpidetype = fopen(medianame,"rb");
if(fpidetype == NULL)
{
continue;
}
fgets(idetype,32,fpidetype);
fclose(fpidetype);
if(strcmp(idetype,"cdrom\n") != 0)
{
continue;