Skip to content

stat() and hence -e fail on an unreadable file on Windows #21598

@tonycoz

Description

@tonycoz

Module:

Description

The -e op reports that a file marked as unreadable doesn't exist.

Steps to Reproduce

C:\Users\Tony\dev\perl\git>echo x >unreadable_file

C:\Users\Tony\dev\perl\git>icacls unreadable_file /deny Everyone:r
processed file: unreadable_file
Successfully processed 1 files; Failed processing 0 files

C:\Users\Tony\dev\perl\git>perl\perl -E "say qq($_: ), -e $_ ? 'Y' : 'N' for @ARGV" unreadable_file test.txt
unreadable_file: N
test.txt: Y

C:\Users\Tony\dev\perl\git>dir unreadable_file test.txt
 Volume in drive C has no label.
 Volume Serial Number is 2824-036D

 Directory of C:\Users\Tony\dev\perl\git

27/10/2023  08:59 AM                 4 unreadable_file

 Directory of C:\Users\Tony\dev\perl\git

02/07/2023  10:51 AM                 3 test.txt
               2 File(s)              7 bytes
               0 Dir(s)  155,203,715,072 bytes free

Expected behavior

-e should return true, as it does on POSIX-likes.

Note that dir is managing to see the file.

Discussion

The UCRT stat also can't stat the file:

C:\Users\Tony\dev\perl\git>type dostat.c
#include <stdio.h>
#include <sys/stat.h>
#include <string.h>

int main(int argc, const char **argv) {
  if (argc < 2) {
    fprintf(stderr, "No filenames\n");
    return 1;
  }

  for (const char **arg = argv+1; *arg; ++arg) {
    struct stat st;

    if (stat(*arg, &st) >= 0) {
      printf("stat %s success\n", *arg);
    }
    else {
      fprintf(stderr, "stat '%s' failed: %s\n", *arg, strerror(errno));
      return -1;
    }
  }
  return 0;
}

C:\Users\Tony\dev\perl\git>cl /W4 dostat.c
Microsoft (R) C/C++ Optimizing Compiler Version 19.35.32217.1 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

dostat.c
dostat.c(18): warning C4996: 'strerror': This function or variable may be unsafe. Consider using strerror_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
Microsoft (R) Incremental Linker Version 14.35.32217.1
Copyright (C) Microsoft Corporation.  All rights reserved.

/out:dostat.exe
dostat.obj

C:\Users\Tony\dev\perl\git>dir test.txt unreadable_file
 Volume in drive C has no label.
 Volume Serial Number is 2824-036D

 Directory of C:\Users\Tony\dev\perl\git

02/07/2023  10:51 AM                 3 test.txt

 Directory of C:\Users\Tony\dev\perl\git

26/10/2023  01:26 PM                 4 unreadable_file
               2 File(s)              7 bytes
               0 Dir(s)  158,618,583,040 bytes free

C:\Users\Tony\dev\perl\git>dostat test.txt unreadable_file
stat test.txt success
stat 'unreadable_file' failed: No such file or directory

Perl configuration

Summary of my perl5 (revision 5 version 39 subversion 4) configuration:
  Commit id: f8f432a1aefb2ccf5facc7c4e4403e3fe6f064bb
  Platform:
    osname=MSWin32
    osvers=10.0.19045.3570
    archname=MSWin32-x64-multi-thread
    uname=''
    config_args='undef'
    hint=recommended
    useposix=true
    d_sigaction=undef
    useithreads=define
    usemultiplicity=define
    use64bitint=define
    use64bitall=undef
    uselongdouble=undef
    usemymalloc=n
    default_inc_excludes_dot=define
  Compiler:
    cc='cl'
    ccflags ='-nologo -GF -W3 -MD -DWIN32 -D_CONSOLE -DNO_STRICT -DDEBUGGING -DWIN64 -D_CRT_SECURE_NO_DEPRECATE -D_CRT_NONSTDC_NO_DEPRECATE -D_WINSOCK_DEPRECATED_NO_WARNINGS  -DPERL_TEXTMODE_SCRIPTS -DMULTIPLICITY -DPERL_IMPLICIT_SYS -DUSE_PERLIO'
    optimize='-Od -Zi -fp:precise'
    cppflags='-DWIN32'
    ccversion='19.35.32217.1'
    gccversion=''
    gccosandvers=''
    intsize=4
    longsize=4
    ptrsize=8
    doublesize=8
    byteorder=12345678
    doublekind=3
    d_longlong=undef
    longlongsize=8
    d_longdbl=define
    longdblsize=8
    longdblkind=0
    ivtype='__int64'
    ivsize=8
    nvtype='double'
    nvsize=8
    Off_t='__int64'
    lseeksize=8
    alignbytes=8
    prototype=define
  Linker and Libraries:
    ld='link'
    ldflags ='-nologo -nodefaultlib -debug -libpath:"c:\perl\lib\CORE" -machine:AMD64 -subsystem:console,"5.02"'
    libpth="C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.35.32215\\lib\x64"
    libs=oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib comctl32.lib msvcrt.lib vcruntime.lib ucrt.lib
    perllibs=oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.lib uuid.lib ws2_32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib comctl32.lib msvcrt.lib vcruntime.lib ucrt.lib
    libc=ucrt.lib
    so=dll
    useshrplib=true
    libperl=perl539.lib
    gnulibc_version=''
  Dynamic Linking:
    dlsrc=dl_win32.xs
    dlext=dll
    d_dlsymun=undef
    ccdlflags=' '
    cccdlflags=' '
    lddlflags='-dll -nologo -nodefaultlib -debug -libpath:"c:\perl\lib\CORE" -machine:AMD64 -subsystem:console,"5.02"'


Characteristics of this binary (from libperl):
  Compile-time options:
    DEBUGGING
    HAS_LONG_DOUBLE
    HAS_TIMES
    HAVE_INTERP_INTERN
    MULTIPLICITY
    PERLIO_LAYERS
    PERL_COPY_ON_WRITE
    PERL_DONT_CREATE_GVSV
    PERL_HASH_FUNC_SIPHASH13
    PERL_HASH_USE_SBOX32
    PERL_IMPLICIT_SYS
    PERL_MALLOC_WRAP
    PERL_OP_PARENT
    PERL_PRESERVE_IVUV
    PERL_TRACK_MEMPOOL
    PERL_USE_SAFE_PUTENV
    USE_64_BIT_INT
    USE_ITHREADS
    USE_LARGE_FILES
    USE_LOCALE
    USE_LOCALE_COLLATE
    USE_LOCALE_CTYPE
    USE_LOCALE_NUMERIC
    USE_LOCALE_TIME
    USE_PERLIO
    USE_PERL_ATOF
    USE_THREAD_SAFE_LOCALE
  Built under MSWin32
  Compiled at Oct 26 2023 13:21:09
  @INC:
    perl\lib
    C:/Users/Tony/dev/perl/git/perl/lib

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions