PDA

View Full Version : special characters as argument of QProcess



dotjan
16th March 2012, 16:45
Hi, in my app it seems I cannot properly pass special characters as argument to the external program started with a QProcess instance.

The final command I have to execute is something like:

/usr/bin/rsync -n -v -a --exclude "/*/*/" --include "*/" --include "*.txt" --exclude "*" /dir1/ /dir2

--exclude and --include are rsync's filter rules which must be written using special characters as you can see.

A snip of the code to generate them:



args << "--exclude";
args << "\"/\*/\*/\"";
args << "--include";
args << "\"\*/\"";
args << "--include";
args << "\"\*.txt\"";
args << "--exclude";
args << "\"\*\"";


You see I use \ as escape character for "" and *. However it seems it is not enough... when I then run the application, I can intercept the rsync command on the system (ps aux | grep rsync) and I can see it running exactly as expected:


/usr/bin/rsync -n -v -a --exclude "/*/*/" --include "*/" --include "*.txt" --exclude "*" /dir1/ /dir2

Also, if I debug with qdebug the command executed I see:


"/usr/bin/rsync" ("-n", "-v", "-a", "--exclude", ""/*/*/"", "--include", ""*/"", "--include", ""*.txt"", "--exclude", ""*"", "/dir1/", "/dir2")

everything seems to be ok.However, the output generated in my app does not reflect it, just like the filter rules are not applied. I suspect there is something with special characters and escaping... but really do not know how to go ahead.. I am struggling...

Thanks,
Jan

ChrisW67
19th March 2012, 03:15
You don't need to escape the asterisk for C++'s sake. Shouldn't do any harm except to readability. I found on my Linux machine the escaped quotes are not required.



StringList args;
args << "-a"
<< "--exclude" << "/*/*/"
<< "--include" << "*/"
<< "--include" << "*.txt"
<< "--exclude" << "*"
<< "/tmp/dir1/"
<< "/tmp/dir2";

QProcess proc;
proc.start("/usr/bin/rsync", args);

Does the trick here, copying any txt file in the first or second level of the dir1 directory structure, plus folders for any immediate child folder of dir1.