~jd-team/jdownloader/appwork-utils

« back to all changes in this revision

Viewing changes to src/org/appwork/utils/Files.java

  • Committer: daniel
  • Date: 2021-03-02 14:36:58 UTC
  • Revision ID: svn-v4:21714237-3853-44ef-a1f0-ef8f03a7d1fe::3482
Files:
-added FileSpaceMethodHelper enum that supports workaround for subst on windows
-updated guessRoot to use FileSpaceMethodHelper.TOTAL

Show diffs side-by-side

added added

removed removed

Lines of Context:
461
461
        }
462
462
    }
463
463
 
464
 
    private static File guessRootByUsableSpace(final File file) {
 
464
    public static enum FileSpaceMethodHelper {
 
465
        FREE {
 
466
            // WARNING: File.getFreeSpace also include reserved blocks (eg Linux FileSystems)! Use File.getUsableSpace
 
467
            @Override
 
468
            public long get(File probeFile) {
 
469
                long space = probeFile.getFreeSpace();
 
470
                if (space == 0) {
 
471
                    final File workaroundFile = getWindowsSubstWorkaround(probeFile);
 
472
                    if (workaroundFile != null) {
 
473
                        space = workaroundFile.getFreeSpace();
 
474
                    }
 
475
                }
 
476
                return space;
 
477
            }
 
478
        },
 
479
        TOTAL {
 
480
 
 
481
            @Override
 
482
            public long get(File probeFile) {
 
483
                long space = probeFile.getTotalSpace();
 
484
                if (space == 0) {
 
485
                    final File workaroundFile = getWindowsSubstWorkaround(probeFile);
 
486
                    if (workaroundFile != null) {
 
487
                        space = workaroundFile.getTotalSpace();
 
488
                    }
 
489
                }
 
490
                return space;
 
491
            }
 
492
 
 
493
        },
 
494
        USABLE {
 
495
 
 
496
            @Override
 
497
            public long get(File probeFile) {
 
498
                long space = probeFile.getUsableSpace();
 
499
                if (space == 0) {
 
500
                    final File workaroundFile = getWindowsSubstWorkaround(probeFile);
 
501
                    if (workaroundFile != null) {
 
502
                        space = workaroundFile.getUsableSpace();
 
503
                    }
 
504
                }
 
505
                return space;
 
506
            }
 
507
 
 
508
        };
 
509
 
 
510
        private static final File getWindowsSubstWorkaround(File probeFile) {
 
511
            if (CrossSystem.isWindows() && (probeFile.getParent() == null || probeFile.isFile())) {
 
512
                // workaround for subst drive
 
513
                final File[] listFiles;
 
514
                if (probeFile.isDirectory()) {
 
515
                    listFiles = probeFile.listFiles();
 
516
                } else {
 
517
                    final File parentFile = probeFile.getParentFile();
 
518
                    listFiles = parentFile != null ? parentFile.listFiles() : null;
 
519
                }
 
520
                if (listFiles != null) {
 
521
                    for (final File listFile : listFiles) {
 
522
                        if (listFile.isDirectory()) {
 
523
                            return listFile;
 
524
                        }
 
525
                    }
 
526
                }
 
527
            }
 
528
            return null;
 
529
        }
 
530
 
 
531
        public abstract long get(File file);
 
532
    }
 
533
 
 
534
    private static File guessRootBy(final File file, final FileSpaceMethodHelper method) {
465
535
        final List<File> bestGuessCandidates = new ArrayList<File>();
466
536
        final int maxRetry = 100;
467
537
        final int maxRetryTrustThreshold = 30;
468
538
        int differentCandidates = 0;
469
539
        for (int retry = 1; retry <= maxRetry; retry++) {
470
 
            long lastUsableSpace = -1;
 
540
            long lastSpace = -1;
471
541
            File lastFile = null;
472
542
            File currentFile = file;
473
543
            while (currentFile != null) {
474
 
                final long usableSpace = getFileUsableSpace(currentFile);
475
 
                if (usableSpace > 0) {
476
 
                    if (lastFile == null || lastUsableSpace == usableSpace) {
 
544
                final long space = method.get(currentFile);
 
545
                if (space > 0) {
 
546
                    if (lastFile == null || lastSpace == space) {
477
547
                        lastFile = currentFile;
478
 
                        lastUsableSpace = usableSpace;
 
548
                        lastSpace = space;
479
549
                    } else {
480
550
                        break;
481
551
                    }
548
618
        if (bestRootMatch == null) {
549
619
            final String destination = file.getAbsolutePath();
550
620
            if (!destination.startsWith("\\")) {
551
 
                final File bestByUsableSpace = guessRootByUsableSpace(file);
 
621
                final File bestByUsableSpace = guessRootBy(file, FileSpaceMethodHelper.TOTAL);
552
622
                if (bestByUsableSpace != null) {
553
623
                    return bestByUsableSpace;
554
624
                }
583
653
        return bestRootMatch == null ? null : new File(bestRootMatch);
584
654
    }
585
655
 
586
 
    private static long getFileUsableSpace(final File probeFile) {
587
 
        long usableSpace = probeFile.getUsableSpace();
588
 
        if (usableSpace == 0 && CrossSystem.isWindows() && (probeFile.getParent() == null || probeFile.isFile())) {
589
 
            // workaround for subst drive
590
 
            final File[] listFiles;
591
 
            if (probeFile.isDirectory()) {
592
 
                listFiles = probeFile.listFiles();
593
 
            } else {
594
 
                final File parentFile = probeFile.getParentFile();
595
 
                listFiles = parentFile != null ? parentFile.listFiles() : null;
596
 
            }
597
 
            if (listFiles != null) {
598
 
                for (final File listFile : listFiles) {
599
 
                    if (listFile.isDirectory()) {
600
 
                        usableSpace = listFile.getUsableSpace();
601
 
                        break;
602
 
                    }
603
 
                }
604
 
            }
605
 
        }
606
 
        return usableSpace;
607
 
    }
608
 
 
609
656
    public static long getUsableSpace(final File file) {
610
657
        final File root = guessRoot(file);
611
 
        // WARNING: File.getFreeSpace also include reserved blocks (eg Linux FileSystems)! Use File.getUsableSpace
612
658
        final File probeFile;
613
659
        if (root != null) {
614
660
            probeFile = root;
615
661
        } else {
616
662
            probeFile = file;
617
663
        }
618
 
        return getFileUsableSpace(probeFile);
 
664
        return FileSpaceMethodHelper.USABLE.get(probeFile);
619
665
    }
620
666
 
621
667
    /**