copy.lisp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. (asdf:operate 'asdf:load-op 'hurd)
  2. (use-package 'hurd)
  3. ;;
  4. ;; This file tries to mimick in Lisp the copy.c program
  5. ;; from The Hurd Hacking Guide (http://www.gnu.org/software/hurd/hacking-guide/hhg.html)
  6. ;;
  7. (defun write-block (dst data)
  8. (let ((total (length data))
  9. (total-written 0))
  10. (loop until (= total total-written)
  11. do (let ((err (io-write dst
  12. (subseq data total-written))))
  13. (if (or (null err)
  14. (= err 0))
  15. (return))
  16. (incf total-written err)))))
  17. (defun copy-file (src dst)
  18. (declare (type string src dst))
  19. "Returns total of bytes copied."
  20. (with-port-deallocate (src-port
  21. (file-name-lookup src '(:read)))
  22. (with-port-deallocate (dst-port
  23. (file-name-lookup dst '(:write :creat :trunc)))
  24. (loop for data = (io-read src-port)
  25. while data
  26. sum (length data)
  27. while (write-block dst-port data)))))
  28. ;; To run:
  29. ;; (copy-file "myfile-a" "myfile-b")