org-persist: Add situational write inhibition

* lisp/org-persist.el (org-persist-write, org-persist--inhibit-write): TODO
This commit is contained in:
TEC 2023-01-08 19:51:38 +08:00
parent 872d322dea
commit cd9abc224b
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 30 additions and 25 deletions

View File

@ -1084,6 +1084,9 @@ have the same meaning as in `org-persist-read'."
"Call `org-persist-load-all' in current buffer." "Call `org-persist-load-all' in current buffer."
(org-persist-load-all (current-buffer))) (org-persist-load-all (current-buffer)))
(defvar org-persist--inhibit-write nil
"Whether `org-persist-write' should be inhibited.")
(defun org-persist-write (container &optional associated ignore-return) (defun org-persist-write (container &optional associated ignore-return)
"Save CONTAINER according to ASSOCIATED. "Save CONTAINER according to ASSOCIATED.
ASSOCIATED can be a plist, a buffer, or a string. ASSOCIATED can be a plist, a buffer, or a string.
@ -1093,31 +1096,33 @@ The return value is nil when writing fails and the written value (as
returned by `org-persist-read') on success. returned by `org-persist-read') on success.
When IGNORE-RETURN is non-nil, just return t on success without calling When IGNORE-RETURN is non-nil, just return t on success without calling
`org-persist-read'." `org-persist-read'."
(setq associated (org-persist--normalize-associated associated)) (unless org-persist--inhibit-write
;; Update hash (setq associated (org-persist--normalize-associated associated))
(when (and (plist-get associated :file) ;; Update hash
(plist-get associated :hash) (when (and (plist-get associated :file)
(get-file-buffer (plist-get associated :file))) (plist-get associated :hash)
(setq associated (org-persist--normalize-associated (get-file-buffer (plist-get associated :file))))) (get-file-buffer (plist-get associated :file)))
(let ((collection (org-persist--get-collection container associated))) (setq associated (org-persist--normalize-associated (get-file-buffer (plist-get associated :file)))))
(setf collection (plist-put collection :associated associated)) (let ((collection (org-persist--get-collection container associated))
(unless (or (org-persist--inhibit-write t))
;; Prevent data leakage from encrypted files. (setf collection (plist-put collection :associated associated))
;; We do it in somewhat paranoid manner and do not (unless (or
;; allow anything related to encrypted files to be ;; Prevent data leakage from encrypted files.
;; written. ;; We do it in somewhat paranoid manner and do not
(and (plist-get associated :file) ;; allow anything related to encrypted files to be
(string-match-p epa-file-name-regexp (plist-get associated :file))) ;; written.
(seq-find (lambda (v) (and (plist-get associated :file)
(run-hook-with-args-until-success 'org-persist-before-write-hook v associated)) (string-match-p epa-file-name-regexp (plist-get associated :file)))
(plist-get collection :container))) (cl-some (lambda (v)
(when (or (file-exists-p org-persist-directory) (org-persist--save-index)) (run-hook-with-args-until-success 'org-persist-before-write-hook v associated))
(let ((file (org-file-name-concat org-persist-directory (plist-get collection :persist-file))) (plist-get collection :container)))
(data (mapcar (lambda (c) (cons c (org-persist-write:generic c collection))) (when (or (file-exists-p org-persist-directory) (org-persist--save-index))
(plist-get collection :container)))) (let ((file (org-file-name-concat org-persist-directory (plist-get collection :persist-file)))
(puthash file data org-persist--write-cache) (data (mapcar (lambda (c) (cons c (org-persist-write:generic c collection)))
(org-persist--write-elisp-file file data) (plist-get collection :container))))
(or ignore-return (org-persist-read container associated))))))) (puthash file data org-persist--write-cache)
(org-persist--write-elisp-file file data)
(or ignore-return (org-persist-read container associated))))))))
(defun org-persist-write-all (&optional associated) (defun org-persist-write-all (&optional associated)
"Save all the persistent data. "Save all the persistent data.