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
-
EaccesPermission denied.
-
EagainResource temporarily unavailable.
-
EbadfBad file number
-
EbadmsgBad message.
-
EbusyFile busy.
-
EdeadlkResource deadlock avoided.
-
EdeadlockOn most architectures, same as
Edeadlk. On some architectures, it means “File locking deadlock error.” -
EdquotDisk quota exceeded.
-
EexistFile already exists.
-
EfaultBad address in system call argument.
-
EfbigFile too large.
-
EftypeInappropriate file type or format. Usually caused by trying to set the “sticky bit” on a regular file (not a directory).
-
EintrInterrupted system call.
-
EinvalInvalid argument.
-
EioI/O error.
-
EisdirIllegal operation on a directory.
-
EloopToo many levels of symbolic links.
-
EmfileToo many open files.
-
EmlinkToo many links.
-
EmultihopMultihop attempted.
-
EnametoolongFilename too long
-
EnfileFile table overflow
-
EnobufsNo buffer space available.
-
EnodevNo such device.
-
EnolckNo locks available.
-
EnolinkLink has been severed.
-
EnoentNo such file or directory.
-
EnomemNot enough memory.
-
EnospcNo space left on device.
-
EnosrNo STREAM resources.
-
EnostrNot a STREAM.
-
EnosysFunction not implemented.
-
EnotblkBlock device required.
-
EnotdirNot a directory.
-
EnotsupOperation not supported.
-
EnxioNo such device or address.
-
EopnotsuppOperation not supported on socket.
-
EoverflowValue too large to be stored in data type.
-
EpermNot owner.
-
EpipeBroken pipe.
-
ErangeResult too large.
-
ErofsRead-only file system.
-
EspipeInvalid seek.
-
EsrchNo such process.
-
EstaleStale remote file handle.
-
EtxtbsyText file busy.
-
ExdevCross-domain link.
-
NotUtf8File was requested to be read as UTF-8, but is not UTF-8 encoded.
-
UnknownAny 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 bitstring 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 copy_directory(at src: String, to dest: String) -> Result(
Nil,
FileError,
)
Copy a directory recursively
pub fn copy_file(at src: String, to dest: String) -> Result(
Nil,
FileError,
)
Copy a file at a given path to another path. Note: destination should include the filename, not just the directory
pub fn create_directory(filepath: String) -> Result(
Nil,
FileError,
)
Create a directory at the provided filepath. Returns an error if the directory already exists.
Example
create_directory("./test")
pub fn create_directory_all(dirpath: String) -> Result(
Nil,
FileError,
)
Recursively creates necessary directories for a given directory
path. Note that if you pass a path that “looks like” a file, i.e.
./a/b.txt, a folder named b.txt will be created, so be sure
to pass only the path to the required directory.
pub fn create_file(at filepath: String) -> Result(Nil, FileError)
Creates an empty file at the given filepath. Returns an Error(Eexist)
if the file already exists.
pub fn delete(file_or_dir_at path: String) -> Result(
Nil,
FileError,
)
Delete a file or directory at a given path. Performs a recursive delete on a directory.
Example
let assert Ok(Nil) = delete(file_at: "./delete_me.txt")
pub fn is_directory(filepath: String) -> Bool
Checks if the provided filepath is a directory
Example
let assert True = is_directory("./test")
pub fn is_file(filepath: String) -> Bool
Returns True if there is a file at the given path, false otherwise.
pub fn list_contents(of directory: String) -> Result(
List(String),
FileError,
)
Lists the contents of a directory. The list contains directory and file names, and is not recursive.
Example
let assert Ok(files_and_folders) = list_contents(of: "./Folder1")
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")
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 rename_directory(at src: String, to dest: String) -> Result(
Nil,
FileError,
)
Copy a directory recursively and then delete the old one.
pub fn rename_file(at src: String, to dest: String) -> Result(
Nil,
FileError,
)
Rename a file at a given path to another path. Note: destination should include the filename, not just the directory
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")