Forum: Ruby on Rails [how can i delete a file system..please help]

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ioana K. (Guest)
on 2006-06-09 13:52
(Received via mailing list)
Hi,
I'm trying to delete a file system (<xml_26548975.xml>)
File.delete("xml_26548975.xml")
But I get this error:
"Permission denied - ./script/../config/../uploads/xml_26548975.xml".
Why?
Chris H. (Guest)
on 2006-06-09 18:12
(Received via mailing list)
well, from the error message, it looks like you are not allowed to
delete
the file due to permissions.  i would start there.
Ian Klier (Guest)
on 2006-06-09 19:12
(Received via mailing list)
What OS are you using for your server?
Ioana K. (Guest)
on 2006-06-09 19:45
(Received via mailing list)
I'm just uploading a file - copying the content to another file, which
afterwards I want to delete it/unlink it/get lost.

uploaded_file = RAILS_ROOT + "/uploads/xml_" + generate_code + ".xml"
     File.open(uploaded_file, "wb") do |f|
     f.write(@file.read)

after some time.....File.delete("....").

I'm using XP and WEBrick( I think he's to blame).

Thanks for your time.
Alder G. (Guest)
on 2006-06-09 21:10
(Received via mailing list)
On 6/9/06, Ioana K. <removed_email_address@domain.invalid> wrote:
>
> Hi,
> I'm trying to delete a file system (<xml_26548975.xml>)

A "file system"?  xml_26548975.xml is a regular file, right?  Since if
it's a directory tree or something, you're not using the correct
method (should look at Dir.delete instead).

If it is a file, the first thing I would check is if you're trying to
File.delete it while it's still open. The code below shows you're
using a block scope for the output file, which is good since  it takes
care of closing the file for you. However, looks like you're not using
the same good pattern for the input file (@file). You can, and should
consider it, to avoid bugs like the one that might be haunting you
right now (there's no problem nesting File.open blocks if that's what
hindered you).
Bill W. (Guest)
on 2006-06-09 23:24
(Received via mailing list)
Ioana K. wrote:


> uploaded_file = RAILS_ROOT + "/uploads/xml_" + generate_code + ".xml"
>
>     File.open(uploaded_file, "wb") do |f|
>     f.write(@file.read)
>
>after some time.....File.delete("....").
>

FileUtils.rm "#{uploaded_file}" should do what you're looking for,
although
you may need to either stip off RAIL_ROOT or save the path without
RAILS_ROOT prior to the File.open.  That is, I think FileUtils assumes
it's
working from RAILS_ROOT whereas File does not.

hth,
Bill
Alder G. (Guest)
on 2006-06-09 23:30
(Received via mailing list)
On 6/9/06, Bill W. <removed_email_address@domain.invalid> wrote:
>
> removed_email_address@domain.invalid
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Why would FileUtils assume its working from RAILS_ROOT?  It's a Ruby
library, not a Rails' one. Does Ruby modify it in some way?  (sounds a
bit unlikely...)
Bill W. (Guest)
on 2006-06-09 23:59
(Received via mailing list)
Hi Alder,

Alder G. wrote:
> Why would FileUtils assume its working from RAILS_ROOT?  It's a Ruby
> library, not a Rails' one. Does Ruby modify it in some way?  (sounds a
> bit unlikely...)

I don't know.  I'm just going on my recent experience.  I'm doing a
little
app that includes a file upload.  After looking back at that code more
closely, I'm just more confused.  FileUtils.copy seems to need
RAILS_ROOT
but FileUtils.rm does not.  Here's the file upload code I'm using.

@filenametouse = "temp-readin" + nextfile.id.to_s + ".xml"

if params[:file_to_upload].instance_of?(Tempfile)
  FileUtils.copy(params[:file_to_upload].local_path,
"#{RAILS_ROOT}/public/#{@filenametouse}")
else
  File.open("#{RAILS_ROOT}/public/#{@filenametouse}","w"){|f|
  f.write(params[:file_to_upload].read)
  f.close}
end

I store the value of @filenametouse in a db record and later retrieve it
to
delete the file which I do with this code.  (emrec.id just identifies
the
group of files I created in a session)

files = Tempfilerec.find(:all,
                                   :conditions => ["emrec_id = ?",
emrec.id])
files.each {|file|
  FileUtils.rm "public/#{file.filename}"
}

I remember being confused when I got the code working but, under
deadline
pressure, just moved on with a note-to-self to come back later and
figure
out "why the difference?"  Any ideas?

Best regards,
Bill
Alder G. (Guest)
on 2006-06-10 00:28
(Received via mailing list)
On 6/9/06, Bill W. <removed_email_address@domain.invalid> wrote:
> >> working from RAILS_ROOT whereas File does not.
>
>
>
> http://lists.rubyonrails.org/mailman/listinfo/rails
>

Check what's the CWD (with Dir.pwd) in each phase. My guess is they
would be different - RAILS_ROOT for the #rm call, somehing else for
the #copy.

The CWD is dependent upon your stack setup. For example, with WEBrick
it's often RAILS_DIR throughout the app, while with FastCGI and
Lighttpd it's /public. What's your setup?
This topic is locked and can not be replied to.