2
0
mirror of https://github.com/meganz/MEGAcmd synced 2025-08-29 12:57:44 +00:00

Merge branch 'task/CMD-625_avoid-double-errors-on-mount-actions' into 'release/v2.1.0'

CMD-625 [to release] avoid broadcasting mount errors while operating on mounts

See merge request apps/MEGAcmd!880
This commit is contained in:
Pablo M 2025-03-20 05:31:31 +13:00
commit 1dff74f9d1
3 changed files with 48 additions and 16 deletions

View File

@ -452,6 +452,8 @@ void MegaCmdMegaListener::onRequestFinish(MegaApi *api, MegaRequest *request, Me
} }
} }
std::atomic<int> DisableMountErrorsBroadcastingGuard::sDisableBroadcasting = 0;
MegaCmdMegaListener::MegaCmdMegaListener(MegaApi *megaApi, MegaListener *parent, MegaCmdSandbox *sandboxCMD) MegaCmdMegaListener::MegaCmdMegaListener(MegaApi *megaApi, MegaListener *parent, MegaCmdSandbox *sandboxCMD)
{ {
this->megaApi = megaApi; this->megaApi = megaApi;
@ -587,7 +589,10 @@ void MegaCmdMegaListener::onMountEvent(std::string_view pastTense, std::string_v
const std::string msg = oss.str(); const std::string msg = oss.str();
LOG_err << msg; LOG_err << msg;
if (DisableMountErrorsBroadcastingGuard::shouldBroadcast())
{
broadcastDelayedMessage(msg, true); broadcastDelayedMessage(msg, true);
}
return; return;
} }

View File

@ -221,6 +221,15 @@ public:
#endif #endif
}; };
class DisableMountErrorsBroadcastingGuard
{
static std::atomic<int> sDisableBroadcasting;
public:
DisableMountErrorsBroadcastingGuard() { sDisableBroadcasting++; }
~DisableMountErrorsBroadcastingGuard() { sDisableBroadcasting--; }
static bool shouldBroadcast() { return sDisableBroadcasting == 0; }
};
class MegaCmdMegaListener : public mega::MegaListener class MegaCmdMegaListener : public mega::MegaListener
{ {

View File

@ -201,11 +201,14 @@ void addMount(mega::MegaApi& api, const fs::path& localPath, MegaNode& node, boo
auto mount = createMount(localPath, node, disabled, transient, readOnly, name); auto mount = createMount(localPath, node, disabled, transient, readOnly, name);
auto listener = std::make_unique<MegaCmdListener>(nullptr); auto listener = std::make_unique<MegaCmdListener>(nullptr);
{
DisableMountErrorsBroadcastingGuard disableErrorBroadcasting;
api.addMount(mount.get(), listener.get()); api.addMount(mount.get(), listener.get());
if (!checkNoErrors(listener.get(), getActionString("add", localPath, nodePath))) if (!checkNoErrors(listener.get(), getActionString("add", localPath, nodePath)))
{ {
return; return;
} }
}
const std::string mountLocalPath = listener->getRequest()->getFile(); const std::string mountLocalPath = listener->getRequest()->getFile();
OUTSTREAM << "Added a new mount from \"" << mountLocalPath << "\" to \"" << nodePath << '"' << endl; OUTSTREAM << "Added a new mount from \"" << mountLocalPath << "\" to \"" << nodePath << '"' << endl;
@ -233,11 +236,14 @@ void removeMount(mega::MegaApi& api, const mega::MegaMount& mount)
{ {
auto listener = std::make_unique<MegaCmdListener>(nullptr); auto listener = std::make_unique<MegaCmdListener>(nullptr);
{
DisableMountErrorsBroadcastingGuard disableErrorBroadcasting;
api.removeMount(mount.getFlags()->getName(), listener.get()); api.removeMount(mount.getFlags()->getName(), listener.get());
if (!checkNoErrors(listener.get(), getActionString("remove", mount))) if (!checkNoErrors(listener.get(), getActionString("remove", mount)))
{ {
return; return;
} }
}
OUTSTREAM << "Removed mount " << getMountId(mount) << " on \"" << mount.getPath() << '"' << endl; OUTSTREAM << "Removed mount " << getMountId(mount) << " on \"" << mount.getPath() << '"' << endl;
} }
@ -247,11 +253,15 @@ void enableMount(mega::MegaApi& api, const mega::MegaMount& mount, bool temporar
auto listener = std::make_unique<MegaCmdListener>(nullptr); auto listener = std::make_unique<MegaCmdListener>(nullptr);
const bool remember = shouldRememberChange(mount, temporarily); const bool remember = shouldRememberChange(mount, temporarily);
{
DisableMountErrorsBroadcastingGuard disableErrorBroadcasting;
api.enableMount(mount.getFlags()->getName(), listener.get(), remember); api.enableMount(mount.getFlags()->getName(), listener.get(), remember);
if (!checkNoErrors(listener.get(), getActionString("enable", mount))) if (!checkNoErrors(listener.get(), getActionString("enable", mount)))
{ {
return; return;
} }
}
OUTSTREAM << (temporarily ? "Temporarily enabled" : "Enabled") << " mount " OUTSTREAM << (temporarily ? "Temporarily enabled" : "Enabled") << " mount "
<< getMountId( mount) << " on \"" << mount.getPath() << '"' << endl; << getMountId( mount) << " on \"" << mount.getPath() << '"' << endl;
@ -262,11 +272,15 @@ void disableMount(mega::MegaApi& api, const mega::MegaMount& mount, bool tempora
auto listener = std::make_unique<MegaCmdListener>(nullptr); auto listener = std::make_unique<MegaCmdListener>(nullptr);
const bool remember = shouldRememberChange(mount, temporarily); const bool remember = shouldRememberChange(mount, temporarily);
{
DisableMountErrorsBroadcastingGuard disableErrorBroadcasting;
api.disableMount(mount.getFlags()->getName(), listener.get(), remember); api.disableMount(mount.getFlags()->getName(), listener.get(), remember);
if (!checkNoErrors(listener.get(), getActionString("disable", mount))) if (!checkNoErrors(listener.get(), getActionString("disable", mount)))
{ {
return; return;
} }
}
OUTSTREAM << (temporarily ? "Temporarily disabled" : "Disabled") << " mount " OUTSTREAM << (temporarily ? "Temporarily disabled" : "Disabled") << " mount "
<< getMountId(mount) << " on \"" << mount.getPath() << '"' << endl; << getMountId(mount) << " on \"" << mount.getPath() << '"' << endl;
@ -379,11 +393,15 @@ void changeConfig(mega::MegaApi& api, const mega::MegaMount& mount, const Config
auto listener = std::make_unique<MegaCmdListener>(nullptr); auto listener = std::make_unique<MegaCmdListener>(nullptr);
{
DisableMountErrorsBroadcastingGuard disableErrorBroadcasting;
api.setMountFlags(flags, currentName.c_str(), listener.get()); api.setMountFlags(flags, currentName.c_str(), listener.get());
if (!checkNoErrors(listener.get(), getActionString("change the flags of", mount))) if (!checkNoErrors(listener.get(), getActionString("change the flags of", mount)))
{ {
return; return;
} }
}
OUTSTREAM << "Mount " << getMountId(mount) << " now has the following flags\n" OUTSTREAM << "Mount " << getMountId(mount) << " now has the following flags\n"
<< " Name: " << flags->getName() << "\n" << " Name: " << flags->getName() << "\n"