From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#72919: 29.1; chart-space-usage in chart.el does not work correctly on windows Date: Sun, 01 Sep 2024 11:13:40 +0300 Message-ID: <86seujaj0r.fsf@gnu.org> References: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="8127"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 72919@debbugs.gnu.org To: Harm Van der Vegt Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Sep 01 10:14:35 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1skfjE-0001wW-7h for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 01 Sep 2024 10:14:32 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1skfio-0000vv-KH; Sun, 01 Sep 2024 04:14:06 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1skfil-0000vi-FH for bug-gnu-emacs@gnu.org; Sun, 01 Sep 2024 04:14:03 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1skfil-0006GJ-5W for bug-gnu-emacs@gnu.org; Sun, 01 Sep 2024 04:14:03 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=debbugs.gnu.org; s=debbugs-gnu-org; h=MIME-version:References:In-Reply-To:From:Date:To:Subject; bh=7su83uDOS+CGT4HY9CyvdjJQP1tiHBdonm7kRqELoH8=; b=V1/H7pVyPZLSNNn0kbOe3nfyozxyffZw9BJ35qqetk9+9YWugngG0F9KoNtSyy582cZ75rKaWCEKOipJWnxo9U/bi4KW0C1WDPseuC3QS6rHrkplw5EvLETCnXTAGuAZ3gJx+9HVvu3cWZNQpeMu0egsg+97zaStDVwoOK/mSEsn5hytqwYwC3UKsSWdN3U+1RzKKRLSlh5Du5FEfTKgjV56t8suuXt5V/9lzifypMF8sV3W3rhfXnhNh4u5jdfqayKEP01eTUlKamShO0mtQOrHshQKiQceZfryrQuuVIK7pUJwbJ99KAT5VlTTbavCa6utebsQZ02DBKFl/vfFJQ==; Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1skfji-0007FJ-3c for bug-gnu-emacs@gnu.org; Sun, 01 Sep 2024 04:15:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 01 Sep 2024 08:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 72919 X-GNU-PR-Package: emacs Original-Received: via spool by 72919-submit@debbugs.gnu.org id=B72919.172517849227793 (code B ref 72919); Sun, 01 Sep 2024 08:15:02 +0000 Original-Received: (at 72919) by debbugs.gnu.org; 1 Sep 2024 08:14:52 +0000 Original-Received: from localhost ([127.0.0.1]:33141 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1skfjX-0007Dk-NH for submit@debbugs.gnu.org; Sun, 01 Sep 2024 04:14:52 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:53250) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1skfjV-0007DP-PK for 72919@debbugs.gnu.org; Sun, 01 Sep 2024 04:14:50 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1skfiT-0006EN-03; Sun, 01 Sep 2024 04:13:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-version:References:Subject:In-Reply-To:To:From: Date; bh=7su83uDOS+CGT4HY9CyvdjJQP1tiHBdonm7kRqELoH8=; b=mgi2UtmOf61Kq4e/QA48 T+A44SffhGKHURIdBmN3ijeZ1w6T5rsPBM38eSucVbKJ1GHv6dotlYWuV8ny59R3Z1nOfoofRd3wJ Hwyx6urcEXO/YV/4q+L1oxX8HsJFshA+VRMViNiNfrc3qkJR/Z9PSJboXypUkW5GFIGqpAjfIkkPF m2sYTQOf0bdtNoyqu7HhOgLEEsPGRrXOD6NUBhsxEFq/1t1asm1iU71nq/Nv4NJiisGMpbAR48xve C/N7pYv5+xJom5oZOpSt7Eey6hvGoJGL7rJJkBJw0NXw8PzjWq7DBCxeUQYjR6j/13nM2kQUhPb95 hVPJPBAtzgNCkw==; In-Reply-To: (message from Harm Van der Vegt on Sat, 31 Aug 2024 20:52:41 +0200) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:291045 Archived-At: > From: Harm Van der Vegt > Date: Sat, 31 Aug 2024 20:52:41 +0200 > > Load the builtin chart package. When chart-space-usage is run, targeting > any directory, chart fails to find any files and therefore can't create > the chart. The expected behavior is that a bar chart is shown, ranking > the files of the directory by size. > > This only fails on my Windows machine, it works on my Ubuntu > installation. > > The output as shown in *Messages* is as follows: > > Collecting statistics... > Running ‘cd g:/Shakespeare/;du -sk *’... > Scanning output ... > chart-space-usage: No files found! Yes, the current implementation is not portable. > chart-space-usage makes use of the du (disk usage) utility, which might > not be present on all machines. In my case I have du installed, but > chart-space-usage still failed to find any files. > > I have patched chart-space-usage to remove the dependency on du and make > it OS agnostic and attached the patch to this email. Thanks. However, I think your changes are not entirely correct: they fail to account for space usage of files inside subdirectories of the directory which the user types at the prompt, whereas the implementation with "du" does account for that. In addition, I think if someone has 'du' on Windows, it should be used. So I came up with the following changes instead. Could you please try them, both with and without du.exe on PATH? If these changes give good results, I will install them. diff --git a/lisp/emacs-lisp/chart.el b/lisp/emacs-lisp/chart.el index da61e45..03dbe33 100644 --- a/lisp/emacs-lisp/chart.el +++ b/lisp/emacs-lisp/chart.el @@ -641,27 +641,63 @@ chart-file-count (lambda (a b) (> (cdr a) (cdr b)))) )) +;; This assumes 4KB blocks +(defun chart--file-size (size) + (* (/ (+ size 4095) 4096) 4096)) + +(defun chart--directory-size (dir) + "Compute total size of files in directory DIR and its subdirectories. +DIR is assumed to be a directory, verified by the caller." + (let ((size 0)) + (dolist (file (directory-files-recursively dir "." t)) + (let ((fsize (nth 7 (file-attributes file)))) + (if (> fsize 0) + (setq size + (+ size (chart--file-size fsize)))))) + size)) + (defun chart-space-usage (d) "Display a top usage chart for directory D." (interactive "DDirectory: ") (message "Collecting statistics...") (let ((nmlst nil) (cntlst nil) - (b (get-buffer-create " *du-tmp*"))) - (set-buffer b) - (erase-buffer) - (insert "cd " d ";du -sk * \n") - (message "Running `cd %s;du -sk *'..." d) - (call-process-region (point-min) (point-max) shell-file-name t - (current-buffer) nil) - (goto-char (point-min)) - (message "Scanning output ...") - (while (re-search-forward "^\\([0-9]+\\)[ \t]+\\([^ \n]+\\)$" nil t) - (let* ((nam (buffer-substring (match-beginning 2) (match-end 2))) - (num (buffer-substring (match-beginning 1) (match-end 1)))) - (setq nmlst (cons nam nmlst) - ;; * 1000 to put it into bytes - cntlst (cons (* (string-to-number num) 1000) cntlst)))) + b) + (if (executable-find "du") + (progn + (setq b (get-buffer-create " *du-tmp*")) + (set-buffer b) + (erase-buffer) + (if (and (memq system-type '(windows-nt ms-dos)) + (fboundp 'w32-shell-dos-semantics) + (w32-shell-dos-semantics)) + (progn + ;; With Windows shells, 'cd' does not change the drive, + ;; and ';' is not reliable for running multiple + ;; commands, so use alternatives. We quote the + ;; directory because otherwise pushd will barf on a + ;; directory with forward slashes. + (insert "pushd \"" d "\" && du -sk * \n") + (message "Running `pushd \"%s\" && du -sk *'..." d)) + (insert "cd " d ";du -sk * \n") + (message "Running `cd %s;du -sk *'..." d)) + (call-process-region (point-min) (point-max) shell-file-name t + (current-buffer) nil) + (goto-char (point-min)) + (message "Scanning output ...") + (while (re-search-forward "^\\([0-9]+\\)[ \t]+\\([^ \n]+\\)$" nil t) + (let* ((nam (buffer-substring (match-beginning 2) (match-end 2))) + (num (buffer-substring (match-beginning 1) (match-end 1)))) + (setq nmlst (cons nam nmlst) + ;; * 1000 to put it into bytes + cntlst (cons (* (string-to-number num) 1000) cntlst))))) + (dolist (file (directory-files d t directory-files-no-dot-files-regexp)) + (setq nmlst (cons (file-name-nondirectory file) nmlst)) + (if (file-regular-p file) + (setq cntlst (cons (chart--file-size + (nth 7 (file-attributes file))) + cntlst)) + (setq cntlst (cons (chart--directory-size file) cntlst))))) (if (not nmlst) (error "No files found!")) (chart-bar-quickie 'vertical (format "Largest files in %s" d)