summaryrefslogtreecommitdiff
path: root/file.c
AgeCommit message (Collapse)Author
2025-09-12merge revision(s) 7a05dbc47831a655a1ef8a1635f88292acd325da: [Backport #21561]Takashi Kokubun
File.dirname: return consistent encoding for `"."` [Bug #21561] It's preferable if the method is consistent in the encoding in the returned string.
2024-10-10[Bug #20631] Workaround for macOS 15.0 fork crashYuta Saito
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. Notes: Merged: https://github.com/ruby/ruby/pull/11849
2024-10-09Fix spellingJohn Bampton
Notes: Merged: https://github.com/ruby/ruby/pull/11835
2024-10-05Introduce `rb_io_blocking_region` which takes `struct rb_io` argument. (#11795)Samuel Williams
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`. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2024-09-30Free a buffer allocated by realpathYusuke Endoh
8350b48cfa7d344d9e2dc9748c26607c1b89d7df introduced a memory leak bug. Will fix [Bug #20773] `loop { File.realpath("foo") }` caused memory leak. Notes: Merged: https://github.com/ruby/ruby/pull/11736
2024-09-26Extract `mutable_CFString_new`Nobuyoshi Nakada
From duplicate code in `rb_CFString_class_initialize_before_fork` and `rb_str_append_normalized_ospath`. Notes: Merged: https://github.com/ruby/ruby/pull/11695
2024-09-26Import patches for old macOS from MacPortsNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/11690
2024-09-12Release GVL for get{pwnam,pwuid,grgid,grnam}_r calls in process.cJeremy Evans
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]. Notes: Merged: https://github.com/ruby/ruby/pull/11202
2024-08-22Check getlogin return value before passing to strcasecmpJeremy Evans
getlogin can return NULL, and this can avoid a segfault in that case. Mentioned as an issue in comment to bug 20586. Notes: Merged: https://github.com/ruby/ruby/pull/11427
2024-07-18Fix `utimesat` availability conditionNobuyoshi Nakada
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-14Release GVL around system calls in dir.cJeremy Evans
* 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-08Doc fixesBurdetteLamar
2024-03-05[DOC] fix some commentscui fliter
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-02Make `File#chown` unblockingNobuyoshi Nakada
2024-03-02Make `File#chmod` unblockingNobuyoshi Nakada
2024-02-23Remove unneeded RUBY_FUNC_EXPORTEDPeter Zhu
2024-02-20[DOC] Fix links in doc (#9970)Burdette Lamar
2024-02-15Do not include a backtick in error messages and backtracesYusuke Endoh
[Feature #16495]
2024-02-01[DOC] Pretty tables for Kernel#test doc (#9751)Burdette Lamar
2024-01-30Fix memory leak in File.expand_pathPeter Zhu
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-24Define `IO_WITHOUT_GVL` macroNobuyoshi Nakada
2024-01-08[DOC] Escape File in documentationPeter Zhu
2023-12-25Markdown table for #flockBurdetteLamar
2023-12-24[DOC] Fix markup in HTMLNobuyoshi Nakada
Inside HTML is not markdown.
2023-12-23[DOC] Clean up doc for File#flock (#9332)Burdette Lamar
2023-12-18[DOC] Add links from `File::Constants` to `File` constantsNobuyoshi Nakada
2023-12-06[DOC] Fixed document positionNobuyoshi Nakada
Method documentation must be placed immediately before each implementation, without any other functions or preprocessor directives.
2023-11-22Embed File::Stat objectsJean Boussier
They are very short lived and fit in a 160B slot.
2023-11-17Fix File.directory? doc hidding File::Stat#directory? docDana Sherson
Now the documentation that was already in the codebase for `File::Stat#directory?` shows up.
2023-09-27[DOC] Missing comment markersNobuyoshi Nakada
2023-09-24[DOC] Correction for doc guide + TOC fix in File (#8505)Burdette Lamar
2023-08-31[DOC] Link fix (#8340)Burdette Lamar
Notes: Merged-By: peterzhu2118 <peter@peterzhu.ca>
2023-08-26Introduce `at_char_boundary` functionNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/8296
2023-08-12[DOC] Don't suppress autolinksBurdetteLamar
Notes: Merged: https://github.com/ruby/ruby/pull/8213
2023-07-24[rubygems/rubygems] Bundler: Update bundle-outdated(1) manKentaro Takeyama
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-23[DOC] RDoc for File::Constants (#8103)Burdette Lamar
Notes: Merged-By: peterzhu2118 <peter@peterzhu.ca>
2023-07-10Use `File::NULL` instead of hard coded null device namesNobuyoshi Nakada
Notes: Merged: https://github.com/ruby/ruby/pull/8050
2023-06-28[DOC] Fixes for link fragments (#7981)Burdette Lamar
Notes: Merged-By: peterzhu2118 <peter@peterzhu.ca>
2023-06-01Hide the usage of `rb_io_t` where possible. (#7880)Samuel Williams
This retries the compatible parts of the previously reverted PR so we can continue to update related code without breaking backwards compatibility. Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-06-01Revert "Hide most of the implementation of `struct rb_io`. (#6511)"NARUSE, Yui
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-05-30Hide most of the implementation of `struct rb_io`. (#6511)Samuel Williams
* 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> Notes: Merged-By: ioquatix <samuel@codeotaku.com>
2023-03-22Should not reach end of non-void functionNobuyoshi Nakada
2023-03-20Fix the macro for conditionNobuyoshi Nakada
2023-02-27Revert "reuse open(2) from rb_file_load_ok on POSIX-like system"Takashi Kokubun
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-26reuse open(2) from rb_file_load_ok on POSIX-like systemEric Wong
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-15file.c: rb_file_load_ok: GC+retry on EMFILE/ENFILE/ENOMEMEric Wong
`require' should make a best effort to avoid failure on recoverable resource exhaustion errors.
2023-02-03Enable write barriers on File::StatJean Boussier
It holds no reference, so no changes needed. Notes: Merged: https://github.com/ruby/ruby/pull/7239
2023-01-30* expand tabs. [ci skip]git
Please consider using misc/expand_tabs.rb as a pre-commit hook.
2023-01-30file.c: add some endif labels where appropriateEric Wong
"Appropriate" for me being ifdefs which: 1) span enough lines to be hard-to-follow (I need big fonts to see) 2) nested within other ifdefs, or within large functions (I have a short attention span) rb_file_expand_path_internal is the prime example of a large function I struggle to understand due to the amount of platform-specific ifdefs. Hopefully this helps others with poor vision and/or short attention spans.
2022-12-09[DOC] Fix false link to ::DataAlan Wu