Xgit v0.1.6 Xgit.Util.TrailingHashDevice View Source

Creates an iodevice process that supports git file formats with a trailing SHA-1 hash.

When reading, the trailing 20 bytes are interpreted as a SHA-1 hash of the remaining file contents and can be verified using the valid_hash?/1 function.

This is an admittedly minimal implementation; just enough is implemented to allow Xgit's index file parser to do its work.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Creates an IO device that reads a file with trailing hash.

Creates an IO device that writes to a file with trailing hash.

Creates an IO device that reads a string with trailing hash.

Returns true if this is process is an TrailingHashDevice instance.

Returns true if the hash at the end of the file matches the hash generated while reading the file.

Link to this section Functions

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

open_file(path)

View Source
open_file(path :: Path.t()) :: {:ok, pid()} | {:error, File.posix()}

Creates an IO device that reads a file with trailing hash.

Unlike File.open/2 and File.open/3, no options or function are accepted.

This device can be passed to IO.binread/2.

Return Value

{:ok, pid} where pid points to an IO device process.

{:ok, reason} if the file could not be opened. See File.open/2 for possible values for reason.

Link to this function

open_file_for_write(path, opts \\ [])

View Source
open_file_for_write(path :: Path.t(), opts :: Keyword.t()) ::
  {:ok, pid()} | {:error, File.posix()}

Creates an IO device that writes to a file with trailing hash.

Unlike File.open/2 and File.open/3, no options or function are accepted.

This device can be passed to IO.binwrite/2.

Options

:max_file_size (non-negative integer) may be passed, which will cause a failure after the _n_th byte is written. This is intended for internal testing purposes.

Return Value

{:ok, pid} where pid points to an IO device process.

{:ok, reason} if the file could not be opened. See File.open/2 for possible values for reason.

Link to this function

open_string(s)

View Source
open_string(s :: binary()) :: {:ok, pid()}

Creates an IO device that reads a string with trailing hash.

This is intended mostly for internal testing purposes.

Unlike StringIO.open/2 and StringIO.open/3, no options or function are accepted.

This device can be passed to IO.binread/2.

Return Value

{:ok, pid} where pid points to an IO device process.

Link to this function

valid?(v)

View Source
valid?(v :: any()) :: boolean()

Returns true if this is process is an TrailingHashDevice instance.

Note the difference between this function and valid_hash?/1.

Link to this function

valid_hash?(io_device)

View Source
valid_hash?(io_device :: pid()) :: boolean()

Returns true if the hash at the end of the file matches the hash generated while reading the file.

Should only be called once and only once when the entire file (sans SHA-1 hash) has been read.

Return Values

true or false if the SHA-1 hash was found and was valid (or not).

:too_soon if called before the SHA-1 hash is expected.

:already_called if called a second (or successive) time.

:opened_for_write if called on a device that was opened for write.