Search in sources :

Example 1 with RAIFile

use of net.metanotion.io.RAIFile in project i2p.i2p by i2p.

the class BlockfileNamingService method initNew.

/**
 *  Create a new database and initialize it from the local files
 *  privatehosts.txt, userhosts.txt, and hosts.txt,
 *  creating a skiplist in the database for each.
 */
private BlockFile initNew(RAIFile f) throws IOException {
    long start = _context.clock().now();
    _version = VERSION;
    _destSerializer = _destSerializerV4;
    _isVersion4 = true;
    try {
        BlockFile rv = new BlockFile(f, true);
        SkipList<String, Properties> hdr = rv.makeIndex(INFO_SKIPLIST, _stringSerializer, _infoSerializer);
        Properties info = new Properties();
        info.setProperty(PROP_VERSION, VERSION);
        info.setProperty(PROP_CREATED, Long.toString(_context.clock().now()));
        String list = _context.getProperty(HostsTxtNamingService.PROP_HOSTS_FILE, HostsTxtNamingService.DEFAULT_HOSTS_FILE);
        info.setProperty(PROP_LISTS, list);
        hdr.put(PROP_INFO, info);
        rv.makeIndex(REVERSE_SKIPLIST, _hashIndexSerializer, _infoSerializer);
        int total = 0;
        for (String hostsfile : getFilenames(list)) {
            _lists.add(hostsfile);
            File file = new File(_context.getRouterDir(), hostsfile);
            if ((!file.exists()) || !(file.canRead()))
                continue;
            int count = 0;
            BufferedReader in = null;
            String sourceMsg = "Imported from " + hostsfile + " file";
            try {
                in = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"), 16 * 1024);
                String line = null;
                while ((line = in.readLine()) != null) {
                    if (line.startsWith("#"))
                        continue;
                    int split = line.indexOf('=');
                    if (split <= 0)
                        continue;
                    String key = line.substring(0, split).toLowerCase(Locale.US);
                    if (line.indexOf('#') > 0) {
                        // trim off any end of line comment
                        line = line.substring(0, line.indexOf('#')).trim();
                        if (line.length() < split + 1)
                            continue;
                    }
                    String b64 = line.substring(split + 1).trim();
                    Destination d = lookupBase64(b64);
                    if (d != null) {
                        addEntry(rv, hostsfile, key, d, sourceMsg);
                        addReverseEntry(rv, key, d, _log);
                        count++;
                    } else {
                        _log.logAlways(Log.WARN, "Unable to import entry for " + key + " from file " + file + " - bad Base 64: " + b64);
                    }
                }
            } catch (IOException ioe) {
                _log.error("Failed to read hosts from " + file, ioe);
            } finally {
                if (in != null)
                    try {
                        in.close();
                    } catch (IOException ioe) {
                    }
            }
            total += count;
            _log.logAlways(Log.INFO, "Migrating " + count + " hosts from " + file + " to new hosts database");
        }
        if (_log.shouldLog(Log.INFO))
            _log.info("DB init took " + DataHelper.formatDuration(_context.clock().now() - start));
        if (total <= 0)
            _log.logAlways(Log.WARN, "No hosts.txt files found, Initialized hosts database with zero entries");
        return rv;
    } catch (RuntimeException e) {
        _log.error("Failed to initialize database", e);
        throw new IOException(e.toString());
    }
}
Also used : Destination(net.i2p.data.Destination) InputStreamReader(java.io.InputStreamReader) BlockFile(net.metanotion.io.block.BlockFile) IOException(java.io.IOException) Properties(java.util.Properties) FileInputStream(java.io.FileInputStream) BufferedReader(java.io.BufferedReader) RAIFile(net.metanotion.io.RAIFile) BlockFile(net.metanotion.io.block.BlockFile) File(java.io.File)

Example 2 with RAIFile

use of net.metanotion.io.RAIFile in project i2p.i2p by i2p.

the class BlockFile method main.

/**
 *  Run an integrity check on the blockfile and all the skiplists in it.
 *
 *  WARNING:
 *  This only works on skiplists using UTF8StringBytes as a key
 *  serializer, unless the exception has been coded in bfck below.
 *  Will CORRUPT other skiplists.
 */
public static void main(String[] args) {
    if (args.length != 1) {
        System.err.println("Usage: BlockFile file");
        return;
    }
    boolean init = !(new File(args[0])).exists();
    RAIFile raif = null;
    BlockFile bf = null;
    try {
        raif = new RAIFile(new File(args[0]), true, true);
        bf = new BlockFile(raif, init);
        bf.bfck(true);
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (bf != null)
            try {
                bf.close();
            } catch (IOException ioe) {
            }
        if (raif != null)
            try {
                raif.close();
            } catch (IOException ioe) {
            }
    }
}
Also used : RAIFile(net.metanotion.io.RAIFile) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) RAIFile(net.metanotion.io.RAIFile)

Aggregations

File (java.io.File)2 IOException (java.io.IOException)2 RAIFile (net.metanotion.io.RAIFile)2 BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 InputStreamReader (java.io.InputStreamReader)1 RandomAccessFile (java.io.RandomAccessFile)1 Properties (java.util.Properties)1 Destination (net.i2p.data.Destination)1 BlockFile (net.metanotion.io.block.BlockFile)1