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.
This commit is contained in:
TEC 2023-01-07 02:54:29 +08:00
parent eb7e65bcaf
commit 90cb3e9507
Signed by: tec
SSH Key Fingerprint: SHA256:eobz41Mnm0/iYWBvWThftS0ElEs1ftBr6jamutnXc/A
1 changed files with 17 additions and 14 deletions

View File

@ -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."