May 19, 2023

What am I doing wrong here?

import std.net.curl: Curl, CurlOption, CurlException;
import std.file: exists;
import std.stdio: File, writefln;
import core.thread: Thread;

void downloadFile(string url, string filename)
{
    while (true) {
        try {
            File fp;
            if (filename.exists())
                fp.open(filename, "a");
            else
                fp.open(filename, "w");
            Curl curl;
            curl.initialize();
            curl.onProgress = delegate int(size_t dltotal, size_t dlnow, size_t ultotal, size_t ulnow)
            {
                writefln("Progress: %s of %s", dlnow, dltotal);
                return 0;
            };
            curl.set(CurlOption.url, url~filename);
            curl.set(CurlOption.resume_from_large, fp.size());

            // Start the download
            curl.set(CurlOption.writedata, &fp);
            curl.perform();

            // Close the file
            fp.close();
            writefln("Download as %s complete.", filename);
            break;
        } catch (CurlException e) {
            writefln("Error while downloading: %s", e.msg);

            // Wait for a bit before retrying
            Thread.sleep(imported!"core.time".seconds(10));
        }
    }
}

void main()
{
    string url = "https://downloads.dlang.org/releases/2.x/2.103.1/";
    string filename = "dmd.2.103.1.dmg";

    downloadFile(url, filename);
}

Output:

./download_file
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
Progress: 0 of 0
zsh: segmentation fault  ./download_file

Thanks.

--anonymouse