GithubHelp home page GithubHelp logo

ruby / fileutils Goto Github PK

View Code? Open in Web Editor NEW
61.0 33.0 34.0 731 KB

Several file utility methods for copying, moving, removing, etc.

License: BSD 2-Clause "Simplified" License

Ruby 99.91% Shell 0.09%
ruby fileutils

fileutils's Introduction

FileUtils

Namespace for several file utility methods for copying, moving, removing, etc.

Installation

Add this line to your application's Gemfile:

gem 'fileutils'

And then execute:

$ bundle

Or install it yourself as:

$ gem install fileutils

Usage

Just call FileUtils methods. For example:

FileUtils.mkdir("somedir")
# => ["somedir"]

FileUtils.cd("/usr/bin")
FileUtils.pwd
# => "/usr/bin"

You can find a full method list in the documentation.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/fileutils.

License

The gem is available as open source under the terms of the 2-Clause BSD License.

fileutils's People

Contributors

akr avatar amatsuda avatar ayumin avatar burdettelamar avatar colby-swandale avatar deivid-rodriguez avatar dependabot[bot] avatar drbrain avatar eban avatar eregon avatar headius avatar hsbt avatar jeremyevans avatar k0kubun avatar knu avatar mame avatar marcandre avatar nobu avatar nurse avatar shirosaki avatar shyouhei avatar st0012 avatar stomar avatar tenderlove avatar unak avatar y-yagi avatar yugui avatar yuki24 avatar znz avatar zverok avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

fileutils's Issues

FileUtils.cp_r uses unsupported options on macOS causing issues with symbolic links

The illegal options are -r instead of -R and --remove-destination instead of -f

Details:
While -r does not seem to be an issue on RHEL 9 it is on macOS 11.
Here is an excerpt from the macOS cp man page:

COMPATIBILITY
Historic versions of the cp utility had a -r option. This implementation
supports that option; however, its use is strongly discouraged, as it
does not correctly copy special files, symbolic links, or fifo's.

--remove-destination functionality should be achieved via -f as the mode will change using -R if the existing file has a different mode then the source

FileUtils.install needs integrated mkdir_p

In the Chromebrew project, we're making heavy use of FileUtils.install as a replacement for system 'install...' , but whereas the standalone install binary will automatically create necessary directories for an install, FileUtils.install will not.

Ideally FileUtils.install would internally also run FileUtils.mkdir_p before it installs a file into a destination, so we don't have to manually do such a step before running the FileUtils.install step.

Fileutils 7.0 yanked from rubygems

Hi,
I appreciate you updating the fileutils code-base and look forward to using the new version. I believe yanking 7.0 from rubygems was a bit premature, however. Until Ruby 2.5 is available more readily (especially in the official docker images) I would greatly appreciate it if you could reinstate version 7.0 or lower the build requirements if possible. Thanks!

Publishing docs

This issue is similar to ruby/logger#10. The README currently links to the generated docs from ruby-src, but It would be better to have docs published from this repository instead. As this is now the authoritative repository.

It also means that any new features/changes wouldn't have to wait for a new version of ruby to be released for their documentation to be available.

Please clarify the license

The license file is BSD-2-Clause license, the readme says The gem is available as open source under the terms of the 2-Clause BSD License., but the gemspec specifies Ruby OR BSD-2-Clause. So is the .gemspec wrong or the other places?

Install error

Install error on latest version of Ruby ruby 2.3.1p112 (2016-04-26) [i386-linux-gnu]

=======================================

gem install fileutils
ERROR: Error installing fileutils:
fileutils requires Ruby version >= 2.5.0dev.

Help Wanted - FileUtils.cp(src, dest) suddenly does not work

Here is the error message

/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:1380:in `close': Operation timed out - /Users/administrator/sample.pkg (Errno::ETIMEDOUT)
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:1380:in `open'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:1380:in `copy_file'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:477:in `copy_file'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:394:in `block in cp'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:1558:in `block in fu_each_src_dest'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:1572:in `fu_each_src_dest0'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:1556:in `fu_each_src_dest'
	from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/fileutils.rb:393:in `cp'
	from check_update_build.rb:68:in `copy_pkg_to_master'
	from check_update_build.rb:74:in `each'
	from check_update_build.rb:96:in `<main>'

I can't install fileutils

i get this error when attempting to install fileutils using "gem install fileutils"

root@kali:~# gem install fileutils
Fetching: fileutils-0.7.2.gem (100%)
ERROR:  Error installing fileutils:
	fileutils requires Ruby version >= 2.5.0dev.

`FileUtils.rm_r` with secure option throws `Errno::ENOTEMPTY`

We have been using FileUtils.rm_r with secure option (FileUtils.rm_r(path, :secure => true)) and as per documentation, FileUtils.rm_r removes the root entries recursively, removes the root at the end. However, somehow we are sometimes (now always) getting Errno::ENOTEMPTY ("Directory not empty") exception with FileUtils.rm_r on the environment: Logstash with ruby 2.6.8, Windows OS (x86_64-mswin32). When checked the directory path, there were no files found in it, means empty dir.

:backtrace=>[
	"org/jruby/RubyDir.java:471:in `rmdir'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:1459:in `block in remove_dir1'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:1473:in `platform_support'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:1458:in `remove_dir1'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:1451:in `remove'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:780:in `block in remove_entry'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:1508:in `postorder_traverse'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:778:in `remove_entry'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:698:in `remove_entry_secure'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:628:in `block in rm_r'", "org/jruby/RubyArray.java:1865:in `each'", 
	"C:/Program Files/logstash/vendor/jruby/lib/ruby/stdlib/fileutils.rb:626:in `rm_r'", 
	"C:/Program Files/logstash/vendor/bundle/jruby/2.6.0/gems/logstash-integration-aws-7.0.1/lib/logstash/outputs/s3/temporary_file.rb:54:in `delete!'", 
	"C:/Program Files/logstash/vendor/bundle/jruby/2.6.0/gems/logstash-integration-aws-7.0.1/lib/logstash/outputs/s3.rb:383:in `clean_temporary_file'", "org/jruby/RubyMethod.java:120:in `call'", 
	"C:/Program Files/logstash/vendor/bundle/jruby/2.6.0/gems/logstash-integration-aws-7.0.1/lib/logstash/outputs/s3/uploader.rb:60:in `upload'", 
	"C:/Program Files/logstash/vendor/bundle/jruby/2.6.0/gems/logstash-integration-aws-7.0.1/lib/logstash/outputs/s3/uploader.rb:30:in `block in upload_async'", 
	"C:/Program Files/logstash/vendor/bundle/jruby/2.6.0/gems/concurrent-ruby-1.1.9/lib/concurrent-ruby/concurrent/executor/java_executor_service.rb:79:in `run'"]

We wonder what would be a cause.
Thanks.

CI failed on ruby 2.4 or earlier

The log pointed out that exception_details is missing.
The line does not reach on 2.5 or later, but the problem exists too.
When 2.4 or earlier, undefined method `full_message' for #<RuntimeError: ok>\nDid you mean? message` raised at

err.full_message(highlight: false, order: :top)
.
It causes the test failures.

https://github.com/ruby/fileutils/runs/254688215#step:5:9

===============================================================================
Error: test_assert_output_lines(TestFileUtils): NoMethodError: undefined method `exception_details' for #<TestFileUtils:0x000055abb296cba8>
/home/runner/work/fileutils/fileutils/test/lib/core_assertions.rb:216:in `block in assert_raise'
/home/runner/work/fileutils/fileutils/test/lib/core_assertions.rb:215:in `rescue in assert_raise'
/home/runner/work/fileutils/fileutils/test/lib/core_assertions.rb:226:in `assert_raise'
/home/runner/work/fileutils/fileutils/test/fileutils/test_fileutils.rb:236:in `test_assert_output_lines'
     233:   #
     234: 
     235:   def test_assert_output_lines
  => 236:     assert_raise(MiniTest::Assertion) {
     237:       Timeout.timeout(0.5) {
     238:         assert_output_lines([]) {
     239:           Thread.current.report_on_exception = false
===============================================================================

`FileUtils.rm_rf` doesn't raise when directory could not be removed

I was investigating the cause of some permission issues in bundler, where files were failing to be created due to missing executable permissions in the parent folder.

I was really confused by this because bundler was supposed to freshly create the parent folder by calling FileUtils.rm_rf first on it, and then FileUtils.mkdir_p with proper permissions.

It turns out that if the folder exists, has some file inside, and has no executable permissions, FileUtils.rm_rf will fail to remove the target folder, but it will do it silently without giving any indication that something went wrong.

Compare shell behaviour:

$ mkdir foo; touch foo/bar; chmod -x foo; rm -rf foo; echo $?
rm: cannot remove 'foo/bar': Permission denied
1

with FileUtils

$ ruby -rfileutils -e 'Dir.mkdir "foo"; FileUtils.touch "foo/bar"; FileUtils.chmod "-x", "foo"; FileUtils.rm_rf "foo"'; echo $?
0

None of them actually manages to remove the "foo" folder, but the shell prints a proper error and exits with a proper status when this happens.

JRuby has modifications to fileutils

The version of fileutils.rb in JRuby differs from MRI in a few JRuby-specific ways. We need to incorporate those changes into the fileutils gem, either as a separate file, branched logic, or a completely separate -java gem (in rough order of preference).

We realized this could be an issue while investigating rubygems/bundler#6532. Bundler is now vendoring a copy of fileutils, and their copy also did not reflect our JRuby-specific logic. However they isolate that vendored copy into a separate namespace, so it should not affect normal use of fileutils.

EOPNOTSUPP added to 1.4.1 in CRuby but not released as 1.4.2

The following commit added an additional rescue for EOPNOTSUPP: 4c4edf4

This appears to have been applied directly to CRuby's copy of fileutils.rb, resulting in the shipped version of fileutils there not corresponding to any released gem: ruby/ruby@a19228f

This prevents JRuby from shippinga matching fileutils, since we source all gem-based libraries from the released gems. Currently we are shipping with fileutils 1.4.1, which is what Ruby 2.6.8 reports that it ships, but this change is present in Ruby 2.6.8 and does not exist in the 1.4.1 gem.

Either a 1.4.2 gem should be released based on the CRuby sources, or CRuby should be updated to a version of fileutils.rb that matches a released gem version (like 1.5.0, which appears to be where this commit was first released).

Alternatively, if it seems safe, we could ship JRuby 9.3 (equivalent to Ruby 2.6.8) using the newer fileutils 1.5.0.

FileUtils.install fails with text files in a docker container under Kernel 6.8-rc5 with a Errno::ENOTSUP error..

When in a docker container on an x86_64 host system under kernel 6.8.0-rc5, the following fails:

chronos@mbp113-x86_64 /tmp $ echo "test" > test
chronos@mbp113-x86_64 /tmp $ irb
irb(main):001> FileUtils.install 'test', 'dira/'
/usr/local/lib64/ruby/3.3.0/fileutils.rb:2280:in `copy_stream': Operation not supported - copy_file_range (Errno::ENOTSUP)
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2280:in `block (2 levels) in copy_file'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2279:in `open'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2279:in `block in copy_file'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2278:in `open'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2278:in `copy_file'                                                       from /usr/local/lib64/ruby/3.3.0/fileutils.rb:1078:in `copy_file'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:1629:in `block in install'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2469:in `block in fu_each_src_dest'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2485:in `fu_each_src_dest0'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:2467:in `fu_each_src_dest'
        from /usr/local/lib64/ruby/3.3.0/fileutils.rb:1623:in `install'
        from (irb):1:in `<main>'
        from <internal:kernel>:187:in `loop'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/irb-1.11.0/exe/irb:9:in `<top (required)>'
        from /usr/local/bin/irb:25:in `load'
        from /usr/local/bin/irb:25:in `<main>'
irb(main):002>

Reproducer container:

docker run --platform linux/amd64 --rm --net=host  -v $(pwd):/output -h $(hostname)-x86_64 -it satmandu/crewbuild:nocturne-x86_64.m90

Docker version info:

docker version
Client: Docker Engine - Community
 Version:           25.0.3
 API version:       1.44
 Go version:        go1.21.6
 Git commit:        4debf41
 Built:             Tue Feb  6 21:14:22 2024
 OS/Arch:           linux/amd64
 Context:           default

Server: Docker Engine - Community
 Engine:
  Version:          25.0.3
  API version:      1.44 (minimum version 1.24)
  Go version:       go1.21.6
  Git commit:       f417435
  Built:            Tue Feb  6 21:14:22 2024
  OS/Arch:          linux/amd64
  Experimental:     true
 containerd:
  Version:          1.6.28                                                                                                GitCommit:        ae07eda36dd25f8a1b98dfbf587313b99c0190bb
 runc:
  Version:          1.1.12                                                                                                GitCommit:        v1.1.12-0-g51d5e94
 docker-init:
  Version:          0.19.0                                                                                                GitCommit:        de40ad0

When I am booted into kernel 6.7.5, I don't have any issues. (I have reproduced this on two machines.)

This is an issue even if I manually do a gem install fileutils first:

chronos@mbp113-x86_64 /usr/local/lib/crew/packages $ ruby --version                                                     ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [x86_64-linux-gnu]                                                          chronos@mbp113-x86_64 /usr/local/lib/crew/packages $ gem install fileutils                                              Fetching fileutils-1.7.2.gem                                                                                            Successfully installed fileutils-1.7.2
1 gem installed
chronos@mbp113-x86_64 /usr/local/lib/crew/packages $ cd /tmp
chronos@mbp113-x86_64 /tmp $ echo 'test' > test
chronos@mbp113-x86_64 /tmp $ irb
irb(main):001> require 'fileutils'
=> false
irb(main):002> FileUtils.install 'test', 'dira/'
/usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2280:in `copy_stream': Operation not supported - copy_file_range (Errno::ENOTSUP)
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2280:in `block (2 levels) in copy_file'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2279:in `open'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2279:in `block in copy_file'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2278:in `open'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2278:in `copy_file'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:1078:in `copy_file'                 [
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:1629:in `block in install'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2469:in `block in fu_each_src_dest'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2485:in `fu_each_src_dest0'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:2467:in `fu_each_src_dest'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/fileutils-1.7.2/lib/fileutils.rb:1623:in `install'
        from (irb):2:in `<main>'
        from <internal:kernel>:187:in `loop'
        from /usr/local/lib64/ruby/gems/3.3.0/gems/irb-1.11.0/exe/irb:9:in `<top (required)>'
        from /usr/local/bin/irb:25:in `load'
        from /usr/local/bin/irb:25:in `<main>'
irb(main):003>  


fileutils conflict in Ruby 2.5.1

Debian Linux 8.10 Jessie
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux]
rubygems-update (2.7.7)

Error log:

pp 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:90: warning: already initialized constant FileUtils::VERSION
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:92: warning: previous definition
 of VERSION was here
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:1188: warning: already initialized constant FileUtils::Entry_:
:S_IF_DOOR
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1267: warning: previous definiti
on of S_IF_DOOR was here
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:1446: warning: already initialized constant FileUtils::Entry_:
:DIRECTORY_TERM
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1541: warning: previous definiti
on of DIRECTORY_TERM was here
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:1448: warning: already initialized constant FileUtils::Entry_:
:SYSCASE
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1543: warning: previous definiti
on of SYSCASE was here
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:1501: warning: already initialized constant FileUtils::OPT_TAB
LE
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1596: warning: previous definiti
on of OPT_TABLE was here
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:1555: warning: already initialized constant FileUtils::LOW_MET
HODS
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1650: warning: previous definiti
on of LOW_METHODS was here
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/2.5.0/fileutils.rb:1562: warning: already initialized constant FileUtils::METHODS
App 8789 output: /home/ruby/.rbenv/versions/2.5.1/lib/ruby/gems/2.5.0/gems/fileutils-1.1.0/lib/fileutils.rb:1657: warning: previous definiti
on of METHODS was here

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.