signon  8.58
misc.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of signon
3  *
4  * Copyright (C) 2011 Nokia Corporation.
5  *
6  * Contact: Alberto Mardegan <alberto.mardegan@canonical.com>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public License
10  * version 2.1 as published by the Free Software Foundation.
11  *
12  * This library is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20  * 02110-1301 USA
21  */
22 
23 #include "debug.h"
24 #include "misc.h"
25 
26 extern "C" {
27  #include <errno.h>
28  #include <sys/stat.h>
29 }
30 
31 #include <QDir>
32 
33 bool setUserOwnership(const QString &filePath)
34 {
35  const char *userHomePath = QDir::homePath().toLatin1().data();
36  struct stat fileInfo;
37  if (stat(userHomePath, &fileInfo) != 0)
38  return false;
39 
40  QByteArray filePathArray = filePath.toLocal8Bit();
41  const char *filePathStr = filePathArray.constData();
42  if (chown(filePathStr, fileInfo.st_uid, fileInfo.st_gid) != 0) {
43  BLAME() << "chown of" << filePathStr << "failed, errno:" << errno;
44  return false;
45  }
46 
47  return true;
48 }
49 
50 bool setFilePermissions(const QString &filePath,
51  const QFile::Permissions desiredPermissions,
52  bool keepExisting)
53 {
54  if (!QFile::exists(filePath)) return false;
55 
56  QFile::Permissions newPermissions = desiredPermissions;
57 
58  QFile file(filePath);
59  QFile::Permissions initialPermissions = file.permissions();
60 
61  if (keepExisting)
62  newPermissions |= initialPermissions;
63 
64  if (newPermissions != initialPermissions)
65  return file.setPermissions(newPermissions);
66 
67  return true;
68 }
69 
#define BLAME()
Definition: debug.h:32
bool setFilePermissions(const QString &filePath, const QFile::Permissions desiredPermissions, bool keepExisting)
Definition: misc.cpp:50
bool setUserOwnership(const QString &filePath)
Definition: misc.cpp:33