Implementing async mkdir

This commit is contained in:
Alex Shpak 2017-02-05 23:34:37 +01:00
parent 542db7fd0d
commit f127be3799
4 changed files with 50 additions and 28 deletions

View File

@ -80,22 +80,44 @@ const Launcher = {
statFileSync: function(path) {
this.req('fs').statSync(path);
},
mkdir: function(dir) {
mkdir: function(dir, callback) {
const fs = this.req('fs');
const path = this.req('path');
const stack = [];
while (true) {
if (fs.existsSync(dir)) {
break;
const collect = function(dir, stack, callback) {
fs.exists(dir, exists => {
if (exists) {
return callback();
}
stack.unshift(dir);
const newDir = path.dirname(dir);
if (newDir === dir || !newDir || newDir === '.' || newDir === '/') {
return callback();
}
collect(newDir, stack, callback);
});
};
const create = function(stack, callback) {
if (!stack.length) {
if (callback) {
callback();
}
}
stack.unshift(dir);
const newDir = path.dirname(dir);
if (newDir === dir || !newDir || newDir === '.' || newDir === '/') {
break;
}
dir = newDir;
}
stack.forEach(dir => fs.mkdirSync(dir));
fs.mkdir(stack.shift(), err => {
if (err) {
return callback(err);
}
create(stack, callback);
});
};
collect(dir, stack, () => create(stack, callback));
},
parsePath: function(fileName) {
const path = this.req('path');

View File

@ -854,14 +854,14 @@ const AppModel = Backbone.Model.extend({
if (Storage[backup.storage].getPathForName) {
folderPath = Storage[backup.storage].getPathForName(folderPath).replace('.kdbx', '');
}
Storage[backup.storage].stat(folderPath, opts, (err) => {
Storage[backup.storage].stat(folderPath, opts, err => {
if (err) {
if (err.notFound) {
logger.info('Backup folder does not exist');
if (!Storage[backup.storage].mkdir) {
return callback('Mkdir not supported by ' + backup.storage);
}
Storage[backup.storage].mkdir(folderPath, (err) => {
Storage[backup.storage].mkdir(folderPath, err => {
if (err) {
logger.error('Error creating backup folder', err);
callback('Error creating backup folder');

View File

@ -23,9 +23,12 @@ const StorageFileCache = StorageBase.extend({
Launcher.fileExists(path, exists => {
if (!exists) {
Launcher.mkdir(path);
this.path = path;
if (callback) { callback(); }
Launcher.mkdir(path, err => {
if (!err) {
this.path = path;
}
if (callback) { callback(); }
});
} else {
if (callback) { callback(); }
}

View File

@ -110,18 +110,15 @@ const StorageFile = StorageBase.extend({
this.logger.debug('Make dir', path);
const ts = this.logger.ts();
try {
Launcher.mkdir(path);
this.logger.debug('Made dir', path, this.logger.ts(ts));
if (callback) {
callback();
Launcher.mkdir(path, err => {
if (err) {
this.logger.error('Error making local dir', path, err);
if (callback) { callback('Error making local dir'); }
} else {
this.logger.debug('Made dir', path, this.logger.ts(ts));
if (callback) { callback(); }
}
} catch (e) {
this.logger.error('Error making local dir', path, e);
if (callback) {
callback(e);
}
}
});
},
watch: function(path, callback) {