[Interest] Changing permissions in a dir
Sensei
senseiwa at gmail.com
Mon Mar 31 13:03:28 CEST 2014
Dear all,
I am trying to figure out how to change permissions "recursively" of
every item in a directory.
I've tried the code below but I found that I have two problems.
First: the code won't work and stops after descending into the target
directory... and I cannot figure out why!
Second: can I change permissions of a directory?
This is the code:
QStack<QString> dirs;
// fileName_: I need to change permissions in there
dirs.push(QFileInfo(fileName_).absolutePath());
while (!dirs.isEmpty())
{
// Get dir
QDir d(dirs.pop());
QFileInfoList list = d.entryInfoList(QDir::NoDotAndDotDot);
// Set dir permissions of dir?
qWarning("> into %d-%d %s", dirs.count(), list.count(),
qPrintable(d.absolutePath()));
for (int i = 0; i < list.count(); i++)
{
QFileInfo info = list[i];
if (info.isFile())
{
qWarning("set permission of file %s",
qPrintable(info.absoluteFilePath()));
QFile f(info.absoluteFilePath());
bool b = f.setPermissions(f.permissions() |
QFile::WriteUser);
if (!b) qWarning("ERR");
}
if (info.isDir())
{
qWarning("PUSH %s", qPrintable(info.absolutePath()));
dirs.push(info.absolutePath());
}
}
}
And this is the output, which is quite self-explanatory:
> into 0-0 /Users/sensei/Desktop/mydir
The directory exists, and contains files and directories.
If you can lend a hand I'd be really happy!
Thanks & Cheers!
More information about the Interest
mailing list