mirror of https://github.com/keeweb/keeweb.git
Implementing async mkdir
This commit is contained in:
parent
542db7fd0d
commit
f127be3799
|
@ -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');
|
||||
|
|
|
@ -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');
|
||||
|
|
|
@ -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(); }
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue