;;;; -*- coding:utf-8 -*-
;;;;FILE:               disk.lisp
;;;;LANGUAGE:           Common-Lisp
;;;;SYSTEM:             clisp
;;;;USER-INTERFACE:     clisp
;;;;    This package exports disk management functions,
;;;;    <PJB> Pascal J. Bourguignon <pjb@informatimago.com>
;;;;    2010-06-27 <PJB> Removed string constant +TAB+.
;;;;    2003-10-21 <PJB> Added du.
;;;;    2002-01-09 <PJB> Moved in df from tar-backup.
;;;;    2002-10-?? <PJB> Creation.
;;;;    AGPL3
;;;;    Copyright Pascal J. Bourguignon 2002 - 2016
;;;;    This program is free software: you can redistribute it and/or modify
;;;;    it under the terms of the GNU Affero General Public License as published by
;;;;    the Free Software Foundation, either version 3 of the License, or
;;;;    (at your option) any later version.
;;;;    This program is distributed in the hope that it will be useful,
;;;;    but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;;    GNU Affero General Public License for more details.
;;;;    You should have received a copy of the GNU Affero General Public License
;;;;    along with this program.  If not, see <http://www.gnu.org/licenses/>
(eval-when (:compile-toplevel :load-toplevel :execute)
  (setf *readtable* (copy-readtable nil)))
   "This package exports disk management functions.

    Copyright Pascal J. Bourguignon 2002 - 2003
    This package is provided under the GNU General Public License.
    See the source file for details.")
  (:use "COMMON-LISP"
           "DF" "DU" ))

(defun old-df ()
RETURN:  A list of sublists each containing:
           (device size used available mount-point)
   (lambda (line) (split-string line " "))
     "/bin/df -T|postodax|/bin/awk '/Available/{next;}{print $1,$2,$3,$4,$5,$7;}'"
     :output :stream))))

(defstruct volinfo
  (path        "" :type string)
  (fs-type     "" :type string)
  (size        0  :type number)
  (used        0  :type number)
  (available   0  :type number)
  (mount-point "" :type string)

(defun df (&optional path)
RETURN:  A list of volinfo structures.
  (unless path (setq path ""))
   (lambda (line)
     (let ((data (split-string line " \\+")))
       (make-volinfo :path (nth 0 data)
                     :fs-type (nth 1 data)
                     :size (* 1024 (read-from-string (nth 2 data)))
                     :used (* 1024 (read-from-string (nth 3 data)))
                     :available (* 1024 (read-from-string (nth 4 data)))
                     :mount-point (nth 5 data))))
      nil "df -k -T ~A|postodax|/bin/awk '/Available/{next;}{print $1,$2,$3,$4,$5,$7;}'"
     :output :stream))))

(defun du (&optional (path (ext:cd)))
RETURN:  The Disk Usage of the given PATH (or the current directory).
         It's given as the underlying du command gives it, either in
         blocks of 1KB or of 512 B.
     (mapcar (lambda (line) (split-string line #.(string (code-char 9))))
              (ext:run-program "du"
                               :arguments (list "-s" path) :output :stream)))))))

;;;; THE END ;;;;