add writelock to file operations

This commit is contained in:
Matias Alejo Garcia 2016-06-21 11:17:42 -03:00
commit f1e67ad0e6
No known key found for this signature in database
GPG key ID: 02470DB551277AB3
3 changed files with 46 additions and 18 deletions

View file

@ -1407,16 +1407,14 @@ angular.module('copayApp.controllers').controller('indexController', function($r
}); });
$rootScope.$on('Local/ValidatingWallet', function(ev, walletId) { $rootScope.$on('Local/ValidatingWallet', function(ev, walletId) {
if (self.isInFocus(walletId)) { ongoingProcess.set('validatingWallet', true);
ongoingProcess.set('validatingWallet', true);
}
}); });
$rootScope.$on('Local/ValidatingWalletEnded', function(ev, walletId, isOK) { $rootScope.$on('Local/ValidatingWalletEnded', function(ev, walletId, isOK) {
profileService.storeProfileIfDirty(); ongoingProcess.set('validatingWallet', false);
if (self.isInFocus(walletId)) { if (self.isInFocus(walletId)) {
// NOTE: If the user changed the wallet, the flag is already turn off. // NOTE: If the user changed the wallet, the flag is already turn off.
ongoingProcess.set('validatingWallet', false);
self.incorrectDerivation = isOK === false; self.incorrectDerivation = isOK === false;
} }
}); });

View file

@ -52,9 +52,25 @@ angular.module('copayApp.services')
}) })
}; };
root.set = function(k, v, cb) { var writelock = {};
root.set = function(k, v, cb, delay) {
delay = delay || 100;
if (writelock[k]) {
return setTimeout(function() {
console.log('## Writelock for:' + k + ' Retrying in ' + delay);
return root.set(k, v, cb, delay + 100);
}, delay);
}
writelock[k] = true;
root.init(function(err, fs, dir) { root.init(function(err, fs, dir) {
if (err) return cb(err); if (err) {
writelock[k] = false;
return cb(err);
}
dir.getFile(k, { dir.getFile(k, {
create: true, create: true,
}, function(fileEntry) { }, function(fileEntry) {
@ -62,13 +78,15 @@ angular.module('copayApp.services')
fileEntry.createWriter(function(fileWriter) { fileEntry.createWriter(function(fileWriter) {
fileWriter.onwriteend = function(e) { fileWriter.onwriteend = function(e) {
console.log('Write completed.'); console.log('Write completed:' + k);
writelock[k] = false;
return cb(); return cb();
}; };
fileWriter.onerror = function(e) { fileWriter.onerror = function(e) {
var err = e.error ? e.error : JSON.stringify(e); var err = e.error ? e.error : JSON.stringify(e);
console.log('Write failed: ' + err); console.log('Write failed: ' + err);
writelock[k] = false;
return cb('Fail to write:' + err); return cb('Fail to write:' + err);
}; };

View file

@ -117,20 +117,32 @@ angular.module('copayApp.services')
return true; return true;
}; };
root.runValidation = function(client) { var validationLock = false;
root.runValidation = function(client, delay, retryDelay) {
delay = delay || 500;
retryDelay = retryDelay || 50;
if (validationLock) {
return $timeout(function() {
$log.debug('ValidatingWallet Locked: Retrying in: ' + retryDelay);
return root.runValidation(client, delay, retryDelay);
}, retryDelay);
}
validationLock = true;
// IOS devices are already checked // IOS devices are already checked
var skipDeviceValidation = isIOS || root.profile.isDeviceChecked(platformInfo.ua); var skipDeviceValidation = isIOS || root.profile.isDeviceChecked(platformInfo.ua);
var walletId = client.credentials.walletId; var walletId = client.credentials.walletId;
$log.debug('ValidatingWallet: ' + walletId + ' skip Device:' + skipDeviceValidation);
$rootScope.$emit('Local/ValidatingWallet', walletId);
$timeout(function() { $timeout(function() {
$log.debug('ValidatingWallet: ' + walletId + ' skip Device:' + skipDeviceValidation);
$rootScope.$emit('Local/ValidatingWallet', walletId);
client.validateKeyDerivation({ client.validateKeyDerivation({
skipDeviceValidation: skipDeviceValidation, skipDeviceValidation: skipDeviceValidation,
}, function(err, isOK) { }, function(err, isOK) {
validationLock = false;
$log.debug('ValidatingWallet End: ' + walletId + ' isOK:' + isOK); $log.debug('ValidatingWallet End: ' + walletId + ' isOK:' + isOK);
if (isOK) { if (isOK) {
root.profile.setChecked(platformInfo.ua, walletId); root.profile.setChecked(platformInfo.ua, walletId);
@ -138,9 +150,10 @@ angular.module('copayApp.services')
$log.warn('Key Derivation failed for wallet:' + walletId); $log.warn('Key Derivation failed for wallet:' + walletId);
storageService.clearLastAddress(walletId, function() {}); storageService.clearLastAddress(walletId, function() {});
} }
root.storeProfileIfDirty();
$rootScope.$emit('Local/ValidatingWalletEnded', walletId, isOK); $rootScope.$emit('Local/ValidatingWalletEnded', walletId, isOK);
}); });
}, 5000); }, delay);
}; };
// Used when reading wallets from the profile // Used when reading wallets from the profile
@ -163,7 +176,7 @@ angular.module('copayApp.services')
var skipKeyValidation = root.profile.isChecked(platformInfo.ua, credentials.walletId); var skipKeyValidation = root.profile.isChecked(platformInfo.ua, credentials.walletId);
if (!skipKeyValidation) if (!skipKeyValidation)
root.runValidation(client); root.runValidation(client, 500);
$log.info('Binding wallet:' + credentials.walletId + ' Validating?:' + !skipKeyValidation); $log.info('Binding wallet:' + credentials.walletId + ' Validating?:' + !skipKeyValidation);
return cb(null, root.bindWalletClient(client)); return cb(null, root.bindWalletClient(client));
@ -390,8 +403,8 @@ angular.module('copayApp.services')
// check if exist // check if exist
if (lodash.find(root.profile.credentials, { if (lodash.find(root.profile.credentials, {
'walletId': walletData.walletId 'walletId': walletData.walletId
})) { })) {
return cb(gettext('Cannot join the same wallet more that once')); return cb(gettext('Cannot join the same wallet more that once'));
} }
} catch (ex) { } catch (ex) {
@ -512,7 +525,6 @@ angular.module('copayApp.services')
saveBwsUrl(function() { saveBwsUrl(function() {
root.setAndStoreFocus(walletId, function() { root.setAndStoreFocus(walletId, function() {
storageService.storeProfile(root.profile, function(err) { storageService.storeProfile(root.profile, function(err) {
var config = configService.getSync(); var config = configService.getSync();
if (config.pushNotifications.enabled) if (config.pushNotifications.enabled)
pushNotificationsService.enableNotifications(root.walletClients); pushNotificationsService.enableNotifications(root.walletClients);