Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add temporary directory creation on Dir class #2911

Closed
wants to merge 4 commits into from
Closed

Add temporary directory creation on Dir class #2911

wants to merge 4 commits into from

Conversation

marceloboeira
Copy link
Contributor

Usage:

Without prefix:

puts Dir.mktmpdir
# => /tmp/c.a39bF4

With prefix:

puts Dir.mktmpdir("foo")
# => /tmp/foo.a39bF4

The solution uses the LibC.mkdtemp bind.
The mkdtemp function creates a directory with a unique name. If it
succeeds, it overwrites template with the name of the directory, and
returns template. As with mktemp and mkstemp, template should be a
string ending with ‘XXXXXX’.

Reference:

Dir class assumes the responsability of resolving the system tmpdir from
the Tempfile class.

Reference:
  - http://rxr.whitequark.org/mri/source/lib/tmpdir.rb
@marceloboeira marceloboeira changed the title Add temporary directory file creation on Dir class Add temporary directory creation on Dir class Jun 25, 2016
@marceloboeira
Copy link
Contributor Author

@jhass for some reason the tests are failing only on linux builds, could you take a look to check what I'm doing wrong?

original_tmpdir = ENV["TMPDIR"]?
ENV["TMPDIR"] = "/my/tmp"
Dir.tmpdir.should eq("/my/tmp")
ENV["TMPDIR"] = original_tmpdir if original_tmpdir
Copy link
Member

@jhass jhass Jun 25, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If TMPDIR wasn't set, this keeps it set at "/my/tmp".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I'll fix that!

@marceloboeira
Copy link
Contributor Author

marceloboeira commented Jun 25, 2016

I was trying to create a better interface to work with temporary directories, something like:

Dir.mktmpdir do |dir|
....
end

On which the tmp dir only exists within the block lifecycle. However for that I'll need a Dir.rm_rf function, which I'll implement later on another pull request.

@asterite
Copy link
Member

There's FileUtils.rm_r

@marceloboeira
Copy link
Contributor Author

@asterite thanks, I didn't remembered to check FileUtils.

@marceloboeira
Copy link
Contributor Author

@jhass @asterite done, can you check it?

@asterite
Copy link
Member

Thanks! Before merging this, I would try to think of a different name, mktmpdir is pretty cryptic and reminiscent of libc.

The mkdtemp function creates a directory with a unique name. If it
succeeds, it overwrites template with the name of the directory, and
returns template. As with mktemp and mkstemp, template should be a
string ending with ‘XXXXXX’.

The directory is created using mode 0700

References:
  - http://www.gnu.org/software/libc/manual/html_node/Temporary-Files.html
  - http://ruby-doc.org/stdlib-2.0.0/libdoc/tmpdir/rdoc/Dir.html
@marceloboeira
Copy link
Contributor Author

marceloboeira commented Jun 25, 2016

@asterite I was trying to keep Ruby's syntax, I would happily take your name suggestions.

@luislavena
Copy link
Contributor

luislavena commented Jun 28, 2016

I'm not sure if is good that we add FileUtils as dependency for Dir, which is part of prelude and means that FileUtils will be parsed, analyzed and potentially included as part of every compiled program.

Perhaps this helper method can be treated in similar way as Tempfile? (ala: a separate class).

Just a thought.

@marceloboeira
Copy link
Contributor Author

marceloboeira commented Jun 28, 2016

@luislavena you are absolutely correct, I was actually wondering why the paths were being concatenated that way (a + b...) but considering your comment, which haven't crossed my mind until now, makes sense.

However I think we should find a more elegant way of doing this.

Either way, for now I will fix this.

@asterite
Copy link
Member

Ideally we should have a way to do rm_r in Dir, with a better name, and have FileUilts.rm_r use that method, like in my comment here.

@marceloboeira
Copy link
Contributor Author

@asterite can I refactor this and move rm_r to Dir ?

As discussed on #2911
@asterite
Copy link
Member

asterite commented Jul 2, 2016

@marceloboeira Ideally I'd like this method to be in Dir and in FileUtils. FileUtils would have methods names similar to bash functions, but Dir would have less cryptic names.

Problem is, I don't know what a good name for Dir is in this case.

@ysbaddaden
Copy link
Contributor

What about:

module Dir
  def self.delete(path, *, recursive= false)
  end
end

@marceloboeira
Copy link
Contributor Author

Could you accept this pull request and we rename the methods later? I really would like this feature for some projects and I would not like to "monkey patch". Also I think this is something important for several projects, not only mine.

@asterite
Copy link
Member

asterite commented Jul 6, 2016

@marceloboeira There's nothing wrong with "monkey patching" until this gets merged with time

@grosser
Copy link
Contributor

grosser commented Oct 16, 2016

so the only thing missing here is rename rm_r to delete ... or what is ?

... would love to use this feature :)

@marceloboeira
Copy link
Contributor Author

@grosser not sure anymore. I need this feature, I believe I will create a temp shard to use it while I wait for it to be merged.

marceloboeira added a commit to marceloboeira/tmpdir.cr that referenced this pull request Nov 1, 2016
TL;DR - This is a monkey-patch to create temporary directories with
Crystal standard library.

The PR is open, but still solving some issues.
crystal-lang/crystal#2911

The mkdtemp function creates a directory with a unique name. If it
succeeds, it overwrites template with the name of the directory, and
returns template. As with mktemp and mkstemp, template should be a
string ending with ‘XXXXXX’.

The directory is created using mode 0700

References:
  - http://www.gnu.org/software/libc/manual/html_node/Temporary-Files.html
  - http://ruby-doc.org/stdlib-2.0.0/libdoc/tmpdir/rdoc/Dir.html
marceloboeira added a commit to marceloboeira/tmpdir.cr that referenced this pull request Nov 1, 2016
TL;DR - This is a monkey-patch to create temporary directories with
Crystal standard library.

The PR is open, but still solving some issues.
crystal-lang/crystal#2911

The mkdtemp function creates a directory with a unique name. If it
succeeds, it overwrites template with the name of the directory, and
returns template. As with mktemp and mkstemp, template should be a
string ending with ‘XXXXXX’.

The directory is created using mode 0700

References:
  - http://www.gnu.org/software/libc/manual/html_node/Temporary-Files.html
  - http://ruby-doc.org/stdlib-2.0.0/libdoc/tmpdir/rdoc/Dir.html
@sdogruyol
Copy link
Member

This is pretty useful, especially for writing tests 👍

bramswenson pushed a commit to bramswenson/crystal that referenced this pull request Mar 2, 2017
@bramswenson bramswenson mentioned this pull request Mar 2, 2017
@bramswenson
Copy link

bramswenson commented Mar 2, 2017

I've submitted a PR that is simply a rebase of this one to master. #4096

@marceloboeira
Copy link
Contributor Author

@bramswenson thx!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

8 participants