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

Hi Monks, I'm developing a web server that would try to run some third party scripts on the local server, format output and display. This third party code is written in C and Fortran and tends to write output in the directory it is being invoked from. Quite (un)surprisingly, when i invoke it from within a CGI script, output is not written into the cgi directory for known reasons. Is there a way around? relevant parts of my directory tree are given below.
1. <something>/htdocs/pkota/ncit/progs/ 2. <samething>/htdocs/pkota/ncit/pdbs/ 3. <samething>/cgi/pkota/ncit/
of the above, "2" is 'world' writeable. 1 and 3 are just 'world' readable. "1" has some scripts that are 'world' executable. I want to invoke a cgi script from "3" such that it calls another from "1" and writes to "2". Hope i'm clear. Tried using Proc::Simple with no luck. I wouldn't want to make "1" 'world' writable for security reasons. Any help is greatly appreciated.

Replies are listed 'Best First'.
Re: running process from CGI
by Anno (Deacon) on Apr 12, 2007 at 11:10 UTC
    Change the current directory to where you have write permission, then call the external program.

    Anno

      Thanks for the reply Anno.. I tried chdir() and $ENV{PWD} and both dont seem to work the way i want them to. :)
Re: running process from CGI
by cdarke (Prior) on Apr 12, 2007 at 11:21 UTC
    I suggest you contact the third party supplier and ask them how the directory is chosen. It is unusual for a program to write to a directory from which it is invoked - usually programs write to the current directory, which of course is not the same thing. I'm not sure what you mean by "tend to", do they or do they not? If they are writing to the current directory then just call chdir.
    You might be able to work-around it by pre-creating the filename as a symbolic link to the real directory (or even to use a named pipe/fifo). That won't work if the program destroys the file and re-creates it.
      Thanks for the suggestion cdarke. I think i didnt put it properly. I meant that the third party program writes to the current directory. for example, if i invoke the program as
      ../../progs/<programname> <options> <parameters>
      it writes to the current directory. I tried using chdir() and it partially solves the problem. There are two scripts written in Fortran, which i call in succession. The first one writes output to the required directory when i use chdir() and the other one does not. The third party one needs user intervention at runtime. I tried writing the required values to a file and giving that as an input during invocation as follows
      ../../progs/<script> < <param input file>
      where <param input file> contains different runtime parameters. This works like a charm when i use it on the command line and it does not work when i use it within backticks from the CGI script after issuing a chdir() to the required working directory.
        OK, different problem now. When you say 'it does not work' when used in backticks, is the file created in the wrong directory, or does it fail to read the input?
        It could be that the chdir is affecting the redirection. This will be done by the bourne shell. Try providing the full path name of the input file with your redirection.
        Failing that, you could try:
        `cd $target;../../progs/<script> < <param input file>`
        You are invoking a shell anyway (because of the redirection) so adding a cd won't be any more of an overhead.
        If you still have problems then we could run it in a pipe, which is much neater.
Re: running process from CGI
by Moron (Curate) on Apr 12, 2007 at 13:00 UTC
    ^B Generally, cgi programs cannot use the file-system directly, but have to rely on what is configured for the webserver.

    ^I For apache, see virtual hosts and aliases.

    __________________________________________________________________________________

    ^M Free your mind!

    Key to hats: ^I=white ^B=black ^P=yellow ^E=red ^C=green ^M=blue - see Moron's scratchpad for fuller explanation.

      Thanks for the reply ! that was certainly informative but i dont have permissions to change configuration settings for apache. thanks anyway..