simplifile
Types
This type represents all of the reasons for why a file system operation could fail.
Most of these reasons are POSIX errors, which come from the operating system and start with E. Others have been added to represent other issues that may arise specific to this library.
pub type FileError {
Eacces
Eagain
Ebadf
Ebadmsg
Ebusy
Edeadlk
Edeadlock
Edquot
Eexist
Efault
Efbig
Eftype
Eintr
Einval
Eio
Eisdir
Eloop
Emfile
Emlink
Emultihop
Enametoolong
Enfile
Enobufs
Enodev
Enolck
Enolink
Enoent
Enomem
Enospc
Enosr
Enostr
Enosys
Enotblk
Enotdir
Enotsup
Enxio
Eopnotsupp
Eoverflow
Eperm
Epipe
Erange
Erofs
Espipe
Esrch
Estale
Etxtbsy
Exdev
NotUtf8
Unknown
}
Constructors
-
Eacces
Permission denied.
-
Eagain
Resource temporarily unavailable.
-
Ebadf
Bad file number
-
Ebadmsg
Bad message.
-
Ebusy
File busy.
-
Edeadlk
Resource deadlock avoided.
-
Edeadlock
On most architectures, same as
Edeadlk
. On some architectures, it means “File locking deadlock error.” -
Edquot
Disk quota exceeded.
-
Eexist
File already exists.
-
Efault
Bad address in system call argument.
-
Efbig
File too large.
-
Eftype
Inappropriate file type or format. Usually caused by trying to set the “sticky bit” on a regular file (not a directory).
-
Eintr
Interrupted system call.
-
Einval
Invalid argument.
-
Eio
I/O error.
-
Eisdir
Illegal operation on a directory.
-
Eloop
Too many levels of symbolic links.
-
Emfile
Too many open files.
-
Emlink
Too many links.
-
Emultihop
Multihop attempted.
-
Enametoolong
Filename too long
-
Enfile
File table overflow
-
Enobufs
No buffer space available.
-
Enodev
No such device.
-
Enolck
No locks available.
-
Enolink
Link has been severed.
-
Enoent
No such file or directory.
-
Enomem
Not enough memory.
-
Enospc
No space left on device.
-
Enosr
No STREAM resources.
-
Enostr
Not a STREAM.
-
Enosys
Function not implemented.
-
Enotblk
Block device required.
-
Enotdir
Not a directory.
-
Enotsup
Operation not supported.
-
Enxio
No such device or address.
-
Eopnotsupp
Operation not supported on socket.
-
Eoverflow
Value too large to be stored in data type.
-
Eperm
Not owner.
-
Epipe
Broken pipe.
-
Erange
Result too large.
-
Erofs
Read-only file system.
-
Espipe
Invalid seek.
-
Esrch
No such process.
-
Estale
Stale remote file handle.
-
Etxtbsy
Text file busy.
-
Exdev
Cross-domain link.
-
NotUtf8
File was requested to be read as UTF-8, but is not UTF-8 encoded.
-
Unknown
Any error not accounted for by this type
Functions
pub fn append(contents: String, to filepath: String) -> Result(
Nil,
FileError,
)
Append a string to the contents of a file at the given path
Example
let assert Ok(Nil) = append("more text", to: "./needs_more_text.txt")
pub fn append_bits(bits: BitString, to filepath: String) -> Result(
Nil,
FileError,
)
Append a string to the contents of a file at the given path
Example
let assert Ok(Nil) = append_bits(<<"more text":utf8>>, to: "./needs_more_text.txt")
pub fn delete(file_at filepath: String) -> Result(Nil, FileError)
Delete a file at a given filepath
Example
let assert Ok(Nil) = delete(file_at: "./delete_me.txt")
pub fn read(from filepath: String) -> Result(String, FileError)
Read a files contents as a string
Example
let assert Ok(records) = read(from: "./users.csv")
A note about utf8
Currently on the erlang target, this function expects a utf8 string
and returns a NotUtf8
error if it reads a non utf8 string.
On the javascript target, it will read any string.
This behavior will probably change soon.
pub fn read_bits(from filepath: String) -> Result(
BitString,
FileError,
)
Read a files contents as a bitstring
Example
let assert Ok(records) = read_bits(from: "./users.csv")
pub fn write(contents: String, to filepath: String) -> Result(
Nil,
FileError,
)
Write a string to a file at the given path
Example
let assert Ok(Nil) = write("Hello, World!", to: "./hello_world.txt")
pub fn write_bits(bits: BitString, to filepath: String) -> Result(
Nil,
FileError,
)
Write a bitstring to a file at the given path
Example
let assert Ok(Nil) = write_bits(<<"Hello, World!":utf8>>, to: "./hello_world.txt")