diff --git a/lib/bcoin.js b/lib/bcoin.js index 6aa91fee..dc8ad7ac 100644 --- a/lib/bcoin.js +++ b/lib/bcoin.js @@ -11,9 +11,26 @@ bcoin.isBrowser = (typeof process !== 'undefined' && process.browser) || typeof window !== 'undefined'; +bcoin.prefix = process.env.BCOIN_PREFIX || process.env.HOME + '/.bcoin'; bcoin.debug = +process.env.BCOIN_DEBUG === 1; bcoin.debugFile = +process.env.BCOIN_DEBUGFILE !== 0; +bcoin.ensurePrefix = function ensurePrefix() { + if (!bcoin.fs) + return; + + if (bcoin._ensured) + return; + + bcoin._ensured = true; + + try { + bcoin.fs.statSync(bcoin.prefix); + } catch (e) { + bcoin.fs.mkdirSync(bcoin.prefix, 0750); + } +}; + bcoin.bn = require('bn.js'); bcoin.elliptic = require('elliptic'); @@ -32,16 +49,6 @@ if (!bcoin.isBrowser) { bcoin.hash = require('hash.js'); } -bcoin.dir = process.env.HOME + '/.bcoin'; - -if (bcoin.fs) { - try { - bcoin.fs.statSync(bcoin.dir, 0o750); - } catch (e) { - bcoin.fs.mkdirSync(bcoin.dir); - } -} - bcoin.ecdsa = bcoin.elliptic.ec('secp256k1'); assert(!bcoin.ecdsa.signature); bcoin.ecdsa.signature = require('elliptic/lib/elliptic/ec/signature'); diff --git a/lib/bcoin/blockdb.js b/lib/bcoin/blockdb.js index 471d348c..5154dd38 100644 --- a/lib/bcoin/blockdb.js +++ b/lib/bcoin/blockdb.js @@ -32,8 +32,10 @@ function BlockDB(options) { this.file = options.indexFile; + bcoin.ensurePrefix(); + if (!this.file) - this.file = bcoin.dir + '/index-' + network.type + '.db'; + this.file = bcoin.prefix + '/index-' + network.type + '.db'; this.options = options; @@ -1078,8 +1080,10 @@ function BlockData(options) { this.options = options; this.file = options.blockFile; + bcoin.ensurePrefix(); + if (!this.file) - this.file = bcoin.dir + '/block-' + network.type + '.db'; + this.file = bcoin.prefix + '/block-' + network.type + '.db'; this.bufferPool = { used: {} }; this.size = 0; diff --git a/lib/bcoin/chaindb.js b/lib/bcoin/chaindb.js index b93034f8..15b4c9cc 100644 --- a/lib/bcoin/chaindb.js +++ b/lib/bcoin/chaindb.js @@ -34,7 +34,7 @@ function ChainDB(chain, options) { this.file = options.file; if (!this.file) - this.file = bcoin.dir + '/chain-' + network.type + '.db'; + this.file = bcoin.prefix + '/chain-' + network.type + '.db'; this.heightLookup = {}; this.queue = {}; @@ -81,6 +81,8 @@ ChainDB.prototype._init = function _init() { return; } + bcoin.ensurePrefix(); + if (+process.env.BCOIN_FRESH === 1) { try { fs.unlinkSync(this.file); diff --git a/lib/bcoin/utils.js b/lib/bcoin/utils.js index 3631423c..912a7aa0 100644 --- a/lib/bcoin/utils.js +++ b/lib/bcoin/utils.js @@ -766,8 +766,9 @@ utils.debug = function debug() { if (bcoin.debugFile && bcoin.fs) { if (!bcoin._debug) { + bcoin.ensurePrefix(); bcoin._debug = bcoin.fs.createWriteStream( - bcoin.dir + '/debug.log', { flags: 'a' }); + bcoin.prefix + '/debug.log', { flags: 'a' }); } msg = utils.format(args, false); bcoin._debug.write(process.pid + ': ' + msg); diff --git a/lib/bcoin/walletdb.js b/lib/bcoin/walletdb.js index 424625c2..c522ecff 100644 --- a/lib/bcoin/walletdb.js +++ b/lib/bcoin/walletdb.js @@ -37,11 +37,13 @@ function WalletDB(options) { this.dir = options.dir; this.type = options.type; + bcoin.ensurePrefix(); + if (!this.file) - this.file = bcoin.dir + '/wallet-' + network.type + '.db'; + this.file = bcoin.prefix + '/wallet-' + network.type + '.db'; if (!this.dir) - this.dir = bcoin.dir + '/wallet-' + network.type; + this.dir = bcoin.prefix + '/wallet-' + network.type; if (!this.type) this.type = 'leveldb'; @@ -66,9 +68,9 @@ WalletDB.prototype._init = function _init() { if (this.type === 'file') { if (bcoin.fs) { try { - bcoin.fs.statSync(this.dir, 0o750); + bcoin.fs.statSync(this.dir); } catch (e) { - bcoin.fs.mkdirSync(this.dir); + bcoin.fs.mkdirSync(this.dir, 0750); } } if (+process.env.BCOIN_FRESH === 1) { @@ -160,7 +162,7 @@ WalletDB.prototype.saveFile = function saveFile(id, json, callback) { options = { encoding: 'utf8', - mode: 0o600 + mode: 0600 }; fs.writeFile(file, json, options, function(err) {