ensure prefix.

This commit is contained in:
Christopher Jeffrey 2016-02-24 00:07:01 -08:00
parent 26969d823a
commit 6a644be03f
5 changed files with 35 additions and 19 deletions

View File

@ -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');

View File

@ -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;

View File

@ -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);

View File

@ -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);

View File

@ -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) {