perl5 icon indicating copy to clipboard operation
perl5 copied to clipboard

Signal readdir() errors by clearing/setting errno

Open Corion opened this issue 5 years ago • 12 comments
trafficstars

At least for Linux, distinguishing an error from the end of the directory listing is done by setting errno to 0 and then calling readdir(). Errno will then be set in the case of an error.

https://linux.die.net/man/3/readdir

This adresses

https://github.com/Perl/perl5/issues/17907

but unfortunately, there are no tests to (re)produce the behaviour at all.

Corion avatar Jun 28 '20 16:06 Corion

I don't think this is correct on systems where we use readdir_r(), which doesn't set errno according to https://pubs.opengroup.org/onlinepubs/9699919799/functions/readdir.html

It probably means the readdir() macro we define needs to set it (and will probably need to be a function instead.)

tonycoz avatar Jun 29 '20 05:06 tonycoz

Just some notes here, while I work on this:

This will likely need changing two macro stanzas we have in reentr.h that define readdir(). They are # ifdef HAS_READDIR_R and # ifdef HAS_READDIR64_R. My rough plan is to convert them to one macro that calls the function and returns the error code and a second macro that accesses the buffer. Those two macros are then used in the _readdir() function which is called from pp_readdir.

#  ifdef HAS_READDIR_R
#    if defined(PERL_REENTR_API) && (PERL_REENTR_API+0 == 1)
#      undef readdir
#      if !defined(readdir) && READDIR_R_PROTO == REENTRANT_PROTO_I_TSR
#        define _readdir_r_buf (PL_reentrant_buffer->_readdir_ptr)
#        define _readdir_r(a) (readdir_r(a, PL_reentrant_buffer->_readdir_struct, &_readdir_r_buf))
#        define readdir(a) (_readdir_r(a) == 0 ? _readdir_buf : 0)
#      endif
#      if !defined(readdir) && READDIR_R_PROTO == REENTRANT_PROTO_I_TS
#        define _readdir_r_buf (PL_reentrant_buffer->_readdir_struct)
#        define _readdir_r(a) (readdir_r(a, PL_reentrant_buffer->_readdir_struct, &_readdir_r_buf))
#        define readdir(a) (_readdir_r(a) == 0 ? _readdir_buf : 0)
#      endif
#      if defined(readdir)
#        define PERL_REENTR_USING_READDIR_R
#      endif
#    endif
#  endif /* HAS_READDIR_R */

reentr.h is generated from reentr.pl, so in the long run, the code needs to go there.

Perl does not seem to use readdir64 at all?

I'm not sure if the zoo of macros each calling each other is worth the bother, but I can't see a good way for a clear cut. Maybe changing the API around, from being readdir() compatible to being readdir_r() compatible, and aliasing readdir64_r and readdir_r makes the code less convoluted, but I'll only know that once I've done such a conversion.

Corion avatar Jun 29 '20 21:06 Corion

@Corion we're still working on this?

toddr avatar Jul 30 '20 23:07 toddr

I'm still unclear on the status of blead, and whether there is a Perl 5 or Perl 7, so I've not looked at it. I still intend to work on it, but it will take a bit more revamping of reentr.pl to generate the appropriate macro definition.

Corion avatar Jul 31 '20 06:07 Corion

The plan is to bump blead to 7.1.0 in the next days.

atoomic avatar Jul 31 '20 06:07 atoomic

On 7/31/20 12:42 AM, Nicolas R. wrote:

The plan is to bump blead to 7.1.0 in the next days.

In the meantime, blead is completely open. Changes to files written in perl must conform to modern standards, such as use strict; use warnings.

khwilliamson avatar Jul 31 '20 12:07 khwilliamson

@Corion we're still working on this?

@toddr , @Corion can we get an update on the status of this ticket?

Thank you very much. Jim Keenan

jkeenan avatar Dec 22 '20 23:12 jkeenan

Yes, I'm still working on this. I'll come in with a new round of review questions once I have a working prototype.

Corion avatar Dec 23 '20 15:12 Corion

This is still on my plate but unlikely to land before end of March as I'm busy with other things...

Corion avatar Feb 15 '21 08:02 Corion

@Corion how goes this?

khwilliamson avatar Mar 04 '22 18:03 khwilliamson

I changed the title to that of the commit, which makes more sense, it was confusing me every time I saw it in the list of open PRs.

hvds avatar Mar 05 '22 22:03 hvds

This is still on my plate but unlikely to land before end of March as I'm busy with other things...

@Corion, since there have been no code changes in this p.r. since February 2021, I would like to close this ticket and have you open a new one as needed.

jkeenan avatar Sep 16 '22 22:09 jkeenan

This is still on my plate but unlikely to land before end of March as I'm busy with other things...

@Corion, since there have been no code changes in this p.r. since February 2021, I would like to close this ticket and have you open a new one as needed.

No updates since last September; closing.

jkeenan avatar Jan 21 '23 13:01 jkeenan