From 48b5a770f247d8c027d209ce941767ab5a7d139d Mon Sep 17 00:00:00 2001 From: Po Lu Date: Fri, 3 Mar 2023 16:00:27 +0800 Subject: [PATCH] Fix visiting and saving writable content provider files * java/org/gnu/emacs/EmacsService.java (checkContentUri): Improve debug output. * lisp/files.el (basic-save-buffer): Check whether or not file itself exists before checking for the existence of the directory containing it. * src/android.c (android_open): Don't forget to set errno after open_content_uri fails. --- java/org/gnu/emacs/EmacsService.java | 2 ++ lisp/files.el | 9 +++++++-- src/android.c | 10 ++++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/java/org/gnu/emacs/EmacsService.java b/java/org/gnu/emacs/EmacsService.java index 67de5d26f53..d9cb25f3e9c 100644 --- a/java/org/gnu/emacs/EmacsService.java +++ b/java/org/gnu/emacs/EmacsService.java @@ -752,6 +752,8 @@ public final class EmacsService extends Service if (writable) mode += "w"; + Log.d (TAG, "checkContentUri: checking against mode " + mode); + try { fd = resolver.openFileDescriptor (Uri.parse (name), mode); diff --git a/lisp/files.el b/lisp/files.el index 387a3b5dc66..35f31711065 100644 --- a/lisp/files.el +++ b/lisp/files.el @@ -5726,9 +5726,14 @@ Before and after saving the buffer, this function runs (run-hook-with-args-until-success 'write-file-functions) ;; If a hook returned t, file is already "written". ;; Otherwise, write it the usual way now. - (let ((dir (file-name-directory + (let ((file (buffer-file-name)) + (dir (file-name-directory (expand-file-name buffer-file-name)))) - (unless (file-exists-p dir) + ;; Some systems have directories (like /content on + ;; Android) in which files can exist without a + ;; corresponding parent directory. + (unless (or (file-exists-p file) + (file-exists-p dir)) (if (y-or-n-p (format-message "Directory `%s' does not exist; create? " dir)) diff --git a/src/android.c b/src/android.c index 1e91abffa6f..656971e154f 100644 --- a/src/android.c +++ b/src/android.c @@ -1715,9 +1715,19 @@ android_open (const char *filename, int oflag, int mode) return -1; } + /* If fd is -1, just assume that the file does not exist, + and return -1 with errno set to ENOENT. */ + + if (fd == -1) + { + errno = ENOENT; + goto skip; + } + if (mode & O_CLOEXEC) android_close_on_exec (fd); + skip: ANDROID_DELETE_LOCAL_REF (string); return fd; } -- 2.39.2