# Directory Listing outside of app structure

**URL:** <https://www.ruby-forum.com/t/directory-listing-outside-of-app-structure/56231>\
**Category:** Rails\
**Created:** [March 29, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/directory-listing-outside-of-app-structure/56231 "2006-03-29T16:21:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [March 29, 2006, 4:21pm UTC](https://www.ruby-forum.com/t/directory-listing-outside-of-app-structure/56231/1 "2006-03-29T16:21:00Z")

</div>

I need to show a directory listing of a folder outside of the Rails app  
structure. Moving the folder is not an option. In fact, changing  
anything (including modes) is not possible for the folder.

I have working directory listing code in Ruby, but if I call chdir in  
Rails to set the working directory to a folder outside the app tree,  
nothing is returned to the browser. It’s fine if I chdir to a folder  
inside the public folder.

To get this to work in Ruby, I needed to change the UID for the Ruby  
file. I don’t think this approach will work in Rails.

I understand the security issues here, but would like to somehow say  
that it’s safe to chdir to a particular directory. I can’t find any  
discussion of Rails safety/security similar to the $SAFE information in  
the Pickaxe book.

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [March 29, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/directory-listing-outside-of-app-structure/56231/2 "2006-03-29T17:08:37Z")

</div>

Brian A. wrote:

> I need to show a directory listing of a folder outside of the Rails app  
> structure

s = `ls /path/to/file`

seems to work for me, unless there’s something I’m not understanding  
about your requirements. For example,

def foop  
render :text =\> `ls -l /opt/local/share`  
end

produces the expected output for http:////foop

–Al Evans

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [March 30, 2006, 1:26am UTC](https://www.ruby-forum.com/t/directory-listing-outside-of-app-structure/56231/3 "2006-03-30T01:26:18Z")

</div>

Al Evans wrote:

> s = `ls /path/to/file`
> 
> seems to work for me, unless there’s something I’m not understanding  
> about your requirements. For example,
> 
> def foop  
> render :text =\> `ls -l /opt/local/share`  
> end
> 
> produces the expected output for http:////foop
> 
> –Al Evans

Thanks for the response, Al. I should have been clearer. I’m working on  
a directory indexer, so I need a listing equivalent to what you’d get  
with

```
Dir["*"].sort_by {|f| test(?M, f)}
```
. In my Ruby version,  
I just chdir to set the working directory.

Ideas?

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [March 30, 2006, 3:16pm UTC](https://www.ruby-forum.com/t/directory-listing-outside-of-app-structure/56231/4 "2006-03-30T15:16:26Z")

</div>

Brian A. wrote:

> Thanks for the response, Al. I should have been clearer. I’m working on  
> a directory indexer, so I need a listing equivalent to what you’d get  
> with
> 
> ```
> Dir["*"].sort_by {|f| test(?M, f)}
> ```
> . In my Ruby version,  
> I just chdir to set the working directory.

Maybe I’m still missing something, but except for the formatting,

def foop  
foo = Dir[’/opt/local/share/\*’].sort\_by {|f| test(?M, f)}  
render :text =\> foo  
end

seems to work for me.

–Al Evans

---

<div class="post-metadata">

**Author:** ![zakifataya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/c89c15/32.png) [@zakifataya](https://www.ruby-forum.com/u/zakifataya)\
**Post date:** [April 9, 2006, 11:21pm UTC](https://www.ruby-forum.com/t/directory-listing-outside-of-app-structure/56231/5 "2006-04-09T23:21:52Z")

</div>

> Maybe I’m still missing something, but except for the formatting,
> 
> def foop  
> foo = Dir[’/opt/local/share/\*’].sort\_by {|f| test(?M, f)}  
> render :text =\> foo  
> end
> 
> seems to work for me.
> 
> –Al Evans

Yeah, it works. I didn’t realize that you could use a string as a file  
ref that way. Thanks, Al.
