From 7b892a97cc1a478813a12d3a24e1768b684a23af Mon Sep 17 00:00:00 2001 From: Albin Mattsson Date: Sun, 22 May 2016 22:50:51 +0200 Subject: [PATCH 1/6] Create target upfront Open target with write and exclusive flag. This closes #65 --- lib/appdmg.js | 12 +++++++++--- lib/hdiutil.js | 1 + lib/pipeline.js | 5 ++++- test/api.js | 2 +- 4 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/appdmg.js b/lib/appdmg.js index b7d98d0..21d24a6 100644 --- a/lib/appdmg.js +++ b/lib/appdmg.js @@ -77,13 +77,19 @@ module.exports = exports = function (options) { **/ pipeline.addStep('Looking for target', function (next) { - fs.exists(global.target, function (exists) { - if (exists) { - next(new Error('Target already exists')) + pipeline.addCleanupStep('unlink-target', 'Removing target image', function (next, hasErrored) { + if (hasErrored) { + fs.unlink(global.target, next) } else { next(null) } }) + + fs.open(global.target, 'wx', function (error) { + if (error) return next(new Error('Target already exists')) + + next(null) + }) }) /** diff --git a/lib/hdiutil.js b/lib/hdiutil.js index c55a065..87163a3 100644 --- a/lib/hdiutil.js +++ b/lib/hdiutil.js @@ -5,6 +5,7 @@ var util = require('./util') exports.convert = function (source, format, target, cb) { var args = [] args.push('convert', source) + args.push('-ov') args.push('-format', format) args.push('-imagekey', 'zlib-level=9') args.push('-o', target) diff --git a/lib/pipeline.js b/lib/pipeline.js index 0040a65..dc43486 100644 --- a/lib/pipeline.js +++ b/lib/pipeline.js @@ -24,6 +24,9 @@ Pipeline.prototype._runStep = function (step, nextAction, cb) { var next = function (err) { if (err) { that._progress({ type: 'step-end', status: 'error' }) + + that.hasErrored = true + that.runRemainingCleanups(function (err2) { if (err2) console.error(err2) cb(err) @@ -67,7 +70,7 @@ Pipeline.prototype.runCleanup = function (id, cb) { delete this.cleanupStore[id] this.cleanupList.splice(idx, 1) - return fn(cb) + return fn(cb, this.hasErrored) } Pipeline.prototype.runRemainingCleanups = function (cb) { diff --git a/test/api.js b/test/api.js index c799807..18e72bd 100644 --- a/test/api.js +++ b/test/api.js @@ -9,7 +9,7 @@ var path = require('path') var temp = require('fs-temp') var assert = require('assert') -var STEPS = 21 +var STEPS = 22 function runAppdmg (opts, verify, cb) { var progressCalled = 0 From c575f0a7f6032015970bb3c2c0d1bfec5133879e Mon Sep 17 00:00:00 2001 From: Albin Mattsson Date: Sun, 22 Oct 2017 12:20:57 +0200 Subject: [PATCH 2/6] =?UTF-8?q?=F0=9F=90=9B=20Fix=20FD=20leak?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/appdmg.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/appdmg.js b/lib/appdmg.js index 21d24a6..04798ab 100644 --- a/lib/appdmg.js +++ b/lib/appdmg.js @@ -85,7 +85,7 @@ module.exports = exports = function (options) { } }) - fs.open(global.target, 'wx', function (error) { + fs.writeFile(global.target, '', { flag: 'wx' }, function (error) { if (error) return next(new Error('Target already exists')) next(null) From ee98f7b0137bbdff08e6e208a48f2dd6071911ee Mon Sep 17 00:00:00 2001 From: Albin Mattsson Date: Sun, 22 Oct 2017 12:22:39 +0200 Subject: [PATCH 3/6] =?UTF-8?q?=F0=9F=90=9B=20Target=20not=20found=20error?= =?UTF-8?q?s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/appdmg.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/appdmg.js b/lib/appdmg.js index 04798ab..b59b2c1 100644 --- a/lib/appdmg.js +++ b/lib/appdmg.js @@ -85,10 +85,10 @@ module.exports = exports = function (options) { } }) - fs.writeFile(global.target, '', { flag: 'wx' }, function (error) { - if (error) return next(new Error('Target already exists')) + fs.writeFile(global.target, '', { flag: 'wx' }, function (err) { + if (err && err.code === 'EEXIST') return next(new Error('Target already exists')) - next(null) + next(err) }) }) From 0755d4852a436ae846249f90f55c3b48a90bfd20 Mon Sep 17 00:00:00 2001 From: Albin Mattsson Date: Sun, 22 Oct 2017 12:27:38 +0200 Subject: [PATCH 4/6] =?UTF-8?q?=F0=9F=90=9B=20Only=20clean=20target=20when?= =?UTF-8?q?=20we=20have=20created=20it?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/appdmg.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/appdmg.js b/lib/appdmg.js index b59b2c1..b52b4e7 100644 --- a/lib/appdmg.js +++ b/lib/appdmg.js @@ -77,14 +77,6 @@ module.exports = exports = function (options) { **/ pipeline.addStep('Looking for target', function (next) { - pipeline.addCleanupStep('unlink-target', 'Removing target image', function (next, hasErrored) { - if (hasErrored) { - fs.unlink(global.target, next) - } else { - next(null) - } - }) - fs.writeFile(global.target, '', { flag: 'wx' }, function (err) { if (err && err.code === 'EEXIST') return next(new Error('Target already exists')) @@ -417,6 +409,14 @@ module.exports = exports = function (options) { **/ pipeline.addStep('Finalizing image', function (next) { + pipeline.addCleanupStep('unlink-target', 'Removing target image', function (next, hasErrored) { + if (hasErrored) { + fs.unlink(global.target, next) + } else { + next(null) + } + }) + var format = global.opts.format || 'UDZO' hdiutil.convert(global.temporaryImagePath, format, global.target, next) From 76a1618a6844a5775376fc182256585700c63bf0 Mon Sep 17 00:00:00 2001 From: Albin Mattsson Date: Mon, 23 Oct 2017 22:12:51 +0200 Subject: [PATCH 5/6] =?UTF-8?q?=F0=9F=90=9B=20Add=20cleanup=20as=20soon=20?= =?UTF-8?q?as=20we=20have=20created=20the=20target?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/appdmg.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/appdmg.js b/lib/appdmg.js index b52b4e7..e75a471 100644 --- a/lib/appdmg.js +++ b/lib/appdmg.js @@ -80,6 +80,14 @@ module.exports = exports = function (options) { fs.writeFile(global.target, '', { flag: 'wx' }, function (err) { if (err && err.code === 'EEXIST') return next(new Error('Target already exists')) + pipeline.addCleanupStep('unlink-target', 'Removing target image', function (next, hasErrored) { + if (hasErrored) { + fs.unlink(global.target, next) + } else { + next(null) + } + }) + next(err) }) }) @@ -409,14 +417,6 @@ module.exports = exports = function (options) { **/ pipeline.addStep('Finalizing image', function (next) { - pipeline.addCleanupStep('unlink-target', 'Removing target image', function (next, hasErrored) { - if (hasErrored) { - fs.unlink(global.target, next) - } else { - next(null) - } - }) - var format = global.opts.format || 'UDZO' hdiutil.convert(global.temporaryImagePath, format, global.target, next) From 5b2ffd16274e0a9adc801206f47d1c7f2d457cd6 Mon Sep 17 00:00:00 2001 From: Albin Mattsson Date: Mon, 23 Oct 2017 22:49:23 +0200 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=90=9B=20Don't=20cleanup=20on=20unkno?= =?UTF-8?q?wn=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/appdmg.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/appdmg.js b/lib/appdmg.js index e75a471..e689436 100644 --- a/lib/appdmg.js +++ b/lib/appdmg.js @@ -79,6 +79,7 @@ module.exports = exports = function (options) { pipeline.addStep('Looking for target', function (next) { fs.writeFile(global.target, '', { flag: 'wx' }, function (err) { if (err && err.code === 'EEXIST') return next(new Error('Target already exists')) + if (err) return next(err) pipeline.addCleanupStep('unlink-target', 'Removing target image', function (next, hasErrored) { if (hasErrored) { @@ -87,8 +88,7 @@ module.exports = exports = function (options) { next(null) } }) - - next(err) + next(null) }) })