GithubHelp home page GithubHelp logo

Comments (7)

zhangkaizhao avatar zhangkaizhao commented on June 19, 2024 3

@watzon Thanks! After some more investigation I think you are right. I made a mistake.
This Dir["**/*"].each is a little magical. 😅
I am closing this issue now.
And I will try to add the implementation in Crystal for the walkdir_test project soon.

from crystal-libraries-needed.

watzon avatar watzon commented on June 19, 2024 1

What's wrong with Dir? What are you wanting the shard to be able to do?

Currently with Dir you can do:

Dir["**/*"].each do |f|
  puts f
end

from crystal-libraries-needed.

zhangkaizhao avatar zhangkaizhao commented on June 19, 2024

Hi @watzon ! Please read at least one of the implementations e.g. Python I referred, and find if it is already implemented in Crystal. Please update me if I missed something. Thank you!

from crystal-libraries-needed.

watzon avatar watzon commented on June 19, 2024

@zhangkaizhao I have looked through all of the implementations and I haven't seen much of anything that can't be done with plain Crystal. That's why I want to know what your use-case is. What are you wanting to do that you can do in one of those libs, but not with Dir?

from crystal-libraries-needed.

zhangkaizhao avatar zhangkaizhao commented on June 19, 2024

This is my pull request lilydjwg/walkdir-test#1 .
The class method Dir.[](*patterns) : Array(String) returns an Array. This might be an issue when there are too many files in the directory.
You can improve it if you have a better idea.

Without this method, I have to open sub directories recursively:

def count(dir)
  count = 0
  dir.each_child do |filename|
    subpath = File.join dir.path, filename
    filetype = File.info(subpath, follow_symlinks: false).type
    if filetype.directory?
      count += count(Dir.new(subpath))
    elsif filetype.file? && filename.downcase.ends_with?(".jpg")
      count += 1
    end
  end
  dir.close
  count
end

puts count(Dir.new(ARGV[0]))

and this maybe hits the recursion limit for too deep sub directories.

And another problem is when I run without the line dir.close from the above source code I get an issue which complains of "Too many open files" if the number of files is a little bigger:

$ ./walk ~/repos/github.com/
Unhandled exception: Error opening directory '/home/kaizhao/repos/github.com/ziglang/zig.wiki/.git/logs/refs': Too many open files (Errno)
Failed to raise an exception: END_OF_STACK
[0x563f100af6b6] ???
[0x563f10096edb] __crystal_raise +43
[0x563f10097e6c] ???
[0x563f1009f380] ???
[0x563f1009a94c] ???
[0x563f10099a3d] ???
[0x563f100a2787] ???
[0x563f100aaf4c] ???
[0x563f10098e97] main +55
[0x7f7f5edf9ee3] __libc_start_main +243
[0x563f100954fe] _start +46
[0x0] ???

I don't know if this is an issue.

from crystal-libraries-needed.

bew avatar bew commented on June 19, 2024

The class method Dir. : Array(String) returns an Array. This might be an issue when there are too many files in the directory.

You can use Dir.glob with a block if you want to avoid creating an array

from crystal-libraries-needed.

zhangkaizhao avatar zhangkaizhao commented on June 19, 2024

@brew Thank you! I have updated the pull request with Dir.glob.

from crystal-libraries-needed.

Related Issues (20)

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.