Search in sources :

Example 1 with SHA1Hash

use of net.i2p.crypto.SHA1Hash in project i2p.i2p by i2p.

the class SnarkManager method cleanupTorrentStatus.

/**
 *  Remove all orphaned torrent status files, which weren't removed
 *  before 0.9.20, and could be left around after a manual delete also.
 *  Run this once at startup.
 *  @since 0.9.20
 */
private void cleanupTorrentStatus() {
    Set<SHA1Hash> torrents = new HashSet<SHA1Hash>(32);
    int found = 0;
    int totalDeleted = 0;
    synchronized (_snarks) {
        for (Snark snark : _snarks.values()) {
            torrents.add(new SHA1Hash(snark.getInfoHash()));
        }
        synchronized (_configLock) {
            for (int i = 0; i < B64.length(); i++) {
                File subdir = new File(_configDir, SUBDIR_PREFIX + B64.charAt(i));
                File[] configs = subdir.listFiles();
                if (configs == null)
                    continue;
                int deleted = 0;
                for (int j = 0; j < configs.length; j++) {
                    File config = configs[j];
                    SHA1Hash ih = configFileToInfoHash(config);
                    if (ih == null)
                        continue;
                    found++;
                    if (torrents.contains(ih)) {
                        if (_log.shouldInfo())
                            _log.info("Torrent for " + config + " exists");
                    } else {
                        boolean ok = config.delete();
                        if (ok) {
                            if (_log.shouldInfo())
                                _log.info("Deleted " + config + " for " + ih);
                            deleted++;
                        } else {
                            if (_log.shouldWarn())
                                _log.warn("Failed to delete " + config + " for " + ih);
                        }
                    }
                }
                if (deleted == configs.length) {
                    if (_log.shouldInfo())
                        _log.info("Deleting " + subdir);
                    subdir.delete();
                }
                totalDeleted += deleted;
            }
        }
    }
    if (_log.shouldInfo())
        _log.info("Cleanup found " + torrents.size() + " torrents and " + found + " configs, deleted " + totalDeleted + " old configs");
}
Also used : SHA1Hash(net.i2p.crypto.SHA1Hash) File(java.io.File) ConcurrentHashSet(net.i2p.util.ConcurrentHashSet) HashSet(java.util.HashSet)

Aggregations

File (java.io.File)1 HashSet (java.util.HashSet)1 SHA1Hash (net.i2p.crypto.SHA1Hash)1 ConcurrentHashSet (net.i2p.util.ConcurrentHashSet)1