-
Notifications
You must be signed in to change notification settings - Fork 278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
rpc dumpzone: dump HNS root zone to file #534
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -6,6 +6,10 @@ | |||||||||||||
|
||||||||||||||
'use strict'; | ||||||||||||||
|
||||||||||||||
const constants = require('bns/lib/constants'); | ||||||||||||||
const {types} = constants; | ||||||||||||||
const fs = require('fs'); | ||||||||||||||
const bns = require('bns'); | ||||||||||||||
const assert = require('bsert'); | ||||||||||||||
const bweb = require('bweb'); | ||||||||||||||
const {Lock} = require('bmutex'); | ||||||||||||||
|
@@ -246,6 +250,7 @@ class RPC extends RPCBase { | |||||||||||||
this.add('validateresource', this.validateResource); | ||||||||||||||
|
||||||||||||||
this.add('resetrootcache', this.resetRootCache); | ||||||||||||||
this.add('dumpzone', this.dumpzone); | ||||||||||||||
|
||||||||||||||
// Compat | ||||||||||||||
// this.add('getnameinfo', this.getNameInfo); | ||||||||||||||
|
@@ -2533,6 +2538,68 @@ class RPC extends RPCBase { | |||||||||||||
return null; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
async dumpzone(args, help) { | ||||||||||||||
if (help || args.length !== 1) | ||||||||||||||
throw new RPCError(errs.MISC_ERROR, 'dumpzone <filename>'); | ||||||||||||||
|
||||||||||||||
const valid = new Validator(args); | ||||||||||||||
const filename = valid.str(0, null); | ||||||||||||||
if (filename == null) | ||||||||||||||
throw new RPCError(errs.MISC_ERROR, 'dumpzone <filename>'); | ||||||||||||||
|
||||||||||||||
const tmp = filename + '~'; | ||||||||||||||
this.logger.debug(`dumping root zone to file: ${filename}`); | ||||||||||||||
|
||||||||||||||
let fd = null; | ||||||||||||||
let fileErr = null; | ||||||||||||||
fd = fs.createWriteStream(tmp, { flags: 'a+' }); | ||||||||||||||
fd.on('error', (err) => { | ||||||||||||||
fd.end(); | ||||||||||||||
fd = null; | ||||||||||||||
fileErr = err; | ||||||||||||||
}); | ||||||||||||||
|
||||||||||||||
const tree = this.chain.db.tree; | ||||||||||||||
const iter = tree.iterator(true); | ||||||||||||||
|
||||||||||||||
let count = 0; | ||||||||||||||
while ((await iter.next()) && (fd != null)) { | ||||||||||||||
count++; | ||||||||||||||
if (count % 10000 === 0) | ||||||||||||||
this.logger.debug('dumpzone names processed: %d', count); | ||||||||||||||
|
||||||||||||||
if (fd == null) | ||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
const {value} = iter; | ||||||||||||||
const ns = NameState.decode(value); | ||||||||||||||
|
||||||||||||||
if (ns.data.length <= 0) | ||||||||||||||
continue; | ||||||||||||||
|
||||||||||||||
const fqdn = bns.util.fqdn(ns.name.toString('ascii')); | ||||||||||||||
const resource = Resource.decode(ns.data); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Per our testing this needs to be updated to: let resource; |
||||||||||||||
const zone = resource.toZone(fqdn); | ||||||||||||||
for (const record of zone) { | ||||||||||||||
if (fd == null) | ||||||||||||||
break; | ||||||||||||||
|
||||||||||||||
if (record.type !== types.RRSIG) | ||||||||||||||
fd.write(record.toString() + '\n'); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
if (fd == null) | ||||||||||||||
return fileErr; | ||||||||||||||
|
||||||||||||||
fd.end(); | ||||||||||||||
fs.renameSync(tmp, filename); | ||||||||||||||
|
||||||||||||||
this.logger.debug('root zone dump complete. Total names: %d', count); | ||||||||||||||
|
||||||||||||||
return filename; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
/* | ||||||||||||||
* Helpers | ||||||||||||||
*/ | ||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this
fd
check redundant with the check on 2571?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably can remove one or other, just trying to catch if the
fd.on('error',...
got triggeredJS isn't my strongest skill, so likely all that error handling can all be done better
I think it was to try & catch things like disk-full