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

FileUtils: Add a recursive chmod_r and chown_r #5230

Closed
wants to merge 2 commits into from

Conversation

j8r
Copy link
Contributor

@j8r j8r commented Nov 1, 2017

Implements a recursive chown and chmod that applies to all subfiles and subdirectories at a given path.

@j8r j8r changed the title FilleUtils: Add a recursive chmod_r and chown_r FileUtils: Add a recursive chmod_r and chown_r Nov 1, 2017
@straight-shoota
Copy link
Member

I'm not sure if these dedicated methods make sense... it's just quite easy to get this done with the non-recursive variants.
With the new glob implementation from #5179 the overhead is really negligible.

Dir["path/**"] { |path| File.chmod(path, mode) }


describe "chown_r" do
it "change the owner of the directory recursively" do
data_path = File.join(__DIR__, "data")
Copy link
Member

Choose a reason for hiding this comment

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

If you don't plan on actually running the method, there is no point in setting up the directories.

@j8r
Copy link
Contributor Author

j8r commented Nov 1, 2017

@straight-shoota I'm thinking now if it stiil makes sense to have FileUtils in the stdlib - there are some non essentials methods like aliases to Dir and File, and others that can be done via the new glob implementation. But if I undestand correctly the implementation, we can't modify the root directory itself this way but the sub directories/files?

@lachlan
Copy link
Contributor

lachlan commented Nov 1, 2017

I realise there is already a precedence for using different method names for different modes in FileUtils, such as rm_r, but could we consider using either method overloads or default values which I feel would be a cleaner API.

So instead of:

def chmod_r(path, mode : Int)
  ...
end

We'd have:

def chmod(path, mode : Int, recurse : Bool = false)
  ...
end

I'd also prefer methods to be named for the conceptual action they take, rather than named after the historic way to take this action in an operating system's shell, and to use words rather than abbreviations or acronyms. So instead of rm, why not remove or delete? Instead of chmod, why not grant or something?

More generally, the use of _suffix method naming in Crystal to provide different ways of taking the same action does not seem idiomatic. Ruby may do this due to the lack of method overloading, however Crystal does not have this problem.

I feel the method name should equal the action taken, it should not equal the action taken + options applied to the action.

@straight-shoota
Copy link
Member

@j8r Yes, the root folder must be handled separtely, but I'd still prefer that over an additional method.

Maybe it would make sense to have something like Dir.walk_tree(path) which yields each item in that path to avoid globbing.

@j8r
Copy link
Contributor Author

j8r commented Nov 1, 2017

I agree with you @lachlan for the recurse Boolean, underscores are not elegant here. We can merge most of the methods of the FileUtils module to the File Object. mkdir_p, rm_r and cp_r can be also be changed this way.

In an other hand I disagree to replace all LibC references by new names, specially chmod which use an octal number like 0o750, or chown that use uids and gids. This are very specific arguments.

For mkdir(_p), which take only one argument, and optionally an other for the permission mode, why not:

Dir.create("dir/", recurse: true)
Dir.delete("dir/")

rm doesn't exist as is in the LibC, so replacing it by delete is good!

I very like the recurse - this translates an Unix argument to a Crystal one.

@straight-shoota
Copy link
Member

Please let's not discuss these advanced ideas in this PR, open a new issue for that.

@j8r
Copy link
Contributor Author

j8r commented Nov 1, 2017

Sorry, I open an issue

@j8r
Copy link
Contributor Author

j8r commented Nov 1, 2017

Issue opened at #5231

@j8r
Copy link
Contributor Author

j8r commented Nov 10, 2017

I close this PR. FileUtils, File and Dir will certainly be cleaned in the future and then a recursive approach will be implemented for most commands in the future.

@j8r j8r closed this Nov 10, 2017
@j8r j8r deleted the recursive_chmod_chown branch October 16, 2018 08:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants