Name: js-handler/node_modules/restify/node_modules/bunyan/node_modules/mv/index.js 
1:
var fs;
2:
 
3:
fs = require('fs');
4:
 
5:
module.exports = function mv(source, dest, cb){
6:
  fs.rename(source, dest, function(err){
7:
    if (!err) return cb();
8:
    if (err.code !== 'EXDEV') return cb(err);
9:
    fs.stat(source, function (err, stats) {
10:
      if (err) return cb(err);
11:
      if (stats.isFile()) {
12:
        moveFileAcrossDevice(source, dest, cb);
13:
      } else if (stats.isDirectory()) {
14:
        moveDirAcrossDevice(source, dest, cb);
15:
      } else {
16:
        var err;
17:
        err = new Error("source must be file or directory");
18:
        err.code = 'NOTFILEORDIR';
19:
        cb(err);
20:
      }
21:
    });
22:
  });
23:
}
24:
 
25:
function moveFileAcrossDevice(source, dest, cb) {
26:
  var ins, outs;
27:
  ins = fs.createReadStream(source);
28:
  outs = fs.createWriteStream(dest);
29:
  ins.once('error', function(err){
30:
    outs.removeAllListeners('error');
31:
    outs.removeAllListeners('close');
32:
    outs.destroy();
33:
    cb(err);
34:
  });
35:
  outs.once('error', function(err){
36:
    ins.removeAllListeners('error');
37:
    outs.removeAllListeners('close');
38:
    ins.destroy();
39:
    cb(err);
40:
  });
41:
  outs.once('close', function(){
42:
    fs.unlink(source, cb);
43:
  });
44:
  ins.pipe(outs);
45:
}
46:
 
47:
// TODO: do this natively instead of shelling out to `mv`
48:
function moveDirAcrossDevice(source, dest, cb) {
49:
  var child, stdout, stderr, err;
50:
  child = require('child_process').spawn('mv', [source, dest], {stdio: 'pipe'});
51:
  child.stderr.setEncoding('utf8');
52:
  child.stdout.setEncoding('utf8');
53:
  stderr = '';
54:
  stdout = '';
55:
  child.stderr.on('data', function(data) { stderr += data; });
56:
  child.stdout.on('data', function(data) { stdout += data; });
57:
  child.on('close', function(code) {
58:
    if (code === 0) {
59:
      cb();
60:
    } else {
61:
      err = new Error("mv had nonzero exit code");
62:
      err.code = 'RETCODE';
63:
      err.stdout = stdout;
64:
      err.stderr = stderr;
65:
      cb(err);
66:
    }
67:
  });
68:
}