Fil.Adapter.Local (Fil v0.5.1)

Copy Markdown View Source

The local filesystem, jailed under a root directory.

disk = Fil.disk(adapter: Fil.Adapter.Local, root: "priv/storage")

Unlike on an object store, directories exist on their own and can be empty. Every path is checked against the root again after expansion. Fil has already rejected ../ escapes, so this is defense in depth. The check only looks at the path string: a symlink inside the root can still point outside it.

Options

  • :root (String.t/0) - The directory every path is resolved against. Defaults to File.cwd!() when the disk is built, so changing the working directory later doesn't move the disk. A relative root is expanded once, when the disk is built.

Operations

For each Fil function, this section says which File or :file function it uses and where it departs from the contract that every adapter follows. Anything it doesn't mention follows the contract.

Fil.read/3

  • Uses File.read/1, or :file.pread/3 to read only the part that offset: and length: ask for.
  • Reading a directory returns a Fil.InvalidPathError.
  • A FIFO, a socket or a device file is missing, as for Fil.stat/3, because opening a FIFO would wait for a writer. The same goes for Fil.stream/3 and the source of Fil.cp/4.
  • Ignores verify_checksum: true, because the filesystem stores no checksums.
  • A file that can't seek to its end, such as one in /proc, returns an error for a range (:einval), because the range needs the file's size. Without a range, it's read as usual.

Fil.stream/3

  • Opens the file once to check that it can be read, and to get its size. A file that can't seek to its end, such as one in /proc, returns an error (:einval).
  • Each time the stream is read, reads the file in chunks of 64 KiB, from offset: on and up to length: bytes.

Fil.write/4

  • Writes the content (in memory or a stream) to a temporary file in the destination directory, named .fil- and a unique suffix, then moves it into place with File.rename/2. Readers never see a partial file.
  • Writing over a file keeps its permissions, as File.write/2 does. A new file gets the default ones.
  • A failed write leaves nothing behind and removes the directories it created. So does a writer that's killed, within the limits in Interrupted writes.
  • With if_exists: :error, hard-links the temporary file to the destination instead, which fails if the destination exists. On a filesystem without hard links, it creates the destination with O_EXCL first.
  • Ignores checksum:.
  • Writing over a directory, or to report.txt/x when report.txt is a file, returns a Fil.InvalidPathError.

Fil.rm/3

Fil.stat/3

  • Uses File.stat/2.
  • :etag is a "size-mtime" tag. It notices most changes, but misses a write in the same second that keeps the size. Fil.Plug sends it as a strong validator anyway, as nginx does.
  • :content_type is nil, because the filesystem doesn't store one (Fil.Plug guesses it from the extension).
  • checksum: reads the whole file to compute the checksum.
  • A FIFO, a socket or a device file returns a Fil.NotFoundError, so Fil.exists?/2 is false for it.

Fil.ls/3

  • Uses File.ls/1, and walks the tree depth-first when recursive.
  • Lists a symlink as the file or directory it points to, and leaves out one that points nowhere. A recursive listing doesn't walk into a symlinked directory, so a link to a parent or to a directory outside the root lists only the link itself.
  • Lists empty directories too, and leaves out FIFOs, sockets and device files.
  • Skips the temporary .fil- files of writes. The disk reserves that prefix, so a file of your own whose name starts with .fil- is skipped as well.
  • A path that isn't a directory lists nothing, the same as a missing one.
  • A directory the process can't read lists nothing as well, in recursive listings and Fil.wildcard/3 too. Otherwise a disk rooted at a mounted volume couldn't list its root recursively, because only the root user can read its lost+found. The files in such a directory still return a Fil.AccessDeniedError from Fil.stat/3 or Fil.read/3.

Fil.cp/4

  • Uses :file.copy/2.
  • Copies to a temporary .fil- file, then moves it into place like a write, so readers never see a partial file. The copy gets the source's permissions.
  • A copy that fails removes the directories it created, like a write.
  • Copying a directory to another path returns a Fil.InvalidPathError.
  • Between two Local disks whose roots overlap, also through a symlink, two paths name the same file when their full paths are equal with the symlinks in their parents followed. A hard link, or a name that differs only in case on a case-insensitive filesystem, still names another file.

Fil.rename/4

  • Uses File.rename/2, which moves directories too, but not onto their own path (see Fil.cp/2).
  • A move that fails removes the directories it created, like a write.
  • On one disk, a move onto the file the source already is (the source is a symlink to it or a hard link of it) removes only the source, so the destination keeps its content. File.rename/2 would make a symlink point to itself.
  • Between two Local disks whose roots overlap, two paths name the same file as for Fil.cp/4. A move onto a hard link of the source, or onto its name in another case on a case-insensitive filesystem, copies the file and then deletes it.
  • With if_exists: :error, hard-links the file to the destination, then removes the source. Where the link fails with :eperm or :enotsup (a filesystem without hard links, or on Linux a file of another user with fs.protected_hardlinks on), it creates the destination with O_EXCL first and then moves the file there, like a write. A symlink is moved that way too, so the destination is the link and not its target.
  • With if_exists: :error, a write that replaces the source after the link fails the move with a Fil.ConflictError and leaves the destination as it was. A write in the short moment between that check and the removal of the source is lost.
  • With if_exists: :error, a directory is still moved with File.rename/2, which replaces an empty directory. A file or a directory with files in it returns a Fil.AlreadyExistsError.

Fil.rm_rf/3

  • Uses File.rm/1 and File.rmdir/1, depth-first, and lists directories with :file.list_dir_all/1. Counts the files it removed.
  • Like File.rm_rf/1, goes on past a directory it can't list or an entry it can't remove, and removes what it can. It then returns the first error it met, such as a Fil.AccessDeniedError.
  • Removes a symlink, not what it points to. The link counts as a file when it points to one.
  • Fil.rm_rf(disk, ".") removes what the root holds and keeps the root, also when the root is a symlink.
  • Removes files whose name starts with .fil- or isn't valid UTF-8 too, but doesn't count them.

Fil.url/3 and Fil.signed_url/3

Fil.Disk.same_storage?/2

  • Compares the expanded :root, so "priv/storage" and its absolute path are the same storage.

Details

Interrupted writes

When a writer is killed before it's done (a request process that the server stops when the client disconnects, for example), Fil's application removes its .fil- file and the directories it created once the process is gone. The same goes for a copy. That's best effort: a filesystem that takes more than a second to create the file or a directory after the kill (a slow network share) can leave it behind, and so can a crash of the whole node (kill -9, a power loss). Local writes also work without the :fil application running, but then nothing is removed after a kill. Stopping the application removes the temporary files of the writes in progress, so those writes fail with a Fil.NotFoundError.

Errors

:reason is the POSIX atom from File, adjusted so it's the same on macOS and Linux (deleting a directory is :eisdir on both, a parent that's a file :enotdir).

SituationFil error:reason
a missing file or directory, or a path through a file (report.txt/x)Fil.NotFoundError:enoent
a FIFO, a socket or a device fileFil.NotFoundError:enoent
missing permissions, a read-only filesystemFil.AccessDeniedError:eacces, :eperm, :erofs
reading, copying or deleting a directory, or putting a file in its placeFil.InvalidPathError:eisdir
writing, copying or moving under a file, or moving a directory onto oneFil.InvalidPathError:enotdir
moving a directory into itselfFil.InvalidPathError:einval
moving a directory onto a directory with files in itFil.AlreadyExistsError:eexist
a name that's too long, a symlink loopFil.InvalidPathError:enametoolong, :eloop
a path that resolves outside the rootFil.InvalidPathError:ebadpath
an exclusive create finding the file already thereFil.AlreadyExistsError:eexist
a source that a write replaced during a move with if_exists: :errorFil.ConflictError:source_changed
a full disk, a used-up quotaFil.StorageFullError:enospc, :edquot
too many open filesFil.UnavailableError:emfile, :enfile
a ranged read or a stream of a file that can't seek (/proc)Fil.UnknownError:einval
any other POSIX errorFil.UnknownErrorthe atom

Summary

Types

t()

@type t() :: %Fil.Adapter.Local{root: String.t()}