Xgit v0.2.2 Xgit.Util.FileSnapshot View Source

Caches when a file was last read, making it possible to detect future edits.

This object tracks the last modified time of a file. Later during an invocation of modified?/2 the object will return true if the file may have been modified and should be re-read from disk.

A snapshot does not "live update" when the underlying filesystem changes. Callers must poll for updates by periodically invoking modified?/2.

To work around the "racy git" problem (where a file may be modified multiple times within the granularity of the filesystem modification clock) this class may return true from modified?/2 if the last modification time of the file is less than 3 seconds ago.

Link to this section Summary

Types

t()

Cache for when a file was last saved.

Functions

An Xgit.Util.FileSnapshot that is considered to always be modified.

An Xgit.Util.FileSnapshot that is clean if the file does not exist.

Return true if the path may have been modified since the snapshot was saved.

Record a snapshot for a specific file path.

Update this snapshot when the content hasn't changed.

Link to this section Types

Link to this type

t()

View Source
t() :: %Xgit.Util.FileSnapshot{
  last_modified: integer() | :missing | :dirty,
  ref: reference() | nil
}

Cache for when a file was last saved.

Struct Members

  • last_modified: Last observed modification time of the path.
  • ref: Reference into a cache for when this file was last read.

Link to this section Functions

An Xgit.Util.FileSnapshot that is considered to always be modified.

This instance is useful for application code that wants to lazily read a file, but only after modified?/2 gets invoked. This snapshot instance contains only invalid status information.

Link to this function

missing_file()

View Source
missing_file() :: t()

An Xgit.Util.FileSnapshot that is clean if the file does not exist.

This instance is useful if the application wants to consider a missing file to be clean. modified?/2 will return false if the file path does not exist.

Link to this function

modified?(file_snapshot, path)

View Source
modified?(snapshot :: t(), path :: Path.t()) :: boolean()

Return true if the path may have been modified since the snapshot was saved.

Link to this function

save(path)

View Source
save(path :: Path.t()) :: t()

Record a snapshot for a specific file path.

This function should be invoked before the file is accessed.

Link to this function

set_clean(sanpshot, other)

View Source
set_clean(snapshot :: t(), other :: t()) :: :ok

Update this snapshot when the content hasn't changed.

If the caller gets true from modified?/2, re-reads the content, discovers the content is identical, it can use to make a future call to modified?/2 return false.

A typical invocation looks something like this:

new_snapshot =
  if FileSnapshot.modified?(snapshot, path) do
    other = FileSnapshot.save(path)
    if old_content_matches_new_content? and snapshot.last_modified == other.last_modified do
      FileSnapshot.set_clean(snapshot, other)
    else
      snapshot
    end
  else
    snapshot
  end

Return Value

:ok