Subject: Re: Option to fail if over quota
From: Ondřej Jirman
Date: Tue, 31 Mar 2020 23:57:34 +0200
Hi,

On Tue, Mar 31, 2020 at 09:18:14PM +0000, CGar . wrote:
> Thank you so much! 😊. That works much better now.

Great. :)

> However I've noticed that my script never ends since the return code is the same for a quota exceeded error and any other error, such as file exists.
> 
> It would be nice if the return codes were specific to the error type.
> For reference this is the little one liner that I'm using:
> while :; do if torify $megatools dl "$link"; then break; fi; killall -HUP tor; done
> That is mostly just a nice thing to have though. I guess I could instead check for the existence of the file and quit once it exists or something though.

For that you can try pretty much the same condition, this time
in the dl.c code. It will set return status to 2 and break the
command line processing loop, because there's probably not
much point continuing with other files if you're out of quota:

diff --git a/tools/dl.c b/tools/dl.c
index a7448f7..5f87975 100644
--- a/tools/dl.c
+++ b/tools/dl.c
@@ -18,6 +18,7 @@
  */
 
 #include "tools.h"
+#include "http.h"
 #include "shell.h"
 #ifdef G_OS_WIN32
 #include <io.h>
@@ -391,8 +392,13 @@ static int dl_main(int ac, char *av[])
                                if (!opt_noprogress && tool_is_stdout_tty())
                                        g_print("\r" ESC_CLREOL "\n");
                                g_printerr("ERROR: Download failed for '%s': %s\n", link, local_err->message);
+                               if (g_error_matches(local_err, HTTP_ERROR, HTTP_ERROR_BANDWIDTH_LIMIT)) {
+                                       status = 2;
+                                       break;
+                               } else {
+                                       status = 1;
+                               }
                                g_clear_error(&local_err);
-                               status = 1;
                        } else {
                                if (!opt_noprogress) {
                                        if (tool_is_stdout_tty())



regards,
	o.

> Thanks for your help 😊
> ________________________________
> From: Ondřej Jirman <megatools@megous.com>
> Sent: 31 March 2020 20:35
> To: CGar . <CGar@hotmail.co.uk>
> Subject: Re: Option to fail if over quota
> 
> Hi,
> 
> On Tue, Mar 31, 2020 at 07:19:39PM +0000, CGar . wrote:
> > Hi, I'm looking to create a script that will reset my IP when mega returns 509 over quota.
> > However it is quite difficult with the way megadl is setup. It runs for a very long time before it fails.
> >
> > Is there an option to have it fail much faster?
> > That way I can catch that with the return code and get a new IP and start again.
> >
> > I suck at C but have managed to reduce the time by editing the line:
> > const gint64 retry_timeout = 1000ll * 1000 * 60 * 640;
> > https://megous.com/git/megatools/tree/lib/mega.c#n4455
> > But I'm not sure if that is the most appropriate way.
> 
> Try with this change, it should fail immediately when quota is reached,
> and leave the half-downloaded file in place for resume later:
> 
> diff --git a/lib/mega.c b/lib/mega.c
> index 84422c0..5cffce5 100644
> --- a/lib/mega.c
> +++ b/lib/mega.c
> @@ -4465,6 +4465,11 @@ retry:
>                 http_free(h);
> 
>                 if (!download_ok) {
> +                       if (g_error_matches(local_err, HTTP_ERROR, HTTP_ERROR_BANDWIDTH_LIMIT)) {
> +                               g_propagate_prefixed_error(err, local_err, "Data download failed: ");
> +                               goto err_noremove;
> +                       }
> +
>                         // retry timeout reached
>                         if (g_get_monotonic_time() > end_time) {
>                                 g_propagate_prefixed_error(err, local_err, "Data download failed: ");
> 
> 
> 
> 
> regards,
>         o.