224 lines
4.8 KiB
Clojure
224 lines
4.8 KiB
Clojure
(ns File
|
|
"Elixir File module — file system operations.
|
|
|
|
In CljElixir: (File/read \"path\"), (File/write \"path\" content), etc.
|
|
Returns {:ok result} or {:error reason} for most operations.")
|
|
|
|
(defn read
|
|
"Reads the contents of `path`. Returns {:ok binary} or {:error reason}.
|
|
(File/read \"myfile.txt\") ;=> {:ok \"contents\"}"
|
|
[path])
|
|
|
|
(defn read!
|
|
"Reads the contents of `path`. Raises on error.
|
|
(File/read! \"myfile.txt\") ;=> \"contents\""
|
|
[path])
|
|
|
|
(defn write
|
|
"Writes `content` to `path`. Returns :ok or {:error reason}.
|
|
(File/write \"myfile.txt\" \"hello\")
|
|
(File/write \"myfile.txt\" \"hello\" [:append])"
|
|
([path content])
|
|
([path content modes]))
|
|
|
|
(defn write!
|
|
"Writes `content` to `path`. Raises on error.
|
|
(File/write! \"myfile.txt\" \"hello\")"
|
|
([path content])
|
|
([path content modes]))
|
|
|
|
(defn exists?
|
|
"Returns true if `path` exists.
|
|
(File/exists? \"myfile.txt\") ;=> true"
|
|
[path])
|
|
|
|
(defn dir?
|
|
"Returns true if `path` is a directory.
|
|
(File/dir? \"/tmp\") ;=> true"
|
|
[path])
|
|
|
|
(defn regular?
|
|
"Returns true if `path` is a regular file.
|
|
(File/regular? \"myfile.txt\") ;=> true"
|
|
[path])
|
|
|
|
(defn mkdir
|
|
"Creates a directory at `path`.
|
|
(File/mkdir \"mydir\") ;=> :ok"
|
|
[path])
|
|
|
|
(defn mkdir-p
|
|
"Creates a directory and all parent directories.
|
|
(File/mkdir-p \"a/b/c\") ;=> :ok"
|
|
[path])
|
|
|
|
(defn rm
|
|
"Removes a file at `path`.
|
|
(File/rm \"myfile.txt\") ;=> :ok"
|
|
[path])
|
|
|
|
(defn rm!
|
|
"Removes a file at `path`. Raises on error."
|
|
[path])
|
|
|
|
(defn rm-rf
|
|
"Removes files and directories recursively.
|
|
(File/rm-rf \"mydir\") ;=> {:ok [\"mydir/a\" \"mydir\"]}"
|
|
[path])
|
|
|
|
(defn rmdir
|
|
"Removes an empty directory.
|
|
(File/rmdir \"mydir\") ;=> :ok"
|
|
[path])
|
|
|
|
(defn cp
|
|
"Copies `source` to `destination`.
|
|
(File/cp \"src.txt\" \"dst.txt\") ;=> :ok"
|
|
([source destination])
|
|
([source destination callback]))
|
|
|
|
(defn cp!
|
|
"Copies. Raises on error."
|
|
([source destination])
|
|
([source destination callback]))
|
|
|
|
(defn cp-r
|
|
"Copies recursively.
|
|
(File/cp-r \"src_dir\" \"dst_dir\")"
|
|
([source destination])
|
|
([source destination callback]))
|
|
|
|
(defn cp-r!
|
|
"Copies recursively. Raises on error."
|
|
([source destination])
|
|
([source destination callback]))
|
|
|
|
(defn rename
|
|
"Renames/moves `source` to `destination`.
|
|
(File/rename \"old.txt\" \"new.txt\") ;=> :ok"
|
|
[source destination])
|
|
|
|
(defn rename!
|
|
"Renames/moves. Raises on error."
|
|
[source destination])
|
|
|
|
(defn ln-s
|
|
"Creates a symbolic link.
|
|
(File/ln-s \"target\" \"link_name\") ;=> :ok"
|
|
[existing new-link])
|
|
|
|
(defn ls
|
|
"Lists files in a directory. Returns {:ok [filenames]} or {:error reason}.
|
|
(File/ls \".\") ;=> {:ok [\"mix.exs\" \"lib\"]}"
|
|
([path])
|
|
([]))
|
|
|
|
(defn ls!
|
|
"Lists files. Raises on error.
|
|
(File/ls! \".\") ;=> [\"mix.exs\" \"lib\"]"
|
|
([path])
|
|
([]))
|
|
|
|
(defn stat
|
|
"Returns file info. Returns {:ok stat} or {:error reason}.
|
|
(File/stat \"myfile.txt\") ;=> {:ok %File.Stat{...}}"
|
|
([path])
|
|
([path opts]))
|
|
|
|
(defn stat!
|
|
"Returns file info. Raises on error."
|
|
([path])
|
|
([path opts]))
|
|
|
|
(defn lstat
|
|
"Like stat but doesn't follow symlinks."
|
|
([path])
|
|
([path opts]))
|
|
|
|
(defn lstat!
|
|
"Like stat! but doesn't follow symlinks."
|
|
([path])
|
|
([path opts]))
|
|
|
|
(defn cwd
|
|
"Returns the current working directory.
|
|
(File/cwd) ;=> {:ok \"/Users/ajet/repos/clje\"}"
|
|
[])
|
|
|
|
(defn cwd!
|
|
"Returns the current working directory. Raises on error."
|
|
[])
|
|
|
|
(defn cd
|
|
"Changes the current working directory.
|
|
(File/cd \"/tmp\") ;=> :ok"
|
|
[path])
|
|
|
|
(defn cd!
|
|
"Changes directory. Raises on error."
|
|
[path])
|
|
|
|
(defn open
|
|
"Opens a file. Returns {:ok io-device} or {:error reason}.
|
|
(File/open \"myfile.txt\" [:read :utf8])
|
|
(File/open \"myfile.txt\" [:write :append])"
|
|
([path])
|
|
([path modes]))
|
|
|
|
(defn open!
|
|
"Opens a file. Raises on error."
|
|
([path])
|
|
([path modes]))
|
|
|
|
(defn close
|
|
"Closes a file IO device.
|
|
(File/close io-device) ;=> :ok"
|
|
[io-device])
|
|
|
|
(defn stream
|
|
"Returns a File.Stream for lazy reading.
|
|
(File/stream \"bigfile.txt\") ;=> %File.Stream{...}
|
|
(File/stream \"bigfile.txt\" [:read] :line) ;=> line-by-line"
|
|
([path])
|
|
([path modes])
|
|
([path modes line-or-bytes]))
|
|
|
|
(defn stream!
|
|
"Returns a File.Stream. Raises on error."
|
|
([path])
|
|
([path modes])
|
|
([path modes line-or-bytes]))
|
|
|
|
(defn touch
|
|
"Updates file timestamps, creating the file if it doesn't exist.
|
|
(File/touch \"myfile.txt\") ;=> :ok"
|
|
([path])
|
|
([path time]))
|
|
|
|
(defn touch!
|
|
"Touch. Raises on error."
|
|
([path])
|
|
([path time]))
|
|
|
|
(defn chmod
|
|
"Changes file permissions.
|
|
(File/chmod \"script.sh\" 0o755) ;=> :ok"
|
|
[path mode])
|
|
|
|
(defn chown
|
|
"Changes file ownership."
|
|
[path uid])
|
|
|
|
(defn chgrp
|
|
"Changes file group."
|
|
[path gid])
|
|
|
|
(defn read-link
|
|
"Reads the target of a symbolic link.
|
|
(File/read-link \"my-link\") ;=> {:ok \"target\"}"
|
|
[path])
|
|
|
|
(defn read-link!
|
|
"Reads symlink target. Raises on error."
|
|
[path])
|