QUrlInfo and ftp permissions
-
Hi everyone.
I am working on a project (for personal use) which includes FTP client built with QFtp and Qt 4.8
I know most of you will say that the right way to go is QNetworkAccessManager, but I found QFtp to be faster to implement.
I am facing problems determining a file or directory permissions. According to the "docs for QUrlInfo::PermissionSpecs":http://qt-project.org/doc/qt-4.8/qurlinfo.html#PermissionSpec-enum
@
QUrlInfo::ReadOwner 00400 The file is readable by the owner of the file.
QUrlInfo::WriteOwner 00200 The file is writable by the owner of the file.
QUrlInfo::ExeOwner 00100 The file is executable by the owner of the file.
QUrlInfo::ReadGroup 00040 The file is readable by the group.
QUrlInfo::WriteGroup 00020 The file is writable by the group.
QUrlInfo::ExeGroup 00010 The file is executable by the group.
QUrlInfo::ReadOther 00004 The file is readable by anyone.
QUrlInfo::WriteOther 00002 The file is writable by anyone.
QUrlInfo::ExeOther 00001 The file is executable by anyone.
@This means that QUrlInfo should return 777 (or 00777?) when there are full permissions, but I receive 511 instead of 777 and 493 instead of 755
Does anyone know why I get such results? How can I test the permissions of a file or directory with QUrlInfo?
Also, since QUrlInfo is "read only" (any changes doesn't affect the remote file or directory, only the information in the object), how can I update the permissions of the remote file or directory? -
It seems the documentation isn't quite complete on this.
I thought I'd check the values which are being returned for the enum with this:
@
qDebug() << QUrlInfo::ExeOwner << QUrlInfo::WriteOwner << QUrlInfo::ReadOwner;
qDebug() << QUrlInfo::ExeGroup << QUrlInfo::WriteGroup << QUrlInfo::ReadGroup;
qDebug() << QUrlInfo::ExeOther << QUrlInfo::WriteOther << QUrlInfo::ReadOther;
@The result is:
@
64 128 256
8 16 32
1 2 4
@So it's not returning the exact permissions, but it rather returns number which is probably best suited for bitwise operations. I am not quite familiar with them, but here is a simple function which actually allows me to calculate the permissions of a directory and return the result as QString:
@
QString MyFtpWindow::permissionsFromDecimal(uint number)
{
int firstbit=0, secondbit=0, thirdbit=0;
if( number >= 256 ) {
firstbit += 4;
number -= 256;
}
if( number >= 128 ) {
firstbit += 2;
number -= 128;
}if( number >= 64 ) { firstbit += 1; number -= 64; } if( number >= 32 ) { secondbit += 4; number -= 32; } if( number >= 16 ) { secondbit += 2; number -= 16; } if( number >= 8 ) { secondbit += 1; number -= 8; } if( number >= 4 ) { thirdbit += 4; number -= 4; } if( number >= 2 ) { thirdbit += 2; number -= 2; } if( number >= 1 ) { thirdbit += 1; number -= 1; } return QString("%1%2%3").arg(firstbit).arg(secondbit).arg(thirdbit);
}
@Certainly, not an elegant way of calculating the value, but it does work. I'll be happy if someone can provide better way of doing this. Also, I have yet to test what it would return if setuid, setgid and sticky bit are present on the file/directory being examined.
-
I know the thread is old, but since the docs are lacking a bit on this subject and I stumped on this thread, I thought I'd share my two cents.
I do this, instead. It's similar, but it doesn't rely on the values from enum QUrlInfo::PermissionSpec having concrete values.
@QString i92ftp::convert_perms_to_unix(int perms)
{
int permissions = perms;
int first = 0;
if(perms & QUrlInfo::ReadOwner)
{
permissions &= QUrlInfo::ReadOwner;
first += 4;
}
if(perms & QUrlInfo::WriteOwner)
{
permissions &= QUrlInfo::WriteOwner;
first += 2;
}
if(perms & QUrlInfo::ExeOwner)
{
permissions &= QUrlInfo::ExeOwner;
first += 1;
}int second = 0; if(perms & QUrlInfo::ReadGroup) { permissions &= QUrlInfo::ReadGroup; second += 4; } if(perms & QUrlInfo::WriteGroup) { permissions &= QUrlInfo::WriteGroup; second += 2; } if(perms & QUrlInfo::ExeGroup) { permissions &= QUrlInfo::ExeGroup; second += 1; } int third = 0; if(perms & QUrlInfo::ReadOther) { permissions &= QUrlInfo::ReadOther; third += 4; } if(perms & QUrlInfo::WriteOther) { permissions &= QUrlInfo::WriteOther; third += 2; } if(perms & QUrlInfo::ExeOther) { permissions &= QUrlInfo::ExeOther; third += 1; } QString permissions_str = QString("%1%2%3").arg(first).arg(second).arg(third); return permissions_str;
}@