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 toFile.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/3to read only the part thatoffset:andlength: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 forFil.stream/3and the source ofFil.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 tolength: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 withFile.rename/2. Readers never see a partial file. - Writing over a file keeps its permissions, as
File.write/2does. 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 withO_EXCLfirst. - Ignores
checksum:. - Writing over a directory, or to
report.txt/xwhenreport.txtis a file, returns aFil.InvalidPathError.
Fil.rm/3
- Uses
File.rm/1. A missing file counts as success. - Removing a directory returns a
Fil.InvalidPathError.
Fil.stat/3
- Uses
File.stat/2. :etagis a"size-mtime"tag. It notices most changes, but misses a write in the same second that keeps the size.Fil.Plugsends it as a strong validator anyway, as nginx does.:content_typeisnil, because the filesystem doesn't store one (Fil.Plugguesses 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, soFil.exists?/2isfalsefor 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/3too. Otherwise a disk rooted at a mounted volume couldn't list its root recursively, because only the root user can read itslost+found. The files in such a directory still return aFil.AccessDeniedErrorfromFil.stat/3orFil.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 (seeFil.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/2would 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:epermor:enotsup(a filesystem without hard links, or on Linux a file of another user withfs.protected_hardlinkson), it creates the destination withO_EXCLfirst 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 aFil.ConflictErrorand 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 withFile.rename/2, which replaces an empty directory. A file or a directory with files in it returns aFil.AlreadyExistsError.
Fil.rm_rf/3
- Uses
File.rm/1andFile.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 aFil.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
- The filesystem has no URLs. Attach
Fil.Plugin.URLto build them, andFil.Plugserves them.
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).
| Situation | Fil 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 file | Fil.NotFoundError | :enoent |
| missing permissions, a read-only filesystem | Fil.AccessDeniedError | :eacces, :eperm, :erofs |
| reading, copying or deleting a directory, or putting a file in its place | Fil.InvalidPathError | :eisdir |
| writing, copying or moving under a file, or moving a directory onto one | Fil.InvalidPathError | :enotdir |
| moving a directory into itself | Fil.InvalidPathError | :einval |
| moving a directory onto a directory with files in it | Fil.AlreadyExistsError | :eexist |
| a name that's too long, a symlink loop | Fil.InvalidPathError | :enametoolong, :eloop |
| a path that resolves outside the root | Fil.InvalidPathError | :ebadpath |
| an exclusive create finding the file already there | Fil.AlreadyExistsError | :eexist |
a source that a write replaced during a move with if_exists: :error | Fil.ConflictError | :source_changed |
| a full disk, a used-up quota | Fil.StorageFullError | :enospc, :edquot |
| too many open files | Fil.UnavailableError | :emfile, :enfile |
a ranged read or a stream of a file that can't seek (/proc) | Fil.UnknownError | :einval |
| any other POSIX error | Fil.UnknownError | the atom |
Summary
Types
@type t() :: %Fil.Adapter.Local{root: String.t()}