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

Feature/mktmpdir #4096

Closed
wants to merge 6 commits into from
Closed

Conversation

bramswenson
Copy link

This is simply a rebase of #2911 to current master (03-01-2017)

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

Reference:
  - http://rxr.whitequark.org/mri/source/lib/tmpdir.rb
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

👍

src/dir.cr Outdated
end

# Creates a new temporary directory within the lifecycle
# of the given block and destroys it, and its content, later on.
Copy link
Contributor

Choose a reason for hiding this comment

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

...and its content, when the block returns

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

src/dir.cr Outdated
# Dir.tmpdir # => "/tmp"
# ```
def self.tmpdir
unless tmpdir = ENV["TMPDIR"]?
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a bit confusing, how about

tmpdir = ENV["TMPDIR"]? || "/tmp"

Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@marceloboeira
Copy link
Contributor

@bramswenson do you want to make the changes? because if I would require access to your fork/branch.

@bramswenson
Copy link
Author

@marceloboeira @RX14 Done! Thanks!

@marceloboeira
Copy link
Contributor

@bramswenson great!

# Dir.rm_r("dir")
# Dir.rm_r("file.cr")
# ```
def self.rm_r(path : String)
Copy link
Member

Choose a reason for hiding this comment

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

This belongs to FileUtils, not Dir

Copy link

Choose a reason for hiding this comment

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

Dir can rm_r , FileUtils can rm_r too .
just like String can have add method , Number can add too .
Right ?

Copy link
Contributor

Choose a reason for hiding this comment

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

@sevk stop trolling please.

@asterite
Copy link
Member

asterite commented Mar 3, 2017

@bramswenson Thank you for this!

In general the PR looks very good. However:

  • I'm not sure about adding Dir.rm_r. In general we have simple methods in File and Dir, and more complex methods in FileUtils. FileUtils has methods that you can find in bash, like rm_r or mkdir_p.
  • I'm also not sure about Dir.tmpdir, but I think it's OK (similar to Ruby, related to Dir and not Tempfile), but this is a small breaking change and we should note it.

I'm all the time unsure about File, Dir, FileUtils and where to put things. Maybe things are too complex/messy right now. I'll discuss this with the team, maybe we can just put all methods in File and Dir and remove FileUtils.

@asterite
Copy link
Member

asterite commented Mar 3, 2017

For now we can merge your change if it doesn't introduce breaking changes (Use Tmpfile.dirname instead of Dir.tmpdir and keeps FileUtils.rm_r instead of Dir.rm_r)). That's at least if we want this included in the next patch release.

@bramswenson
Copy link
Author

@asterite Thanks for the kind words, but I did nothing but the rebase to master out of selfish desire to use this feature ;) @marceloboeira Did the actual work and there was reasonable discussion about the move between Dir and FileUtils in #2911 between @marceloboeira and @RX14. I really don't have a dog in that fight and will be happy to see the feature wherever you all decide is most appropriate. Thanks!

@bramswenson
Copy link
Author

I will say that the removal of FileUtils in preference for File and Dir makes sense to me. I also think these methods should closely resemble their unix counterparts in naming as unix file modification executable names are common knowledge (or should be). But that is just my 2 cents.

@marceloboeira
Copy link
Contributor

I didn't have time to take a look at the comments yet folks, but I will do it soon. @bramswenson thanks for the invitation to your repository and the efforts on making it happen. 👍

@bew
Copy link
Contributor

bew commented Sep 1, 2017

I was about to (suggest to) add this!
@bramswenson, will you work on this anytime soon ? (I'd really like something like that to refactor some compiler specs)

About naming I was thinking, since we already have the Tempfile class, we could have Tempdir ?
And put the two in a Temporary module with a simple API like:

Temporary::File.new # gives a file
Temporary::Dir.new # gives a dir

Temporary::Dir.open do |dir|
  # do something with the `dir`
  puts dir.path
end # the block ensures that the temp dir is deleted

Temporary.create_name("foo.XXX") # gives a (file/dir)name following a pattern

# etc..

@RX14
Copy link
Contributor

RX14 commented Sep 1, 2017

I think we shouldn't have "tempdir" and "tempfile" classes, just methods on Dir and File which create tempoary versions of themselves. Why does Tempfile currently not inherit File? It makes little sense to me.

@bew
Copy link
Contributor

bew commented Sep 1, 2017

Why does Tempfile currently not inherit File? It makes little sense to me.

I thought the same.

@marceloboeira
Copy link
Contributor

@RX14 any updates here?

@RX14
Copy link
Contributor

RX14 commented Sep 14, 2018

@marceloboeira Tempfile was removed in #6485. We still need to decide what to do about FileUtils, I say that most of it's methods belong on Path once that is merged.

@j8r
Copy link
Contributor

j8r commented Jan 3, 2021

Too old, I think should be closed now. A new PR can be opened if wanted.

As now, to create a temp dir:

dir = File.tempname 
Dir.mkdir dir
begin
  # do things
ensure
  FileUtils.rm_rf dir
end

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

10 participants