Subject: bug#39672: 28.0.50; can't visit directory in Dired

From: Kan Sasaki <[email-masked]>
Date: Wed, 19 Feb 2020 11:52:55 +0900 (JST)

In Dired, I can't visit directory which encoding is euc-jp.

When I type 'i' on the directory, emacs says 'Attempt to insert a
non-directory: DIRECTORY.' When I type 'RET' on the directory, emacs
says 'File exists, but cannot be read' and shows empty buffer.

This issue has occured since the following change:

9dc306b1db Improve reporting of I/O, access errors

I compared the files with the previous ones. I guess the attached
patch may fix this issue.

In GNU Emacs 28.0.50 (build 1, x86_64--freebsd, GTK+ Version 3.24.10, cairo version 1.16.0)
 of 2020-02-19 built on ayoki.noc.fcc.ad.jp
Repository revision: 2b04ca05ad9f3679a047ffc06b35d2d61d74b12e
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.11804000
System Description: 12.1-STABLE

Recent messages:
Connecting to the POP server...
Opening a TCP connection ...
Connecting to the POP server...done
Communicating with the POP server...
Sending your POP password to the POP server...
No new messages
[2] is nil. C)reate D)up P)reserve F)indfile B)uff X)M-x k)KeyFunc&exit N)o:
Switch to window[2]
Memorize current window configuration into 2
Opening a TCP connection ...

Configured using:
 'configure amd64--freebsd --prefix=/home/sasaki/local/emacs
 --with-x-toolkit=gtk3 --without-xim --disable-acl 'CFLAGS=-O2 -pipe'
 'CPPFLAGS=-O2 -pipe' LDFLAGS='

Configured features:
XPM JPEG TIFF GIF PNG RSVG CAIRO SOUND DBUS GSETTINGS GLIB NOTIFY
KQUEUE GNUTLS LIBXML2 FREETYPE HARFBUZZ M17N_FLT LIBOTF ZLIB
TOOLKIT_SCROLL_BARS GTK3 X11 XDBE XIM MODULES THREADS JSON PDUMPER
LCMS2 GMP

Important settings:
  value of $LC_COLLATE: C
  value of $LC_TIME: C
  value of $LANG: ja_JP.eucJP
  value of $XMODIFIERS: @im=uim
  locale-coding-system: japanese-iso-8bit-unix

Major mode: Lisp Interaction

Minor modes in effect:
  recentf-mode: t
  override-global-mode: t
  avy-migemo-mode: t
  global-ace-isearch-mode: t
  ace-isearch-mode: t
  helm-migemo-mode: t
  delete-selection-mode: t
  cua-mode: t
  show-paren-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  mouse-wheel-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  column-number-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
/home/sasaki/.emacs.d/elpa/28/magit-20200208.551/magit-section hides /home/sasaki/.emacs.d/elpa/28/magit-section-20200123.2235/magit-section
/home/sasaki/local/emacs/share/emacs/28.0.50/lisp/tempo hides /home/sasaki/.emacs.d/elisp/html-helper/OLD/tempo
/home/sasaki/.emacs.d/elisp/html-helper/html-helper-mode hides /home/sasaki/.emacs.d/elisp/html-helper/OLD/html-helper-mode

Features:
(shadow autoinsert emacsbug message rfc822 mml mml-sec epa derived epg
epg-config gnus-util rmail rmail-loaddefs text-property-search
time-date mm-decode mm-bodies mm-encode mail-parse rfc2231 mailabbrev
gmm-utils mailheader sendmail pp network-stream nsm rmc mew-varsx
mew-unix mail-utils c-sig mew-ical cal-menu calendar cal-loaddefs
rfc2047 rfc2045 mm-util mail-prsvr ietf-drums ht clearcite-mew
clearcite mew-w3m w3m mailcap doc-view jka-compr recentf-ext dired
dired-loaddefs image-mode exif timezone w3m-hist w3m-fb bookmark-w3m
w3m-ems w3m-favicon w3m-image tab-line w3m-proc w3m-util mew-auth
mew-config mew-imap2 mew-imap mew-nntp2 mew-nntp mew-pop mew-smtp
gnutls puny mew-ssl mew-ssh mew-net mew-highlight mew-sort mew-fib
mew-ext mew-refile mew-demo mew-attach mew-draft mew-message
mew-thread mew-virtual mew-summary4 mew-summary3 mew-summary2
mew-summary mew-search mew-pick mew-passwd mew-scan mew-syntax mew-bq
mew-smime mew-pgp mew-header mew-exec mew-mark mew-mime mew-edit
mew-decode mew-encode mew-cache mew-minibuf mew-complete mew-addrbook
mew-local mew-vars3 mew-vars2 mew-vars mew-env mew-lang-jp mew-mule3
mew-mule mew-gemacs mew-key mew-func mew-blvs mew-const mew ccc s dash
helm-ag compile comint ansi-color windows revive web-mode disp-table
sgml-mode dom sync-recentf recentf tree-widget wid-edit diff-mode
dabbrev-highlight dabbrev use-package-bind-key bind-key avy-migemo
migemo avy ring ace-isearch helm-swoop helm-grep helm-regexp
format-spec helm-utils helm-help helm easy-mmode edmacro kmacro
helm-source eieio-compat helm-multi-match helm-lib async ace-jump-mode
cl cl-extra help-mode use-package-core atomic-chrome websocket
url-cookie url-domsuf url-util bindat let-alist server delsel cua-base
windmove utf-7 paren term/bobcat session finder-inf tex-site advice
w3m-load info package easymenu browse-url url-handlers url-parse
auth-source cl-seq eieio eieio-core cl-macs eieio-loaddefs
password-cache json subr-x map url-vars seq byte-opt gv bytecomp
byte-compile cconv cl-loaddefs cl-lib japan-util tooltip eldoc
electric uniquify ediff-hook vc-hooks lisp-float-type mwheel
term/x-win x-win term/common-win x-dnd tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode
elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch timer select scroll-bar mouse jit-lock font-lock
syntax facemenu font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet
lao korean japanese eucjp-ms cp51932 hebrew greek romanian slovak
czech european ethiopic indian cyrillic chinese composite charscript
charprop case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice loaddefs button faces cus-face macroexp files
text-properties overlay sha1 md5 base64 format env code-pages mule
custom widget hashtable-print-readable backquote threads dbusbind
kqueue lcms2 dynamic-setting system-font-setting font-render-setting
cairo move-toolbar gtk x-toolkit x multi-tty make-network-process
emacs)

Memory information:
((conses 16 375112 16620)
 (symbols 48 29028 1)
 (strings 32 130163 2393)
 (string-bytes 1 3826791)
 (vectors 16 38708)
 (vector-slots 8 605645 18128)
 (floats 8 130 23)
 (intervals 56 827 0)
 (buffers 1000 11))
diff --git a/src/fileio.c b/src/fileio.c
index 87a17eab42..6b223e7078 100644
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -2924,16 +2924,18 @@ DEFUN ("file-directory-p", Ffile_directory_p, Sfile_directory_p, 1, 1, 0,
 bool
 file_directory_p (Lisp_Object file)
 {
+  Lisp_Object encoded = ENCODE_FILE (file);
+
 #ifdef DOS_NT
   /* This is cheaper than 'stat'.  */
-  bool retval = faccessat (AT_FDCWD, SSDATA (file), D_OK, AT_EACCESS) == 0;
+  bool retval = faccessat (AT_FDCWD, SSDATA (encoded), D_OK, AT_EACCESS) == 0;
   if (!retval && errno == EACCES)
     errno = ENOTDIR;	/* like the non-DOS_NT branch below does */
   return retval;
 #else
 # ifdef O_PATH
   /* Use O_PATH if available, as it avoids races and EOVERFLOW issues.  */
-  int fd = emacs_openat (AT_FDCWD, SSDATA (file),
+  int fd = emacs_openat (AT_FDCWD, SSDATA (encoded),
 			 O_PATH | O_CLOEXEC | O_DIRECTORY, 0);
   if (0 <= fd)
     {
@@ -2950,12 +2952,12 @@ file_directory_p (Lisp_Object file)
      is inaccessible, fall back on fstatat; if the latter fails with
      EOVERFLOW then FILE must have been a directory unless a race
      condition occurred (a problem hard to work around portably).  */
-  if (file_accessible_directory_p (file))
+  if (file_accessible_directory_p (encoded))
     return true;
   if (errno != EACCES)
     return false;
   struct stat st;
-  if (emacs_fstatat (AT_FDCWD, SSDATA (file), &st, 0) != 0)
+  if (emacs_fstatat (AT_FDCWD, SSDATA (encoded), &st, 0) != 0)
     return errno == EOVERFLOW;
   if (S_ISDIR (st.st_mode))
     return true;