From bad854d5fa9cf1d839c71866e96a1ca9f4755105 Mon Sep 17 00:00:00 2001 From: Ihor Radchenko Date: Tue, 19 Oct 2021 20:06:08 +0800 Subject: [PATCH] org-persist.el: Catch write errors * lisp/org-persist.el (org-persist--get-index): Check index file. (org-persist--read-index): Warn when index file is missing. (org-persist-write): Report write access errors. --- lisp/org-persist.el | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/lisp/org-persist.el b/lisp/org-persist.el index 903667f2e..ab636ec69 100644 --- a/lisp/org-persist.el +++ b/lisp/org-persist.el @@ -78,6 +78,7 @@ a data variable. Each plist contains the following properties: (defun org-persist--get-index (var &optional buffer) "Return plist used to store VAR in BUFFER. When BUFFER is nil, return plist for global VAR." + (org-persist--read-index) (let* ((buffer-file (when buffer (buffer-file-name (or (buffer-base-buffer buffer) buffer)))) (inode (when buffer-file (file-attribute-inode-number (file-attributes buffer-file))))) @@ -103,10 +104,12 @@ When BUFFER is nil, return plist for global VAR." (defun org-persist--read-index () "Read `org-persist--index'" (unless org-persist--index - (when (file-exists-p (org-file-name-concat org-persist-directory org-persist-index-file)) - (with-temp-buffer - (insert-file-contents (org-file-name-concat org-persist-directory org-persist-index-file)) - (setq org-persist--index (read (current-buffer))))))) + (if (file-exists-p (org-file-name-concat org-persist-directory org-persist-index-file)) + (with-temp-buffer + (insert-file-contents (org-file-name-concat org-persist-directory org-persist-index-file)) + (setq org-persist--index (read (current-buffer)))) + (warn "Cannot read org-persist index from %s." + (org-file-name-concat org-persist-directory org-persist-index-file))))) (cl-defun org-persist-register (var &optional buffer &key inherit) "Register VAR in BUFFER to be persistent. @@ -172,6 +175,17 @@ When BUFFER is `all', unregister VAR in all buffers." (plist-get index :variable)) (unless (file-exists-p org-persist-directory) (make-directory org-persist-directory)) + (unless (file-exists-p org-persist-directory) + (warn "Failed to create org-persist storage in %s." + org-persist-directory) + (let ((dir (directory-file-name + (file-name-as-directory org-persist-directory)))) + (while (and (not (file-exists-p dir)) + (not (equal dir (setq dir (directory-file-name + (file-name-directory dir))))))) + (unless (file-writable-p dir) + (message "Missing write access rights to org-persist-directory: %S" + org-persist-directory)))) (with-temp-file (org-file-name-concat org-persist-directory org-persist-index-file) (prin1 org-persist--index (current-buffer))) (let ((file (org-file-name-concat org-persist-directory (plist-get index :persist-file)))