From fea98dfa59c952eae4478f24379e36fa11d65f65 Mon Sep 17 00:00:00 2001
From: Alex Kocharin <alex@kocharin.ru>
Date: Tue, 22 Oct 2013 11:53:59 +0400
Subject: [PATCH] unlink directory when package is unpublished

---
 lib/local-fs.js      | 22 +++++++++++++---------
 lib/local-storage.js | 13 +++++++++----
 2 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/lib/local-fs.js b/lib/local-fs.js
index a5b71cccd..a19a49a05 100644
--- a/lib/local-fs.js
+++ b/lib/local-fs.js
@@ -208,39 +208,43 @@ Storage.prototype.path_to = function(file) {
 }
 
 Storage.prototype.create = function(name, value, cb) {
-	create(this.path + '/' + name, value, cb);
+	create(this.path + '/' + name, value, cb)
 }
 
 Storage.prototype.create_json = function(name, value, cb) {
-	create(this.path + '/' + name, JSON.stringify(value, null, '\t'), cb);
+	create(this.path + '/' + name, JSON.stringify(value, null, '\t'), cb)
 }
 
 Storage.prototype.update = function(name, value, cb) {
-	update(this.path + '/' + name, value, cb);
+	update(this.path + '/' + name, value, cb)
 }
 
 Storage.prototype.update_json = function(name, value, cb) {
-	update(this.path + '/' + name, JSON.stringify(value, null, '\t'), cb);
+	update(this.path + '/' + name, JSON.stringify(value, null, '\t'), cb)
 }
 
 Storage.prototype.write = function(name, value, cb) {
-	write(this.path + '/' + name, value, cb);
+	write(this.path + '/' + name, value, cb)
 }
 
 Storage.prototype.write_json = function(name, value, cb) {
-	write(this.path + '/' + name, JSON.stringify(value, null, '\t'), cb);
+	write(this.path + '/' + name, JSON.stringify(value, null, '\t'), cb)
 }
 
 Storage.prototype.write_stream = function(name, value, cb) {
-	return write_stream(this.path + '/' + name, value, cb);
+	return write_stream(this.path + '/' + name, value, cb)
 }
 
 Storage.prototype.read_stream = function(name, cb) {
-	return read_stream(this.path + '/' + name, cb);
+	return read_stream(this.path + '/' + name, cb)
 }
 
 Storage.prototype.unlink = function(name, cb) {
-	fs.unlink(this.path + '/' + name, cb);
+	fs.unlink(this.path + '/' + name, cb)
+}
+
+Storage.prototype.rmdir = function(name, cb) {
+	fs.rmdir(this.path + '/' + name, cb)
 }
 
 module.exports = Storage;
diff --git a/lib/local-storage.js b/lib/local-storage.js
index 9ccacfa4d..874ec35b1 100644
--- a/lib/local-storage.js
+++ b/lib/local-storage.js
@@ -59,15 +59,20 @@ Storage.prototype.add_package = function(name, metadata, callback) {
 }
 
 Storage.prototype.remove_package = function(name, callback) {
-	this.storage.unlink(name + '/' + info_file, function(err) {
+	var self = this
+	self.storage.unlink(name + '/' + info_file, function(err) {
 		if (err && err.code === 'ENOENT') {
 			return callback(new UError({
 				status: 404,
 				msg: 'no such package available',
-			}));
+			}))
 		}
-		callback();
-	});
+
+		// try to unlink the directory, but ignore errors because it can fail
+		self.storage.rmdir(name, function(err) {
+			callback()
+		})
+	})
 }
 
 Storage.prototype._read_create_package = function(name, callback) {