I want An agent on machine, so that it can fake user actions.
class Agent
def initialize(user, dir) @user = user @dir = dir
end
def mkdir(dir)
bak = Process.euid mkdir
Process.euid = bak
end
def create_file(file, content)
bak = Process.euid echo #{content} > #{file}
Process.euid = bak
end
end
My problem is, I have a very long list of actions, like mv, touch,
mkdir,
rm, etc.
I don’t want to add save and restore euid for all methods.
I kind of guess here I should use some meta programming skills. here’s
what
i tried.
class Agent
def self.as_me_do(method_name, &block)
define_method method_name do
block
end
end
as_me_do mkdir do mkdir #{dir}
end
end
but this still generate code duplications, and it doesn’t work.
I’m not sure how this could be done gracefully.
bak = Process.euid
My problem is, I have a very long list of actions, like mv, touch, mkdir,
rm, etc.
I don’t want to add save and restore euid for all methods.
Btw, your code is not safe as it does not restore on exception.
Any one been here before?
Could be that AspectR is for you. I also once created code to insert
methods before and after a method invocation but I can’t seem to find
it in the archives.
I want An agent on machine, so that it can fake user actions.
…
My problem is, I have a very long list of actions, like mv, touch, mkdir, rm,
etc.
I don’t want to add save and restore euid for all methods.
First thing I’d do is remove all the duplication, maybe something like
this…
class Agent
def initialize(user, dir) @user = user @dir = dir
end
def as_me_do
bak = Process.euid
yield
ensure # hat tip Robert
Process.euid = bak
end
def mkdir(dir)
as_me_do {mkdir}
end
def create_file(file, content)
as_me_do {echo #{content} > #{file}}
end
end
The next step might be more obvious.
I kind of guess here I should use some meta programming skills. here’s what i
tried.
Defining methods is going to be tricky, because you don’t have
consistent method signatures and system calls. Maybe a have a hash of
methods => system calls and method missing?
Also, why back ticks, why not use ruby File methods?