Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Ruby Programming Language

Newbie... help with permissions


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 Carrick

  Developer - http://www.micahcarrick.com
  GTK+ Forums - http://www.gtkforums.com

On Jun 4, 2007, at 10:24 PM, Micah Carrick 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)?
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 Carrick

  Developer - http://www.micahcarrick.com
  GTK+ Forums - http://www.gtkforums.com

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc