From 90cb3e9507f53ef6ba26b0868ec498cd4db85b74 Mon Sep 17 00:00:00 2001 From: TEC Date: Sat, 7 Jan 2023 02:54:29 +0800 Subject: [PATCH] org-persist: Fallback to associated files in write * lisp/org-persist.el (org-persist-write:file): If the path given in a file container does not exist, instead of returning nothing return the associated path if possible. This fixes an issue where should the original file be deleted and `org-persist-write-all' be run, then the file container's association will be set to nil and so when `org-persist-gc' is run the nil path will cause org-persist-associated-files:file to produce an error. --- lisp/org-persist.el | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/lisp/org-persist.el b/lisp/org-persist.el index ae8483158..8a7562fe5 100644 --- a/lisp/org-persist.el +++ b/lisp/org-persist.el @@ -828,20 +828,23 @@ COLLECTION is the plist holding data collection." (defun org-persist-write:file (c collection) "Write file container C according to COLLECTION." (org-persist-collection-let collection - (when (or (and path (file-exists-p path)) - (and (stringp (cadr c)) (file-exists-p (cadr c)))) - (when (and (stringp (cadr c)) (file-exists-p (cadr c))) - (setq path (cadr c))) - (let* ((persist-file (plist-get collection :persist-file)) - (ext (file-name-extension path)) - (file-copy (org-file-name-concat - org-persist-directory - (format "%s-%s.%s" persist-file (md5 path) ext)))) - (unless (file-exists-p file-copy) - (unless (file-exists-p (file-name-directory file-copy)) - (make-directory (file-name-directory file-copy) t)) - (copy-file path file-copy 'overwrite)) - (format "%s-%s.%s" persist-file (md5 path) ext))))) + (if (or (and path (file-exists-p path)) + (and (stringp (cadr c)) (file-exists-p (cadr c)))) + (progn + (when (and (stringp (cadr c)) (file-exists-p (cadr c))) + (setq path (cadr c))) + (let* ((persist-file (plist-get collection :persist-file)) + (ext (file-name-extension path)) + (file-copy (org-file-name-concat + org-persist-directory + (format "%s-%s.%s" persist-file (md5 path) ext)))) + (unless (file-exists-p file-copy) + (unless (file-exists-p (file-name-directory file-copy)) + (make-directory (file-name-directory file-copy) t)) + (copy-file path file-copy 'overwrite)) + (format "%s-%s.%s" persist-file (md5 path) ext))) + (when-let ((file-copy (org-persist-read c associated))) + (file-relative-name file-copy org-persist-directory))))) (defun org-persist-write:url (c collection) "Write url container C according to COLLECTION."