# `NervesHubLink.UpdateManager.PartsUpdater`
[🔗](https://github.com/nerves-hub/nerves_hub_link/blob/v2.14.0/lib/nerves_hub_link/update_manager/parts_updater.ex#L5)

Downloads firmware to disk one part at a time, checking each part against the
checksums NervesHub sends in the update payload before it is used.

Like `NervesHubLink.UpdateManager.CachingUpdater` this keeps the download on
disk so an interrupted update doesn't have to start again from the beginning.
The difference is what "where did I get to" means. `CachingUpdater` trusts the
size of the partial file, so a part that was being written when the power went
out, or that arrived corrupted, is silently treated as good and only shows up
much later as an `fwup` error. Here each part is hashed as it arrives and
compared to the checksum for that part:

  * a part that fails is thrown away and downloaded again, without discarding
    the parts before it
  * a part that has already been downloaded and verified is not downloaded
    again after a restart
  * the firmware handed to `fwup` is known to match what NervesHub sent

Once every part has been verified the parts are streamed into `fwup` in order,
so the whole firmware never needs to exist on disk twice. The parts are left
where they are afterwards, and are removed when a different firmware starts
downloading, so an update that is applied but doesn't stick doesn't have to be
downloaded again.

To use this strategy, configure NervesHubLink with:

    config :nerves_hub_link,
      updater: NervesHubLink.UpdateManager.PartsUpdater

## Configuration

    config :nerves_hub_link, NervesHubLink.UpdateManager.PartsUpdater,
      cache_dir: "/data/nerves_hub_link/firmware",
      part_size: 1_048_576,
      max_part_attempts: 3,
      max_concurrent_parts: 1

  * `:cache_dir` - where downloads are kept. Each firmware gets its own
    directory underneath, named after its UUID. This directory belongs to
    `NervesHubLink`, which removes anything in it that isn't part of the
    firmware being downloaded, so don't point it at somewhere shared.
    Defaults to `/data/nerves_hub_link/firmware`.

  * `:part_size` - the number of bytes covered by each checksum. This has to
    match the size NervesHub used when it hashed the firmware, which is 1 MiB.
    A value that would split the firmware into a different number of parts
    than the payload has checksums for is rejected, but one that happens to
    produce the same count is not, and shows up as every part failing
    verification. Defaults to `1_048_576`.

  * `:max_part_attempts` - how many times a single part may fail verification
    before the update is failed. Defaults to `3`.

  * `:max_concurrent_parts` - how many downloads may run at once. See below.
    Defaults to `1`.

## Downloading more than one part at a time

With `:max_concurrent_parts` set above `1`, the parts still needed are divided
into that many contiguous ranges and each range is downloaded by its own
connection. Parts are still verified as they arrive and are still applied in
order once they are all present, so nothing about the result changes - only
how many connections are open while it happens.

This is worth doing on links where a single connection can't fill the
available bandwidth, such as high latency satellite or cellular. It costs a
connection per range on both the device and whatever is serving the firmware,
which is multiplied by every device in a deployment, so raise it deliberately
rather than by default. Each connection also reports a `:started` update
status of its own, so NervesHub sees one per range.

Each download labels its log lines with the parts it is fetching, so the log
can still be followed when several are running.

## Older NervesHub servers

`partials_checksums` is only sent by newer NervesHub servers. When it is
missing the firmware is downloaded as a single part and verified against the
whole file `checksum` if one was sent, which is the same guarantee
`CachingUpdater` gives. A warning is logged when this happens.

# `child_spec`

Returns a specification to start this module under a supervisor.

See `Supervisor`.

# `send_update?`

# `start_link`

```elixir
@spec start_link(
  NervesHubLink.Message.UpdateInfo.t(),
  NervesHubLink.FwupConfig.t(),
  fwup_public_keys :: [],
  GenServer.options()
) :: GenServer.on_start()
```

---

*Consult [api-reference.md](api-reference.md) for complete listing*
