signon  8.58
misc.cpp
Go to the documentation of this file.
00001 /*
00002  * This file is part of signon
00003  *
00004  * Copyright (C) 2011 Nokia Corporation.
00005  *
00006  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
00007  *
00008  * This library is free software; you can redistribute it and/or
00009  * modify it under the terms of the GNU Lesser General Public License
00010  * version 2.1 as published by the Free Software Foundation.
00011  *
00012  * This library is distributed in the hope that it will be useful, but
00013  * WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with this library; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
00020  * 02110-1301 USA
00021  */
00022 
00023 #include "debug.h"
00024 #include "misc.h"
00025 
00026 extern "C" {
00027     #include <errno.h>
00028     #include <sys/stat.h>
00029 }
00030 
00031 #include <QDir>
00032 
00033 bool setUserOwnership(const QString &filePath)
00034 {
00035     const char *userHomePath = QDir::homePath().toLatin1().data();
00036     struct stat fileInfo;
00037     if (stat(userHomePath, &fileInfo) != 0)
00038         return false;
00039 
00040     QByteArray filePathArray = filePath.toLocal8Bit();
00041     const char *filePathStr = filePathArray.constData();
00042     if (chown(filePathStr, fileInfo.st_uid, fileInfo.st_gid) != 0) {
00043         BLAME() << "chown of" << filePathStr << "failed, errno:" << errno;
00044         return false;
00045     }
00046 
00047     return true;
00048 }
00049 
00050 bool setFilePermissions(const QString &filePath,
00051                         const QFile::Permissions desiredPermissions,
00052                         bool keepExisting)
00053 {
00054     if (!QFile::exists(filePath)) return false;
00055 
00056     QFile::Permissions newPermissions = desiredPermissions;
00057 
00058     QFile file(filePath);
00059     QFile::Permissions initialPermissions = file.permissions();
00060 
00061     if (keepExisting)
00062         newPermissions |= initialPermissions;
00063 
00064     if (newPermissions != initialPermissions)
00065         return file.setPermissions(newPermissions);
00066 
00067     return true;
00068 }
00069