oxyromon 0.14.1

Rusty ROM OrgaNizer
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
#[cfg(feature = "chd")]
use super::chdman;
use super::checksum::*;
use super::config::*;
use super::database::*;
#[cfg(feature = "rvz")]
use super::dolphin;
#[cfg(feature = "cso")]
use super::maxcso;
use super::model::*;
use super::progress::*;
use super::prompt::*;
use super::sevenzip;
use super::util::*;
use super::SimpleResult;
use async_std::path::Path;
use cfg_if::cfg_if;
use clap::builder::PossibleValuesParser;
use clap::{Arg, ArgMatches, Command};
use indicatif::ProgressBar;
use rayon::prelude::*;
use sqlx::sqlite::SqliteConnection;
use std::collections::HashSet;
use std::ffi::OsString;
use std::path::PathBuf;
use std::str::FromStr;
use std::time::Duration;
use strum::VariantNames;
#[cfg(feature = "ird")]
use walkdir::WalkDir;

pub fn subcommand() -> Command {
    Command::new("import-roms")
        .about("Validate and import ROM files or directories into oxyromon")
        .arg(
            Arg::new("ROMS")
                .help("Set the ROM files or directories to import")
                .required(true)
                .num_args(1..)
                .index(1)
                .value_parser(value_parser!(PathBuf)),
        )
        .arg(
            Arg::new("SYSTEM")
                .short('s')
                .long("system")
                .help("Set the system number to use")
                .required(false)
                .num_args(1),
        )
        .arg(
            Arg::new("HASH")
                .short('a')
                .long("hash")
                .help("Set the hash algorithm")
                .required(false)
                .num_args(1)
                .value_parser(PossibleValuesParser::new(HashAlgorithm::VARIANTS)),
        )
}

pub async fn main(
    connection: &mut SqliteConnection,
    matches: &ArgMatches,
    progress_bar: &ProgressBar,
) -> SimpleResult<()> {
    let romfile_paths: Vec<&PathBuf> = matches.get_many::<PathBuf>("ROMS").unwrap().collect();
    let system = prompt_for_system(
        connection,
        matches
            .get_one::<String>("SYSTEM")
            .map(|s| FromStr::from_str(s).expect("Failed to parse number")),
    )
    .await?;
    let header = find_header_by_system_id(connection, system.id).await;
    let hash_algorithm = match matches.get_one::<String>("HASH").map(String::as_str) {
        Some("crc") => HashAlgorithm::Crc,
        Some("md5") => HashAlgorithm::Md5,
        Some(&_) | None => {
            match find_setting_by_key(connection, "HASH_ALGORITHM")
                .await
                .unwrap()
                .value
                .as_deref()
            {
                Some("crc") => HashAlgorithm::Crc,
                Some("md5") => HashAlgorithm::Md5,
                Some(&_) | None => bail!("Not possible"),
            }
        }
    };

    for romfile_path in romfile_paths {
        progress_bar.println(format!("Processing \"{:?}\"", &romfile_path));
        let romfile_path = get_canonicalized_path(&romfile_path).await?;
        if romfile_path.is_dir().await {
            cfg_if! {
                if #[cfg(feature = "ird")] {
                    if romfile_path.join(PS3_DISC_SFB).is_file().await {
                        import_jbfolder(connection, progress_bar, &system, &romfile_path).await?;
                    } else {
                        let walker = WalkDir::new(&romfile_path).into_iter();
                        for entry in walker.filter_map(|e| e.ok()) {
                            if entry.path().is_file() {
                                import_rom(
                                    connection,
                                    progress_bar,
                                    &system,
                                    &header,
                                    &entry.path(),
                                    &hash_algorithm,
                                )
                                .await?;
                            }
                        }
                    }
                } else {
                    let walker = WalkDir::new(&romfile_path).into_iter();
                    for entry in walker.filter_map(|e| e.ok()) {
                        if entry.path().is_file() {
                            import_rom(
                                connection,
                                progress_bar,
                                &system,
                                &header,
                                &entry.path(),
                                &hash_algorithm,
                            )
                            .await?;
                        }
                    }
                }
            }
        } else {
            import_rom(
                connection,
                progress_bar,
                &system,
                &header,
                &romfile_path,
                &hash_algorithm,
            )
            .await?;
        }
        progress_bar.println("");
    }

    // mark games and system as complete if they are
    progress_bar.set_style(get_none_progress_style());
    progress_bar.enable_steady_tick(Duration::from_millis(100));
    progress_bar.set_message("Computing system completion");
    update_games_by_system_id_mark_complete(connection, system.id).await;
    cfg_if! {
        if #[cfg(feature = "ird")] {
            update_jbfolder_games_by_system_id_mark_complete(connection, system.id).await;
        }
    }
    update_system_mark_complete(connection, system.id).await;

    Ok(())
}

pub async fn import_rom<P: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
    header: &Option<Header>,
    romfile_path: &P,
    hash_algorithm: &HashAlgorithm,
) -> SimpleResult<()> {
    let mut transaction = begin_transaction(connection).await;

    // abort if the romfile is already in the database
    if find_romfile_by_path(
        &mut transaction,
        romfile_path.as_ref().as_os_str().to_str().unwrap(),
    )
    .await
    .is_some()
    {
        progress_bar.println("Already in database");
        return Ok(());
    }

    let romfile_extension = romfile_path
        .as_ref()
        .extension()
        .unwrap_or(&OsString::new())
        .to_str()
        .unwrap()
        .to_lowercase();

    let system_directory = get_system_directory(&mut transaction, progress_bar, system).await?;

    if ARCHIVE_EXTENSIONS.contains(&romfile_extension.as_str()) {
        import_archive(
            &mut transaction,
            progress_bar,
            &system_directory,
            system,
            header,
            &romfile_path,
            &romfile_extension,
            hash_algorithm,
        )
        .await?;
    } else if CHD_EXTENSION == romfile_extension {
        cfg_if! {
            if #[cfg(feature = "chd")] {
                import_chd(
                    &mut transaction,
                    progress_bar,
                    &system_directory,
                    system,
                    header,
                    &romfile_path,
                    hash_algorithm,
                )
                .await?;
            } else {
                progress_bar.println("Please rebuild with the CHD feature enabled");
            }
        }
    } else if CSO_EXTENSION == romfile_extension {
        cfg_if! {
            if #[cfg(feature = "cso")] {
                import_cso(
                    &mut transaction,
                    progress_bar,
                    &system_directory,
                    system,
                    header,
                    &romfile_path,
                    hash_algorithm,
                )
                .await?;
            } else {
                progress_bar.println("Please rebuild with the CSO feature enabled");
            }
        }
    } else if RVZ_EXTENSION == romfile_extension {
        cfg_if! {
            if #[cfg(feature = "rvz")] {
                import_rvz(
                    &mut transaction,
                    progress_bar,
                    &system_directory,
                    system,
                    header,
                    &romfile_path,
                    hash_algorithm,
                )
                .await?;
            } else {
                progress_bar.println("Please rebuild with the RVZ feature enabled");
            }
        }
    } else {
        import_other(
            &mut transaction,
            progress_bar,
            &system_directory,
            system,
            header,
            &romfile_path,
            &romfile_extension,
            hash_algorithm,
        )
        .await?;
    }

    commit_transaction(transaction).await;

    Ok(())
}

#[cfg(feature = "ird")]
async fn import_jbfolder<P: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
    folder_path: &P,
) -> SimpleResult<()> {
    let sfb_romfile_path = folder_path.as_ref().join(PS3_DISC_SFB);

    // abort if the romfile is already in the database
    if find_romfile_by_path(connection, sfb_romfile_path.as_os_str().to_str().unwrap())
        .await
        .is_some()
    {
        progress_bar.println("Already in database");
        return Ok(());
    }

    let mut transaction = begin_transaction(connection).await;

    // find the correct game
    let (size, md5) = get_size_and_hash(
        &mut transaction,
        progress_bar,
        &sfb_romfile_path,
        &None,
        1,
        1,
        &HashAlgorithm::Md5,
    )
    .await?;
    let sfb_rom =
        match find_sfb_rom_by_md5(&mut transaction, size, &md5, system, progress_bar).await? {
            Some(rom) => rom,
            None => {
                move_to_trash(&mut transaction, progress_bar, system, &folder_path).await?;
                return Ok(());
            }
        };
    let game = find_game_by_id(&mut transaction, sfb_rom.game_id).await;

    let system_directory = get_system_directory(&mut transaction, progress_bar, system).await?;

    let walker = WalkDir::new(folder_path.as_ref()).into_iter();
    for entry in walker.filter_map(|e| e.ok()) {
        if entry.path().is_file() {
            progress_bar.println(format!(
                "Processing \"{}\"",
                &entry.path().as_os_str().to_str().unwrap()
            ));
            // force MD5 as IRD files only provide those
            let (size, md5) = get_size_and_hash(
                &mut transaction,
                progress_bar,
                &entry.path(),
                &None,
                1,
                1,
                &HashAlgorithm::Md5,
            )
            .await?;

            let rom: Option<Rom>;
            let mut roms = find_roms_without_romfile_by_size_and_md5_and_parent_id(
                &mut transaction,
                size,
                &md5,
                sfb_rom.parent_id.unwrap(),
            )
            .await;

            // abort if no match
            if roms.is_empty() {
                if count_roms_with_romfile_by_size_and_md5_and_parent_id(
                    &mut transaction,
                    size,
                    &md5,
                    sfb_rom.parent_id.unwrap(),
                )
                .await
                    > 0
                {
                    progress_bar.println("Already imported");
                } else {
                    progress_bar.println("No match");
                }
                continue;
            }

            // select the first rom if there is only one
            if roms.len() == 1 {
                rom = Some(roms.remove(0));
                progress_bar.println(format!("Matches \"{}\"", rom.as_ref().unwrap().name));
            } else {
                // select the first rom that matches the file name if there multiple matches
                if let Some(rom_index) = roms.iter().position(|rom| {
                    entry
                        .path()
                        .as_os_str()
                        .to_str()
                        .unwrap()
                        .ends_with(&rom.name)
                }) {
                    rom = Some(roms.remove(rom_index));
                } else {
                    // let the user select the rom if all else fails
                    rom = prompt_for_rom(&mut roms, None)?;
                }
            }

            if let Some(rom) = rom {
                // abort if rom already has a file
                if rom.romfile_id.is_some() {
                    let romfile =
                        find_romfile_by_id(&mut transaction, rom.romfile_id.unwrap()).await;
                    progress_bar.println(format!("Duplicate of \"{}\"", romfile.path));
                    continue;
                }

                // put arcade roms in subdirectories as their names aren't unique
                let new_path = system_directory.join(&game.name).join(&rom.name);

                // move file if needed
                rename_file(progress_bar, &entry.path(), &new_path, false).await?;

                // persist in database
                create_or_update_romfile(&mut transaction, &new_path, &[rom]).await;

                // remove directories if empty
                let mut directory = entry.path().parent().unwrap();
                while directory.read_dir().unwrap().next().is_none() {
                    remove_directory(progress_bar, &directory, false).await?;
                    if directory == entry.path() {
                        break;
                    }
                    directory = directory.parent().unwrap();
                }
            }
        }
    }

    commit_transaction(transaction).await;

    Ok(())
}

async fn import_archive<P: AsRef<Path>, Q: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system_directory: &Q,
    system: &System,
    header: &Option<Header>,
    romfile_path: &P,
    romfile_extension: &str,
    hash_algorithm: &HashAlgorithm,
) -> SimpleResult<()> {
    let tmp_directory = create_tmp_directory(connection).await?;
    let sevenzip_infos = sevenzip::parse_archive(progress_bar, romfile_path)?;

    let mut roms_sevenzip_infos: Vec<(Rom, &sevenzip::ArchiveInfo)> = Vec::new();
    let mut game_ids: HashSet<i64> = HashSet::new();

    for sevenzip_info in &sevenzip_infos {
        progress_bar.println(format!("Processing \"{}\"", &sevenzip_info.path));

        let size: u64;
        let hash: String;

        // system has a header, crc is absent, or selected checksum is not crc
        if header.is_some() || sevenzip_info.crc.is_empty() || hash_algorithm != &HashAlgorithm::Crc
        {
            let extracted_path = sevenzip::extract_files_from_archive(
                progress_bar,
                romfile_path,
                &[&sevenzip_info.path],
                &tmp_directory.path(),
            )?
            .remove(0);
            let size_hash = get_size_and_hash(
                connection,
                progress_bar,
                &extracted_path,
                header,
                1,
                1,
                hash_algorithm,
            )
            .await?;
            remove_file(progress_bar, &extracted_path, true).await?;
            size = size_hash.0;
            hash = size_hash.1;
        } else {
            size = sevenzip_info.size;
            hash = sevenzip_info.crc.clone();
        }

        match find_rom_by_hash(
            connection,
            progress_bar,
            size,
            &hash,
            system,
            hash_algorithm,
        )
        .await?
        {
            Some(rom) => {
                game_ids.insert(rom.game_id);
                roms_sevenzip_infos.push((rom, sevenzip_info));
            }
            None => {
                if sevenzip_infos.len() == 1 {
                    move_to_trash(connection, progress_bar, system, romfile_path).await?;
                }
            }
        }
    }

    // archive contains a single full game with no invalid file
    if roms_sevenzip_infos.len() == sevenzip_infos.len() && game_ids.len() == 1 {
        let game_id = game_ids.drain().last().unwrap();
        let rom_ids: HashSet<i64> = find_roms_by_game_id_no_parents(connection, game_id)
            .await
            .into_par_iter()
            .map(|rom| rom.id)
            .collect();
        if rom_ids
            .difference(
                &roms_sevenzip_infos
                    .par_iter()
                    .map(|(rom, _)| rom.id)
                    .collect(),
            )
            .count()
            == 0
        {
            let game = find_game_by_id(connection, game_id).await;
            for (rom, sevenzip_info) in &roms_sevenzip_infos {
                if sevenzip_info.path != rom.name {
                    sevenzip::rename_file_in_archive(
                        progress_bar,
                        romfile_path,
                        &sevenzip_info.path,
                        &rom.name,
                    )?;
                }
            }

            let new_path = match roms_sevenzip_infos.len() {
                1 => {
                    let rom = &roms_sevenzip_infos.get(0).unwrap().0;
                    let rom_extension = Path::new(&rom.name)
                        .extension()
                        .unwrap_or(&OsString::new())
                        .to_str()
                        .unwrap()
                        .to_lowercase();
                    let mut archive_path;
                    if system.arcade || PS3_EXTENSIONS.contains(&rom_extension.as_str()) {
                        archive_path = system_directory
                            .as_ref()
                            .join(format!("{}.{}", &game.name, &romfile_extension));
                    } else {
                        archive_path = system_directory.as_ref().join(&rom.name);
                        archive_path.set_extension(romfile_extension);
                    }
                    archive_path
                }
                _ => system_directory
                    .as_ref()
                    .join(format!("{}.{}", &game.name, &romfile_extension)),
            };

            // move file
            rename_file(progress_bar, romfile_path, &new_path, false).await?;

            // persist in database
            create_or_update_romfile(
                connection,
                &new_path,
                &roms_sevenzip_infos
                    .into_iter()
                    .map(|(rom, _)| rom)
                    .collect::<Vec<Rom>>(),
            )
            .await;

            return Ok(());
        }
    }

    // all other cases
    for (rom, sevenzip_info) in roms_sevenzip_infos {
        let extracted_path = sevenzip::extract_files_from_archive(
            progress_bar,
            romfile_path,
            &[&sevenzip_info.path],
            &tmp_directory.path(),
        )?
        .remove(0);

        let game = find_game_by_id(connection, rom.game_id).await;

        let new_path;
        // put arcade roms and JB folders in subdirectories
        if system.arcade || game.jbfolder {
            let game = find_game_by_id(connection, rom.game_id).await;
            new_path = system_directory.as_ref().join(game.name).join(&rom.name)
        // use game name for PS3 updates and DLCs because rom name is usually gibberish
        } else if PS3_EXTENSIONS.contains(&romfile_extension) {
            new_path = system_directory
                .as_ref()
                .join(format!("{}.{}", &game.name, romfile_extension));
        } else {
            new_path = system_directory.as_ref().join(&rom.name);
        }

        // move file
        copy_file(progress_bar, &extracted_path, &new_path, false).await?;

        // persist in database
        create_or_update_romfile(connection, &new_path, &[rom]).await;
    }

    Ok(())
}

#[cfg(feature = "chd")]
async fn import_chd<P: AsRef<Path>, Q: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system_directory: &Q,
    system: &System,
    header: &Option<Header>,
    romfile_path: &P,
    hash_algorithm: &HashAlgorithm,
) -> SimpleResult<()> {
    let tmp_directory = create_tmp_directory(connection).await?;

    let mut cue_path = romfile_path.as_ref().to_path_buf();
    cue_path.set_extension(CUE_EXTENSION);

    if cue_path.is_file().await {
        progress_bar.println("CUE file found, using multiple tracks mode");
        let (size, hash) = get_size_and_hash(
            connection,
            progress_bar,
            &cue_path,
            header,
            1,
            1,
            hash_algorithm,
        )
        .await?;
        let cue_rom = match find_rom_by_hash(
            connection,
            progress_bar,
            size,
            &hash,
            system,
            hash_algorithm,
        )
        .await?
        {
            Some(rom) => rom,
            None => {
                move_to_trash(connection, progress_bar, system, &cue_path).await?;
                return Ok(());
            }
        };

        let roms: Vec<Rom> = find_roms_by_game_id_no_parents(connection, cue_rom.game_id)
            .await
            .into_iter()
            .filter(|rom| rom.id != cue_rom.id)
            .collect();

        let names_sizes: Vec<(&str, u64)> = roms
            .iter()
            .map(|rom| (rom.name.as_str(), rom.size as u64))
            .collect();
        let bin_paths = chdman::extract_chd_to_multiple_tracks(
            progress_bar,
            romfile_path,
            &tmp_directory.path(),
            &names_sizes,
            true,
        )
        .await?;
        let mut hashes: Vec<String> = Vec::new();
        for (i, bin_path) in bin_paths.iter().enumerate() {
            let (_, hash) = get_size_and_hash(
                connection,
                progress_bar,
                &bin_path,
                header,
                i,
                bin_paths.len(),
                hash_algorithm,
            )
            .await?;
            hashes.push(hash);
            remove_file(progress_bar, &bin_path, true).await?;
        }

        if roms
            .iter()
            .enumerate()
            .any(|(i, rom)| &hashes[i] != rom.crc.as_ref().unwrap())
        {
            progress_bar.println("CRC mismatch");
            move_to_trash(connection, progress_bar, system, romfile_path).await?;
            return Ok(());
        }

        let new_cue_path = system_directory.as_ref().join(&cue_rom.name);
        let mut new_chd_path = new_cue_path.clone();
        new_chd_path.set_extension(CHD_EXTENSION);

        // move cue and chd if needed
        rename_file(progress_bar, &cue_path, &new_cue_path, false).await?;
        rename_file(progress_bar, romfile_path, &new_chd_path, false).await?;

        // persist in database
        create_or_update_romfile(connection, &new_cue_path, &[cue_rom]).await;
        create_or_update_romfile(connection, &new_chd_path, &roms).await;

        Ok(())
    } else {
        progress_bar.println("CUE file not found, using single track mode");
        let bin_path =
            chdman::extract_chd_to_single_track(progress_bar, romfile_path, &tmp_directory.path())
                .await?;
        let (size, hash) = get_size_and_hash(
            connection,
            progress_bar,
            &bin_path,
            header,
            1,
            1,
            hash_algorithm,
        )
        .await?;
        remove_file(progress_bar, &bin_path, true).await?;
        let rom = match find_rom_by_hash(
            connection,
            progress_bar,
            size,
            &hash,
            system,
            hash_algorithm,
        )
        .await?
        {
            Some(rom) => rom,
            None => {
                move_to_trash(connection, progress_bar, system, romfile_path).await?;
                return Ok(());
            }
        };

        let mut new_chd_path = system_directory.as_ref().join(&rom.name);
        new_chd_path.set_extension(CHD_EXTENSION);

        // move CHD if needed
        rename_file(progress_bar, romfile_path, &new_chd_path, false).await?;

        // persist in database
        create_or_update_romfile(connection, &new_chd_path, &[rom]).await;

        Ok(())
    }
}

#[cfg(feature = "cso")]
async fn import_cso<P: AsRef<Path>, Q: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system_directory: &Q,
    system: &System,
    header: &Option<Header>,
    romfile_path: &P,
    hash_algorithm: &HashAlgorithm,
) -> SimpleResult<()> {
    let tmp_directory = create_tmp_directory(connection).await?;
    let iso_path = maxcso::extract_cso(progress_bar, romfile_path, &tmp_directory.path())?;
    let (size, hash) = get_size_and_hash(
        connection,
        progress_bar,
        &iso_path,
        header,
        1,
        1,
        hash_algorithm,
    )
    .await?;
    remove_file(progress_bar, &iso_path, true).await?;
    let rom = match find_rom_by_hash(
        connection,
        progress_bar,
        size,
        &hash,
        system,
        hash_algorithm,
    )
    .await?
    {
        Some(rom) => rom,
        None => {
            move_to_trash(connection, progress_bar, system, romfile_path).await?;
            return Ok(());
        }
    };

    let mut new_cso_path = system_directory.as_ref().join(&rom.name);
    new_cso_path.set_extension(CSO_EXTENSION);

    // move CSO if needed
    rename_file(progress_bar, romfile_path, &new_cso_path, false).await?;

    // persist in database
    create_or_update_romfile(connection, &new_cso_path, &[rom]).await;

    Ok(())
}

#[cfg(feature = "rvz")]
async fn import_rvz<P: AsRef<Path>, Q: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system_directory: &Q,
    system: &System,
    header: &Option<Header>,
    romfile_path: &P,
    hash_algorithm: &HashAlgorithm,
) -> SimpleResult<()> {
    let tmp_directory = create_tmp_directory(connection).await?;
    let iso_path = dolphin::extract_rvz(progress_bar, romfile_path, &tmp_directory.path())?;
    let (size, hash) = get_size_and_hash(
        connection,
        progress_bar,
        &iso_path,
        header,
        1,
        1,
        hash_algorithm,
    )
    .await?;
    remove_file(progress_bar, &iso_path, true).await?;
    let rom = match find_rom_by_hash(
        connection,
        progress_bar,
        size,
        &hash,
        system,
        hash_algorithm,
    )
    .await?
    {
        Some(rom) => rom,
        None => {
            move_to_trash(connection, progress_bar, system, romfile_path).await?;
            return Ok(());
        }
    };

    let mut new_rvz_path = system_directory.as_ref().join(&rom.name);
    new_rvz_path.set_extension(RVZ_EXTENSION);

    // move RVZ if needed
    rename_file(progress_bar, romfile_path, &new_rvz_path, false).await?;

    // persist in database
    create_or_update_romfile(connection, &new_rvz_path, &[rom]).await;

    Ok(())
}

async fn import_other<P: AsRef<Path>, Q: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system_directory: &Q,
    system: &System,
    header: &Option<Header>,
    romfile_path: &P,
    romfile_extension: &str,
    hash_algorithm: &HashAlgorithm,
) -> SimpleResult<()> {
    let (size, hash) = get_size_and_hash(
        connection,
        progress_bar,
        romfile_path,
        header,
        1,
        1,
        hash_algorithm,
    )
    .await?;
    let rom = match find_rom_by_hash(
        connection,
        progress_bar,
        size,
        &hash,
        system,
        hash_algorithm,
    )
    .await?
    {
        Some(rom) => rom,
        None => {
            move_to_trash(connection, progress_bar, system, romfile_path).await?;
            return Ok(());
        }
    };
    let game = find_game_by_id(connection, rom.game_id).await;

    let new_path;
    // put arcade roms and JB folders in subdirectories
    if system.arcade || game.jbfolder {
        let game = find_game_by_id(connection, rom.game_id).await;
        new_path = system_directory.as_ref().join(game.name).join(&rom.name)
    // use game name for PS3 updates and DLCs because rom name is usually gibberish
    } else if PS3_EXTENSIONS.contains(&romfile_extension) {
        new_path = system_directory
            .as_ref()
            .join(format!("{}.{}", &game.name, romfile_extension));
    } else {
        new_path = system_directory.as_ref().join(&rom.name);
    }

    // move file if needed
    rename_file(progress_bar, romfile_path, &new_path, false).await?;

    // persist in database
    create_or_update_romfile(connection, &new_path, &[rom]).await;

    Ok(())
}

async fn find_rom_by_hash(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    size: u64,
    hash: &str,
    system: &System,
    hash_algorithm: &HashAlgorithm,
) -> SimpleResult<Option<Rom>> {
    let rom: Option<Rom>;
    let mut roms = match hash_algorithm {
        HashAlgorithm::Crc => {
            find_roms_without_romfile_by_size_and_crc_and_system_id(
                connection, size, hash, system.id,
            )
            .await
        }
        HashAlgorithm::Md5 => {
            find_roms_without_romfile_by_size_and_md5_and_system_id(
                connection, size, hash, system.id,
            )
            .await
        }
        HashAlgorithm::Sha1 => {
            find_roms_without_romfile_by_size_and_sha1_and_system_id(
                connection, size, hash, system.id,
            )
            .await
        }
    };

    // abort if no match
    if roms.is_empty() {
        let rom_count = match hash_algorithm {
            HashAlgorithm::Crc => {
                count_roms_with_romfile_by_size_and_crc_and_system_id(
                    connection, size, hash, system.id,
                )
                .await
            }
            HashAlgorithm::Md5 => {
                count_roms_with_romfile_by_size_and_md5_and_system_id(
                    connection, size, hash, system.id,
                )
                .await
            }
            HashAlgorithm::Sha1 => {
                count_roms_with_romfile_by_size_and_sha1_and_system_id(
                    connection, size, hash, system.id,
                )
                .await
            }
        };
        if rom_count > 0 {
            progress_bar.println("Already imported");
        } else {
            progress_bar.println("No match");
        }
        return Ok(None);
    }

    // let user choose the rom if there are multiple matches
    if roms.len() == 1 {
        rom = Some(roms.remove(0));
        progress_bar.println(format!("Matches \"{}\"", rom.as_ref().unwrap().name));
    } else {
        let mut roms_games: Vec<(Rom, Game)> = vec![];
        for rom in roms {
            let game = find_game_by_id(connection, rom.game_id).await;
            roms_games.push((rom, game));
        }
        rom = prompt_for_rom_game(&mut roms_games)?;
    }

    // abort if rom already has a file
    if rom.is_some() && rom.as_ref().unwrap().romfile_id.is_some() {
        let romfile =
            find_romfile_by_id(connection, rom.as_ref().unwrap().romfile_id.unwrap()).await;
        progress_bar.println(format!("Duplicate of \"{}\"", romfile.path));
        return Ok(None);
    }

    Ok(rom)
}

#[cfg(feature = "ird")]
async fn find_sfb_rom_by_md5(
    connection: &mut SqliteConnection,
    size: u64,
    md5: &str,
    system: &System,
    progress_bar: &ProgressBar,
) -> SimpleResult<Option<Rom>> {
    let rom: Option<Rom>;
    let mut roms = find_roms_without_romfile_by_name_and_size_and_md5_and_system_id(
        connection,
        PS3_DISC_SFB,
        size,
        md5,
        system.id,
    )
    .await;

    // abort if no match
    if roms.is_empty() {
        if count_roms_with_romfile_by_name_and_size_and_md5_and_system_id(
            connection,
            PS3_DISC_SFB,
            size,
            md5,
            system.id,
        )
        .await
            > 0
        {
            progress_bar.println("Already imported");
        } else {
            progress_bar.println("No match");
        }
        return Ok(None);
    }

    // let user choose the rom if there are multiple matches
    if roms.len() == 1 {
        rom = Some(roms.remove(0));
        progress_bar.println(format!("Matches \"{}\"", rom.as_ref().unwrap().name));
    } else {
        let mut roms_games: Vec<(Rom, Game)> = vec![];
        for rom in roms {
            let game = find_game_by_id(connection, rom.game_id).await;
            roms_games.push((rom, game));
        }
        rom = prompt_for_rom_game(&mut roms_games)?;
    }

    // abort if rom already has a file
    if rom.is_some() && rom.as_ref().unwrap().romfile_id.is_some() {
        let romfile =
            find_romfile_by_id(connection, rom.as_ref().unwrap().romfile_id.unwrap()).await;
        progress_bar.println(format!("Duplicate of \"{}\"", romfile.path));
        return Ok(None);
    }

    Ok(rom)
}

async fn create_or_update_romfile<P: AsRef<Path>>(
    connection: &mut SqliteConnection,
    romfile_path: &P,
    roms: &[Rom],
) {
    let romfile = find_romfile_by_path(
        connection,
        romfile_path.as_ref().as_os_str().to_str().unwrap(),
    )
    .await;
    let romfile_id = match romfile {
        Some(romfile) => {
            update_romfile(
                connection,
                romfile.id,
                &romfile.path,
                romfile_path.as_ref().metadata().await.unwrap().len(),
            )
            .await;
            romfile.id
        }
        None => {
            create_romfile(
                connection,
                romfile_path.as_ref().as_os_str().to_str().unwrap(),
                romfile_path.as_ref().metadata().await.unwrap().len(),
            )
            .await
        }
    };
    for rom in roms {
        update_rom_romfile(connection, rom.id, Some(romfile_id)).await;
    }
}

async fn move_to_trash<P: AsRef<Path>>(
    connection: &mut SqliteConnection,
    progress_bar: &ProgressBar,
    system: &System,
    romfile_path: &P,
) -> SimpleResult<()> {
    let new_path = get_trash_directory(connection, progress_bar, system)
        .await?
        .join(romfile_path.as_ref().file_name().unwrap());
    rename_file(progress_bar, romfile_path, &new_path, false).await?;
    match find_romfile_by_path(connection, new_path.as_os_str().to_str().unwrap()).await {
        Some(romfile) => {
            update_romfile(
                connection,
                romfile.id,
                new_path.as_os_str().to_str().unwrap(),
                new_path.metadata().await.unwrap().len(),
            )
            .await;
        }
        None => {
            create_romfile(
                connection,
                new_path.as_os_str().to_str().unwrap(),
                new_path.metadata().await.unwrap().len(),
            )
            .await;
        }
    }
    Ok(())
}

#[cfg(all(test, feature = "chd"))]
mod test_chd_multiple_tracks;
#[cfg(all(test, feature = "chd"))]
mod test_chd_multiple_tracks_without_cue_should_fail;
#[cfg(all(test, feature = "chd"))]
mod test_chd_single_track;
#[cfg(all(test, feature = "cso"))]
mod test_cso;
#[cfg(test)]
mod test_original;
#[cfg(test)]
mod test_original_headered;
#[cfg(test)]
mod test_sevenzip_multiple_files_full_game;
#[cfg(test)]
mod test_sevenzip_multiple_files_headered_mixed_games;
#[cfg(test)]
mod test_sevenzip_multiple_files_mixed_games;
#[cfg(test)]
mod test_sevenzip_multiple_files_partial_game;
#[cfg(test)]
mod test_sevenzip_single_file;
#[cfg(test)]
mod test_sevenzip_single_file_headered;
#[cfg(test)]
mod test_zip_single_file;