Fil.Adapter behaviour (Fil v0.5.1)

Copy Markdown View Source

The contract every adapter implements.

An adapter is a stateless module. init/1 turns the options into a state term once, when Fil.disk/1 builds the disk, and every other callback gets that state. There's no process to start, supervise or shut down.

Plugins run in Fil before an adapter is called, so an adapter never needs to know about them.

Two rules keep adapters small:

  • Mutations return a bare :ok. Fil wraps results into {:ok, %Fil.Ref{}} and handles cross-disk copies, so adapters don't have to. Errors are Fil's error structs (see Errors), and Fil fills in the operation and the path.
  • Paths arrive normalized and jailed. Fil has already resolved . and .., collapsed repeated / and rejected escapes and control characters, and "." means the disk root. Adapters may check again (Fil.Adapter.Local does), but they don't need to parse paths defensively.

Contract

Fil uses the vocabulary of Elixir's File module, but every adapter behaves like an object store, so the semantics differ where the two disagree. Beyond the callback signatures, this is the contract:

SituationFileEvery Fil adapter
stream/3 on a missing fileFile.stream!/3 raises when it's read{:error, %Fil.NotFoundError{}}
write/4 into a missing directory{:error, :enoent}creates the missing parents
exclusive write/4 on an existing file{:error, :eexist}{:error, %Fil.AlreadyExistsError{}}
exclusive cp/4 or rename/4 onto an existing filereplaces it{:error, %Fil.AlreadyExistsError{}}
cp/4 or rename/4 into a missing directory{:error, :enoent}creates the missing parents
rm/3 on a missing file{:error, :enoent}:ok
ls/3 on a missing directory{:error, :enoent}{:ok, []}
ls/3 resultsnames, one level deep{path, stat} pairs, directories included, also in recursive listings
rm_rf/3 results{:ok, removed_paths}, directories included{:ok, count} of removed files
rm_rf/3 on "reports"the directory reportsreports and all of reports/, not reports.txt
pathsrelative to the working directory, or absoluterelative to the disk root, never above it
control characters in a pathallowed, except NUL{:error, %Fil.InvalidPathError{}}

An exclusive write is File.write/3 with [:exclusive], and write/4 with if_exists: :error. An exclusive copy or move is cp/4 or rename/4 with if_exists: :error. It fails with the destination's path and leaves both files as they were.

The disk root "." is a directory. Fil refuses it as a file, for a read, a write, rm/3, either side of cp/4 and rename/4, and URLs, with Fil.InvalidPathError (:eisdir), so an adapter never gets it there. On an object store, its name is the bucket's or the prefix's own.

cp/4 and rename/4 always get two different paths. Fil answers a copy or a move onto its own path with stat/3, also from another disk of the same storage or with the same location/2: the file stays where it is, an exclusive call fails with Fil.AlreadyExistsError, and a missing file or a directory fails with Fil.NotFoundError. A copy keeps the content type, where the storage keeps one. Across disks, it comes with the stream from stream/3.

The :etag that stat/3 returns changes when a write changes the size of the file. A weak etag may miss other changes. What an adapter's etag covers is under Operations on the adapter's page.

Where an adapter returns :ok, the Fil function returns {:ok, %Fil.Ref{}}, and Fil.ls/2 turns the pairs into refs.

The control characters are 0x00 to 0x1F and 0x7F. A newline in a path would break storage that signs newline-separated fields, such as Azure's SAS, so Fil refuses them on every adapter. An adapter lists names that other programs stored with one as they are, and Fil leaves them out of Fil.ls/2 and Fil.wildcard/2, because no call could use them. The same goes for names that aren't normalized paths, such as the keys dir//x or /lead on an object store: a ref to one would name another file. An adapter lists a prefix without its last / only, so dir// stays dir/ and is left out.

Options that File has no equivalent for:

  • size: on write/4 is the size of a stream, when the caller knows it or Fil.write/4 found it. Storage that needs the size before the content uses it, and uploads a stream without one in parts
  • checksum: on write/4 sends a checksum of the content where the storage keeps one, and storage that finds the content doesn't match fails with Fil.ChecksumMismatchError. Storage without checksums ignores the option
  • checksum: on stat/3 fills in Fil.Stat's :checksum, from the storage or computed from the content. A directory has no checksum, and a copy keeps the checksum of a file written in one part
  • verify_checksum: true on read/3 fails with Fil.ChecksumMismatchError when the content doesn't match a stored checksum
  • offset: and length: on read/3 and stream/3 read only part of the file: up to length: bytes from byte offset: on, or to the end without length:. A part that reaches past the end is shorter, and one that starts at or after the end is empty, without an error. stream/3 returns the size of the part. Fil never passes them together with verify_checksum: true

Fil.AdapterCase (internal for now) tests this contract against a live disk.

Every adapter Fil ships reads, writes and streams files, lists, copies and checks checksums (except Fil.Adapter.Null, whose writes never read back, and the writes of the read-only Fil.Adapter.Zip), and every disk builds public and signed URLs (S3 and Azure themselves, the others with Fil.Plugin.URL), so code written against one disk runs on the others. What's left are edge cases, such as whether directories exist on their own, and each adapter lists them under Operations on its own page.

Errors

Every error is an exception struct, listed under Errors in the sidebar. Which struct you get tells you what to do next, and it's the same on every adapter: a missing file is a Fil.NotFoundError on the filesystem and on S3. Fil.UnavailableError and Fil.ConflictError are the ones where retrying can help.

Every error has the same fields:

  • :op: the operation, such as :read (see Fil.Op.name/0)
  • :path: the path as the caller passed it (before plugins rewrote it), relative to the disk root. For a copy or a move, the side that failed
  • :disk: the Fil.Disk. Fil.ref(error.disk, error.path) is a ref to the file, and Fil.Disk.adapter(error.disk) returns the adapter
  • :reason: the exact cause, as the storage reported it, in the form its adapter documents (:enoent from Fil.Adapter.Local, "NoSuchKey" from Fil.Adapter.S3). nil only when a plugin built the error

Fil returns two of them itself: Fil.InvalidPathError with reason: :ebadpath for a path that escapes the disk root or contains a control character, and Fil.UnsupportedError with reason: :no_callback for a URL on an adapter without that callback.

An adapter returns the struct with :reason set and leaves :op, :path and :disk nil: Fil fills in those that are still nil. An adapter that fails on the destination of a cp/4 or rename/4 sets :path to the destination, because Fil fills in the source. Anything in {:error, _} that isn't an exception raises ArgumentError.

A failure that fits a struct is returned as that struct, whatever the storage reported: Fil.Adapter.Local turns the :eperm that deleting a directory gives into :eisdir, and Fil.Adapter.S3 checks a copy that the server refused with a plain 400 for a missing source. Directory errors (Fil.InvalidPathError) only come from storage with directories of its own, such as the filesystem. Where directories are only key prefixes, reading or copying one is a Fil.NotFoundError, and a write under a file's path succeeds, next to the file.

Summary

Callbacks

Returns the address of the files: what decides which file a path names, such as the root directory, or the bucket and prefix.

Whether the storage refuses an upload from signed_upload/3 with another content-type or content-length than it was signed with.

Copies a file within the same disk.

Validates options and builds the state passed to every other callback.

Returns the location of the file at path: a term that's equal for two disks of the adapter exactly when they name the same stored file, such as the full path on the filesystem, or the endpoint, bucket and full key. The file doesn't have to exist.

Lists prefix, one level deep unless recursive: true.

Reads the whole file.

Whether the storage refuses every change, such as Fil.Adapter.Zip.

Moves a file within the same disk.

Deletes a file. Idempotent: a missing file is still :ok.

Removes everything under prefix and returns the number of deleted files.

Signs an upload to a file: the request a client sends to store it, as a Fil.signed_upload/0.

Builds a URL that downloads a file for a limited time.

Returns metadata for a file or directory.

Checks that a file can be read and returns a stream of its content.

Builds the public URL of a file, without a signature.

Writes content, creating parent directories as needed.

Types

error()

@type error() :: {:error, Fil.error()}

opts()

@type opts() :: keyword()

path()

@type path() :: String.t()

state()

@type state() :: term()

Callbacks

address(state)

(optional)
@callback address(state()) :: term()

Returns the address of the files: what decides which file a path names, such as the root directory, or the bucket and prefix.

Optional. Fil.Disk.same_storage?/2 compares two disks of the adapter by it. Leave out credentials and options that only change how the storage is reached, so a disk with rotated credentials is still the same storage. Without this callback, the whole state is compared.

binds_uploads?(state)

(optional)
@callback binds_uploads?(state()) :: boolean()

Whether the storage refuses an upload from signed_upload/3 with another content-type or content-length than it was signed with.

Optional, and false without it. Fil.Plugin.Validation takes the signed :content_type and :size as the upload's only where the storage binds them, and otherwise refuses to sign an upload its rules can't check.

cp(state, path, path, opts)

@callback cp(state(), path(), path(), opts()) :: :ok | error()

Copies a file within the same disk.

A copy across two disks doesn't call this: Fil reads the file with read/3 on the source adapter and writes it with write/4 on the destination adapter.

With if_exists: :error, an existing destination fails the copy with Fil.AlreadyExistsError, :path set to the destination, and the destination keeps its content.

init(opts)

@callback init(opts()) :: {:ok, state()} | {:error, term()}

Validates options and builds the state passed to every other callback.

Not an operation, so an error is anything that explains the bad option, such as a NimbleOptions.ValidationError. Fil.Disk.new/1 raises it as an ArgumentError.

location(state, path)

(optional)
@callback location(state(), path()) :: term()

Returns the location of the file at path: a term that's equal for two disks of the adapter exactly when they name the same stored file, such as the full path on the filesystem, or the endpoint, bucket and full key. The file doesn't have to exist.

Optional, but an adapter whose disks can have overlapping roots implements it. A copy or a move between two disks of the adapter whose locations are equal is one onto its own path (see the contract), so the file stays where it is instead of being copied onto itself and then deleted. Without this callback, only the same path on the same storage (Fil.Disk.same_storage?/2) counts as the same file. It must not raise: Fil calls it before the copy or the move, outside the operation.

ls(state, path, opts)

@callback ls(state(), path(), opts()) :: {:ok, [{path(), Fil.Stat.t()}]} | error()

Lists prefix, one level deep unless recursive: true.

Returns {path, stat} pairs. Paths are relative to the disk root, and each stat holds what the listing returned. Fil turns the pairs into Fil.Refs; adapters don't build those themselves.

A recursive listing includes the directories of the subtree, at least every directory that holds a file, so storage without directories derives them from its keys. Fil filters by type: and leaves out the directories of a recursive listing unless the caller asked for them, so adapters can ignore that option. The pattern of Fil.wildcard/3 doesn't reach the adapter either: Fil matches it with one listing per directory level, and a recursive one from a ** on.

read(state, path, opts)

@callback read(state(), path(), opts()) :: {:ok, binary()} | error()

Reads the whole file.

read_only?(state)

(optional)
@callback read_only?(state()) :: boolean()

Whether the storage refuses every change, such as Fil.Adapter.Zip.

Optional, and false without it. A move to another disk copies the file and then deletes it here, so Fil.rename/2 refuses a move off a read-only disk before the copy, with the Fil.AccessDeniedError and reason: :read_only that the delete would return.

rename(state, path, path, opts)

@callback rename(state(), path(), path(), opts()) :: :ok | error()

Moves a file within the same disk.

A move across two disks doesn't call this: Fil copies the file as described in cp/4, then removes the source with rm/3.

With if_exists: :error, an existing destination fails the move as it fails cp/4, and the source stays where it is.

rm(state, path, opts)

@callback rm(state(), path(), opts()) :: :ok | error()

Deletes a file. Idempotent: a missing file is still :ok.

rm_rf(state, path, opts)

@callback rm_rf(state(), path(), opts()) :: {:ok, non_neg_integer()} | error()

Removes everything under prefix and returns the number of deleted files.

signed_upload(state, path, opts)

(optional)
@callback signed_upload(state(), path(), opts()) :: {:ok, Fil.signed_upload()} | error()

Signs an upload to a file: the request a client sends to store it, as a Fil.signed_upload/0.

Optional, like signed_url/3: without it, Fil.signed_upload/2 returns a Fil.UnsupportedError, and Fil.Plugin.URL signs uploads for the disk instead.

opts has :signed_at and :expires_at (both a DateTime, at most 7 days apart) as for signed_url/3, so the adapter doesn't read the clock, :query as for signed_url/3, and may have :content_type, :size and :if_exists. The result is a PUT with the file as the body, or a POST with form fields before it. Its headers are the ones the client has to send: content-type for :content_type, if-none-match: * for if_exists: :error, and any the storage needs. Its :signed_at and :expires_at are the ones from opts, :expires_in apart. The storage refuses an upload with another content-type or content-length where it can (see binds_uploads?/1), and with if_exists: :error one that finds a file at the path. A request with another path or method, or a changed parameter, writes nothing.

signed_url(state, path, opts)

(optional)
@callback signed_url(state(), path(), opts()) :: {:ok, String.t()} | error()

Builds a URL that downloads a file for a limited time.

Optional. Adapters whose storage can't sign URLs leave it out, and Fil.signed_url/2 then returns a Fil.UnsupportedError. Fil.Plugin.URL signs URLs for those disks, and Fil.Plug serves them.

opts has :signed_at and :expires_at (both a DateTime, at most 7 days apart), :query as Fil.signed_url/3 validated it, and with disposition:, :disposition as the finished content-disposition header value. The URL works until :expires_at, and is the same for the same options, so the adapter doesn't read the clock. The storage has to send the disposition header with the download, and the URL's signature has to cover it and the :query parameters. A request with another path, method or parameter than the URL was signed for gets a status in the 4xx range. A storage that can't sign :query returns a Fil.UnsupportedError when there is one.

stat(state, path, opts)

@callback stat(state(), path(), opts()) :: {:ok, Fil.Stat.t()} | error()

Returns metadata for a file or directory.

With type: :regular or type: :directory, the adapter may skip looking for the other type and return Fil.NotFoundError instead, so a check for a file costs no directory probe. Fil checks the type of the result either way, so adapters can ignore the option.

stream(state, path, opts)

(optional)
@callback stream(state(), path(), opts()) ::
  {:ok, Enumerable.t()} | {:ok, Enumerable.t(), Fil.Stat.t()} | error()

Checks that a file can be read and returns a stream of its content.

Optional. Without it, Fil.stream/3 calls read/3 and streams the file as one chunk.

Returns {:ok, stream, stat} with a Fil.Stat of what the check found, nil in each field it didn't, or {:ok, stream} when it found nothing. :size is the size of the stream (of the range, when the call asked for one), and :content_type the stored one. Fil.write/4 passes both on when the stream is written, to another disk or in a copy across disks: the size so that storage that needs it before the content (S3) can stream it too, and the content type so that the copy keeps it, unless the call gives its own content_type:.

The stream is lazy: it reads the file only when it's enumerated, and again each time it is, in whatever process enumerates it. It yields binaries of any size and raises an error struct when reading fails (Fil fills in its context). opts are those of read/3, and a verify_checksum: true mismatch raises Fil.ChecksumMismatchError at the latest after the last chunk.

url(state, path, opts)

(optional)
@callback url(state(), path(), opts()) :: {:ok, String.t()} | error()

Builds the public URL of a file, without a signature.

Optional. Adapters whose storage has no URLs leave it out, and Fil.url/2 then returns a Fil.UnsupportedError. Fil.Plugin.URL builds URLs for those disks, and Fil.Plug serves them.

write(state, path, arg3, opts)

@callback write(state(), path(), iodata() | Enumerable.t(), opts()) :: :ok | error()

Writes content, creating parent directories as needed.

content is iodata, or a stream of non-empty binaries. Fil checks a stream against the :size option while the adapter reads it, when the caller gave one or Fil.write/4 found one. If the stream raises, at any point and also after its last chunk, the adapter lets the error propagate and writes nothing: the destination keeps what it had before. That's how a plugin refuses content while it's written (Fil.Op.scan_content/4). Fil reads a stream with size: 0 to its end before it calls the adapter and passes "" instead, so a plugin that refuses empty content leaves the destination as it was.