diff --git a/core/autoload/files.el b/core/autoload/files.el index fbdae80a2..7365641f6 100644 --- a/core/autoload/files.el +++ b/core/autoload/files.el @@ -167,6 +167,33 @@ single file or nested compound statement of `and' and `or' statements." `(let ((p ,(doom--resolve-path-forms files directory))) (and p (expand-file-name p ,directory)))) +;;;###autoload +(defun doom-file-size (file &optional dir) + "Returns the size of FILE (in DIR) in kilobytes." + (when-let (file (file-exists-p! file dir)) + (unless (file-readable-p file) + (error "File %S is unreadable; can't acquire its filesize" + file)) + (nth 7 (file-attributes file)))) + +;;;###autoload +(defun doom-directory-size (dir) + "Returns the size of FILE (in DIR) in kilobytes." + (if (executable-find "du") + (/ (string-to-number (cdr (doom-call-process "du" "-sb" dir))) + 1024.0) + ;; REVIEW This is slow and terribly inaccurate, but it's something + (let ((w32-get-true-file-attributes t) + (file-name-handler-alist dir) + (max-lisp-eval-depth 5000) + (sum 0.0)) + (dolist (attrs (directory-files-and-attributes dir nil nil t) sum) + (unless (member (car attrs) '("." "..")) + (cl-incf + sum (if (eq (nth 1 attrs) t) ; is directory + (doom-directory-size (expand-file-name (car attrs) dir)) + (/ (nth 8 attrs) 1024.0)))))))) + ;; ;;; Helpers