diff --git a/lib/bcoin/env.js b/lib/bcoin/env.js index 8e4573ca..d0bea69b 100644 --- a/lib/bcoin/env.js +++ b/lib/bcoin/env.js @@ -208,14 +208,14 @@ Environment.prototype.set = function set(options) { options = utils.merge({}, options); - options.prefix = options.prefix - || process.env.BCOIN_PREFIX - || utils.HOME + '/.bcoin'; - options.network = options.network || process.env.BCOIN_NETWORK || 'main'; + options.prefix = options.prefix + || process.env.BCOIN_PREFIX + || utils.HOME + '/.bcoin/' + options.network; + if (!options.db) options.db = process.env.BCOIN_DB; @@ -244,7 +244,7 @@ Environment.prototype.set = function set(options) { options.workerTimeout = +process.env.BCOIN_WORKER_TIMEOUT; if (options.debugFile && typeof options.debugFile !== 'string') - options.debugFile = options.prefix + '/debug-' + options.network + '.log'; + options.debugFile = options.prefix + '/debug.log'; this.prefix = options.prefix; this.networkType = options.network; @@ -284,14 +284,7 @@ Environment.prototype.ensurePrefix = function ensurePrefix() { this._ensured = true; - try { - fs.statSync(this.prefix); - } catch (e) { - if (e.code === 'ENOENT') - fs.mkdirSync(this.prefix, 488 /* 0750 */); - else - throw e; - } + mkdirp(this.prefix); }; /** @@ -376,7 +369,7 @@ Environment.prototype.write = function write(msg) { this._debug = fs.createWriteStream(this.debugFile, { flags: 'a' }); } - this._debug.write(process.pid + ': ' + msg + '\n'); + this._debug.write(process.pid + ' (' + utils.date() + '): ' + msg + '\n'); }; /** @@ -388,6 +381,47 @@ Environment.prototype.now = function now() { return this.time.now(); }; +/** + * Create a full directory structure. + * @param {String} dir + */ + +function mkdirp(dir) { + var path = require('path'); + var i, parts, name; + + if (!fs) + return; + + dir = path.normalize(dir); + dir = dir.replace(/\\/g, '/'); + dir = dir.replace(/\/$/, ''); + parts = dir.split('/'); + + for (i = 0; i < parts.length; i++) { + if (i === 0) { + if (process.platform === 'win32') { + if (parts[0].indexOf(':') !== -1) + continue; + } else { + if (parts[0].length === 0) + continue; + } + } + + name = parts.slice(0, i + 1).join('/'); + + try { + fs.statSync(name); + } catch (e) { + if (e.code === 'ENOENT') + fs.mkdirSync(name, 488 /* 0750 */); + else + throw e; + } + } +} + /* * Expose by converting `exports` to an * Environment. diff --git a/lib/bcoin/ldb.js b/lib/bcoin/ldb.js index acdac7ef..44600bc4 100644 --- a/lib/bcoin/ldb.js +++ b/lib/bcoin/ldb.js @@ -117,13 +117,7 @@ ldb.getLocation = function getLocation(options) { if (options.location) return options.location; - return bcoin.prefix - + '/' - + options.name - + '-' - + bcoin.network.get(options.network).type - + '.' - + backend.ext; + return bcoin.prefix + '/' + options.name + '.' + backend.ext; }; /**