PDA

View Full Version : Qt file copy issue, treating the copied file is text document only not as exe



ejoshva
10th April 2015, 11:37
I am copying a certail file from one path to another using the below code




QString curdir = QCoreApplication::applicationDirPath().append("/db_ssparkl.sqlite");
profileName= qd.homePath().append("/ssparkl.sqlite");
QFile dbfile;
QFile destifile;
destifile.setFileName(profileName);
dbfile.setFileName(curdir);

if(dbfile.exists() && !destifile.exists())
{
bool success = true;
success &= dbfile.open( QFile::ReadOnly );
success &= destifile.open( QFile::WriteOnly | QFile::Truncate );
success &= destifile.write( dbfile.readAll(),dbfile.size()) >= 0;
destifile.close();
dbfile.close();

}



I am writing by reading the source file content onto destination file content, since QFile::copy didn't work as expected as the file copied show size as zero bytes.


Now the file is getting copied fully as expected. But it's being treated text document and not as exec(app)

How to make it treated as app.

Added after 9 minutes:

I googled it and found that, after writing the file has to be given +x option.

chmod +x filename

how do I do this in QT

Added after 16 minutes:

Got it solved as below



QString changeToExec = "chmod +x "+destifile.fileName();
QProcess::execute(changeToExec);

yeye_olive
10th April 2015, 11:40
You should retry QFile::copy() and figure out what does not work, because your approach is much more fragile. You have no way of knowing whether you successfully read the whole input file and no way of knowing whether you successfully wrote the whole data to the output file (remember, QIODevice::write() write some bytes, but maybe not all of them). Your approach does not scale well because you copy the whole file in memory instead of copying it by fixed-sized chunks. By the way, you should use "&&=" instead of "&=".

To answer your question, call setPermissions() on your target QFile and make sure you include ExeOwner (and possibly other Exe* constants) in the permissions.

anda_skoa
10th April 2015, 12:32
How to make it treated as app.

Why? Its filename suggests that it is an SQLite database file, not a program.

Cheers,
_

ChrisW67
10th April 2015, 20:56
Indeed, the execute privilege makes no sense at all on a data file like this appears to be. If it is actually an executable then copying it to a user modifiable location seems an odd thing to do.

Setting that permission can be done more efficiently with QFile::setPermissions(), just as the original copy can be done with QFile::copy()

ejoshva
11th April 2015, 05:09
Thought Its a program , it's not treating as a program. Its just treating as an document with only read,write permission.

wysota
11th April 2015, 07:42
Thought Its a program , it's not treating as a program.
So it is not an sqlite database? What does command "file ssparkl.sqlite" print out?