PDA

View Full Version : How to extract and compress a cpio archive file in Qt



sarode
29th December 2007, 08:27
Hello Everyone!

I have a cpio archive file and want to extract the same and compress later after modifications are done to the archive file.

I tried doing this using QProcess from Qt 4.

Please anyone can guide me to do this.

Thanks and Regards,
Sarode

wysota
29th December 2007, 09:15
Your approach was correct. What went wrong?

sarode
29th December 2007, 11:23
Hi,

I tried by passing the arguments to QProcess::start(...) function with arguments
command name as cpio and arguments as the file name but the process was not able to produce the output. Even when i checked the standard output for the output the data was not available.

Do we need to set any mode before calling this function to copy all the data which is extracted from the archive file?

Please clarify.

Regards,
Sarode

wysota
29th December 2007, 12:16
Maybe the process didn't start successfully?
Either use waitForStarted() or check the state of the process after a while. Also make sure the executable is in the PATH and that you provided correct arguments for the process - i.e. remember that you shouldn't surround arguments with quotes or use any wildcards.

sarode
2nd January 2008, 01:52
Hello,

I tried to call the process cpio in the following way and used the conditions like waitForFinished() and waitForStated() - none of them gave me any indications. I think i am not using these flags properly.

I have sent a piece of code below which i have written. Please clarify where went wrong.




QProcess proc1;
.....

void RunProcess::runProcess()
{
QString program = "/usr/bin/cpio";
QStringList arguments;
arguments << "-i < " << "archive file path";

proc1->start(program, arguments);
if(proc1->waitForFinished())
return;
}



If i dont include the waitForFinished() flag the program says that the process is destroyed when the application is closed.

Thanks and Regards,
Wish you Happy new year
Sarode

jpn
2nd January 2008, 08:10
Try

arguments << "-i" << "<" << "archive file path";

sarode
2nd January 2008, 08:46
Hello,

Tried passing the arguments as you suggest.




QString program1 = "/usr/bin/cpio";
QStringList arguments1;
arguments1 << "-i" << "<" << "/tmp/rootfs/archivefile";

proc1->start(program1, arguments1);
if(proc1->waitForStarted())
qWarning("Not Started");



But still not able to execute the process. Here even the if condition will not be true.

Please clarify

Regards,
Sarode

jpn
2nd January 2008, 08:59
Try what's being suggested in this post (http://www.qtcentre.org/forum/p-qprocess-and-spumux-post57472/postcount2.html).

wysota
2nd January 2008, 09:16
"<" is a shell redirect command, isn't it? It won't work here as there is no shell. You need to perform the redirection yourself by writing to stdin of the process.

sarode
2nd January 2008, 09:28
Hi,

Tried the other option specified the following output.



QStringList arguments1;
arguments1 << "-i" << "<filename_with_absolute_path";
// other option
// arguments1 << "-i" << "<" << "filename_with_absolute_path";

proc1->start("/usr/bin/cpio", arguments1);

if(!proc1->waitForFinished())
qWarning("Returned");

connect(proc1, SIGNAL(started()), this, SLOT(readData()));

void Process::readData()
{
qWarning("Started");
}


I got the following output. the process is able to start when i checked for the pid of the process, the state of the process is also S+ in #pa ax

The program halts with following output
Returned
QProcess: Destroyed while process is still running.

I am not able to understand, can we read the output generated by any buffer?

Regards,
Sarode

wysota
2nd January 2008, 09:59
You still use a redirection! You can't do that as there is no shell running over QProcess unless you start one yourself. Your "<" gets sent to cpio instead of the file contents it expects. The process starts but does nothing useful as it gets wrong data.

Thomas
4th January 2008, 20:55
Man, you don't get it, right? Ok, let me repeat it a last time: you are using redirection which is a Shell feature. Through Qt you are starting a process. So no Shell available, only cpio. Result: no redirection possible.

Why don't you simply read the info page of cpio? It is clearly explained there how to use a file instead of redirection. For the impaired: it is the option -F[SPACE]FILENAME or --file=FILENAME

sarode
5th January 2008, 09:11
Hello Thomas,

I did it that 3 days back. I could not post it back.
its working fine.

Thanks to all.
Sarode

Thomas
5th January 2008, 09:20
Never mind; glad that it finally works.