-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathvobcopy.c
2337 lines (2080 loc) · 83.4 KB
/
vobcopy.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
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
/* vobcopy 1.2.0
*
* Copyright (c) 2001 - 2009 [email protected]
* Lots of contribution and cleanup from [email protected]
* Critical bug-fix from Bas van den Heuvel
* Takeshi HIYAMA made lots of changes to get it to run on FreeBSD
* Erik Hovland made changes for solaris
* This file is part of vobcopy.
*
* vobcopy 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.
*
* vobcopy 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 vobcopy; if not, see <http://www.gnu.org/licenses/>.
*/
/* CONTRIBUTORS (direct or through source "borrowing")
* [email protected] - helped me a lot!
* Billy Biggs <[email protected]> - took some of his play_title.c code
* and implemeted it here
* Håkan Hjort <[email protected]> and Billy Biggs - libdvdread
* Stephen Birch <[email protected]> - debian packaging
*/
/*TODO:
* mnttab reading is "wrong" on solaris
* - error handling with the errno and strerror function
* with -t still numbers get added to the name, -T with no numbers or so.
*/
/*THOUGHT-ABOUT FEATURES
* - being able to specify
- which title
- which chapter
- which angle
- which language(s)
- which subtitle(s)
to copy
* - print the total playing time of that title
*/
/*If you find a bug, contact me at [email protected]*/
#include "vobcopy.h"
extern int errno;
char name[300];
bool overwrite_flag = FALSE;
bool overwrite_all_flag = FALSE;
int overall_skipped_blocks = 0;
/* --------------------------------------------------------------------------*/
/* MAIN */
/* --------------------------------------------------------------------------*/
int main ( int argc, char *argv[] )
{
int streamout, block_count, blocks, file_block_count;
int op;
char dvd_path[255], logfile_name[20],logfile_path[280]="\0"; /* TODO: fill logfile_path with all zeros so that
" if strncpy() finds the source too long IT DOES NOT TERMINATE the sink, so the following strcat() is undefined
and potentially fatal." - Thanks Leigh!*/
char dvd_name[35], vobcopy_call[255], provided_dvd_name[35];
char *size_suffix;
char pwd[255],provided_output_dir[255],provided_input_dir[255];
char alternate_output_dir[4][255], onefile[255];
unsigned char bufferin[ DVD_VIDEO_LB_LEN * BLOCK_COUNT ];
int i = 0,j = 0, argc_i = 0, alternate_dir_count = 0;
int partcount = 0, get_dvd_name_return, options_char = 0;
int dvd_count = 0, verbosity_level = 0, paths_taken = 0, fast_factor = 1;
int watchdog_minutes = 0;
long long unsigned int seek_start = 0, stop_before_end = 0, temp_var;
off_t pwd_free, vob_size = 0, disk_vob_size = 0;
off_t offset = 0, free_space = 0;
off_t max_filesize_in_blocks = 1048571; /* for 2^31 / 2048 */
off_t max_filesize_in_blocks_summed = 0, angle_blocks_skipped = 0;
ssize_t file_size_in_blocks = 0;
bool mounted = FALSE, provided_output_dir_flag = FALSE;
bool verbose_flag = FALSE, provided_input_dir_flag = FALSE;
bool force_flag = FALSE, info_flag = FALSE, cut_flag = FALSE;
bool large_file_flag = FALSE, titleid_flag = FALSE;
bool mirror_flag = FALSE, provided_dvd_name_flag = FALSE;
bool stdout_flag = FALSE, space_greater_2gb_flag = TRUE;
bool fast_switch = FALSE, onefile_flag = FALSE;
bool quiet_flag = FALSE, longest_title_flag = FALSE;
struct stat buf;
float lastpos = 0;
int starttime;
dvd_reader_t *dvd = NULL;
dvd_file_t *dvd_file = NULL;
extern char *optarg;
extern int optind, optopt;
/**
*this is taken from play_title.c
*/
int titleid = 2, chapid = 0, pgc_id, start_cell;
int angle = 0, ttn, pgn, sum_chapters = 0;
int sum_angles = 0, most_chapters = 0;
ifo_handle_t *vmg_file;
tt_srpt_t *tt_srpt;
ifo_handle_t *vts_file;
vts_ptt_srpt_t *vts_ptt_srpt;
pgc_t *cur_pgc;
/*
this is for the mirror feature (readdir)
*/
struct dirent * directory;
DIR *dir;
/**
*getopt-long
*/
#ifdef HAVE_GETOPT_LONG
int option_index = 0;
static struct option long_options[] =
{
{"1st_alt_output_dir", 1, 0, '1'
},
{"2st_alt_output_dir", 1, 0, '2'},
{"3st_alt_output_dir", 1, 0, '3'},
{"4st_alt_output_dir", 1, 0, '4'},
{"angle", 1, 0, 'a'},
{"begin", 1, 0, 'b'},
{"chapter", 1, 0, 'c'},
{"end", 1, 0, 'e'},
{"force", 0, 0, 'f'},
{"fast", 1, 0, 'F'},
{"help", 0, 0, 'h'},
{"input-dir", 1, 0, 'i'},
{"info", 0, 0, 'I'},
{"large-file", 0, 0, 'l'},
{"longest", 0, 0, 'M'},
{"mirror", 0, 0, 'm'},
{"title-number", 1, 0, 'n'},
{"output-dir", 1, 0, 'o'},
{"quiet", 0, 0, 'q'},
{"onefile", 1, 0, 'O'},
{"name", 1, 0, 't'},
{"verbose", 0, 0, 'v'},
{"version", 0, 0, 'V'},
{"watchdog", 1, 0, 'w'},
{"overwrite-all", 0, 0, 'x'},
{0, 0, 0, 0}
};
#endif
/*for gettext - i18n */
#if defined( __gettext__ )
setlocale(LC_ALL, "");
textdomain("vobcopy");
bindtextdomain("vobcopy", "/usr/share/locale");
#endif
/* initialize string */
dvd_path[0] = '\0';
/*
* the getopt part (getting the options from command line)
*/
while (1)
{
#ifdef HAVE_GETOPT_LONG
options_char = getopt_long( argc, argv,
"1:2:3:4:a:b:c:e:i:n:o:qO:t:vfF:lmMhL:Vw:Ix",
long_options ,&option_index);
#else
options_char = getopt( argc, argv, "1:2:3:4:a:b:c:e:i:n:o:qO:t:vfF:lmMhL:Vw:Ix-" );
#endif
if ( options_char == -1 ) break;
switch( options_char )
{
case'a': /*angle*/
if ( !isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Error] The thing behind -a has to be a number! \n") );
exit(1);
}
sscanf( optarg, "%i", &angle );
angle--;/*in the ifo they start at zero */
if (angle < 0)
{
fprintf( stderr, _("[Hint] Um, you set angle to 0, try 1 instead ;-)\n") );
exit(1);
}
break;
case'b': /*size to skip from the beginning (beginning-offset) */
if ( !isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Error] The thing behind -b has to be a number! \n") );
exit(1);
}
temp_var = atol( optarg );
size_suffix = strpbrk( optarg, "bkmgBKMG" );
if( !size_suffix)
{
fprintf( stderr, _("[Error] Wrong suffix behind -b, only b,k,m or g \n") );
exit(1);
}
switch( *size_suffix )
{
case'b':
case'B':
temp_var *= 512;/*blocks (normal, not dvd)*/
break;
case'k':
case'K':
temp_var *= 1024; /*kilo*/
break;
case'm':
case'M':
temp_var *= ( 1024 * 1024 );/*mega*/
break;
case'g':
case'G':
temp_var *= ( 1024 * 1024 * 1024 );/*wow, giga *g */
break;
case'?':
fprintf( stderr, _("[Error] Wrong suffix behind -b, only b,k,m or g \n") );
exit(1);
break;
}
/* sscanf( optarg, "%lli", &temp_var ); */
seek_start = temp_var / 2048;
cut_flag = TRUE;
break;
case'c': /*chapter*/ /*NOT WORKING!!*/
if ( !isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Error] The thing behind -c has to be a number! \n") );
exit(1);
}
sscanf( optarg, "%i", &chapid );
chapid--;/*in the ifo they start at zero */
break;
case'e': /*size to stop from the end (end-offset) */
if ( !isdigit( (int) optarg[0] ) )
{
fprintf( stderr, _("[Error] The thing behind -e has to be a number! \n") );
exit(1);
}
temp_var = atol( optarg );
size_suffix = strpbrk( optarg, "bkmgBKMG" );
if( !size_suffix)
{
fprintf( stderr, _("[Error] Wrong suffix behind -b, only b,k,m or g \n") );
exit(1);
}
switch( *size_suffix )
{
case'b':
case'B':
temp_var *= 512; /*blocks (normal, not dvd)*/
break;
case'k':
case'K':
temp_var *= 1024; /*kilo*/
break;
case'm':
case'M':
temp_var *= ( 1024 * 1024 ); /*mega*/
break;
case'g':
case'G':
temp_var *= ( 1024 * 1024 * 1024 );/*wow, giga *g */
break;
case'?':
fprintf( stderr, _("[Error] Wrong suffix behind -b, only b,k,m or g \n") );
exit(1);
break;
}
stop_before_end = temp_var / 2048;
cut_flag = TRUE;
break;
case'f': /*force flag, some options like -o, -1..-4 set this
themselves */
force_flag = TRUE;
break;
case'h': /* good 'ol help */
usage( argv[0] );
break;
case'i': /*input dir, if the automatic needs to be overridden */
if ( isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Error] Erm, the number comes behind -n ... \n") );
exit(1);
}
fprintf( stderr, _("[Hint] You use -i. Normally this is not necessary, vobcopy finds the input dir by itself. This option is only there if vobcopy makes trouble.\n") );
fprintf( stderr, _("[Hint] If vobcopy makes trouble, please mail me so that I can fix this ([email protected]). Thanks\n") );
safestrncpy( provided_input_dir, optarg, sizeof(provided_input_dir)-1 );
if( strstr( provided_input_dir, "/dev" ) )
{
fprintf( stderr, _("[Error] Please don't use -i /dev/something in this version, only the next version will support this again.\n") );
fprintf( stderr, _("[Hint] Please use the mount point instead (/cdrom, /dvd, /mnt/dvd or something)\n") );
}
provided_input_dir_flag = TRUE;
break;
case'l': /*large file output*/
max_filesize_in_blocks = 8388608; /*16 GB /2048 (block) */
/* 2^63 / 2048 (not exactly) */
large_file_flag = TRUE;
break;
case'm':/*mirrors the dvd to harddrive completly*/
mirror_flag = TRUE;
info_flag = TRUE;
break;
case'M':/*Main track - i.e. the longest track on the dvd*/
titleid_flag = TRUE;
longest_title_flag = TRUE;
break;
case'n': /*title number*/
if ( !isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Error] The thing behind -n has to be a number! \n") );
exit(1);
}
sscanf( optarg, "%i", &titleid );
titleid_flag = TRUE;
break;
case'o': /*output destination */
if ( isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Hint] Erm, the number comes behind -n ... \n") );
}
safestrncpy( provided_output_dir, optarg, sizeof(provided_output_dir)-1 );
if ( !strcasecmp( provided_output_dir, "stdout" ) || !strcasecmp( provided_output_dir, "-" ) )
{
stdout_flag = TRUE;
force_flag = TRUE;
}
else
{
provided_output_dir_flag = TRUE;
alternate_dir_count++;
}
/* force_flag = TRUE; */
break;
case'q':/*quiet flag* - meaning no progress and such output*/
quiet_flag = TRUE;
break;
case'1': /*alternate output destination*/
case'2':
case'3':
case'4':
if( alternate_dir_count < options_char - 48 )
{
fprintf( stderr, _("[Error] Please specify output dirs in this order: -o -1 -2 -3 -4 \n") );
exit( 1 );
}
if ( isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Hint] Erm, the number comes behind -n ... \n") );
}
safestrncpy( alternate_output_dir[ options_char-49 ], optarg, sizeof(alternate_output_dir[ options_char-49 ])-1 );
provided_output_dir_flag = TRUE;
alternate_dir_count++;
force_flag = TRUE;
break;
case't': /*provided title instead of the one from dvd,
maybe even stdout output */
if ( strlen( optarg ) > 33 )
printf( "[Hint] The max title-name length is 33, the remainder got discarded" );
safestrncpy( provided_dvd_name, optarg, sizeof(provided_dvd_name)-1 );
provided_dvd_name_flag = TRUE;
if ( !strcasecmp( provided_dvd_name,"stdout" ) || !strcasecmp( provided_dvd_name,"-" ) )
{
stdout_flag = TRUE;
force_flag = TRUE;
}
for( i=0; i< strlen(provided_dvd_name);i++ )
{
if( provided_dvd_name[i] == ' ')
provided_dvd_name[i] = '_';
}
break;
case'v': /*verbosity level, can be called multiple times*/
verbose_flag = TRUE;
verbosity_level++;
break;
case'w': /*sets a watchdog timer to cap the amount of time spent
grunging about on a heavily protected disc */
if ( !isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Error] The thing behind -w has to be a number! \n") );
exit(1);
}
sscanf( optarg, "%i", &watchdog_minutes );
if( watchdog_minutes < 1 )
{
fprintf( stderr, _("[Hint] Negative minutes aren't allowed - disabling watchdog.\n") );
watchdog_minutes = 0;
}
break;
case'x': /*overwrite all existing files without (further) question */
overwrite_all_flag = TRUE;
break;
case'F': /*Fast-switch*/
if ( !isdigit( (int) *optarg ) )
{
fprintf( stderr, _("[Error] The thing behind -F has to be a number! \n") );
exit(1);
}
sscanf( optarg, "%i", &fast_factor );
if( fast_factor > BLOCK_COUNT ) /*atm is BLOCK_COUNT == 64 */
{
fprintf( stderr, _("[Hint] The largest value for -F is %d at the moment - used that one...\n"), BLOCK_COUNT );
fast_factor = BLOCK_COUNT;
}
fast_switch = TRUE;
break;
case'I': /*info, doesn't do anything, but simply prints some infos
( about the dvd )*/
info_flag = TRUE;
break;
case'L': /*logfile-path (in case your system crashes every time you
call vobcopy (and since the normal logs get written to
/tmp and that gets cleared at every reboot... )*/
strncpy(logfile_path, optarg, sizeof(logfile_path)-2); /* -s so room for '/' */
strcat(logfile_path, "/");
logfile_path[sizeof(logfile_path)-1] = '\0';
verbose_flag = TRUE;
verbosity_level = 2;
break;
case'O': /*only one file will get copied*/
onefile_flag = TRUE;
safestrncpy( onefile, optarg, sizeof(onefile)-1 );
i = 0; /*this i here could be bad... */
while( onefile[ i ] )
{
onefile[ i ] = toupper ( onefile[ i ] );
i++;
}
if( onefile[ i - 1 ] == ',' )
onefile[ i ] = 0;
else
{
onefile[ i ] = ',';
onefile[ i + 1 ] = 0;
}
mirror_flag = TRUE;
i = 0;
break;
case'V': /*version number output */
printf( "Vobcopy "PACKAGE_VERSION" - GPL Copyright (c) 2001 - 2009 [email protected]\n" );
exit( 0 );
break;
case'?': /*probably never gets here, the others should catch it */
fprintf( stderr, _("[Error] Wrong option.\n") );
usage( argv[0] );
exit( 1 );
break;
#ifndef HAVE_GETOPT_LONG
case'-': /* no getopt, complain */
fprintf( stderr, _("[Error] %s was compiled without support for long options.\n"), argv[0] );
usage( argv[0] );
exit( 1 );
break;
#endif
default: /*probably never gets here, the others should catch it */
fprintf( stderr, _("[Error] Wrong option.\n") );
usage( argv[0] );
exit( 1 );
}
}
fprintf( stderr, _("Vobcopy "PACKAGE_VERSION" - GPL Copyright (c) 2001 - 2009 [email protected]\n") );
fprintf( stderr, _("[Hint] All lines starting with \"libdvdread:\" are not from vobcopy but from the libdvdread-library\n") );
/*get the current working directory*/
if ( provided_output_dir_flag )
{
strcpy( pwd, provided_output_dir );
}
else
{
if ( getcwd( pwd, 255 ) == NULL )
{
fprintf( stderr, _("\n[Error] Hmm, the path length of your current directory is really large (>255)\n") );
fprintf( stderr, _("[Hint] Change to a path with shorter path length pleeeease ;-)\n") );
exit( 1 );
}
}
add_end_slash( pwd );
if( quiet_flag )
{
int temp;
char tmp_path[268];
strcpy( tmp_path, pwd );
strcat( tmp_path, "vobcopy.bla" );
fprintf( stderr, _("[Hint] Quiet mode - All messages will now end up in %s\n"), tmp_path );
if ( ( temp = open( tmp_path , O_RDWR | O_CREAT | O_EXCL, 0666 ) ) == -1 )
{
if ( errno == EEXIST )
{
if ( !force_flag )
{
printf( "[Error] Error: %s\n", strerror( errno ) );
printf( "\n[Error] The file %s already exists. Since overwriting it can be seen as a security problem I stop here.\n", tmp_path );
printf( "[Hint] Use -f to override or simply delete that file\n" );
exit( 1 );
}
else
{
printf( "[Warning] Overwriting %s as requested with -f\n", tmp_path );
}
}
else
{
printf( "[Error] Aaah! Re-direct of stderr to %s didn't work! If -f is not used I stop here... \n", tmp_path );
printf( "[Hint] Use -f to continue (at your risk of stupid ascii text ending up in your VOBs)\n" );
if ( !force_flag )
exit( 1 );
}
}
else
{
close( temp );
}
if ( chmod( tmp_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) == -1 )
{
printf( "[Error] Error: %s\n", strerror( errno ) );
printf( "[Error] Changing mode of %s didn't work! If -f is not used I stop here... \n", tmp_path );
printf( "[Hint] Use -f to continue (at your risk of stupid ascii text ending up in your VOBs)\n" );
if ( !force_flag )
exit( 1 );
}
/*reopen the already tested file and attach stderr to it*/
if ( freopen( tmp_path , "a" , stderr ) == NULL )
{
printf( "[Error] Error: %s\n", strerror( errno ) );
printf( "[Error] Aaah! Re-direct of stderr to %s didn't work! If -f is not used I stop here... \n", tmp_path );
printf( "[Hint] Use -f to continue (at your risk of stupid ascii text ending up in your VOBs)\n" );
if ( !force_flag )
exit( 1 );
}
}
if( verbosity_level > 1 ) /* this here starts writing the logfile */
{
int temp;
fprintf( stderr, _("[Info] Uhu, super-verbose\n") );
if( strlen( logfile_path ) < 3 )
strcpy( logfile_path, pwd );
strcpy( logfile_name, PACKAGE_TARNAME"_" );
strcat( logfile_name, PACKAGE_VERSION );
strcat( logfile_name, ".log" );
strcat( logfile_path, logfile_name );
if ( ( temp = open ( logfile_path , O_RDWR | O_CREAT | O_EXCL, 0666 ) ) == -1 )
{
printf( "[Error] Error: %s\n", strerror( errno ) );
if ( errno == EEXIST )
{
printf( "\n[Error] The file %s already exists. Since overwriting it can be seen as a security problem I stop here. \n", logfile_path );
printf( "[Hint] Use -f to override or simply delete that file\n" );
if ( !force_flag )
exit( 1 );
}
else
{
printf( "[Error] Aaah! Re-direct of stderr to %s didn't work! If -f is not used I stop here... \n", logfile_path );
if ( !force_flag )
exit( 1 );
}
}
else
{
close( temp );
}
if ( chmod( logfile_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH ) == -1 )
{
printf( "[Error] Error: %s\n", strerror( errno ) );
printf( "[Error] Changing mode of %s didn't work! If -f is not used I stop here... \n", logfile_path );
printf( "[Hint] Use -f to try to continue anyway\n" );
if ( !force_flag )
exit( 1 );
}
fprintf( stderr, _("[Info] The log-file is written to %s\n"), logfile_path );
fprintf( stderr, _("[Hint] Make sure that vobcopy doesn't have to ask questions (like overwriting of old files), these questions end up in the log file so you don't see them...\n") );
fprintf( stderr, _("[Hint] If you don't like that position, use -L /path/to/logfile/ instead of -v -v\n") );
if ( freopen( logfile_path, "a" , stderr ) == NULL )
{
printf( "[Error] Error: %s\n", strerror( errno ) );
printf( "[Error] Aaah! Re-direct of stderr to %s didn't work! \n", logfile_path );
/* oh no! redirecting of stderr failed, do best to quit gracefully */
exit( 1 );
}
strcpy( vobcopy_call, argv[0] );
for( argc_i = 1; argc_i != argc; argc_i++ )
{
strcat( vobcopy_call, " " );
strcat( vobcopy_call, argv[argc_i] );
}
fprintf( stderr, _("--------------------------------------------------------------------------------\n") );
fprintf( stderr, _("[Info] Called: %s\n"), vobcopy_call );
}
/*sanity check: -m and -n are mutually exclusive... */
if( titleid_flag && mirror_flag )
{
fprintf( stderr, _("\n[Error] There can be only one: either -m or -n...'\n") );
exit( 1 );
}
/*
* Check if the provided path is too long
*/
if ( optind < argc ) /* there is still the path as in vobcopy-0.2.0 */
{
provided_input_dir_flag = TRUE;
if ( strlen( argv[optind] ) >= 255 )
{
fprintf( stderr, _("\n[Error] Bloody path to long '%s'\n"), argv[optind] );
exit( 1 );
}
safestrncpy( provided_input_dir, argv[optind],sizeof(provided_input_dir)-1 );
}
if ( provided_input_dir_flag ) /*the path has been given to us */
{
int result;
/* 'mounted' is an enum, it should not get assigned an int -- lb */
if ( ( result = get_device( provided_input_dir, dvd_path ) ) < 0 )
{
fprintf( stderr, _("[Error] Could not get the device which belongs to the given path!\n") );
fprintf( stderr, _("[Hint] Will try to open it as a directory/image file\n") );
/* exit( 1 ); */
}
if (result == 0)
mounted = FALSE;
if (result == 1)
mounted = TRUE;
}
else /*need to get the path and device ourselves ( oyo = on your own ) */
{
if ( ( dvd_count = get_device_on_your_own( provided_input_dir, dvd_path ) ) <= 0 )
{
fprintf( stderr, _("[Warning] Could not get the device and path! Maybe not mounted the dvd?\n") );
fprintf( stderr, _("[Hint] Will try to open it as a directory/image file\n") );
/* exit( 1 ); */
}
if( dvd_count > 0 )
mounted = TRUE;
else
mounted = FALSE;
}
if (! mounted )
{
/*see if the path given is a iso file or a VIDEO_TS dir */
safestrncpy( dvd_path, provided_input_dir, sizeof(dvd_path)-1 );
}
/*
* Is the path correct
*/
fprintf( stderr, _("\n[Info] Path to dvd: %s\n"), dvd_path );
if( !( dvd = DVDOpen( dvd_path ) ) )
{
fprintf( stderr, _("\n[Error] Path thingy didn't work '%s'\n"), dvd_path);
fprintf( stderr, _("[Error] Try something like -i /cdrom, /dvd or /mnt/dvd \n") );
if( dvd_count > 1 )
fprintf( stderr, _("[Hint] By the way, you have %i cdroms|dvds mounted, that probably caused the problem\n"), dvd_count );
DVDClose( dvd );
exit( 1 );
}
/*
* this here gets the dvd name
*/
if ( provided_dvd_name_flag )
safestrncpy( dvd_name, provided_dvd_name, sizeof(dvd_name)-1 );
else
get_dvd_name_return = get_dvd_name( dvd_path, dvd_name );
fprintf( stderr, _("[Info] Name of the dvd: %s\n"), dvd_name );
/* The new part taken from play-title.c*/
/**
* Load the video manager to find out the information about the titles on
* this disc.
*/
vmg_file = ifoOpen( dvd, 0 );
if( !vmg_file )
{
fprintf( stderr, _("[Error] Can't open VMG info.\n") );
DVDClose( dvd );
return -1;
}
tt_srpt = vmg_file->tt_srpt;
/**
* Get the title with the most chapters since this is probably the main part
*/
for( i = 0;i < tt_srpt->nr_of_srpts;i++ )
{
sum_chapters += tt_srpt->title[ i ].nr_of_ptts;
if( i > 0 )
{
if( tt_srpt->title[ i ].nr_of_ptts > tt_srpt->title[ most_chapters ].nr_of_ptts )
{
most_chapters = i;
}
}
}
if( longest_title_flag ) /*no title specified (-n ) */
{
titleid = get_longest_title( dvd );
fprintf( stderr, _("[Info] longest title %d.\n"), titleid );
}
if( !titleid_flag ) /*no title specified (-n ) */
{
titleid = most_chapters + 1; /*they start counting with 0, I with 1...*/
}
/**
* Make sure our title number is valid.
*/
fprintf( stderr, _("[Info] There are %d titles on this DVD.\n"),
tt_srpt->nr_of_srpts );
if( titleid <= 0 || ( titleid-1 ) >= tt_srpt->nr_of_srpts )
{
fprintf( stderr, _("[Error] Invalid title %d.\n"), titleid );
ifoClose( vmg_file );
DVDClose( dvd );
return -1;
}
/**
* Make sure the chapter number is valid for this title.
*/
fprintf( stderr, _("[Info] There are %i chapters on the dvd.\n"), sum_chapters );
fprintf( stderr, _("[Info] Most chapters has title %i with %d chapters.\n"),
( most_chapters + 1 ), tt_srpt->title[ most_chapters ].nr_of_ptts );
if( info_flag )
{
fprintf( stderr, _("[Info] All titles:\n") );
for( i = 0;i < tt_srpt->nr_of_srpts;i++ )
{
int chapters = tt_srpt->title[ i ].nr_of_ptts;
if( chapters > 1 )
fprintf( stderr, _("[Info] Title %i has %d chapters.\n"),
( i+1 ), chapters );
else
fprintf( stderr, _("[Info] Title %i has %d chapter.\n"),
( i+1 ), chapters );
}
}
if( chapid < 0 || chapid >= tt_srpt->title[ titleid-1 ].nr_of_ptts )
{
fprintf( stderr, _("[Error] Invalid chapter %d\n"), chapid + 1 );
ifoClose( vmg_file );
DVDClose( dvd );
return -1;
}
/**
* Make sure the angle number is valid for this title.
*/
for( i = 0;i < tt_srpt->nr_of_srpts;i++ )
{
sum_angles += tt_srpt->title[ i ].nr_of_angles;
}
fprintf( stderr, _("\n[Info] There are %d angles on this dvd.\n"), sum_angles );
if( angle < 0 || angle >= tt_srpt->title[ titleid-1 ].nr_of_angles )
{
fprintf( stderr, _("[Error] Invalid angle %d\n"), angle + 1 );
ifoClose( vmg_file );
DVDClose( dvd );
return -1;
}
if( info_flag )
{
fprintf( stderr, _("[Info] All titles:\n") );
for(i = 0;i < tt_srpt->nr_of_srpts;i++ )
{
int angles = tt_srpt->title[ i ].nr_of_angles;
if( angles > 1 )
fprintf( stderr, _("[Info] Title %i has %d angles.\n"),
( i+1 ), angles );
else
fprintf( stderr, _("[Info] Title %i has %d angle.\n"),
( i+1 ), angles );
}
disk_vob_size = get_used_space( provided_input_dir, verbosity_level );
}
/*
* get the whole vob size via stat( ) manually
*/
if( mounted && !mirror_flag )
{
vob_size = ( get_vob_size( titleid, provided_input_dir ) ) -
( seek_start * 2048 ) - ( stop_before_end * 2048 );
if( vob_size == 0 || vob_size > 9663676416LL) /*9663676416 equals 9GB */
{
fprintf( stderr, _("\n[Error] Something went wrong during the size detection of the") );
fprintf( stderr, _("\n[Error] vobs, size check at the end won't work (probably), but I continue anyway\n\n") );
vob_size = 0;
}
}
if( mirror_flag ) /* this gets the size of the whole dvd */
{
add_end_slash( provided_input_dir );
disk_vob_size = get_used_space( provided_input_dir, verbosity_level );
/* vob_size = get_free_space( provided_input_dir,verbosity_level ); */
}
/*
* check if on the target directory is enough space free
* see man statfs for more info
*/
pwd_free = get_free_space( pwd,verbosity_level );
if( fast_switch )
block_count = fast_factor;
else
block_count = 1;
install_signal_handlers();
if( watchdog_minutes )
{
fprintf( stderr, _("\n[Info] Setting watchdog timer to %d minutes\n"), watchdog_minutes );
alarm( watchdog_minutes * 60 );
}
/***
this is the mirror section
***/
if( mirror_flag )
{/*mirror beginning*/
fprintf( stderr, _("\n[Info] DVD-name: %s\n"), dvd_name );
if( provided_dvd_name_flag )
{
fprintf( stderr, _("\n[Info] Your name for the dvd: %s\n"), provided_dvd_name );
safestrncpy( dvd_name, provided_dvd_name, sizeof(dvd_name)-1 );
}
fprintf( stderr, _("[Info] Disk free: %.0f MB\n"), (float) pwd_free / ( 1024*1024 ) );
fprintf( stderr, _("[Info] Vobs size: %.0f MB\n"), (float) disk_vob_size / ( 1024*1024 ) );
if( ( force_flag || pwd_free > disk_vob_size ) && alternate_dir_count < 2 )
/* no dirs behind -1, -2 ... since its all in one dir */
{
char video_ts_dir[263];
char number[8];
char input_file[280];
char output_file[255];
int i, start, title_nr = 0;
off_t file_size;
double tmp_i = 0, tmp_file_size = 0;
int k = 0;
char d_name[256];
safestrncpy( name, pwd, sizeof(name)-34 ); /* 255 */
strncat( name, dvd_name, 33 );
if( !stdout_flag )
{
makedir ( name );
strcat( name, "/VIDEO_TS/" );
makedir ( name );
fprintf( stderr, _("[Info] Writing files to this dir: %s\n"), name );
}
/*TODO: substitute with open_dir function */
strcpy( video_ts_dir, provided_input_dir );
strcat( video_ts_dir, "video_ts"); /*it's either video_ts */
dir = opendir( video_ts_dir ); /*or VIDEO_TS*/
if ( dir == NULL )
{
strcpy( video_ts_dir, provided_input_dir );
strcat( video_ts_dir, "VIDEO_TS");
dir = opendir( video_ts_dir );
if ( dir == NULL )
{
fprintf( stderr, _("[Error] Hmm, weird, the dir video_ts|VIDEO_TS on the dvd couldn't be opened\n"));
fprintf( stderr, _("[Error] The dir to be opened was: %s\n"), video_ts_dir );
fprintf( stderr, _("[Hint] Please mail me what your vobcopy call plus -v -v spits out\n"));
exit( 1 );
}
}
directory = readdir( dir ); /* thats the . entry */
directory = readdir( dir ); /* thats the .. entry */
/* according to the file type (vob, ifo, bup) the file gets copied */
while( ( directory = readdir( dir ) ) != NULL )
{/*main mirror loop*/
k = 0;
safestrncpy( output_file, name, sizeof(output_file)-1 );
/*in dvd specs it says it must be uppercase VIDEO_TS/VTS...
but iso9660 mounted dvd's sometimes have it lowercase */
while( directory->d_name[k] )
{
d_name[k] = toupper (directory->d_name[k] );
k++;
}
d_name[k] = 0;
/*in order to copy only _one_ file */
/* if( onefile_flag )
{
if( !strstr( onefile, d_name ) ) maybe other way around*/
/* continue;
}*/
/*in order to copy only _one_ file and do "globbing" a la: -O 02 will copy vts_02_1, vts_02_2 ... all that have 02 in it*/
if( onefile_flag )
{
char *tokenpos, *tokenpos1;
char tmp[12];
tokenpos = onefile;
if( strstr( tokenpos, "," ) )
{
while( ( tokenpos1 = strstr( tokenpos, "," ) ) ) /*tokens separated by , */
{ /*this parses every token without modifying the onefile var */
int len_begin;
len_begin = tokenpos1 - tokenpos;
safestrncpy( tmp, tokenpos, len_begin );