906 Commits

Author SHA1 Message Date
Samuel Williams
20a1c1dc6b
Ensure struct rb_io is passed through to thread.c. (#13134) 2025-04-19 09:55:16 +09:00
Yuta Saito
b4f74e24d9 [wasm] Fallback to emulated realpath on wasi-libc realpath ENOTSUP
wasi-libc 22 and later support realpath(3)
(https://github.com/WebAssembly/wasi-libc/pull/463) but the underlying
host syscall may return ENOTSUP. This is typically the case when using
incomplete WASI polyfills on web browsers. For such cases, we should
fallback to the emulated realpath.
2025-04-18 07:47:49 +09:00
Alan Wu
6fbc32b5d0 GCC defines __linux__, not __LINUX__
Fixes: 2c6512fe67a275ce4d251f3c5d304bc523597f42
2025-04-01 16:25:01 -04:00
Earlopain
7c88cbb4a6 [Feature #20971] Deprecate rb_path_check
https://bugs.ruby-lang.org/issues/20971
2025-03-13 18:43:48 +09:00
tomoya ishida
2d3d7a74e9
[DOC] Fix wrong call-seq format (#12662) 2025-01-29 21:53:00 +09:00
Nobuyoshi Nakada
e433e6515e
[DOC] Exclude 'Class' and 'Module' from RDoc's autolinking 2025-01-02 12:36:06 +09:00
Yuta Saito
c77f0b9134 [Bug #20631] Workaround for macOS 15.0 fork crash
macOS 15.0 24A5331b seems to have a weird issue that
`CFStringCreateWithBytesNoCopy` does not return `NSTaggedPointerString`
instance for the first call. This issue is fixed in macOS 15.1 but we
need to workaround it for macOS 15.0.
2024-10-10 13:40:59 +09:00
John Bampton
3fc1495c30 Fix spelling 2024-10-09 07:14:44 +09:00
Samuel Williams
c50298d7d4
Introduce rb_io_blocking_region which takes struct rb_io argument. (#11795)
This does not change any actual behaviour, but provides a choke point for blocking IO operations.

* Update `IO::Buffer` to use `rb_io_blocking_region`.

* Update `File` to use `rb_io_blocking_region`.

* Update `IO` to use `rb_io_blocking_region`.
2024-10-05 15:10:12 +13:00
Yusuke Endoh
b93c51c114 Free a buffer allocated by realpath
8350b48cfa7d344d9e2dc9748c26607c1b89d7df introduced a memory leak bug.

Will fix [Bug #20773]
`loop { File.realpath("foo") }` caused memory leak.
2024-09-30 20:35:46 +09:00
Nobuyoshi Nakada
2a65f4c907
Extract mutable_CFString_new
From duplicate code in `rb_CFString_class_initialize_before_fork` and
`rb_str_append_normalized_ospath`.
2024-09-26 18:17:13 +09:00
Nobuyoshi Nakada
8350b48cfa
Import patches for old macOS from MacPorts 2024-09-26 10:32:30 +09:00
Jeremy Evans
ad761ad2d0 Release GVL for get{pwnam,pwuid,grgid,grnam}_r calls in process.c
Do not release GVL around get{pwuid,pwnam,grgid,grnam} calls,
as doing so is not thread-safe.  Another C extension could have
a concurrent call, and derefencing the returned pointer from
these calls could result in a segfault.

Have rb_home_dir_of call rb_getpwdirnam_for_login if available,
so it can use getpwnam_r and release GVL in a thread-safe manner.

This is related to GVL releasing work in [Bug #20587].
2024-09-12 07:24:02 -07:00
Jeremy Evans
ae886e0c83 Check getlogin return value before passing to strcasecmp
getlogin can return NULL, and this can avoid a segfault
in that case.

Mentioned as an issue in comment to bug 20586.
2024-08-22 11:20:47 +09:00
Nobuyoshi Nakada
d11d615ba6
Fix utimesat availability condition
As `__has_attribute` macro is always defined in internal/compilers.h,
gcc warns `-Wunguarded-availability-new` as unknown option.  Check if
the warning option is usable instead.
2024-07-18 11:20:17 +09: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
BurdetteLamar
a57b4340d0 Doc fixes 2024-07-08 14:35:29 -04:00
cui fliter
226a889dc7
[DOC] fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 18:50:47 +09:00
Nobuyoshi Nakada
5a4cd732f0
Make File#chown unblocking 2024-03-02 14:37:57 +09:00
Nobuyoshi Nakada
061c684084
Make File#chmod unblocking 2024-03-02 14:37:53 +09:00
Peter Zhu
df5b8ea4db Remove unneeded RUBY_FUNC_EXPORTED 2024-02-23 10:24:21 -05:00
Burdette Lamar
d4b4b53bc0
[DOC] Fix links in doc (#9970) 2024-02-20 16:10:52 -05:00
Yusuke Endoh
25d74b9527 Do not include a backtick in error messages and backtraces
[Feature #16495]
2024-02-15 18:42:31 +09:00
Burdette Lamar
7b93e65e9f
[DOC] Pretty tables for Kernel#test doc (#9751) 2024-02-01 17:55:13 -05:00
Peter Zhu
dddef93bbd Fix memory leak in File.expand_path
File.expand_path leaks the dir if the encodings are not compatible.

For example:

    Encoding.default_external = Encoding::UTF_16BE

    10.times do
      100_000.times do
        File.expand_path("./a")
      rescue
      end

      puts `ps -o rss= -p #{$$}`
    end

Before:

    12288
    15488
    18656
    21872
    25056
    28240
    31392
    34688
    37856
    41056

After:

    9680
    9728
    9728
    9792
    9792
    9792
    9792
    9792
    9792
2024-01-30 16:43:44 -05:00
Nobuyoshi Nakada
d86c4e553e
Define IO_WITHOUT_GVL macro 2024-01-24 20:51:50 +09:00
Peter Zhu
85a7da742a [DOC] Escape File in documentation 2024-01-08 11:30:29 -05:00
BurdetteLamar
45b0e77881 Markdown table for #flock 2023-12-25 22:10:20 -05:00
Nobuyoshi Nakada
0fef890b4f
[DOC] Fix markup in HTML
Inside HTML is not markdown.
2023-12-24 13:59:34 +09:00
Burdette Lamar
688a1314e6
[DOC] Clean up doc for File#flock (#9332) 2023-12-23 16:08:26 -05:00
Nobuyoshi Nakada
9b00b2bfb2
[DOC] Add links from File::Constants to File constants 2023-12-18 20:17:44 +09:00
Nobuyoshi Nakada
ca8733daaf
[DOC] Fixed document position
Method documentation must be placed immediately before each
implementation, without any other functions or preprocessor
directives.
2023-12-06 19:56:38 +09:00
Jean Boussier
4f031a745d Embed File::Stat objects
They are very short lived and fit in a 160B slot.
2023-11-22 14:52:32 +01:00
Dana Sherson
ef72970a04
Fix File.directory? doc hidding File::Stat#directory? doc
Now the documentation that was already in the codebase for
`File::Stat#directory?` shows up.
2023-11-17 16:41:30 -05:00
Nobuyoshi Nakada
50520cc193
[DOC] Missing comment markers 2023-09-27 16:18:05 +09:00
Burdette Lamar
36cd2b2a52
[DOC] Correction for doc guide + TOC fix in File (#8505) 2023-09-24 17:57:21 -04:00
Burdette Lamar
b8e782c1b5
[DOC] Link fix (#8340) 2023-08-31 10:27:10 -04:00
Nobuyoshi Nakada
00ac3a64ba Introduce at_char_boundary function 2023-08-26 08:58:02 +09:00
BurdetteLamar
546c5cfe4c [DOC] Don't suppress autolinks 2023-08-12 13:04:04 -04:00
Kentaro Takeyama
f72ddd92be [rubygems/rubygems] Bundler: Update bundle-outdated(1) man
Updated output examples to current format since the output format has changed since version 2.2.0.dev

https://github.com/rubygems/rubygems/commit/48d98f906a
2023-07-24 23:58:06 +00:00
Burdette Lamar
bcc160b449
[DOC] RDoc for File::Constants (#8103) 2023-07-23 10:24:19 -04:00
Nobuyoshi Nakada
c8d0470bb0
Use File::NULL instead of hard coded null device names 2023-07-10 19:21:47 +09:00
Burdette Lamar
6528cf9fcf
[DOC] Fixes for link fragments (#7981) 2023-06-28 09:05:43 -04:00
Samuel Williams
a218ed5692
Hide the usage of rb_io_t where possible. (#7880)
This retries the compatible parts of the previously reverted PR so we can continue to update related code without breaking backwards compatibility.
2023-06-01 14:23:30 +09:00
NARUSE, Yui
85dcc4866d Revert "Hide most of the implementation of struct rb_io. (#6511)"
This reverts commit 18e55fc1e1ec20e8f3166e3059e76c885fc9f8f2.

fix [Bug #19704]
https://bugs.ruby-lang.org/issues/19704
This breaks compatibility for extension libraries. Such changes
need a discussion.
2023-06-01 08:43:22 +09:00
Samuel Williams
18e55fc1e1
Hide most of the implementation of struct rb_io. (#6511)
* Add rb_io_path and rb_io_open_descriptor.

* Use rb_io_open_descriptor to create PTY objects

* Rename FMODE_PREP -> FMODE_EXTERNAL and expose it

FMODE_PREP I believe refers to the concept of a "pre-prepared" file, but
FMODE_EXTERNAL is clearer about what the file descriptor represents and
aligns with language in the IO::Buffer module.

* Ensure that rb_io_open_descriptor closes the FD if it fails

If FMODE_EXTERNAL is not set, then it's guaranteed that Ruby will be
responsible for closing your file, eventually, if you pass it to
rb_io_open_descriptor, even if it raises an exception.

* Rename IS_EXTERNAL_FD -> RUBY_IO_EXTERNAL_P

* Expose `rb_io_closed_p`.

* Add `rb_io_mode` to get IO mode.

---------

Co-authored-by: KJ Tsanaktsidis <ktsanaktsidis@zendesk.com>
2023-05-30 10:02:40 +09:00
Nobuyoshi Nakada
6cc8eb4daa
Should not reach end of non-void function 2023-03-22 18:53:11 +09:00
Nobuyoshi Nakada
1aa2555044
Fix the macro for condition 2023-03-20 17:28:24 +09:00
Takashi Kokubun
526111290b Revert "reuse open(2) from rb_file_load_ok on POSIX-like system"
This reverts commit 35136e1e9c232ad7a03407b992b2e86b6df43f63.

test-spec has been failing since this revision.

.github/workflows/compilers.yml:82
https://github.com/ruby/ruby/actions/runs/4276884159/jobs/7445299562

```
env:
  # Minimal flags to pass the check.
  default_cc: 'gcc-11 -fcf-protection -Wa,--generate-missing-build-notes=yes'
  optflags: '-O2'
  LDFLAGS: '-Wl,-z,now'
  # FIXME: Drop skipping options
  # https://bugs.ruby-lang.org/issues/18061
  # https://sourceware.org/annobin/annobin.html/Test-pie.html
  TEST_ANNOCHECK_OPTS: "--skip-pie --skip-gaps"
```

Failure:

```
  1)
  An exception occurred during: Kernel#require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:317
  Kernel#require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
  LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'

  2)
  An exception occurred during: Kernel#require ($LOADED_FEATURES) stores an absolute path
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:330
  Kernel#require ($LOADED_FEATURES) stores an absolute path ERROR
  LeakError: Closed file descriptor: 8
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'

  3)
  An exception occurred during: Kernel#require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:535
  Kernel#require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded ERROR
  LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'

  4)
  An exception occurred during: Kernel#require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:551
  Kernel#require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded ERROR
  LeakError: Leaked file descriptor: 9 : #<File:../code/load_fixture.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'

  5)
  An exception occurred during: Kernel#require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:563
  Kernel#require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required ERROR
  LeakError: Closed file descriptor: 8
  Closed file descriptor: 9
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:5:in `<top (required)>'

  6)
  An exception occurred during: Kernel.require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:317
  Kernel.require (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
  LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'

  7)
  An exception occurred during: Kernel.require ($LOADED_FEATURES) stores an absolute path
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:330
  Kernel.require ($LOADED_FEATURES) stores an absolute path ERROR
  LeakError: Closed file descriptor: 8
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'

  8)
  An exception occurred during: Kernel.require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:535
  Kernel.require ($LOADED_FEATURES) does not load a non-canonical path for a file already loaded ERROR
  LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'

  9)
  An exception occurred during: Kernel.require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:551
  Kernel.require ($LOADED_FEATURES) does not load a ../ relative path for a file already loaded ERROR
  LeakError: Leaked file descriptor: 9 : #<File:../code/load_fixture.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'

  10)
  An exception occurred during: Kernel.require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required
  /__w/ruby/ruby/src/spec/ruby/core/kernel/shared/require.rb:563
  Kernel.require ($LOADED_FEATURES) complex, enumerator, rational, thread, ruby2_keywords are already required ERROR
  LeakError: Closed file descriptor: 8
  Closed file descriptor: 9
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_spec.rb:23:in `<top (required)>'

  11)
  An exception occurred during: Kernel#require_relative with a relative path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:197
  Kernel#require_relative with a relative path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
  LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:4:in `<top (required)>'

  12)
  An exception occurred during: Kernel#require_relative with a relative path ($LOADED_FEATURES) stores an absolute path
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:205
  Kernel#require_relative with a relative path ($LOADED_FEATURES) stores an absolute path ERROR
  LeakError: Closed file descriptor: 8
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:4:in `<top (required)>'

  13)
  An exception occurred during: Kernel#require_relative with an absolute path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:399
  Kernel#require_relative with an absolute path (file extensions) does not load a C-extension file if a complex-extensioned .rb file is already loaded ERROR
  LeakError: Leaked file descriptor: 8 : #<File:/__w/ruby/ruby/src/spec/ruby/fixtures/code/load_fixture.ext.rb>
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:277:in `<top (required)>'

  14)
  An exception occurred during: Kernel#require_relative with an absolute path ($LOAD_FEATURES) stores an absolute path
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:407
  Kernel#require_relative with an absolute path ($LOAD_FEATURES) stores an absolute path ERROR
  LeakError: Closed file descriptor: 8
  /__w/ruby/ruby/src/spec/ruby/core/kernel/require_relative_spec.rb:277:in `<top (required)>'
```
2023-02-27 09:24:45 -08:00
Eric Wong
35136e1e9c reuse open(2) from rb_file_load_ok on POSIX-like system
When loading Ruby source files, we can save the result of
successful opens as open(2)/openat(2) are a fairly expensive
syscalls.  This also avoids a time-of-check-to-time-of-use
(TOCTTOU) problem.

This reduces open(2) syscalls during `require'; but should be
most apparent when users have a small $LOAD_PATH.  Users with
large $LOAD_PATH will benefit less since there'll be more
open(2) failures due to ENOENT.

With `strace -c -e openat ruby -e exit' under Linux, this
results in a ~14% reduction of openat(2) syscalls
(glibc uses openat(2) to implement open(2)).

 % time     seconds  usecs/call     calls    errors syscall
 ------ ----------- ----------- --------- --------- ----------------
   0.00    0.000000           0       296       110 openat
   0.00    0.000000           0       254       110 openat

Additionally, the introduction of `struct ruby_file_load_state'
may make future optimizations more apparent.

This change cannot benefit binary (.so) loading since the
dlopen(3) API requires a filename and I'm not aware of an
alternative that takes a pre-existing FD.  In typical
situations, Ruby source files outnumber the mount of .so
files.
2023-02-26 20:39:41 +00:00