Forum: Ruby Newbie... help with permissions

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.
Micah C. (Guest)
on 2007-06-05 09:25
(Received via mailing list)
Hey ya'll. I'm brand new to Ruby. Just a few hours into "Programming
Ruby" in fact. I'm loving ruby so far. I can't wait to get into some
GTK+ with ruby (since I do a lot of GTK+ stuff in C).

I work best by solving real-world problems I have. So... an easy one...
deleting some log files on a client's server that are over 30 days old.
These log files are named by date and time. So here's what I have:

#-------------------------------------------------------------------------------
#! /usr/bin/env ruby

LOG_DIR = '/var/custom_logs/cache/'

def delete_if_old(file)
    t = (Time.new-(60*60*24*30)).strftime("%Y%m%d_%H%M%S").to_i
    if file.to_i < t
        File.delete(LOG_DIR + file)
    end
end

Dir.foreach(LOG_DIR) { |f| delete_if_old f }
#-------------------------------------------------------------------------------

So, when I replace the delete with a puts, I can see that all the logic
is fine. However, when I try to delete I get a permissions error. I'm
running the script as a user that does have access to the directory and
can delete those files. So, my assumption is that ruby is NOT running as
this user (the files can ONLY be deleted by the user, not the group).
With my php scripts on this server I use cgiwrap to allow rwx access to
my scripts.

Is there anything I can do ruby-wise to allow this ruby script to delete
this file or do I have to change how the files are logged to allow group
access?

The error is something like:

... in `delete': Operation not permitted - /var/custom_logs/cache/.
(Errno::EPERM)


Any help is appreciated. I guess I'm just trying to understand how the
script is executed... as what user. May be more of a standard linux
permissions questions but still seems odd to me that the user that's
executing the script can delete the files but eh script itself cannot.

--
- Micah C.

  Developer - http://www.micahcarrick.com
  GTK+ Forums - http://www.gtkforums.com
Zachary H. (Guest)
on 2007-06-05 09:46
(Received via mailing list)
On Jun 4, 2007, at 10:24 PM, Micah C. wrote:
> Hey ya'll.
Hey.

> I'm brand new to Ruby.
Welcome.  This is my first post to this list.  So we're on a par.

> def delete_if_old(file)
>    t = (Time.new-(60*60*24*30)).strftime("%Y%m%d_%H%M%S").to_i
You might want Time.now.to_i + ( 60 * 60 * 24 * 30 )

>    if file.to_i < t
You're calling to_i on a String here.  I bet that "if" clause always
hits.

>        File.delete(LOG_DIR + file)
>    end
> end
>
> Dir.foreach(LOG_DIR) { |f| delete_if_old f }
> #---------------------------------------------------------------------
> ----------

Is it possible that the first time delete_if_old is called, it's
passed "." (dot)?
Micah C. (Guest)
on 2007-06-05 10:38
(Received via mailing list)
Okay-- that was my bad. I wasn't filtering out '.' and '..'

Here's the code that works:

#-------------------------------------------------------------------------------

#! /usr/bin/env ruby

LOG_DIR = '/var/custom_logs/cache/'

def delete_if_old(file)
    t = (Time.new-(60*60*24*30)).strftime("%Y%m%d%H%M%S").to_i
    if file.to_i < t && file != '.' && file != '..'
        puts 'Deleting: '+file
        File.delete(LOG_DIR + file)
    end
end

Dir.foreach(LOG_DIR) { |f| delete_if_old f }
#-------------------------------------------------------------------------------


Now, if anybody has any suggestions as to coding conventions or things I
should do differntly I'm all ears. Just because this works, doesn't mean
I don't want to know how to do it the way ruby programmers
conventionally would approach this.

- Micah C.

  Developer - http://www.micahcarrick.com
  GTK+ Forums - http://www.gtkforums.com
This topic is locked and can not be replied to.