Changeset View
Standalone View
src/server/utils.cpp
Show All 27 Lines | |||||
28 | #include <QFileInfo> | 28 | #include <QFileInfo> | ||
29 | #include <QSettings> | 29 | #include <QSettings> | ||
30 | #include <QHostInfo> | 30 | #include <QHostInfo> | ||
31 | 31 | | |||
32 | #if !defined(Q_OS_WIN) | 32 | #if !defined(Q_OS_WIN) | ||
33 | #include <cstdlib> | 33 | #include <cstdlib> | ||
34 | #include <sys/types.h> | 34 | #include <sys/types.h> | ||
35 | #include <cerrno> | 35 | #include <cerrno> | ||
36 | #include <pwd.h> | | |||
37 | #include <unistd.h> | 36 | #include <unistd.h> | ||
38 | 37 | | |||
39 | static QString akonadiSocketDirectory(); | 38 | static QString akonadiSocketDirectory(); | ||
40 | static bool checkSocketDirectory(const QString &path); | 39 | static bool checkSocketDirectory(const QString &path); | ||
41 | static bool createSocketDirectory(const QString &link, const QString &tmpl); | 40 | static bool createSocketDirectory(const QString &link, const QString &tmpl); | ||
42 | #endif | 41 | #endif | ||
43 | 42 | | |||
44 | #ifdef Q_OS_LINUX | 43 | #ifdef Q_OS_LINUX | ||
Show All 18 Lines | 60 | #else | |||
63 | if (!serverSettings.contains(QStringLiteral("Connection/SocketDirectory"))) { | 62 | if (!serverSettings.contains(QStringLiteral("Connection/SocketDirectory"))) { | ||
64 | // if no socket directory is defined, use the symlinked from /tmp | 63 | // if no socket directory is defined, use the symlinked from /tmp | ||
65 | socketDir = akonadiSocketDirectory(); | 64 | socketDir = akonadiSocketDirectory(); | ||
66 | 65 | | |||
67 | if (socketDir.isEmpty()) { // if that does not work, fall back on default | 66 | if (socketDir.isEmpty()) { // if that does not work, fall back on default | ||
68 | socketDir = defaultDirectory; | 67 | socketDir = defaultDirectory; | ||
69 | } | 68 | } | ||
70 | } else { | 69 | } else { | ||
71 | socketDir = serverSettings.value(QStringLiteral("Connection/SocketDirectory"), defaultDirectory).toString(); | 70 | socketDir = serverSettings.value(QStringLiteral("Connection/SocketDirectory"), defaultDirectory).toString(); | ||
dfaure: should be >=, you need room for the trailing NUL character as well. | |||||
72 | } | 71 | } | ||
73 | 72 | | |||
dfaure: Let's hope that one does fit in the length limit :-) | |||||
74 | if (socketDir.contains(QLatin1String("$USER"))) { | 73 | if (socketDir.contains(QLatin1String("$USER"))) { | ||
75 | const QString userName = QString::fromLocal8Bit(qgetenv("USER")); | 74 | const QString userName = QString::fromLocal8Bit(qgetenv("USER")); | ||
76 | if (!userName.isEmpty()) { | 75 | if (!userName.isEmpty()) { | ||
77 | socketDir.replace(QLatin1String("$USER"), userName); | 76 | socketDir.replace(QLatin1String("$USER"), userName); | ||
78 | } | 77 | } | ||
79 | } | 78 | } | ||
80 | 79 | | |||
81 | if (socketDir[0] != QLatin1Char('/')) { | 80 | if (socketDir[0] != QLatin1Char('/')) { | ||
Show All 14 Lines | |||||
96 | { | 95 | { | ||
97 | const QString hostname = QHostInfo::localHostName(); | 96 | const QString hostname = QHostInfo::localHostName(); | ||
98 | 97 | | |||
99 | if (hostname.isEmpty()) { | 98 | if (hostname.isEmpty()) { | ||
100 | qCCritical(AKONADISERVER_LOG) << "QHostInfo::localHostName() failed"; | 99 | qCCritical(AKONADISERVER_LOG) << "QHostInfo::localHostName() failed"; | ||
101 | return QString(); | 100 | return QString(); | ||
102 | } | 101 | } | ||
103 | 102 | | |||
104 | const uid_t uid = getuid(); | | |||
105 | const struct passwd *pw_ent = getpwuid(uid); | | |||
106 | if (!pw_ent) { | | |||
107 | qCCritical(AKONADISERVER_LOG) << "Could not get passwd entry for user id" << uid; | | |||
108 | return QString(); | | |||
109 | } | | |||
110 | | ||||
111 | const QString link = StandardDirs::saveDir("data") + QLatin1Char('/') + QLatin1String("socket-") + hostname; | 103 | const QString link = StandardDirs::saveDir("data") + QLatin1Char('/') + QLatin1String("socket-") + hostname; | ||
112 | QString tmpl = QLatin1String("akonadi-") + QString::fromLocal8Bit(pw_ent->pw_name) + QLatin1String(".XXXXXX"); | 104 | QString tmpl = QLatin1String("XXXXXX"); | ||
Since on Linux you are now creating the directory in /var/run/user/<uid>/, I'd very much prefer to keep akonadi in the name... Actually, since you cannot have two same instances of Akonadi running at the same time, it should be perfectly fine to call the folder akonadi for the default instance and akonadi-<instancename> when Akonadi::Instance::hasIdentifier() is true, no more random strings. This would make the directory deterministic for each instance, simplifying the code. dvratil: Since on Linux you are now creating the directory in `/var/run/user/<uid>/`, I'd very much… | |||||
createSocketDirectory is calling saveDir("runtime"), which already append "akonadi" tho the runtime path, ie "/path/to/runtime/akonadi", which result to path like "path/tp/runtime/akonadi/TMPL". I could then just use fixed (no) template name like 'default' and 'identifier Name' in this folder then ? fazevedo: createSocketDirectory is calling saveDir("runtime"), which already append "akonadi" tho the… | |||||
113 | 105 | | |||
114 | // Workaround for QLocalServer encoding bug | 106 | // Workaround for QLocalServer encoding bug | ||
115 | // basically replace non-latin characters | 107 | // basically replace non-latin characters | ||
116 | tmpl = QString::fromLatin1(tmpl.toLatin1()); | 108 | tmpl = QString::fromLatin1(tmpl.toLatin1()); | ||
dfaure: This line is no longer necessary. | |||||
117 | 109 | | |||
Don't use QDir::separator(). That's '\' on Windows, while all this code uses '/' everywhere. Just make it "/socket..." (the existing code is the result of automated conversion) dfaure: Don't use QDir::separator(). That's '\' on Windows, while all this code uses '/' everywhere. | |||||
118 | if (checkSocketDirectory(link)) { | 110 | if (checkSocketDirectory(link)) { | ||
119 | return QFileInfo(link).symLinkTarget(); | 111 | return QFileInfo(link).symLinkTarget(); | ||
120 | } | 112 | } | ||
121 | 113 | | |||
122 | if (createSocketDirectory(link, tmpl)) { | 114 | if (createSocketDirectory(link, tmpl)) { | ||
123 | return QFileInfo(link).symLinkTarget(); | 115 | return QFileInfo(link).symLinkTarget(); | ||
124 | } | 116 | } | ||
125 | 117 | | |||
Show All 21 Lines | 138 | if (info.ownerId() != getuid()) { | |||
147 | return false; | 139 | return false; | ||
148 | } | 140 | } | ||
149 | 141 | | |||
150 | return true; | 142 | return true; | ||
151 | } | 143 | } | ||
152 | 144 | | |||
153 | static bool createSocketDirectory(const QString &link, const QString &tmpl) | 145 | static bool createSocketDirectory(const QString &link, const QString &tmpl) | ||
154 | { | 146 | { | ||
155 | QString directory = QStringLiteral("%1%2%3").arg(QDir::tempPath()).arg(QDir::separator()).arg(tmpl); | 147 | QString directory = QStringLiteral("%1%2%3").arg(StandardDirs::saveDir("runtime")).arg(QDir::separator()).arg(tmpl); | ||
dfaure: %1/%2, no need for QDir::separator, this uses Qt APIs, not native APIs. | |||||
156 | 148 | | |||
157 | QByteArray directoryString = directory.toLocal8Bit(); | 149 | QByteArray directoryString = directory.toLocal8Bit(); | ||
158 | 150 | | |||
159 | if (!mkdtemp(directoryString.data())) { | 151 | if (!mkdtemp(directoryString.data())) { | ||
Once you change to the deterministic format, mkdtemp can be switched to QDir::mkpath() dvratil: Once you change to the deterministic format, `mkdtemp ` can be switched to `QDir::mkpath()` | |||||
160 | qCCritical(AKONADISERVER_LOG) << "Creating socket directory with template" << directoryString << "failed:" << strerror(errno); | 152 | qCCritical(AKONADISERVER_LOG) << "Creating socket directory with template" << directoryString << "failed:" << strerror(errno); | ||
161 | return false; | 153 | return false; | ||
162 | } | 154 | } | ||
163 | 155 | | |||
164 | directory = QString::fromLocal8Bit(directoryString); | 156 | directory = QString::fromLocal8Bit(directoryString); | ||
165 | 157 | | |||
166 | QFile::remove(link); | 158 | QFile::remove(link); | ||
167 | 159 | | |||
168 | if (!QFile::link(directory, link)) { | 160 | if (!QFile::link(directory, link)) { | ||
169 | qCCritical(AKONADISERVER_LOG) << "Creating symlink from" << directory << "to" << link << "failed"; | 161 | qCCritical(AKONADISERVER_LOG) << "Creating symlink from" << directory << "to" << link << "failed"; | ||
170 | return false; | 162 | return false; | ||
171 | } | 163 | } | ||
172 | 164 | | |||
173 | return true; | 165 | return true; | ||
174 | } | 166 | } | ||
175 | #endif | 167 | #endif | ||
176 | 168 | | |||
177 | QString Utils::getDirectoryFileSystem(const QString &directory) | 169 | QString Utils::getDirectoryFileSystem(const QString &directory) | ||
178 | { | 170 | { | ||
179 | #ifndef Q_OS_LINUX | 171 | #ifndef Q_OS_LINUX | ||
172 | Q_UNUSED(directory); | ||||
180 | return QString(); | 173 | return QString(); | ||
181 | #else | 174 | #else | ||
182 | QString bestMatchPath; | 175 | QString bestMatchPath; | ||
183 | QString bestMatchFS; | 176 | QString bestMatchFS; | ||
184 | 177 | | |||
185 | FILE *mtab = setmntent("/etc/mtab", "r"); | 178 | FILE *mtab = setmntent("/etc/mtab", "r"); | ||
186 | if (!mtab) { | 179 | if (!mtab) { | ||
187 | return QString(); | 180 | return QString(); | ||
▲ Show 20 Lines • Show All 64 Lines • Show Last 20 Lines |
should be >=, you need room for the trailing NUL character as well.