ibmman has asked for the wisdom of the Perl Monks concerning the following question:

I kindly request your assistance... I was experiencing some very strange behavior while trying to use mkpath to create a number of directories in a cgi app (using cgi::application). It seems that the directories were successfully created, but it dies when the method goes to return with a malformed header error- which showed 'mkdir' and the start of one of the paths. This is with verbose on or off, and regardless of if I store the return value of mkpath. It does not happen when no directories need to be created. I tested a number of options by then using rmtree to delete the directories (which shows the same error, but with rmdir) and really couldn't figure out what was going on. I was going to try to write the header manually, but cgi-application usually takes care of that, and I've no idea how it could be getting messed up. Luckily, I won't need to use the method often, and the directories did get created, but it still seems like a good idea to try to debug it in case it is needed again in the future. Thank you!
  • Comment on mkpath (and rmtree) throw strange error when used in CGI

Replies are listed 'Best First'.
Re: mkpath (and rmtree) throw strange error when used in CGI
by Tanktalus (Canon) on Feb 10, 2009 at 22:02 UTC

    It'd likely be more helpful if you actually showed the text you're getting... without that, I just have to imagine it.

    Most likely problem in CGI when dealing with the filesystem is write/execute permission. Remember that the CGI app is usually not running as your user, but as whatever apache drops privileges to (apache and nobody are common choices, but could be anything). So if that user doesn't have permission to make the directory, it will fail.

    What you are probably seeing is mkpath's failure. According to the docs, it'll use Carp::croak - so trap it with eval:

    unless (eval { mkpath($dir); 1 }) { show_error_page("Couldn't create $dir: $@"); }
    But that's just a guess.

      Hmm, it might be worth trying to see if perhaps there's something sneaking into the array with the paths that shouldn't be there- but I've displayed every element in the array before and it looks okay. I'm also not running mkpath when a directory already exists.
        Doesn't mean you're not fooling yourself, hexdump
        print "<pre>\n"; for my $i ( 0 .. $#foo ){ print "$i)", unpack('H*', $foo[$i]),"\n"; } print "</pre>\n";
Re: mkpath (and rmtree) throw strange error when used in CGI
by moritz (Cardinal) on Feb 10, 2009 at 22:00 UTC
    So what's in the error log of your web server? That's the easiest way to find out about the "malformed header"s.

    I bet it's either an issue with missing permissions, or wrong relative paths.

       [Tue Feb 10 17:08:48 2009] [error] [client 129.21.196.233] malformed header from script. Bad header=mkdir /home/abc5565/media/T051 as an example- but the directories DO get created where they should be, and they're accessible (though I was trying to set them as 777 and it seems they get 755 regardless of if I specify mode=>0777 to mkpath).
        So some part of your script actually prints out something before the header is printed. Maybe a left-over debugging statement?

        Or do you have any environment variables with debug in the name?