Add a queue to setMetadata to aovid race conditions

pull/1/head
yflory 5 years ago
parent e12e5e1f25
commit 5f37aae990

@ -340,6 +340,7 @@ var getMetadata = function (Env, channel, cb) {
value: value value: value
} }
*/ */
var metadataSem = Saferphore.create(1);
var setMetadata = function (Env, data, unsafeKey, cb) { var setMetadata = function (Env, data, unsafeKey, cb) {
var channel = data.channel; var channel = data.channel;
var command = data.command; var command = data.command;
@ -347,9 +348,17 @@ var setMetadata = function (Env, data, unsafeKey, cb) {
if (!command || typeof (command) !== 'string') { return void cb ('INVALID_COMMAND'); } if (!command || typeof (command) !== 'string') { return void cb ('INVALID_COMMAND'); }
if (Meta.commands.indexOf(command) === -1) { return void('UNSUPPORTED_COMMAND'); } if (Meta.commands.indexOf(command) === -1) { return void('UNSUPPORTED_COMMAND'); }
metadataSem.take(function (give) {
var g = give();
getMetadata(Env, channel, function (err, metadata) { getMetadata(Env, channel, function (err, metadata) {
if (err) { return void cb(err); } if (err) {
if (!(metadata && Array.isArray(metadata.owners))) { return void cb('E_NO_OWNERS'); } g();
return void cb(err);
}
if (!(metadata && Array.isArray(metadata.owners))) {
g();
return void cb('E_NO_OWNERS');
}
// Confirm that the channel is owned by the user in question // Confirm that the channel is owned by the user in question
// or the user is accepting a pending ownerhsip offer // or the user is accepting a pending ownerhsip offer
@ -358,12 +367,15 @@ var setMetadata = function (Env, data, unsafeKey, cb) {
metadata.owners.indexOf(unsafeKey) === -1) { metadata.owners.indexOf(unsafeKey) === -1) {
// If you are a pending owner, make sure you can only add yourelf as an owner // If you are a pending owner, make sure you can only add yourelf as an owner
if (command !== 'ADD_OWNERS' || !Array.isArray(data.value) || data.value.length !== 1 if (command !== 'ADD_OWNERS' || !Array.isArray(data.value)
|| data.value.length !== 1
|| data.value[0] !== unsafeKey) { || data.value[0] !== unsafeKey) {
g();
return void cb('INSUFFICIENT_PERMISSIONS'); return void cb('INSUFFICIENT_PERMISSIONS');
} }
} else if (metadata.owners.indexOf(unsafeKey) === -1) { } else if (metadata.owners.indexOf(unsafeKey) === -1) {
g();
return void cb('INSUFFICIENT_PERMISSIONS'); return void cb('INSUFFICIENT_PERMISSIONS');
} }
@ -372,16 +384,19 @@ var setMetadata = function (Env, data, unsafeKey, cb) {
try { try {
Meta.handleCommand(metadata, line); Meta.handleCommand(metadata, line);
} catch (e) { } catch (e) {
g();
return void cb(e); return void cb(e);
} }
return void Env.msgStore.writeMetadata(channel, JSON.stringify(line), function (e) { Env.msgStore.writeMetadata(channel, JSON.stringify(line), function (e) {
g();
if (e) { if (e) {
return void cb(e); return void cb(e);
} }
cb(void 0, metadata); cb(void 0, metadata);
}); });
}); });
});
}; };
var getMultipleFileSize = function (Env, channels, cb) { var getMultipleFileSize = function (Env, channels, cb) {

Loading…
Cancel
Save