Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Why does File::Find try to chdir to $PWD (and fail) even if $PWD is not itself in search list?

by puterboy (Scribe)
on Nov 24, 2008 at 17:05 UTC ( #725644=perlquestion: print w/replies, xml ) Need Help??
puterboy has asked for the wisdom of the Perl Monks concerning the following question:

I am running a routine su'd to another user that does not have access to $PWD.

The other user *does* have full rwx access to *all* the directories in the @directories_to_search list.

However, I keep getting the error:
Can't cd to /home/present_working_directory: Permission denied

The error seems to occur *after* the first run through \&wanted. So, it seems like it is trying to go (back?) to $PWD after it finishes going through \&wanted.

Note this error goes away if I run the routine from *any* directory that the su'd user can chdir to (even if that direcotry has nothing to do with the search list).

Am I missing something obvious here?

Note: I would prefer not to have to use the no_chdir option since I do want to chdir to directories where files are indeed found.

I tried googling but didn't find anything on this.

  • Comment on Why does File::Find try to chdir to $PWD (and fail) even if $PWD is not itself in search list?

Replies are listed 'Best First'.
Re: Why does File::Find try to chdir to $PWD (and fail) even if $PWD is not itself in search list?
by ikegami (Pope) on Nov 24, 2008 at 17:38 UTC
Re: Why does File::Find try to chdir to $PWD (and fail) even if $PWD is not itself in search list?
by mr_mischief (Monsignor) on Nov 24, 2008 at 17:47 UTC
    You're already putting in the effort to change user IDs. Why stop there? If it doesn't matter to the search what the current working directory is, just take one more step and change the current working directory in the code before the find. That way it'll try to change back to that directory instead of the one from which the program was run.

    When the program exits, any path changes will be lost anyway. Your prompt will still come back to your shell's current working directory since environment changes do not flow upstream.

    I recommend using chdir to change to the root directory for sake of portability, but you could make it any other directory the account can access.

    Below is some example code. I can run this as root in root's home directory without any errors. It changes to my non-root account, changes to the root ('/', not to be confused with '/root') directory, searches a directory my non--root account can access (and actually owns in this case, but that's not important), and File::Find returns to '/' when it's done.

    use File::Find; $< = $> = 500; sub w { print $_ . "\n" } chdir q(/); find( \&w, qw( /home/chris/Pictures ) );

    There's no reason for File::Find not to return to the directory from which it started. There's also no reason to have it return to a directory where the user account under which it is operating has no privileges. No code that might be executed afterward would have rights to that directory either anyway.

      Thanks - that makes a lot of sense. I hadn't considered that changing directory to root or elsewhere is harmless since it doesn't outlast the routine.
        The root directory will tend to be readable by all users by default anyway. A directory that's by default world-readable is the ideal place to not have permission errors upon reading.

        Many daemon programs actually change directory to '/' as one of the first things they do. Some others are written to have a specific directory set up just for a chroot jail.

Re: Why does File::Find try to chdir to $PWD (and fail) even if $PWD is not itself in search list?
by JavaFan (Canon) on Nov 24, 2008 at 17:09 UTC
    Seems to me that File::Find just wants to restore the current working directory. That is, after finishing the search, it current working directory should be the same as it was when the search started.

    I think that's perfectly fine attitude for a subroutine; to have as few side effects as possible.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://725644]
Approved by planetscape
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2018-06-18 13:42 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (109 votes). Check out past polls.