summaryrefslogtreecommitdiff
path: root/support/pkgcheck/src/main.rs
blob: c69423252d5a0670c2852e2a91b4d52590bd492c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
extern crate pest;
#[macro_use]
extern crate pest_derive;

mod gparser;

#[macro_use]
mod messages;
use messages::{explains, explains_all};

#[macro_use]
mod utils;
mod linkcheck;

mod recode;
use recode::crlf2lf;

mod filemagic;

use linkcheck::LinkCheck;
use std::fmt;
use std::fmt::Display;
use std::str;
use utils::*;

use std::os::unix::fs::MetadataExt;

use scoped_threadpool::Pool;


use tempdir::TempDir;
#[macro_use]
extern crate lazy_static;
use colored::*;

use regex::Regex;

use std::fs::File;
use std::fs::Metadata;
use std::io::prelude::*;
use std::io::BufReader;
use std::os::unix::fs::FileTypeExt;
use std::{fs, process};
// use std::os::unix::fs::PermissionsExt;

use std::ffi::OsStr;
use std::io::{self, Read};
use std::path::Path;
use std::path::PathBuf;
use std::sync::atomic::{AtomicBool, Ordering};

use blake2::{Blake2b, Digest};
use fnv::{FnvHashMap as HashMap, FnvHashSet as HashSet};
use std::sync::mpsc::{channel, Sender};
use structopt::StructOpt;
#[cfg(unix)]
use walkdir::{DirEntry, WalkDir};

fn err(path: &PathBuf, err: &io::Error) {
    e0027!(path.display(), err);
}

type HashSender = Sender<(u64, PathBuf, Vec<u8>)>;
//type DupeSender = Sender<(u64, Vec<PathBuf>)>;

const BLOCKSIZE: usize = 4096;

fn hash_file_inner(path: &PathBuf) -> io::Result<Vec<u8>> {
    let mut buf = [0u8; BLOCKSIZE];
    let mut fp = File::open(&path)?;
    let mut digest = Blake2b::default();
    // When we compare byte-by-byte, we don't need to hash the whole file.
    // Instead, hash a block of 4kB, skipping 100kB.
    loop {
        match fp.read(&mut buf)? {
            0 => break,
            n => digest.input(&buf[..n]),
        }
    }
    Ok(digest.result().to_vec())
}

fn hash_file(fsize: u64, path: PathBuf, tx: &HashSender) {
    match hash_file_inner(&path) {
        Ok(hash) => tx.send((fsize, path, hash)).unwrap(),
        Err(e) => err(&path, &e),
    }
}

fn fix_perms(entry: &str) {
    i0005!(entry);
    let rc = run_cmd("/bin/chmod", &["-v", "ug=rwX,o=rX", entry]);

    if rc.status {
        if let Some(op) = rc.output {
            print!("{}", op);
        }
    }
}

fn filename_has_bad_chars(entry: &DirEntry) -> Option<(usize, char)> {
    let s = entry.file_name().to_str().unwrap();

    for (i, c) in s.char_indices() {
        match c {
            '\x00'..='\x1f' => return Some((i, c)),
            ' ' | '!' | '"' | '#' | '$' | '%' | '&' | '\'' => return Some((i, c)),
            '(' | ')' | '*' => return Some((i, c)),
            '+' | ',' | '-' | '.' => (),
            '0'..='9' => (),
            ':' | ';' | '<' | '=' | '>' | '?' | '@' => return Some((i, c)),
            'A'..='Z' => (),
            '[' | '\\' | ']' | '^' => return Some((i, c)),
            '_' => (),
            '`' => return Some((i, c)),
            'a'..='z' => (),
            '{' | '|' | '}' | '~' => return Some((i, c)),
            '/' => return None,
            _ => return Some((i, c)),
        };
    }
    None
}

// returns false if CRLF, otherwise true
fn fix_crlf(fname: &str) -> bool {
    i0004!(fname);
    //run_cmd("/usr/bin/fromdos", &["-p", "-e", "-v", fname]).status
    match crlf2lf(fname) {
        Ok(_) => {
            i0007!(fname);
            true
        }
        Err(e) => {
            e0027!(fname, e);
            false
        }
    }
}

fn check_readme(dir_entry: &str, is_readme: &ReadmeKind, ft: &filemagic::Mimetype) -> bool {
    let msg_name = if let ReadmeKind::Symlink(s) = is_readme {
        format!("{} (symlinked from {})", dir_entry, &s)
    } else {
        dir_entry.to_string()
    };

    match ft {
        filemagic::Mimetype::Pdf
        | filemagic::Mimetype::Binary
        | filemagic::Mimetype::Archive
        | filemagic::Mimetype::Zip => {
            e0003!(msg_name);
            return false;
        }
        filemagic::Mimetype::BOM(b) => {
            e0029!(msg_name, b.as_ref());
            return false;
        }
        filemagic::Mimetype::Text | filemagic::Mimetype::TextCRLF => match File::open(dir_entry) {
            Ok(f) => {
                if !check_readme_inner(&msg_name, &f) {
                    return false;
                }
            }
            Err(e) => {
                e0027!(msg_name, e);
                return false;
            }
        },
        _ => (),
    }
    true
}

fn check_readme_inner(fname: &str, f: &std::fs::File) -> bool {
    let reader = BufReader::new(f);

    let lines = reader.split(b'\n').map(|l| l.unwrap());
    let mut result = true;

    for (lineno, line) in lines.enumerate() {
        if let Err(e) = String::from_utf8(line.clone()) {
            e0021!(fname, lineno + 1, e);
            result = false;
        }
    }
    result
}

fn is_readme(entry: &DirEntry) -> bool {
    let r = entry.file_name().to_str().unwrap();

    match r {
        "README" | "README.txt" | "README.md" => true,
        _ => false,
    }
}

fn get_devno(meta: &Metadata) -> u64 {
    meta.dev()
}

fn _get_devno(entry: &DirEntry) -> u64 {
    let meta = fs::metadata(entry.path().to_str().unwrap());
    match meta {
        Ok(m) => m.dev(),
        _ => 0,
    }
}

#[derive(StructOpt, Debug)]
#[structopt(about = "A checker for uploaded packages to CTAN.")]
struct Args {
    #[structopt(short = "I", long = "ignore-dupes", help = "Ignore dupes")]
    ignore_dupes: bool,
    #[structopt(short = "v", long = "verbose", help = "Verbose operation?")]
    verbose: bool,
    #[structopt(short = "L", long = "correct-crlf", help = "Correct CRLF line endings")]
    correct_crlf: bool,
    #[structopt(short = "C", long = "correct-perms", help = "Correct permissions")]
    correct_perms: bool,
    #[structopt(long = "no-colors", help = "Don't display messages in color")]
    no_colors: bool,
    #[structopt(long = "urlcheck", help = "Check URLs found in README files")]
    urlcheck: bool,
    #[structopt(short = "T", long = "tds-zip", help = "tds zip archive", group = "tds")]
    tds_zip: Option<String>,
    #[structopt(
        short = "e",
        long = "explain",
        help = "Explain error or warning message",
        group = "only_one"
    )]
    explain: Option<String>,
    #[structopt(
        long = "explain-all",
        help = "Explains all error or warning messages",
        group = "only_one"
    )]
    explain_all: bool,
    #[structopt(
        long = "show-temp-endings",
        help = "Show file endings for temporary files",
        group = "only_one"
    )]
    show_tmp_endings: bool,
    #[structopt(short = "d", long = "package-dir", help = "Package directory")]
    pkg_dir: Option<String>,
}

// We take care to avoid visiting a single inode twice,
// which takes care of (false positive) hardlinks.
#[cfg(unix)]
fn check_inode(set: &mut HashSet<(u64, u64)>, meta: &Metadata) -> bool {
    set.insert((get_devno(meta), meta.ino()))
}

#[cfg(not(unix))]
fn check_inode(_: &mut HashSet<u64>, _: &Metadata) -> bool {
    true
}

lazy_static! {
    static ref ARGS: Args = Args::from_args();
    static ref ERROR_OCCURED: AtomicBool = AtomicBool::new(false);
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DPath {
    Both(PathBuf),
    Tds(PathBuf),
}

impl fmt::Display for DPath {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            DPath::Both(p) => write!(f, "{}", p.display()),
            DPath::Tds(p) => write!(f, "{}", p.display()),
        }
    }
}

#[derive(Default)]
pub struct DupPath {
    len: usize,
    plen: usize,
    dupes: Vec<DPath>,
}

impl DupPath {
    pub fn new() -> DupPath {
        DupPath {
            len: 0,
            plen: 0,
            dupes: Vec::new(),
        }
    }

    pub fn push(&mut self, pb: PathBuf) {
        let pname = pb.to_string_lossy();
        self.len += 1;
        if pname.ends_with(".tfm") {
            self.dupes.push(DPath::Tds(pb.clone()));
        } else {
            self.plen += 1;
            self.dupes.push(DPath::Both(pb.clone()));
        }
    }
}

type Hashes = HashMap<(u64, Vec<u8>), DupPath>;

fn main() {
    match &ARGS.explain {
        None => (),
        Some(e) => {
            explains(&e);
            process::exit(0);
        }
    }

    if ARGS.explain_all {
        explains_all();
        process::exit(0);
    }

    if ARGS.show_tmp_endings {
        show_tmp_endings();
        process::exit(0);
    }

    let pkg_dir = match &ARGS.pkg_dir {
        None => {
            f0001!();
            process::exit(1);
        }
        Some(d) => {
            if !exists_dir(&d) {
                f0002!(d);
                process::exit(1);
            };
            d
        }
    };

    let tds_zip = &ARGS.tds_zip;
    let pkg_name = match tds_zip {
        None => None,
        Some(tz) => {
            let pn = check_tds_archive_name(tds_zip);
            if !exists_file(&tz) {
                f0003!(&tz);
                process::exit(1);
            }
            let mut fmagic = filemagic::Filetype::new();
            match fmagic.analyze(&tz) {
                Ok(filemagic::Mimetype::Zip) => (),
                _ => {
                    f0004!(&tz);
                    process::exit(1)
                }
            };
            pn
        }
    };

    if let Some(hashes) = check_package(pkg_dir, tds_zip) {
        if tds_zip.is_some() {
            if let Some(pn) = pkg_name {
                if let Some(s) = ARGS.tds_zip.as_ref() {
                    match check_tds_archive(&pn, s, &hashes) {
                        Ok(_) => (),
                        Err(e) => {
                            e0027!(s, e);
                        }
                    }
                }
            }
        }
    }

    if ERROR_OCCURED.load(Ordering::Relaxed) {
        process::exit(1);
    } else {
        process::exit(0);
    }
}

fn print_duplicates(hashes: &Hashes) {
    let mut total_dupes = 0;
    let mut total_files = 0;
    let mut total_size = 0;

    let mut header_printed = false;
    for (k, paths) in hashes.iter() {
        let (sz, _hash) = k;

        if paths.plen <= 1 {
            total_files += 1;
            total_size += sz;
            continue;
        } else if !header_printed {
            w0002!();
            header_printed = true;
        }

        total_files += paths.plen;
        total_size += sz * (paths.plen - 1) as u64;
        total_dupes += (paths.plen - 1) as u64;

        println!("Size: {}", sz);
        for p in &paths.dupes {
            if let DPath::Both(p) = p {
                let ps = p.as_path().to_str().unwrap();
                println!("   >>> {}", ps);
            }
        }
        println!();
    }

    if ARGS.verbose && total_dupes > 0 {
        println!("Duplicate statistics");
        println!("  Found {} duplicate files", total_files);
        println!("  Size of duplicate files: {}", total_size);
    }
}

//#[derive(Debug, PartialEq, Eq)]
#[derive(Debug, PartialEq, Eq)]
pub enum FType {
    Regular,
    Directory,
    Symlink,
    BlockDevice,
    CharDevice,
    Fifo,
    Socket,
    Error(String),
}

fn get_filetype(entry: &DirEntry) -> FType {
    match entry.metadata() {
        Ok(mt) => {
            let ft = mt.file_type();
            if ft.is_symlink() {
                return FType::Symlink;
            }
            if ft.is_dir() {
                return FType::Directory;
            }
            if ft.is_block_device() {
                return FType::BlockDevice;
            }
            if ft.is_char_device() {
                return FType::CharDevice;
            }
            if ft.is_fifo() {
                return FType::Fifo;
            }
            if ft.is_socket() {
                return FType::Socket;
            }
            FType::Regular
        }
        Err(e) => FType::Error(format!("{}", e)),
    }
}

//
// read file into buffer[u8]
// then convert into string
//
fn check_generated_files(entry: &str, generated: &mut HashMap<PathBuf, PathBuf>) {
    // unwrap() is ok here as we only call this function for files,
    // specifically .ins or .dtx files
    let entry_fname = filename(entry).unwrap().to_string();
    let entry_base = &entry_fname[0..entry_fname.len() - 4];

    let fhdl = File::open(&entry);
    match fhdl {
        Ok(mut f) => {
            let mut buf = Vec::new();

            match f.read_to_end(&mut buf) {
                Ok(_bytes_read) => {
                    if let Some(found) =
                        gparser::parse_generate(&String::from_utf8_lossy(&buf.clone()))
                    {
                        for fname in found {
                            // As we request a README in the top level directory of
                            // a package we ignore if a README was generated by an
                            // .ins or .dtx file
                            // CAVEAT: If this happens in a subdirectory it could be an error!!!!
                            if fname == "README" || fname == "README.txt" || fname == "README.md" {
                                continue;
                            }

                            // If the filename in the 'file{} statement is jobname.<ext>
                            // we replace jobname with the filename before we investigate further
                            let fname_x = if fname.starts_with("\\jobname") {
                                let ext = match get_extension_from_filename(&fname) {
                                    Some(e) => e,
                                    None => "",
                                };
                                format!("{}.{}", entry_base, ext)
                            } else {
                                fname.to_string()
                            };

                            let pb = PathBuf::from(fname_x);

                            generated.entry(pb).or_insert_with(|| PathBuf::from(entry));
                        }
                    };
                }
                Err(e) => println!("Error reading file {}: {:?}", entry, e),
            }
        }
        Err(e) => println!("Error opening file {}: {:?}", entry, e),
    }
}

fn x_bit_set(p: u32) -> bool {
    let p1 = p & 0o7777;
    p1 & 0o111 != 0
}

// checks that archive name is in the format x.tds.zip and returns:
//  None if not
//  Option<x> if x.tds.zip
// Caveat: we currently don't know if x is really the package name.
fn check_tds_archive_name(tds_zip: &Option<String>) -> Option<String> {
    match tds_zip {
        None => None,
        Some(tz) => {
            // 8 for ".tds.zip", 1 means a package name is at least a character
            if tz.len() < 8 + 1 || !tz.ends_with(".tds.zip") {
                f0005!(tz);
                process::exit(1);
            }
            let mut pname = String::from(utils::basename(&tz));
            let plen = pname.len();
            pname.truncate(plen - 8);
            Some(pname.to_string())
        }
    }
}

fn unzip_tds_archive(tds_zip: &str, tmp_dir: &str) {
    match run_cmd("/usr/bin/unzip", &["-q", "-d", tmp_dir, &tds_zip]) {
        CmdReturn { status: true, .. } => (),
        CmdReturn {
            status: false,
            output: out,
        } => {
            if let Some(o) = out {
                e0033!(&tds_zip, o);
            } else {
                e0033!(&tds_zip, "<no error given");
            }
            process::exit(1);
        }
    }
}

fn check_tds_archive(
    pkg_name: &str,
    tds_zip: &str,
    hashes: &HashMap<(u64, Vec<u8>), DupPath>,
) -> Result<(), io::Error> {
    i0003!(tds_zip);

    let dir_entry = Path::new(tds_zip);
    let p = get_perms(&dir_entry);
    if !owner_has(p, 4) || !others_have(p, 4) || x_bit_set(p) {
        e0024!(tds_zip, perms_to_string(p));
        if ARGS.correct_perms {
            i0005!(&tds_zip);
            set_perms(&tds_zip, 0o664);
        }
    };

    // We have a discrepancy between
    // TDS zip archive name: babel-base.tds.zip
    // and package name: babel
    // which yields a false test when checking for the package name
    // the path names of the TDS zip archive files. 
    //    Therefore, we correct here.
    let real_pkg_name = if pkg_name == "babel-base" {
        "babel"
    } else {
        pkg_name
    };


    let tmp_dir = TempDir::new("pkgcheck")?;
    let tmp_dir_offset = tmp_dir.path().to_str().unwrap().len() + 1;
    unzip_tds_archive(tds_zip, tmp_dir.path().to_str().unwrap());

    let tds_toplevel_dirs: HashSet<String> = [
        "tex", "fonts", "metafont", "metapost", "bibtex", "scripts", "doc", "source",
    ]
    .iter()
    .map(|&s| s.to_string())
    .collect();

    for entry in fs::read_dir(tmp_dir.path())? {
        let entry = entry?;
        let path = entry.path();

        match path.file_stem() {
            None => (),
            Some(file_stem) => {
                let fs = file_stem.to_str().expect("bad file name");
                if !tds_toplevel_dirs.contains(fs) {
                    if path.is_dir() {
                        e0020!(fs);
                    } else {
                        e0034!(fs);
                    }
                }
            }
        }

        // if path.is_dir() {
        //     match path.file_stem() {
        //         None => (),
        //         Some(file_stem) => {
        //             let fs = file_stem.to_str().expect("bad file name");
        //             if !tds_toplevel_dirs.contains(fs) {
        //                 e0020!(fs);
        //             }
        //         }
        //     }
        // } else {
        //     println!("mist");
        //     e0020!(path.display());
        // }
    }

    // in order to compare the package files with the content of the
    // tds zip archive we need to checksum the files in the tds zip
    // archive.

    let mut sizes = HashMap::default();
    let mut pool = Pool::new(num_cpus::get() as u32 + 1);
    {
        // Processing a single file entry, with the "sizes" hashmap collecting
        // same-size files.  Entries are either Found::One or Found::Multiple,
        // so that we can submit the first file's path as a hashing job when the
        // first duplicate is found.  Hashing each file is submitted as a job to
        // the pool.
        let mut process = |fsize, dir_entry: &DirEntry| {
            let path = dir_entry.path().to_path_buf();
            let sizeref = &mut sizes;

            sizeref.entry(fsize).or_insert_with(Vec::new).push(path);
        };

        let mut map_files_found = false;
        let mut map_dvips_found = false;

        for dir_entry in WalkDir::new(tmp_dir.path().to_str().unwrap()).follow_links(false) {
            match dir_entry {
                Ok(dir_entry) => {
                    //let dir_entry_str = dir_entry.path().to_str().unwrap();

                    let ft = get_filetype(&dir_entry);

                    if ft == FType::Directory {
                        continue;
                    }

                    if let FType::Error(e) = ft {
                        e0023!(e);
                        continue;
                    }

                    match dir_entry.metadata() {
                        Ok(meta) => {
                            let fsize = meta.len();
                            process(fsize, &dir_entry);

                            let dir_entry_str = match dir_entry.path().to_str() {
                                Some(d) => d,
                                None => {
                                    e0031!(dir_entry.path().to_string_lossy());
                                    continue;
                                }
                            };
                            let dir_entry_display = &dir_entry_str[tmp_dir_offset..];

                            if is_temporary_file(&dir_entry_str) {
                                e0008!(dir_entry_display);
                            }
                            if fsize > 40 * 1024 * 1024 {
                                w0006!(dir_entry_display, fsize / 1024 / 1024);
                            }

                            // if the path doesn't contain a man page...
                            if dir_entry_str.find("/man/").is_none() {
                                let pkg_name_s = format!("/{}/", real_pkg_name);
                                // ...then we want to have the package name in the path
                                if dir_entry_str.find(&pkg_name_s).is_none() {
                                    e0028!(real_pkg_name, dir_entry_display);
                                }
                            }

                            if dir_entry_str.ends_with(".map") {
                                map_files_found = true;
                                let re: Regex = Regex::new(r"fonts[/]map[/]dvips[/]").unwrap();
                                if re.is_match(dir_entry_str) {
                                    map_dvips_found = true;
                                }

                                // if ( $fname =~ m{ fonts [/] map [/] dvips [/] }xms ) {
                                //     $map_dvips_found = 1;
                                // }
                            }
                        }
                        Err(e) => {
                            e0027!(dir_entry.path().display(), e);
                        }
                    }
                }
                Err(e) => {
                    eprintln!("{}", e);
                }
            }
        }

        if map_files_found && !map_dvips_found {
            println!( "Map files found for package {} but none of them is in a path starting with fonts/map/dvips", pkg_name);
        }
    };

    let mut tds_hashes: HashMap<(u64, Vec<u8>), Vec<PathBuf>> = HashMap::default();
    pool.scoped(|scope| {
        let (tx, rx) = channel();

        let hashref = &mut tds_hashes;
        scope.execute(move || {
            for (size, path, hash) in rx.iter() {
                hashref
                    .entry((size, hash))
                    .or_insert_with(Vec::new)
                    .push(path);
            }
        });

        for size in sizes.keys() {
            for p in &sizes[size] {
                let txc = tx.clone();
                scope.execute(move || hash_file(*size, p.to_path_buf(), &txc));
            }
        }
    });

    tmp_dir.close()?;
    // now check if each package file is in the tds archive
    for (k, paths) in hashes.iter() {
        if !tds_hashes.contains_key(k) {
            let p = &paths.dupes[0];
            e0026!(p);
        }
    }

    Ok(())
}

fn get_extension_from_filename(filename: &str) -> Option<&str> {
    Path::new(filename).extension().and_then(OsStr::to_str)
}

fn found_unwanted_filetype(fname: &str, ft: &FType) -> bool {
    match ft {
        FType::Socket => {
            e0013!(fname);
            true
        }
        FType::Fifo => {
            e0014!(fname);
            true
        }
        FType::BlockDevice => {
            e0015!(fname);
            true
        }
        FType::CharDevice => {
            e0016!(fname);
            true
        }
        FType::Error(e) => {
            e0023!(e);
            true
        }
        _ => false,
    }
}

fn check_and_correct_perms(dir_entry: &str, p: u32) {
    if owner_has(p, 5) || !others_have(p, 4) {
        e0002!(dir_entry, perms_to_string(p));
        if ARGS.correct_perms {
            i0005!(&dir_entry);
            set_perms(&dir_entry, 0o664);
        }
    };
}


#[derive(Debug, Clone, PartialEq)]
enum FileKind {
    File,
    Directory,
    Symlink(String),
}


impl Display for FileKind {
    fn fmt(&self, f: &mut ::std::fmt::Formatter) -> Result<(), ::std::fmt::Error> {
        match *self {
            FileKind::File => f.write_str("file"),
            FileKind::Directory => f.write_str("directory"),
            FileKind::Symlink(_) => f.write_str("symlink"),
        }
    }
}



#[derive(Debug, Clone, PartialEq)]
enum ReadmeKind {
    No,
    Yes,
    Symlink(String),
}

fn check_package(root: &str, tds_zip: &Option<String>) -> Option<Hashes> {
    //let mut urls = HashMap::default(); //: <String,UrlStatus>

    let mut lcnames: HashMap<PathBuf, Vec<(PathBuf, FileKind)>> = HashMap::default();

    let mut doublenames: HashMap<PathBuf, Vec<PathBuf>> = HashMap::default();

    let mut inodes = HashSet::default();

    i0002!(root);
    // This hash contains all package file names.
    //
    //  PathBuf: the full path starting at the directory specified at the command line
    //  Metadata: the meta data of the file
    //  String: the file name without any directory part
    //  ReadmeKind: is it a certain README, file or symlink?
    //      A special case of a README file is a file with has a different name but
    //      was poinded to by a symlink. Example: README --> README.rst
    let mut file_names: HashMap<PathBuf, (Metadata, String, ReadmeKind)> = HashMap::default();
    {
        let mut readme_found = false;

        let root_absolute = PathBuf::from(root)
            .canonicalize()
            .unwrap()
            .to_string_lossy()
            .to_string();
        for dir_entry in WalkDir::new(root).follow_links(false) {
            match dir_entry {
                Ok(dir_entry) => {
                    let dir_entry_str = match dir_entry.path().to_str() {
                        Some(d) => d,
                        None => {
                            e0031!(dir_entry.path().to_string_lossy());
                            continue;
                        }
                    };
                    let meta = match dir_entry.metadata() {
                        Ok(meta) => meta,
                        Err(e) => {
                            e0023!(e);
                            continue;
                        }
                    };

                    if !check_inode(&mut inodes, &meta) {
                        continue;
                    }

                    // this is the file_name without the directory part
                    // unwrap() is ok here as we covered potential UTF-8 related errors
                    // above in the definition of dir_entry_str
                    let file_name = dir_entry.file_name().to_str().unwrap().to_string();

                    let ft = get_filetype(&dir_entry);

                    if found_unwanted_filetype(dir_entry_str, &ft) {
                        continue;
                    }

                    if let Some((offset, ch)) = filename_has_bad_chars(&dir_entry) {
                        e0001!(ch, dir_entry_str, offset);
                    }

                    // 1. dealing with symlinks
                    if ft == FType::Symlink {
                        match get_symlink(&dir_entry) {
                            Ok(None) => {
                                e0010!(dir_entry_str);
                                continue;
                            }
                            Err(e) => {
                                e0027!(dir_entry_str, e);
                                continue;
                            }
                            Ok(Some(p)) => {
                                let pd: String =
                                    p.canonicalize().unwrap().to_string_lossy().to_string();
                                if !pd.starts_with(&root_absolute) {
                                    e0030!(dir_entry_str, p.display());
                                    continue;
                                }

                                let lc_dir_entry_str = dir_entry_str.to_lowercase();
                                if let Some(_dir_name) = filename(dir_entry_str) {
                                    let lcnref = &mut lcnames;
                                    lcnref
                                        .entry(PathBuf::from(lc_dir_entry_str))
                                        .or_insert_with(Vec::new)
                                        .push((
                                            PathBuf::from(dir_entry_str),
                                            //FileKind::Symlink(dir_entry_str.into()),
                                            FileKind::Symlink(pd.clone()),
                                        ));
                                }
                                if is_readme(&dir_entry) {
                                    readme_found = true;
                                    file_names.insert(
                                        p,
                                        (
                                            meta,
                                            file_name,
                                            ReadmeKind::Symlink(dir_entry_str.to_string()),
                                        ),
                                    );
                                }
                                    continue;
                            }
                        }
                    }

                    let p = get_perms(&dir_entry.path());

                    // 2. dealing with directories
                    if ft == FType::Directory {
                        let lc_dir_entry_str = dir_entry_str.to_lowercase();
                        if let Some(_dir_name) = filename(dir_entry_str) {
                            let lcnref = &mut lcnames;

                            lcnref
                                .entry(PathBuf::from(lc_dir_entry_str))
                                .or_insert_with(Vec::new)
                                .push((PathBuf::from(dir_entry_str), FileKind::Directory));
                        }

                        if !owner_has(p, 5) || !others_have(p, 5) {
                            e0011!(dir_entry_str, perms_to_string(p));
                            if ARGS.correct_perms {
                                i0005!(&dir_entry_str);
                                set_perms(&dir_entry_str, 0o775);
                            }
                        }

                        match is_empty_directory(&dir_entry) {
                            Ok(true) => {
                                e0004!(dir_entry_str);
                            }
                            Err(e) => {
                                e0027!(dir_entry_str, e);
                            }
                            Ok(false) => (),
                        }
                        if is_hidden_directory(&dir_entry) {
                            e0006!(dir_entry_str);
                        }

                        if is_unwanted_directory(&dir_entry.file_name().to_str().unwrap()) {
                            e0018!(dir_entry_str);
                        }
                        continue;
                    }

                    // 3. dealing with regular files
                    if is_hidden(&file_name) {
                        e0007!(dir_entry_str);
                    }
                    if is_temporary_file(&dir_entry_str) {
                        e0008!(dir_entry_str);
                    }


                    if let Some(file_name) = filename(dir_entry_str) {
                        let doubleref = &mut doublenames;

                        doubleref
                            .entry(PathBuf::from(file_name))
                            .or_insert_with(Vec::new)
                            .push(PathBuf::from(dir_entry_str));
                    }

                    // detecting large files in package directory

                    if is_readme(&dir_entry) {
                        // We want to deal with README files only if they are
                        // in the root directory of the package.
                        let f = format!(
                            "{}{}{}",
                            root,
                            // we have to pay attention if `root` ends already with '/'
                            if root.ends_with('/') { "" } else { "/" },
                            &file_name
                        );

                        if dir_entry_str == f {
                            readme_found = true;
                            file_names.insert(
                                dir_entry.path().to_path_buf(),
                                (meta, file_name.clone(), ReadmeKind::Yes),
                            );
                        } else {
                            file_names.entry(dir_entry.path().to_path_buf()).or_insert((
                                meta,
                                file_name.clone(),
                                ReadmeKind::No,
                            ));
                        }
                    } else {
                        file_names.entry(dir_entry.path().to_path_buf()).or_insert((
                            meta,
                            file_name.clone(),
                            ReadmeKind::No,
                        ));
                    }

                    let lc_dir_entry_str = dir_entry_str.to_lowercase();

                    let lcnref = &mut lcnames;

                    lcnref
                        .entry(PathBuf::from(lc_dir_entry_str))
                        .or_insert_with(Vec::new)
                        .push((PathBuf::from(dir_entry_str), FileKind::File));

                }

                Err(e) => {
                    eprintln!("{}", e);
                }
            }
        }

        if !readme_found {
            e0009!();
        }
    }

    let lc = LinkCheck::new(4, false);
    let mut detective = filemagic::Filetype::new();

    let mut sizes = HashMap::default();
    let mut generated: HashMap<PathBuf, PathBuf> = HashMap::default();

    {
        // Processing a single file entry, with the "sizes" hashmap collecting
        // same-size files.  Entries are either Found::One or Found::Multiple,
        // so that we can submit the first file's path as a hashing job when the
        // first duplicate is found.  Hashing each file is submitted as a job to
        // the pool.
        let mut process = |fsize, path: &PathBuf| {
            let sizeref = &mut sizes;

            let path = path.clone();
            sizeref.entry(fsize).or_insert_with(Vec::new).push(path);
        };
        for (path, (meta, _file_name, is_readme)) in file_names.iter() {

            //println!("DEBUG: path {:?}, file_name {:?}, is_readme: {} ", &path, &file_name, is_readme);
            let dir_entry_str = match path.to_str() {
                Some(d) => d,
                None => {
                    e0031!(&path.to_string_lossy());
                    continue;
                }
            };

            let fsize = meta.len();

            // if it is an empty file we don't need
            // to check further
            if fsize == 0 {
                e0005!(dir_entry_str);
                continue;
            }
            if fsize > 40 * 1024 * 1024 {
                w0005!(dir_entry_str, fsize / 1024 / 1024);
                continue;
            }

            let p = get_perms(&path);
            if !owner_has(p, 4) {
                e0002!(dir_entry_str, perms_to_string(p));
                if ARGS.correct_perms {
                    fix_perms(&dir_entry_str);
                }
                continue;
            }
            let ftr = detective.analyze(&dir_entry_str);
            //println!(">>> {:?}", ftr);
            // we ignore errors from filetype recognition
            if ftr.is_err() {
                continue;
            }

            let ft = ftr.unwrap();

            // DEBUG !readme_symlinked.contains(dir_entry_str)
            if ReadmeKind::No != *is_readme {
                if !check_readme(dir_entry_str, is_readme, &ft) {
                    continue;
                }
                if ARGS.urlcheck {
                    lc.check_urls(dir_entry_str);
                }
            }

            match ft {
                filemagic::Mimetype::Text | filemagic::Mimetype::TextCRLF => {
                    check_and_correct_perms(dir_entry_str, p);
                    let fext = get_extension_from_filename(&dir_entry_str);
                    if fext == Some("ins") || fext == Some("dtx") {
                        check_generated_files(&dir_entry_str, &mut generated);
                    }
                    match fext {
                        Some("bat") | Some("cmd") |
                        Some("nsh") | Some("reg") => (),
                        Some(_) | None => {
                            if ft == filemagic::Mimetype::TextCRLF {
                                e0012!(dir_entry_str);
                                if ARGS.correct_crlf {
                                    fix_crlf(dir_entry_str);
                                }
                            }
                        }
                    };
                }

                filemagic::Mimetype::BOM(b) => {
                    //println!("{}: {} with BOM detected", dir_entry_str, b.as_ref());
                    w0004!(dir_entry_str, b.as_ref());
                    check_and_correct_perms(dir_entry_str, p);
                }
                filemagic::Mimetype::Binary
                | filemagic::Mimetype::Script
                | filemagic::Mimetype::ScriptCRLF => {
                    if !owner_has(p, 4) || !others_have(p, 4) {
                        e0002!(dir_entry_str, perms_to_string(p));
                    };
                    if ARGS.correct_perms {
                        fix_perms(&dir_entry_str);
                    }
                }
                filemagic::Mimetype::Pdf => {
                    check_and_correct_perms(dir_entry_str, p);
                    let ret = is_pdf_ok(&dir_entry_str);
                    if !ret.status {
                        e0017!(dir_entry_str);
                        if let Some(output) = ret.output {
                            println!("{}", &output);
                        };
                    }
                }

                filemagic::Mimetype::Archive | filemagic::Mimetype::Zip => {
                    w0001!(dir_entry_str);
                    check_and_correct_perms(dir_entry_str, p);
                }

                filemagic::Mimetype::Data => check_and_correct_perms(dir_entry_str, p),
                _ => continue,
            }
            if others_match(p, 0) {
                e0002!(dir_entry_str, perms_to_string(p));
                if ARGS.correct_perms {
                    i0005!(&dir_entry_str);
                    set_perms(&dir_entry_str, 0o664);
                }
            }
            if !(ARGS.ignore_dupes && tds_zip.is_none()) {
                process(fsize, &path);
            }
        }
    }

    print_casefolding(&lcnames);
    print_generated(&doublenames, &generated);
    print_doublenames(&doublenames);

    if ARGS.ignore_dupes && tds_zip.is_none() {
        return None;
    }

    // Set up thread pool for the task to hash a file.  Number of CPUs + 1 has been
    // found to be a good pool size, likely since the walker thread should be
    // doing mostly IO.
    let mut pool = Pool::new(num_cpus::get() as u32 + 1);

    let mut hashes: HashMap<(u64, Vec<u8>), DupPath> = HashMap::default();
    pool.scoped(|scope| {
        let (tx, rx) = channel();

        let hashref = &mut hashes;
        scope.execute(move || {
            for (size, path, hash) in rx.iter() {
                hashref
                    .entry((size, hash))
                    .or_insert_with(DupPath::new)
                    .push(path);
            }
        });

        for size in sizes.keys() {
            let paths = &sizes[size];
            if paths.len() == 1 && tds_zip.is_none() {
                continue;
            };

            for p in &sizes[size] {
                let txc = tx.clone();
                scope.execute(move || hash_file(*size, p.to_path_buf(), &txc));
            }
        }
    });

    if !ARGS.ignore_dupes {
        print_duplicates(&hashes);
    }
    Some(hashes)
}

fn print_casefolding(hashes: &HashMap<PathBuf, Vec<(PathBuf, FileKind)>>) {

    for (k, eles) in hashes.iter() {
        //println!("pcf: {:?}, {:?}", k, &eles);
        if eles.len() == 1 {
            continue;
        }

        e0025!(k.display());

        for (p, ty) in eles {
            println!("   >>> {} ({})", p.display(), ty);
        }
    }
}

fn print_generated(hashes: &HashMap<PathBuf, Vec<PathBuf>>, generated: &HashMap<PathBuf, PathBuf>) {
    for (k, gen) in generated.iter() {
        if hashes.contains_key(k) {
            let v = &hashes[k];
            for fname in v {
                e0019!(fname.to_str().unwrap(), gen.to_str().unwrap());
            }
        }
    }
}

fn print_doublenames(hashes: &HashMap<PathBuf, Vec<PathBuf>>) {
    for (k, paths) in hashes.iter() {
        if paths.len() == 1 {
            continue;
        }
        let ks = k.to_str().unwrap();
        if ks == "README"
            || ks == "README.txt"
            || ks == "README.md"
            || ks == "Makefile"
            || ks == "Makefile.am"
            || ks == "Makefile.in"
            || ks == "makefile"
        {
            continue;
        }

        w0003!(k.to_str().unwrap());
        //        println!(":: {}", k.display());

        for p in paths {
            println!("   >>> {}", p.display());
        }
    }
}

fn show_tmp_endings() {
    i0006!();
    for (t, c) in temp_file_endings() {
        println!("{:23} {}", t, c);
    }
}