file.janet (1200B)
1 (import ./libc) 2 (import ./native/nftw) 3 4 (defn file-exists? [path] 5 (= (os/stat path :mode) :file)) 6 7 (defn dir-exists? [path] 8 (= (os/stat path :mode) :directory)) 9 10 (defn mkdirp [path] 11 (if (not (dir-exists? path)) (do (mkdirp (libc/dirname path)) (os/mkdir path)))) 12 13 (defn copy-file [src dst] 14 (def src_fd (libc/ctry (nftw/open src :r))) 15 (def dst_fd (libc/ctry (nftw/open dst :wxc (nftw/fstat src_fd :int-permissions)))) 16 (libc/sendfile dst_fd src_fd) 17 (nftw/close src_fd) 18 (nftw/close dst_fd)) 19 20 (defn move-file [src dst] 21 (mkdirp (libc/dirname dst)) 22 (try (os/rename src dst) 23 ([a b] (copy-file src dst)))) 24 25 (defn rmrf [path] 26 (nftw/nftw path (fn [file stat ftype info] 27 (if (= ftype :dp) 28 (os/rmdir file) 29 (os/rm file)) 30 0) 1024 :depth :phys)) 31 32 (defn which [exec] 33 (var ret nil) 34 (if (or (string/has-prefix? "/" exec) (string/has-prefix? "./" exec)) 35 (if (file-exists? exec) (set ret exec)) 36 (each dir (string/split ":" (os/getenv "PATH")) 37 (let [path (string/join [dir "/" exec])] 38 (if (file-exists? path) 39 (do 40 (set ret path) 41 (break)))))) 42 ret) 43