emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* function for cleaning org-attach directories
@ 2015-07-16  8:57 Eric Abrahamsen
  2015-07-16  9:38 ` Alan Schmitt
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2015-07-16  8:57 UTC (permalink / raw)
  To: emacs-orgmode

I use org-attach a lot, and if you're not careful you can get a "data/"
directory of many gigabytes. Not a problem, until you want to rsync it
and it takes all day...

I wrote this to clean my attach directories. I'm not sure how portable
it is (and I'm really not pleased with the (concat attach-dir "/" d "/"
d+)), but I'm posting it to see if it's useful to anyone.

Comments/improvements welcome! If the final product is desirable, I can
work it up as a patch.

Eric

(defun org-attach-clean-dirs (&optional attach-dir clean-archived)
  (interactive)
  (let ((attach-dir
	 (if attach-dir
	     (file-name-as-directory attach-dir)
	   (concat (file-name-as-directory org-directory)
		   org-attach-directory)))
	(valid-dir-re "\\`[0-9a-z-]+\\'")
	(org-id-search-archives (if clean-archived nil org-id-search-archives)))
    (dolist (d (directory-files attach-dir nil valid-dir-re))
      (dolist (d+ (directory-files
		   (concat attach-dir d) nil valid-dir-re))
	(let ((id (format "%s%s" d d+))
	      (full-path (concat attach-dir "/" d "/" d+)))
	  (unless (org-id-find id)
	    (shell-command (format "rm -fr %s" full-path))))))))

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: function for cleaning org-attach directories
  2015-07-16  8:57 function for cleaning org-attach directories Eric Abrahamsen
@ 2015-07-16  9:38 ` Alan Schmitt
  2015-07-16 12:43   ` Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Schmitt @ 2015-07-16  9:38 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 824 bytes --]

Hi Eric,

On 2015-07-16 10:57, Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> I use org-attach a lot, and if you're not careful you can get a "data/"
> directory of many gigabytes. Not a problem, until you want to rsync it
> and it takes all day...
>
> I wrote this to clean my attach directories. I'm not sure how portable
> it is (and I'm really not pleased with the (concat attach-dir "/" d "/"
> d+)), but I'm posting it to see if it's useful to anyone.
>
> Comments/improvements welcome! If the final product is desirable, I can
> work it up as a patch.

This would be most useful indeed. One quick question: why do you use
"rm" instead of "delete-file"?

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Last week athmospheric CO₂ average (Updated July 14, 2015, Mauna Loa Obs.):
401.73 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: function for cleaning org-attach directories
  2015-07-16  9:38 ` Alan Schmitt
@ 2015-07-16 12:43   ` Eric Abrahamsen
  2015-07-17  5:19     ` Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2015-07-16 12:43 UTC (permalink / raw)
  To: emacs-orgmode

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> Hi Eric,
>
> On 2015-07-16 10:57, Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> I use org-attach a lot, and if you're not careful you can get a "data/"
>> directory of many gigabytes. Not a problem, until you want to rsync it
>> and it takes all day...
>>
>> I wrote this to clean my attach directories. I'm not sure how portable
>> it is (and I'm really not pleased with the (concat attach-dir "/" d "/"
>> d+)), but I'm posting it to see if it's useful to anyone.
>>
>> Comments/improvements welcome! If the final product is desirable, I can
>> work it up as a patch.
>
> This would be most useful indeed. One quick question: why do you use
> "rm" instead of "delete-file"?

Because I was copy-pasting from org-attach! The real question is: why
didn't I use `delete-directory' :)

I'll do another version!

E

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: function for cleaning org-attach directories
  2015-07-16 12:43   ` Eric Abrahamsen
@ 2015-07-17  5:19     ` Eric Abrahamsen
  2015-07-17 15:01       ` Alan Schmitt
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2015-07-17  5:19 UTC (permalink / raw)
  To: emacs-orgmode

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>
>> Hi Eric,
>>
>> On 2015-07-16 10:57, Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>>
>>> I use org-attach a lot, and if you're not careful you can get a "data/"
>>> directory of many gigabytes. Not a problem, until you want to rsync it
>>> and it takes all day...
>>>
>>> I wrote this to clean my attach directories. I'm not sure how portable
>>> it is (and I'm really not pleased with the (concat attach-dir "/" d "/"
>>> d+)), but I'm posting it to see if it's useful to anyone.
>>>
>>> Comments/improvements welcome! If the final product is desirable, I can
>>> work it up as a patch.
>>
>> This would be most useful indeed. One quick question: why do you use
>> "rm" instead of "delete-file"?
>
> Because I was copy-pasting from org-attach! The real question is: why
> didn't I use `delete-directory' :)
>
> I'll do another version!
>
> E

Here we go, and this one ought to be a little more portable. I guess
I'll do it as a proper patch in a bit.

(defun org-attach-clean-dirs (&optional attach-dir clean-archived)
  (interactive)
  (let ((attach-dir
	 (if attach-dir
	     (file-name-as-directory attach-dir)
	   (concat (file-name-as-directory org-directory)
		   org-attach-directory)))
	(valid-dir-re "\\`[0-9a-z-]+\\'")
	(org-id-search-archives (if clean-archived nil org-id-search-archives)))
    (dolist (d (directory-files attach-dir nil valid-dir-re))
      (dolist (d+ (directory-files
		   (concat attach-dir d) nil valid-dir-re))
	(let ((id (format "%s%s" d d+))
	      (full-path (concat
			  attach-dir
			  (file-name-as-directory d)
			  d+)))
	  (unless (org-id-find id)
	    (delete-directory full-path t)))))))

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: function for cleaning org-attach directories
  2015-07-17  5:19     ` Eric Abrahamsen
@ 2015-07-17 15:01       ` Alan Schmitt
  2015-07-18  3:11         ` Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Schmitt @ 2015-07-17 15:01 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 587 bytes --]

On 2015-07-17 07:19, Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Here we go, and this one ought to be a little more portable. I guess
> I'll do it as a proper patch in a bit.

I gave this a try and it seems that `org-attach-directory' needs to be
defined for it to work. I'm surprised because I never configured this
and with gnorb I have had files attached using org-attach. Does gnorb
use a default value for this?

Thanks,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Last week athmospheric CO₂ average (Updated July 14, 2015, Mauna Loa Obs.):
401.73 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: function for cleaning org-attach directories
  2015-07-17 15:01       ` Alan Schmitt
@ 2015-07-18  3:11         ` Eric Abrahamsen
  2015-07-20 15:04           ` Alan Schmitt
  0 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2015-07-18  3:11 UTC (permalink / raw)
  To: emacs-orgmode

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> On 2015-07-17 07:19, Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Here we go, and this one ought to be a little more portable. I guess
>> I'll do it as a proper patch in a bit.
>
> I gave this a try and it seems that `org-attach-directory' needs to be
> defined for it to work. I'm surprised because I never configured this
> and with gnorb I have had files attached using org-attach. Does gnorb
> use a default value for this?

Gnorb has calls to (require 'org-attach) in certain places -- unless
you've loaded and used gnorb in your current session, you'll probably
want to require that yourself.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: function for cleaning org-attach directories
  2015-07-18  3:11         ` Eric Abrahamsen
@ 2015-07-20 15:04           ` Alan Schmitt
  2015-08-01  9:23             ` [PATCH] " Eric Abrahamsen
  0 siblings, 1 reply; 8+ messages in thread
From: Alan Schmitt @ 2015-07-20 15:04 UTC (permalink / raw)
  To: Eric Abrahamsen; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 930 bytes --]

On 2015-07-18 05:11, Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>
>> I gave this a try and it seems that `org-attach-directory' needs to be
>> defined for it to work. I'm surprised because I never configured this
>> and with gnorb I have had files attached using org-attach. Does gnorb
>> use a default value for this?
>
> Gnorb has calls to (require 'org-attach) in certain places -- unless
> you've loaded and used gnorb in your current session, you'll probably
> want to require that yourself.

Ah, yes, of course. It seems to be working, but it’s awfully silent. It
would be great if it gave some feedback (I don’t know if it actually
deleted anything, as there was nothing in the *Messages* buffer).

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Last week athmospheric CO₂ average (Updated July 14, 2015, Mauna Loa Obs.):
401.73 ppm

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [PATCH] Re: function for cleaning org-attach directories
  2015-07-20 15:04           ` Alan Schmitt
@ 2015-08-01  9:23             ` Eric Abrahamsen
  0 siblings, 0 replies; 8+ messages in thread
From: Eric Abrahamsen @ 2015-08-01  9:23 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1026 bytes --]

Alan Schmitt <alan.schmitt@polytechnique.org> writes:

> On 2015-07-18 05:11, Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
>> Alan Schmitt <alan.schmitt@polytechnique.org> writes:
>>
>>> I gave this a try and it seems that `org-attach-directory' needs to be
>>> defined for it to work. I'm surprised because I never configured this
>>> and with gnorb I have had files attached using org-attach. Does gnorb
>>> use a default value for this?
>>
>> Gnorb has calls to (require 'org-attach) in certain places -- unless
>> you've loaded and used gnorb in your current session, you'll probably
>> want to require that yourself.
>
> Ah, yes, of course. It seems to be working, but it’s awfully silent. It
> would be great if it gave some feedback (I don’t know if it actually
> deleted anything, as there was nothing in the *Messages* buffer).

Whoops, forgot about this! Feedback is a very good idea. I'm sending
another version, this time as a patch in case this is something that
would be welcome in Org proper.

Eric


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-attach.el-New-function-to-delete-unused-dirs.patch --]
[-- Type: text/x-diff, Size: 1835 bytes --]

From 03a8ddacf004d49a51eb7b0b48660fc31da955ac Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Sat, 1 Aug 2015 17:19:49 +0800
Subject: [PATCH] org-attach.el: New function to delete unused dirs

* lisp/org-attach.el (org-attach-clean-dirs): New function.
---
 lisp/org-attach.el | 33 +++++++++++++++++++++++++++++++++
 1 file changed, 33 insertions(+)

diff --git a/lisp/org-attach.el b/lisp/org-attach.el
index 7f61910..84dc5a0 100644
--- a/lisp/org-attach.el
+++ b/lisp/org-attach.el
@@ -498,6 +498,39 @@ This function is called by `org-archive-hook'.  The option
 
 (add-hook 'org-archive-hook 'org-attach-archive-delete-maybe)
 
+(defun org-attach-clean-dirs (&optional attach-dir clean-archived)
+  "Delete attach directories (and their contents) for headings
+which no longer exist."
+  (interactive)
+  (let ((attach-dir
+	 (if attach-dir
+	     (file-name-as-directory attach-dir)
+	   (concat (file-name-as-directory org-directory)
+		   org-attach-directory)))
+	(valid-dir-re "\\`[0-9a-z-]+\\'")
+	(org-id-search-archives (if clean-archived nil org-id-search-archives))
+	dead-dirs)
+    (dolist (d (directory-files attach-dir nil valid-dir-re))
+      (dolist (d+ (directory-files
+		   (concat attach-dir d) nil valid-dir-re))
+	(let ((id (format "%s%s" d d+))
+	      (full-path (concat
+			  attach-dir
+			  (file-name-as-directory d)
+			  d+)))
+	  (unless (org-id-find id)
+	    (push full-path dead-dirs)))))
+    (if dead-dirs
+	(progn
+	  (message "Deleting %d dead attach directories..." (length dead-dirs))
+	  (mapcar
+	   (lambda (d)
+	     (with-demoted-errors
+		 (delete-directory d t)))
+	   dead-dirs)
+	  (message "Deleting %d dead attach directories... done"))
+      (message "No dead directories to delete."))))
+
 (provide 'org-attach)
 
 ;; Local variables:
-- 
2.5.0


^ permalink raw reply related	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2015-08-01  9:23 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-07-16  8:57 function for cleaning org-attach directories Eric Abrahamsen
2015-07-16  9:38 ` Alan Schmitt
2015-07-16 12:43   ` Eric Abrahamsen
2015-07-17  5:19     ` Eric Abrahamsen
2015-07-17 15:01       ` Alan Schmitt
2015-07-18  3:11         ` Eric Abrahamsen
2015-07-20 15:04           ` Alan Schmitt
2015-08-01  9:23             ` [PATCH] " Eric Abrahamsen

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).