# `Kuroneko.Gadget.MassStorage`
[🔗](https://github.com/cocoa-xu/kuroneko_gadget/blob/v0.5.0/lib/kuroneko/gadget/mass_storage.ex#L1)

The kernel's mass storage function: drives the target computer sees,
each holding an image file.

    Kuroneko.Gadget.MassStorage.insert("/data/images/debian.iso")
    Kuroneko.Gadget.MassStorage.medium()
    #=> {:ok, %{image: "/data/images/debian.iso", as: :dvd, read_only: true, tracks: nil}}
    Kuroneko.Gadget.MassStorage.eject()

An instance is one USB mass storage device whose logical units (LUNs)
are its drives. The `:storage` of `Kuroneko.Gadget` is an instance named
`"disk"`, and `{:storage, luns: 3}` has three drives.

A drive holds its image as a removable disk or as a disc in an optical
drive: a CD, a DVD or a BD. A CD is also addressed in minutes, seconds
and frames, which end at 255:59:74, about 2.3 GB. A CD can have audio and
data tracks, from an image that keeps its sectors as the disc has them,
as a CUE sheet's BIN does. DVDs, BDs and tracks need Kuroneko's kernel
patches, the LUN's `profile` and `tracks` attributes.

The target learns of a new medium when it next looks at the drive. A
target that has locked the medium, such as macOS or Linux with the disc
mounted, is first asked to eject it, as by an optical drive's button, and
is cut off from it when it does not: the gadget leaves the USB bus and
comes back, and the target sees the drive anew. The same happens when a
drive turns from optical to removable or back, which a target reads only
when the drive appears.

## Options

  * `:luns` - the drives, 1 to 16; 1 by default
  * `:inquiry` - the drives' SCSI vendor, product and revision as
    `{vendor, product, revision}`, of up to 8, 16 and 4 ASCII characters;
    the kernel's by default

The drive functions take `:name`, the instance, `"disk"` by default;
`:lun`, the drive, 0 by default; `:gadget`; and `:server`, the
`Kuroneko.Gadget` process that replugs the gadget, `Kuroneko.Gadget` by
default.

# `as`

```elixir
@type as() :: :disk | :cd | :dvd | :bd
```

How a drive holds its image: as a removable disk, or as a disc.

# `drive_option`

```elixir
@type drive_option() ::
  {:name, String.t()}
  | {:lun, non_neg_integer()}
  | {:gadget, Path.t()}
  | {:server, GenServer.server()}
```

# `eject_option`

```elixir
@type eject_option() ::
  {:wait, non_neg_integer()} | {:replug, :auto | boolean()} | drive_option()
```

# `insert_option`

```elixir
@type insert_option() ::
  {:as, as()}
  | {:tracks, [track()]}
  | {:read_only, boolean()}
  | {:replug, :auto | boolean()}
  | drive_option()
```

# `medium`

```elixir
@type medium() :: %{
  image: Path.t(),
  as: as(),
  read_only: boolean(),
  tracks: [track()] | nil
}
```

The image in a drive and how the target sees it.

# `track`

```elixir
@type track() :: %{
  :number =&gt; 1..99,
  :mode =&gt; :audio | :mode1 | :mode2,
  :size =&gt; 2048 | 2336 | 2352,
  :offset =&gt; non_neg_integer(),
  optional(:pregap) =&gt; non_neg_integer(),
  optional(:flags) =&gt; [:copy_permitted | :pre_emphasis | :four_channel]
}
```

A track of a CD: its number, 1 to 99, in order; `:mode`, `:audio`,
`:mode1` or `:mode2`; the `:size` of a sector in the image, 2352 bytes,
or 2048 for mode 1 and 2336 for mode 2 without what comes before the
data; the `:offset` in the image of its first sector; the sectors of its
`:pregap` (index 0), none by default; its `:flags`, from
`:copy_permitted`, `:pre_emphasis` and `:four_channel`.

# `eject`

```elixir
@spec eject([eject_option()]) :: :ok | {:error, term()}
```

Takes the image out of a drive, even when the target holds it: an
optical drive first asks the target to eject it and waits `:wait`
milliseconds, 3000 by default. Takes `:replug` and the drive options as
`insert/2` does.

# `insert`

```elixir
@spec insert(Path.t(), [insert_option()]) :: :ok | {:error, term()}
```

Puts an image in a drive, in place of the one there.

## Options

  * `:as` - `:disk`, a removable disk, or a disc: `:cd`, `:dvd` or `:bd`.
    By default an image holding ISO 9660 or UDF is a DVD, or a CD where
    the kernel has no `profile` attribute, and any other image a disk.
  * `:tracks` - the tracks of a CD whose image keeps its sectors as the
    disc has them (see `t:track/0`), which makes the image a CD
  * `:read_only` - whether the target cannot write; by default for discs
  * `:replug` - whether the gadget leaves the USB bus and comes back
    after, for the target to see the drive anew: `:auto`, the default,
    when the drive changes between optical and removable or a medium the
    target had locked was taken out; `true`; `false`. All of the gadget's
    devices reconnect.
  * the drive options of the module documentation

    Kuroneko.Gadget.MassStorage.insert("/data/images/tools.iso", name: "boot", lun: 1)

    Kuroneko.Gadget.MassStorage.insert("/data/images/album.bin",
      tracks: [
        %{number: 1, mode: :audio, size: 2352, offset: 0},
        %{number: 2, mode: :audio, size: 2352, offset: 41_395_200, pregap: 150}
      ]
    )

# `lun_count`

```elixir
@spec lun_count([drive_option()]) :: non_neg_integer()
```

The drives of an instance, 0 without it. Takes `:name` and `:gadget`.

# `media`

```elixir
@spec media([drive_option()]) :: [medium() | nil]
```

The image in each drive of an instance, `nil` for an empty one, in the
order of the drives. Takes `:name` and `:gadget`.

# `medium`

```elixir
@spec medium([drive_option()]) :: {:ok, medium() | nil} | {:error, term()}
```

The image in a drive, `nil` when there is none. Takes the drive options.

---

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