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 <unistd.h>
29  #include <sys/stat.h>
30 }
31 
32 #include <QDir>
33 
34 bool setUserOwnership(const QString &filePath)
35 {
36  const char *userHomePath = QDir::homePath().toLatin1().data();
37  struct stat fileInfo;
38  if (stat(userHomePath, &fileInfo) != 0)
39  return false;
40 
41  QByteArray filePathArray = filePath.toLocal8Bit();
42  const char *filePathStr = filePathArray.constData();
43  if (chown(filePathStr, fileInfo.st_uid, fileInfo.st_gid) != 0) {
44  BLAME() << "chown of" << filePathStr << "failed, errno:" << errno;
45  return false;
46  }
47 
48  return true;
49 }
50 
51 bool setFilePermissions(const QString &filePath,
52  const QFile::Permissions desiredPermissions,
53  bool keepExisting)
54 {
55  if (!QFile::exists(filePath)) return false;
56 
57  QFile::Permissions newPermissions = desiredPermissions;
58 
59  QFile file(filePath);
60  QFile::Permissions initialPermissions = file.permissions();
61 
62  if (keepExisting)
63  newPermissions |= initialPermissions;
64 
65  if (newPermissions != initialPermissions)
66  return file.setPermissions(newPermissions);
67 
68  return true;
69 }
70 
#define BLAME()
Definition: debug.h:32
bool setFilePermissions(const QString &filePath, const QFile::Permissions desiredPermissions, bool keepExisting)
Definition: misc.cpp:51
bool setUserOwnership(const QString &filePath)
Definition: misc.cpp:34