Граф коммитов

563 Коммитов

Автор SHA1 Сообщение Дата
Jeremy Evans f2919bd11c
Add error checking to readdir, telldir, and closedir calls in dir.c
Raise SystemCallError exception when these functions return an error.

This changes behavior for the following case (found by the tests):

```ruby
dir1 = Dir.new('..')
dir2 = Dir.for_fd(dir1.fileno)
dir1.close
dir2.close
```

The above code is basically broken, as `dir1.close` closed the file
descriptor.  The subsequent `dir2.close` call is undefined behavior.
When run in isolation, it raises Errno::EBADF after the change, but
if another thread opens a file descriptor between the `dir1.close`
and `dir2.close` calls, the `dir2.close` call could close the file
descriptor opened by the other thread.  Raising an exception is much
better in this case as it makes it obvious there is a bug in the code.

For the readdir check, since the GVL has already been released,
reacquire it rb_thread_call_with_gvl if an exception needs to be
raised.

Due to the number of closedir calls, this adds static close_dir_data
and check_closedir functions.  The close_dir_data takes a
struct dir_data * and handles setting the dir entry to NULL regardless
of failure.

Fixes [Bug #20586]

Co-authored-by: Nobuyoshi Nakada <nobu.nakada@gmail.com>
2024-09-12 10:04:10 -07:00
Nobuyoshi Nakada 05502c1dda
Add a macro to initialize `struct getattrlist_args` 2024-07-17 12:00:27 +09:00
Jeremy Evans dabb6c49aa
Release GVL around {,f}getattrlist calls in dir.c
Fixes [Bug #20587]
2024-07-16 19:20:17 -07:00
Jeremy Evans 8ade9994bf
Release GVL around system calls in dir.c
* Release GVL for fdopendir calls
* Release GVL for readdir calls
* Release GVL for chdir call in dir_chdir0
* Release GVL for fchdir call in dir_fchdir
* Release GVL for chroot calls
* Release GVL for lstat calls
* Release GVL for stat calls
* Release GVL for fstatat calls
* Release GVL for getpwnam call in rb_home_dir_of
  (technically in file.c, but called from dir.c)

This does not release GVL for readdir/stat/lstat on Windows,
as that causes issues because the emulation functions that
are called in win32.c require the GVL.

This also removes some explicit casts either to or from void *,
which are allowed implicitly.  The remaining casts to or from
void * are part of function pointer casts, which are not
allowed implicitly and will generate a warning.
2024-07-14 12:02:00 -07:00
Yudai Takada 278a7cb057
[DOC] Fix a typo in dir.c
s/Retruns/Returns/
2024-06-12 10:42:01 +00:00
Jean Boussier 7c12169230 Eliminate internal uses of `Data_Wrap_Struct`
Ref: https://github.com/ruby/ruby/pull/10872

These should be the last internal uses of the old `Data` API
inside Ruby itself. Some use remain in a couple default gems.
2024-06-02 13:59:11 +02:00
Nobuyoshi Nakada 5326337d4f [Feature #20244] Issue a single `Warning.warn` call
Make the entire series of message lines a multiline string so that the
`Warning.warn` hook can receive them in a single call.
2024-03-14 18:33:28 +09:00
Nobuyoshi Nakada 67fe047821 [Feature #20244] Register global variable for another thread
Co-authored-by: Alan Wu <XrXr@users.noreply.github.com>
2024-03-14 18:33:28 +09:00
Nobuyoshi Nakada 4bb8f8582f [Feature #20244] Show the conflicting another chdir block 2024-03-14 18:33:28 +09:00
Nobuyoshi Nakada 8fe86feecd [Feature #20244] Extract `chdir_lock` and its stuffs 2024-03-14 18:33:28 +09:00
Nobuyoshi Nakada d86c4e553e
Define `IO_WITHOUT_GVL` macro 2024-01-24 20:51:50 +09:00
Xavier Noria aad246feba s/SafeStringValue/StringValue/
The macro SafeStringValue() became just StringValue() in c5c05460ac,
and it is deprecated nowadays.

This patch replaces remaining macro usage. Some occurrences are left in
ext/stringio and ext/win32ole, they should be fixed upstream.

The macro itself is not deleted, because it may be used in extensions.
2024-01-12 12:24:48 -08:00
Nobuyoshi Nakada 494e6b54ab
[DOC] Add links from `File::Constants` in dir.c to `File` constants
Due to the bug ruby/rdoc#1067, `rb_file_const` needs `Document-const`
directives.
2023-12-18 20:17:44 +09:00
Victor Shepelev 570d7b2c3e
[DOC] Adjust some new features wording/examples. (#9183)
* Reword Range#overlap? docs last paragraph.

* Docs: add explanation about Queue#freeze

* Docs: Add :rescue event docs for TracePoint

* Docs: Enhance Module#set_temporary_name documentation

* Docs: Slightly expand Process::Status deprecations

* Fix MatchData#named_captures rendering glitch

* Improve Dir.fchdir examples

* Adjust Refinement#target docs
2023-12-14 23:01:48 +02:00
Jeremy Evans f49af3c969 Make Dir#chdir never yield args, and return block return value
If no block is given, return 0 instead of nil for consistency
with Dir.chdir and Dir.fchdir.
2023-12-11 22:31:16 -08:00
Victor Shepelev 07734b51c6
[DOC] Small fixes for documentation rendering
Mostly just fixing RDoc's incorrect treatment of `+`
2023-12-09 13:54:33 +09:00
Nobuyoshi Nakada 0cdad3b92a Add `RUBY_REFERENCES`
Instead of `RUBY_REFERENCES_START` and `RUBY_REFERENCES_END`, so that
auto-indent works well.
2023-11-30 21:39:28 +09:00
Nobuyoshi Nakada 30f7b7a053 Prefix `REF_EDGE` and `REFS_LIST_PTR` with `RUBY_`
Also move `struct` so that `typedef`-ed names can be used.
2023-11-30 21:39:28 +09:00
Peter Zhu b040c529a0 Implement dir on VWA 2023-11-22 09:36:54 -05:00
Nobuyoshi Nakada 63fdfa1312
[DOC] Link Dir.exist? to File.directory? 2023-11-14 17:29:15 +09:00
Nobuyoshi Nakada b01118f29c
[DOC] Describe Dir.home 2023-11-14 17:20:57 +09:00
Koichi Sasada be1bbd5b7d M:N thread scheduler for Ractors
This patch introduce M:N thread scheduler for Ractor system.

In general, M:N thread scheduler employs N native threads (OS threads)
to manage M user-level threads (Ruby threads in this case).
On the Ruby interpreter, 1 native thread is provided for 1 Ractor
and all Ruby threads are managed by the native thread.

From Ruby 1.9, the interpreter uses 1:1 thread scheduler which means
1 Ruby thread has 1 native thread. M:N scheduler change this strategy.

Because of compatibility issue (and stableness issue of the implementation)
main Ractor doesn't use M:N scheduler on default. On the other words,
threads on the main Ractor will be managed with 1:1 thread scheduler.

There are additional settings by environment variables:

`RUBY_MN_THREADS=1` enables M:N thread scheduler on the main ractor.
Note that non-main ractors use the M:N scheduler without this
configuration. With this configuration, single ractor applications
run threads on M:1 thread scheduler (green threads, user-level threads).

`RUBY_MAX_CPU=n` specifies maximum number of native threads for
M:N scheduler (default: 8).

This patch will be reverted soon if non-easy issues are found.

[Bug #19842]
2023-10-12 14:47:01 +09:00
Nobuyoshi Nakada 50520cc193
[DOC] Missing comment markers 2023-09-27 16:18:05 +09:00
Burdette Lamar bcc160b449
[DOC] RDoc for File::Constants (#8103) 2023-07-23 10:24:19 -04:00
Stan Lo 0db58dd0db [ruby/irb] Declare rdoc as dependency
(https://github.com/ruby/irb/pull/648)

IRB already has several features that rely on rdoc, such as:

- Autocompletion's document dialog
- Autocompletion's `PerfectMatchedProc`
- The `show_doc` command
- Easter egg

And we could use its pager more in the future too. So it makes sense to
declare rdoc as a dependency instead of relying on the one bundled with
Ruby.

https://github.com/ruby/irb/commit/4dffbb1dd3
2023-07-18 13:53:31 +00:00
Burdette Lamar 7f2bd17fad
[DOC] RDoc for dir.c (#8037) 2023-07-07 16:37:36 -04:00
Burdette Lamar 00f9231534
[DOC] RDoc for some of dir.c (#8026) 2023-07-05 09:45:54 -04:00
Nobuyoshi Nakada c803e92d66
Return `errno` as the result instead of the global variable 2023-05-12 18:36:02 +09:00
Nobuyoshi Nakada 05b7bbecc6
Suppress `-Wdiscarded-qualifiers` warning where `fchdir` is unusable 2023-04-04 11:27:43 +09:00
Jeremy Evans c08fe40872 Fix Dir.for_fd call-seq 2023-03-24 16:16:32 -07:00
Jeremy Evans 836e9a192b Add Dir.for_fd
This returns a Dir instance for the given directory file descriptor.
If fdopendir is not supported, this raises NotImplementedError.

Implements [Feature #19347]
2023-03-24 11:18:57 -07:00
Jeremy Evans 3be65f63c7 Add Dir#chdir
This uses Dir.fchdir if supported, or Dir.chdir otherwise.

Implements [Feature #19347]
2023-03-24 11:18:57 -07:00
Jeremy Evans 466ca7ae20 Add Dir.fchdir
This is useful for passing directory file descriptors over UNIX
sockets or to child processes to avoid TOCTOU vulnerabilities.

The implementation follows the Dir.chdir code.

This will raise NotImplementedError on platforms not supporting
both fchdir and dirfd.

Implements [Feature #19347]
2023-03-24 11:18:57 -07:00
Matt Valentine-House 405966e239 Implement declarative references for dir_data_type 2023-03-17 19:20:40 +00:00
S-H-GAMELINKS 040e0c8d67 Reuse NIL_OR_UNDEF_P macro 2022-12-02 01:19:55 +09:00
S-H-GAMELINKS 1f4f6c9832 Using UNDEF_P macro 2022-11-16 18:58:33 +09:00
Hiroshi Shirosaki 329d5424a4 [Bug #19042] Fix Dir.glob brace with '/'
Dir.glob brace pattern with '/' after '**' does not match
paths in recursive expansion process.
We expand braces with '/' before expanding a recursive.

Co-authored-by: Nobuyoshi Nakada <nobu@ruby-lang.org>
2022-10-18 09:18:03 +09:00
Takashi Kokubun 5b21e94beb Expand tabs [ci skip]
[Misc #18891]
2022-07-21 09:42:04 -07:00
Jean Boussier d084585f01 Rename ENCINDEX_ASCII to ENCINDEX_ASCII_8BIT
Otherwise it's way too easy to confuse it with US_ASCII.
2022-07-19 08:48:56 +02:00
Nobuyoshi Nakada a58611dfb1 Allow to just warn as bool expected, without an exception 2022-06-20 19:35:12 +09:00
Yuta Saito 44d0caa1ca dir.c: refresh pathtype when emulating `IFTODT` in `glob_helper`
When using `IFTODT` defined in libc, `dirent.d_type` oriented pathtype
is compatible with `IFTODT(stat.st_mode)`. However they are not
compatible when emulating `IFTODT`, so `glob_helper` has to stat instead
of reusing dirent result by passing unknown pathtype to `glob_helper`.

This is a follow-up fix of 0c90ca4dd0
2022-04-03 01:13:16 +09:00
Yuta Saito 0c90ca4dd0 dir.c: use self-made IFTODT in rb_pathtype_t if available
dir.c defines IFTODT if the system doesn't have it. The macro is used
when comparing with rb_pathtype_t's cases. rb_pathtype_t's cases are
defined by DT_XXX macro if they are available, or defined using IFTODT.
Most POSIX-compatible platforms have both IFTODT and DT_XXX and most of
other platforms like MinGW have neither of them. On those platforms,
DT_XXX-oriented rb_pathtype_t is always compared with values converted
by system's IFTODT, and emulated-IFTODT-oriented rb_pathtype_t is always
compared with values converted by emulated-IFTODT.

However, when IFTODT is *not defined* and DT_XXX is *defined*, like
on wasi-libc, DT_XXX-oriented rb_pathtype_t was compared with values
converted by emulated-IFTODT, and they are not guaranteed to be
compatible.

This patch fixes such a situation by using emulated-IFTODT to define
rb_pathtype_t when either IFTODT or DT_XXX is not available.
2022-03-02 12:50:53 +09:00
Yuta Saito 9033ac3e2c dir.c: ignore ENOTCAPABLE while glob similar to EACCES 2022-01-19 11:19:06 +09:00
Nobuyoshi Nakada 069cca6f74
Negative RBOOL usage 2022-01-01 17:02:04 +09:00
Nobuyoshi Nakada bf97415c02 Removed deprecated Dir.exists? and File.exists? 2021-12-28 18:36:30 +09:00
Nobuyoshi Nakada 89b440bf72
Expect bool as `sort:` option at glob [Feature #18287] 2021-11-18 21:47:18 +09:00
S.H dc9112cf10
Using NIL_P macro instead of `== Qnil` 2021-10-03 22:34:45 +09:00
S-H-GAMELINKS bdd6d8746f Replace RBOOL macro 2021-09-05 23:01:27 +09:00
Jeremy Evans a2592702ae Actually ignore FNM_CASEFOLD flag in Dir.glob
This was already documented as being ignored, but it wasn't being
ignored, causing an issue in a particular case where a UTF-8
pattern was provided and a filename was tested that wasn't valid
UTF-8.

Fixes [Bug #14456]
2021-06-24 12:22:09 -07:00
Nobuyoshi Nakada 4bd538e847
dir.rb: moved class rdoc from dir.c 2021-06-07 20:44:05 +09:00