Question

I have written a simple piece of code for extracting zip files using unzip. It works fine when output directory is not set but returns error is directory is set

"Archive: /home/vishvesh.kumar/tempFolder/test.zip checkdir: cannot create extraction directory: /home/vishvesh.kumar/tempFolder No such file or directory"

Code:

int main(int argc, char *argv1[])
{
    std::cout << "Creating child process...";
    std::vector<std::string> arguements;

    char* argv[4] = {0};
    argv [0] = "/usr/bin/unzip";
    argv [2] = "/home/vishvesh.kumar/tempFolder/test.zip";
    argv [1] = "-d /home/vishvesh.kumar/tempFolder/";
    argv [3] = NULL;
   createChildProcess(argv);

}


void createChildProcess(char* argv[])
{
    pid_t pid = fork();
    if (pid == -1)
    {
            std::cout << "error creating child process, exiting...";
            exit(1);
    }
    else if (pid == 0)
    { // This is the child process
       std::cout << "This is the child process. About to sleep\n";
       std::cout << "Woke up\n";
       if (execvp(argv[0], argv))
        { // execvp failed
            std::cout << "fatal - execvp failed!";
            exit(1);
        }

     }
    else
    { // This is the parent process.
        std::cout << "This is the parent process\n";
        int status;
        std::cout << " done. PID: " << pid << ".\n";

        double start = 0;
        waitpid(pid, &status, 0); // Wait for program to finish executing.
        double dur = (clock() - start) / CLOCKS_PER_SEC; // Get execution time
        std::cout << "Sad my son died\n";
        std::cout << "Program returned " << WEXITSTATUS(status);
        std::cout << " and lasted " << dur << " seconds.\nPress enter.\n";
        std::cin.get();
    }
}
Was it helpful?

Solution

char* argv[4] = {0};
argv [0] = "/usr/bin/unzip";
argv [2] = "/home/vishvesh.kumar/tempFolder/test.zip";
argv [1] = "-d /home/vishvesh.kumar/tempFolder/";
argv [3] = NULL;

Should be:

char* argv[5];
argv [0] = "/usr/bin/unzip";
argv [1] = "-d";
argv [2] = "/home/vishvesh.kumar/tempFolder/";
argv [3] = "/home/vishvesh.kumar/tempFolder/test.zip";
argv [4] = NULL;

The way you have it, the parameter to -d is the zip file, since that's the parameter after the -d. So it tries to create that directory and can't because it's a file.

OTHER TIPS

argv [1] = "-d /home/vishvesh.kumar/tempFolder/";

should be

argv [1] = "-d";
argv [2] = "/home/vishvesh.kumar/tempFolder/";

They are two separate parameters passed to the new process through execvp().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top