40 #include <config-prefix.h>
41 #include <config-kstandarddirs.h>
46 #ifdef HAVE_SYS_STAT_H
52 #include <sys/param.h>
53 #include <sys/types.h>
64 #define interface struct
67 #include <QtCore/QVarLengthArray>
70 #include <QtCore/QMutex>
71 #include <QtCore/QRegExp>
72 #include <QtCore/QDir>
73 #include <QtCore/QFileInfo>
74 #include <QtCore/QSettings>
76 class KStandardDirs::KStandardDirsPrivate
80 : m_restrictionsActive(false),
81 m_checkRestrictions(true),
86 bool hasDataRestrictions(
const QString &relPath)
const;
88 void createSpecialResource(
const char*);
90 bool m_restrictionsActive : 1;
91 bool m_checkRestrictions : 1;
188 "share/kde4/services\0"
190 "share/kde4/servicetypes\0"
204 "%lib/kde4/plugins\0"
206 "share/config.kcfg\0"
216 "desktop-directories\0"
220 "xdgconf-autostart\0"
225 0, 5, 16, 21, 36, 41, 53, 60,
226 73, 80, 94, 99, 112, 118, 131, 138,
227 151, 160, 180, 193, 217, 222, 236, 240,
228 248, 258, 275, 285, 301, 305, 309, 316,
229 326, 336, 354, 359, 377, 387, 403, 416,
230 429, 442, 448, 463, 471, 484, 504, 217,
231 517, 530, 536, 554, -1
238 : d(new KStandardDirsPrivate(this))
250 if (!d->m_restrictionsActive)
253 if (d->m_restrictions.value(type,
false))
256 if (strcmp(type,
"data")==0 && d->hasDataRestrictions(relPath))
262 bool KStandardDirs::KStandardDirsPrivate::hasDataRestrictions(
const QString &relPath)
const
265 const int i = relPath.indexOf(QLatin1Char(
'/'));
267 key = QString::fromLatin1(
"data_") + relPath.left(i);
269 key = QString::fromLatin1(
"data_") + relPath;
271 return m_restrictions.value(key.toLatin1(),
false);
281 list.append(QString::fromLatin1(
"lib"));
285 list.append(QString::fromLatin1(
"socket"));
286 list.append(QString::fromLatin1(
"tmp"));
287 list.append(QString::fromLatin1(
"cache"));
289 list.append(QString::fromLatin1(
"include"));
298 if (priority && !prefixes.isEmpty())
301 QStringList::iterator it = prefixes.begin();
303 prefixes.insert(it, dir);
307 prefixes.append(dir);
322 if (dir.at(dir.length() - 1) != QLatin1Char(
'/'))
323 dir += QLatin1Char(
'/');
325 if (!d->m_prefixes.contains(dir)) {
327 d->m_dircache.clear();
342 if (dir.at(dir.length() - 1) != QLatin1Char(
'/'))
343 dir += QLatin1Char(
'/');
345 if (!d->xdgconf_prefixes.contains(dir)) {
347 d->m_dircache.clear();
362 if (dir.at(dir.length() - 1) != QLatin1Char(
'/'))
363 dir += QLatin1Char(
'/');
365 if (!d->xdgdata_prefixes.contains(dir)) {
367 d->m_dircache.clear();
373 return d->m_prefixes.join(
QString(QLatin1Char(KPATH_SEPARATOR)));
378 return d->xdgconf_prefixes.join(
QString(QLatin1Char(KPATH_SEPARATOR)));
383 return d->xdgdata_prefixes.join(
QString(QLatin1Char(KPATH_SEPARATOR)));
386 #ifndef KDE_NO_DEPRECATED
396 const char *basetype,
400 if (relativename.isEmpty())
405 copy = QLatin1Char(
'%') + QString::fromLatin1(basetype) + QLatin1Char(
'/') + relativename;
407 if (!copy.endsWith(QLatin1Char(
'/')))
408 copy += QLatin1Char(
'/');
410 QByteArray typeBa = type;
413 if (!rels.contains(copy)) {
419 d->m_dircache.remove(typeBa);
420 d->m_savelocations.remove(typeBa);
430 if (absdir.isEmpty() || !type)
434 if (copy.at(copy.length() - 1) != QLatin1Char(
'/'))
435 copy += QLatin1Char(
'/');
437 QByteArray typeBa = type;
439 if (!paths.contains(copy)) {
445 d->m_dircache.remove(typeBa);
446 d->m_savelocations.remove(typeBa);
453 const QString& _filename )
const
455 if (!QDir::isRelativePath(_filename))
460 kDebug(180) <<
"Find resource: " << type;
461 for (QStringList::ConstIterator pit = m_prefixes.begin();
462 pit != m_prefixes.end();
465 kDebug(180) <<
"Prefix: " << *pit;
471 if(strcmp(type,
"exe") == 0) {
472 if(!filename.endsWith(QLatin1String(
".exe")))
473 filename += QLatin1String(
".exe");
486 KDE_struct_stat buff;
487 if ((
KDE::access(file, R_OK) == 0) && (
KDE::stat(file, &buff) == 0) && (S_ISREG(buff.st_mode))) {
488 hash = hash +
static_cast<quint32>(buff.st_ctime);
495 SearchOptions options )
const
499 if (!QDir::isRelativePath(filename))
504 QStringList candidates = d->resourceDirs(type, filename);
506 foreach (
const QString& candidate, candidates )
508 hash =
updateHash(candidate + filename, hash);
509 if ( !( options &
Recursive ) && hash ) {
522 if (!QDir::isRelativePath(reldir))
524 testdir.setPath(reldir);
525 if (testdir.exists())
527 if (reldir.endsWith(QLatin1Char(
'/')))
530 list.append(reldir+QLatin1Char(
'/'));
535 const QStringList candidates = d->resourceDirs(type, reldir);
537 for (QStringList::ConstIterator it = candidates.begin();
538 it != candidates.end(); ++it) {
539 testdir.setPath(*it + reldir);
540 if (testdir.exists())
541 list.append(testdir.absolutePath() + QLatin1Char(
'/'));
548 const QString& _filename)
const
551 if (_filename.isEmpty()) {
552 kWarning() <<
"filename for type " << type <<
" in KStandardDirs::findResourceDir is not supposed to be empty!!";
559 if(strcmp(type,
"exe") == 0) {
560 if(!filename.endsWith(QLatin1String(
".exe")))
561 filename += QLatin1String(
".exe");
564 const QStringList candidates = d->resourceDirs(type, filename);
566 for (QStringList::ConstIterator it = candidates.begin();
567 it != candidates.end(); ++it) {
568 if (
exists(*it + filename)) {
574 if(
false && strcmp(type,
"locale"))
575 kDebug(180) <<
"KStdDirs::findResDir(): can't find \"" << filename <<
"\" in type \"" << type <<
"\".";
587 if (fullPath.endsWith(QLatin1Char(
'/')))
588 return QDir(fullPath).exists();
589 return QFileInfo(fullPath).exists();
591 KDE_struct_stat buff;
592 QByteArray cFullPath = QFile::encodeName(fullPath);
593 if (
access(cFullPath, R_OK) == 0 && KDE_stat( cFullPath, &buff ) == 0) {
594 if (!fullPath.endsWith(QLatin1Char(
'/'))) {
595 if (S_ISREG( buff.st_mode ))
598 if (S_ISDIR( buff.st_mode ))
606 const QRegExp ®exp,
609 bool recursive,
bool unique)
611 const QString pattern = regexp.pattern();
612 if (recursive || pattern.contains(QLatin1Char(
'?')) || pattern.contains(QLatin1Char(
'*')))
617 QString path_ = path + QLatin1String(
"*.*" );
618 WIN32_FIND_DATA findData;
619 HANDLE hFile = FindFirstFile( (LPWSTR)path_.utf16(), &findData );
620 if( hFile == INVALID_HANDLE_VALUE )
623 const int len = wcslen( findData.cFileName );
624 if (!( findData.cFileName[0] ==
'.' &&
625 findData.cFileName[1] ==
'\0' ) &&
626 !( findData.cFileName[0] ==
'.' &&
627 findData.cFileName[1] ==
'.' &&
628 findData.cFileName[2] ==
'\0' ) &&
629 ( findData.cFileName[len-1] !=
'~' ) ) {
630 QString fn = QString::fromUtf16( (
const unsigned short*)findData.cFileName );
631 if (!recursive && !regexp.exactMatch(fn))
634 bool bIsDir = ( ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY );
638 relPart + fn + QLatin1Char(
'/'),
639 regexp, list, relList, recursive, unique);
641 if (!regexp.exactMatch(fn))
646 if ( !unique || !relList.contains(relPart + fn) )
648 list.append( pathfn );
649 relList.append( relPart + fn );
653 }
while( FindNextFile( hFile, &findData ) != 0 );
657 DIR *dp = opendir( QFile::encodeName(path));
661 assert(path.endsWith(QLatin1Char(
'/')));
665 while( ( ep = readdir( dp ) ) != 0L )
667 QString fn( QFile::decodeName(ep->d_name));
668 if (fn == QString::fromLatin1(
".") || fn == QString::fromLatin1(
"..") || fn.at(fn.length() - 1) == QLatin1Char(
'~'))
671 if (!recursive && !regexp.exactMatch(fn))
678 #ifdef HAVE_DIRENT_D_TYPE
679 isDir = ep->d_type == DT_DIR;
680 isReg = ep->d_type == DT_REG;
682 if (ep->d_type == DT_UNKNOWN || ep->d_type == DT_LNK)
685 KDE_struct_stat buff;
687 kDebug(180) <<
"Error stat'ing " << pathfn <<
" : " <<
perror;
690 isReg = S_ISREG (buff.st_mode);
691 isDir = S_ISDIR (buff.st_mode);
696 lookupDirectory(pathfn + QLatin1Char(
'/'), relPart + fn + QLatin1Char(
'/'), regexp, list, relList, recursive, unique);
698 if (!regexp.exactMatch(fn))
703 if (!unique || !relList.contains(relPart + fn))
705 list.append( pathfn );
706 relList.append( relPart + fn );
718 KDE_struct_stat buff;
721 if ( S_ISREG( buff.st_mode))
723 if (!unique || !relList.contains(relPart + fn))
725 list.append( pathfn );
726 relList.append( relPart + fn );
734 const QRegExp ®exp,
737 bool recursive,
bool unique)
739 if (relpath.isEmpty()) {
741 Q_ASSERT(prefix != QLatin1String(
"/"));
743 relList, recursive, unique);
749 int slash = relpath.indexOf(QLatin1Char(
'/'));
751 rest = relpath.left(relpath.length() - 1);
753 path = relpath.left(slash);
754 rest = relpath.mid(slash + 1);
757 if (prefix.isEmpty())
761 assert(prefix.endsWith(QLatin1Char(
'/')));
763 if (path.contains(QLatin1Char(
'*')) || path.contains(QLatin1Char(
'?'))) {
765 QRegExp pathExp(path, Qt::CaseSensitive, QRegExp::Wildcard);
768 QString prefix_ = prefix + QLatin1String(
"*.*" );
769 WIN32_FIND_DATA findData;
770 HANDLE hFile = FindFirstFile( (LPWSTR)prefix_.utf16(), &findData );
771 if( hFile == INVALID_HANDLE_VALUE )
774 const int len = wcslen( findData.cFileName );
775 if (!( findData.cFileName[0] ==
'.' &&
776 findData.cFileName[1] ==
'\0' ) &&
777 !( findData.cFileName[0] ==
'.' &&
778 findData.cFileName[1] ==
'.' &&
779 findData.cFileName[2] ==
'\0' ) &&
780 ( findData.cFileName[len-1] !=
'~' ) ) {
781 const QString fn = QString::fromUtf16( (
const unsigned short*)findData.cFileName );
782 if ( !pathExp.exactMatch(fn) )
784 if ( ( findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) == FILE_ATTRIBUTE_DIRECTORY )
786 rest, relPart + fn + QLatin1Char(
'/'),
787 regexp, list, relList, recursive, unique);
789 }
while( FindNextFile( hFile, &findData ) != 0 );
792 DIR *dp = opendir( QFile::encodeName(prefix) );
799 while( ( ep = readdir( dp ) ) != 0L )
801 QString fn( QFile::decodeName(ep->d_name));
802 if (fn == QLatin1String(
".") || fn == QLatin1String(
"..") || fn.at(fn.length() - 1) == QLatin1Char(
'~'))
805 if ( !pathExp.exactMatch(fn) )
812 #ifdef HAVE_DIRENT_D_TYPE
813 isDir = ep->d_type == DT_DIR;
815 if (ep->d_type == DT_UNKNOWN || ep->d_type == DT_LNK)
819 KDE_struct_stat buff;
821 kDebug(180) <<
"Error stat'ing " << fn <<
" : " <<
perror;
824 isDir = S_ISDIR (buff.st_mode);
827 lookupPrefix(fn + QLatin1Char(
'/'), rest, rfn + QLatin1Char(
'/'), regexp, list, relList, recursive, unique);
836 relPart + path + QLatin1Char(
'/'), regexp, list,
837 relList, recursive, unique);
844 SearchOptions options,
850 if ( !filter.isEmpty() )
852 int slash = filter.lastIndexOf(QLatin1Char(
'/'));
856 filterPath = filter.left(slash + 1);
857 filterFile = filter.mid(slash + 1);
862 if ( !QDir::isRelativePath(filter) )
865 candidates << filterPath.left(3);
866 filterPath = filterPath.mid(3);
868 candidates << QString::fromLatin1(
"/");
869 filterPath = filterPath.mid(1);
874 candidates = d->resourceDirs(type, filter);
877 if (filterFile.isEmpty()) {
878 filterFile =
QString(QLatin1Char(
'*'));
881 QRegExp regExp(filterFile, Qt::CaseSensitive, QRegExp::Wildcard);
884 foreach (
const QString& candidate, candidates )
896 SearchOptions options )
const
912 if (!strRet.endsWith(QLatin1Char(
'/')))
913 return strRet + QLatin1Char(
'/');
916 if (dirname.isEmpty() || (dirname.size() == 1 && dirname.at(0) == QLatin1Char(
'/')))
919 if (dirname.at(0) != QLatin1Char(
'/')) {
920 qWarning(
"realPath called with a relative path '%s', please fix", qPrintable(dirname));
924 char realpath_buffer[MAXPATHLEN + 1];
925 memset(realpath_buffer, 0, MAXPATHLEN + 1);
928 if (realpath( QFile::encodeName(dirname).constData(), realpath_buffer) != 0) {
930 int len = strlen(realpath_buffer);
931 realpath_buffer[len] =
'/';
932 realpath_buffer[len+1] = 0;
933 return QFile::decodeName(realpath_buffer);
941 if (!dir.endsWith(QLatin1Char(
'/')))
942 dir += QLatin1Char(
'/');
946 const int pos = dir.lastIndexOf(QLatin1Char(
'/'), -2);
948 relative.prepend(dir.mid(pos+1));
949 dir = dir.left(pos+1);
950 Q_ASSERT(dir.endsWith(QLatin1Char(
'/')));
952 Q_ASSERT(!relative.isEmpty());
953 if (!relative.isEmpty()) {
970 LPCWSTR lpIn = (LPCWSTR)filename.utf16();
971 QVarLengthArray<WCHAR, MAX_PATH> buf(MAX_PATH);
972 DWORD len = GetFullPathNameW(lpIn, buf.size(), buf.data(), NULL);
973 if (len > (DWORD)buf.size()) {
975 len = GetFullPathNameW(lpIn, buf.size(), buf.data(), NULL);
979 return QString::fromUtf16((
const unsigned short*)buf.data()).replace(QLatin1Char(
'\\'),QLatin1Char(
'/')).toLower();
981 char realpath_buffer[MAXPATHLEN + 1];
982 memset(realpath_buffer, 0, MAXPATHLEN + 1);
985 if (realpath( QFile::encodeName(filename).constData(), realpath_buffer) != 0) {
987 return QFile::decodeName(realpath_buffer);
995 void KStandardDirs::KStandardDirsPrivate::createSpecialResource(
const char *type)
999 gethostname(hostname, 255);
1000 const QString localkdedir = m_prefixes.first();
1001 QString dir = localkdedir + QString::fromLatin1(type) + QLatin1Char(
'-') + QString::fromLocal8Bit(hostname);
1004 int result = readlink(QFile::encodeName(dir).constData(), link, 1023);
1005 bool relink = (result == -1) && (errno == ENOENT);
1009 if (!QDir::isRelativePath(QFile::decodeName(link)))
1011 KDE_struct_stat stat_buf;
1012 int res =
KDE::lstat(QFile::decodeName(link), &stat_buf);
1013 if ((res == -1) && (errno == ENOENT))
1017 else if ((res == -1) || (!S_ISDIR(stat_buf.st_mode)))
1019 fprintf(stderr,
"Error: \"%s\" is not a directory.\n", link);
1022 else if (stat_buf.st_uid != getuid())
1024 fprintf(stderr,
"Error: \"%s\" is owned by uid %d instead of uid %d.\n", link, stat_buf.st_uid, getuid());
1032 if (!makeDir(dir, 0700))
1033 fprintf(stderr,
"failed to create \"%s\"", qPrintable(dir));
1035 result = readlink(QFile::encodeName(dir).constData(), link, 1023);
1040 QString srv = findExe(QLatin1String(
"lnusertemp"), installPath(
"libexec"));
1042 srv = findExe(QLatin1String(
"lnusertemp"));
1045 if (system(QByteArray(QFile::encodeName(srv) +
' ' + type)) == -1) {
1046 fprintf(stderr,
"Error: unable to launch lnusertemp command" );
1048 result = readlink(QFile::encodeName(dir).constData(), link, 1023);
1055 dir = QFile::decodeName(link);
1057 dir = QDir::cleanPath(dir + QFile::decodeName(link));
1060 q->addResourceDir(type, dir + QLatin1Char(
'/'),
false);
1065 return d->resourceDirs(type,
QString());
1070 QMutexLocker lock(&m_cacheMutex);
1071 const bool dataRestrictionActive = m_restrictionsActive
1072 && (strcmp(type,
"data") == 0)
1073 && hasDataRestrictions(subdirForRestrictions);
1079 if (dirCacheIt != m_dircache.constEnd() && !dataRestrictionActive) {
1081 candidates = *dirCacheIt;
1086 if (strcmp(type,
"socket") == 0)
1087 createSpecialResource(type);
1088 else if (strcmp(type,
"tmp") == 0)
1089 createSpecialResource(type);
1090 else if (strcmp(type,
"cache") == 0)
1091 createSpecialResource(type);
1095 bool restrictionActive =
false;
1096 if (m_restrictionsActive) {
1097 if (dataRestrictionActive)
1098 restrictionActive =
true;
1099 if (m_restrictions.value(
"all",
false))
1100 restrictionActive =
true;
1101 else if (m_restrictions.value(type,
false))
1102 restrictionActive =
true;
1106 dirs = m_relatives.value(type);
1107 const QString typeInstallPath = installPath(type);
1110 const QString installdir = typeInstallPath.isEmpty() ?
QString() : realPath(typeInstallPath).toLower();
1111 const QString installprefix = installPath(
"kdedir").toLower();
1113 const QString installdir = typeInstallPath.isEmpty() ?
QString() : realPath(typeInstallPath);
1114 const QString installprefix = installPath(
"kdedir");
1116 if (!dirs.isEmpty())
1120 for (QStringList::ConstIterator it = dirs.constBegin();
1121 it != dirs.constEnd(); ++it)
1123 if ((*it).startsWith(QLatin1Char(
'%'))) {
1125 const int pos = (*it).indexOf(QLatin1Char(
'/'));
1126 QString rel = (*it).mid(1, pos - 1);
1127 QString rest = (*it).mid(pos + 1);
1128 const QStringList basedirs = resourceDirs(rel.toUtf8().constData(), subdirForRestrictions);
1129 for (QStringList::ConstIterator it2 = basedirs.begin();
1130 it2 != basedirs.end(); ++it2)
1133 const QString path = realPath( *it2 + rest ).toLower();
1135 const QString path = realPath( *it2 + rest );
1137 testdir.setPath(path);
1138 if ((local || testdir.exists()) && !candidates.contains(path))
1139 candidates.append(path);
1146 if (strncmp(type,
"xdgdata-", 8) == 0)
1147 prefixList = &(xdgdata_prefixes);
1148 else if (strncmp(type,
"xdgconf-", 8) == 0)
1149 prefixList = &(xdgconf_prefixes);
1151 prefixList = &m_prefixes;
1153 for (QStringList::ConstIterator pit = prefixList->begin();
1154 pit != prefixList->end();
1159 if((*pit)!=installprefix||installdir.isEmpty()||!strcmp(
"exe", type))
1161 for (QStringList::ConstIterator it = dirs.constBegin();
1162 it != dirs.constEnd(); ++it)
1164 if ((*it).startsWith(QLatin1Char(
'%')))
1167 const QString path = realPath( *pit + *it ).toLower();
1169 const QString path = realPath( *pit + *it );
1171 testdir.setPath(path);
1172 if (local && restrictionActive)
1174 if ((local || testdir.exists()) && !candidates.contains(path))
1175 candidates.append(path);
1180 if (local && !strcmp(
"config", type))
1181 candidates.append(QLatin1String(
"/etc/kde/"));
1187 testdir.setPath(installdir);
1188 if(testdir.exists() && ! candidates.contains(installdir))
1189 candidates.append(installdir);
1195 if (!installdir.isEmpty()) {
1197 foreach (
const QString &s, candidates) {
1198 if (installdir.startsWith(s)) {
1204 candidates.append(installdir);
1207 dirs = m_absolutes.value(type);
1208 if (!dirs.isEmpty())
1209 for (QStringList::ConstIterator it = dirs.constBegin();
1210 it != dirs.constEnd(); ++it)
1212 testdir.setPath(*it);
1213 if (testdir.exists()) {
1215 const QString filename = realPath( *it ).toLower();
1217 const QString filename = realPath( *it );
1219 if (!candidates.contains(filename)) {
1220 candidates.append(filename);
1227 if (!dataRestrictionActive) {
1229 m_dircache.insert(type, candidates);
1234 kDebug(180) <<
"found dirs for resource" << type <<
":" << candidates;
1243 QStringList ret = QString::fromLocal8Bit(qgetenv(
"PATHEXT")).split(QLatin1Char(
';'));
1244 if (!ret.contains(QLatin1String(
".exe"), Qt::CaseInsensitive)) {
1247 ret << QLatin1String(
".exe")
1248 << QLatin1String(
".com")
1249 << QLatin1String(
".bat")
1250 << QLatin1String(
".cmd");
1263 p = QString::fromLocal8Bit( qgetenv(
"PATH" ) );
1266 QString delimiters(QLatin1Char(KPATH_SEPARATOR));
1267 delimiters += QLatin1Char(
'\b');
1273 for(
int i = 0; i < tokens.count(); i++ )
1287 bundle += QLatin1String(
".app/Contents/MacOS/") + bundle.section(QLatin1Char(
'/'), -1);
1288 info.setFile( bundle );
1290 if (file =
fopen(info.absoluteFilePath().toUtf8().constData(),
"r")) {
1293 if ((
stat(info.absoluteFilePath().toUtf8().constData(), &_stat)) < 0) {
1296 if ( ignore || (_stat.st_mode & S_IXUSR) ) {
1297 if ( ((_stat.st_mode & S_IFMT) == S_IFREG) || ((_stat.st_mode & S_IFMT) == S_IFLNK) ) {
1311 if ( !bundle.isEmpty() ) {
1316 QFileInfo info( path );
1317 QFileInfo orig = info;
1318 #if defined(Q_OS_DARWIN) || defined(Q_OS_MAC)
1320 if (file =
fopen(orig.absoluteFilePath().toUtf8().constData(),
"r")) {
1323 if ((
stat(orig.absoluteFilePath().toUtf8().constData(), &_stat)) < 0) {
1326 if ( ignoreExecBit || (_stat.st_mode & S_IXUSR) ) {
1327 if ( ((_stat.st_mode & S_IFMT) == S_IFREG) || ((_stat.st_mode & S_IFMT) == S_IFLNK) ) {
1328 orig.makeAbsolute();
1329 return orig.filePath();
1335 if( info.exists() && info.isSymLink() )
1336 info = QFileInfo( info.canonicalFilePath() );
1337 if( info.exists() && ( ignoreExecBit || info.isExecutable() ) && info.isFile() ) {
1341 orig.makeAbsolute();
1342 return orig.filePath();
1351 SearchOptions options )
1357 if (!executable_extensions.contains(appname.section(QLatin1Char(
'.'), -1, -1, QString::SectionIncludeLeadingSep), Qt::CaseInsensitive)) {
1359 foreach (
const QString& extension, executable_extensions) {
1360 found_exe =
findExe(appname + extension, pstr, options);
1361 if (!found_exe.isEmpty()) {
1371 if (appname.contains(QDir::separator()))
1382 if (!result.isEmpty()) {
1389 for (QStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
1391 p = (*it) + QLatin1Char(
'/');
1396 if (!result.isEmpty()) {
1406 if (!result.isEmpty()) {
1419 const QString& pstr, SearchOptions options )
1423 if (!executable_extensions.contains(appname.section(QLatin1Char(
'.'), -1, -1, QString::SectionIncludeLeadingSep), Qt::CaseInsensitive)) {
1425 foreach (
const QString& extension, executable_extensions) {
1426 total +=
findAllExe (list, appname + extension, pstr, options);
1436 for (QStringList::ConstIterator it = exePaths.begin(); it != exePaths.end(); ++it)
1438 p = (*it) + QLatin1Char(
'/');
1443 if ( !bundle.isEmpty() ) {
1445 list.append( bundle );
1451 if( info.exists() && ( ( options &
IgnoreExecBit ) || info.isExecutable())
1452 && info.isFile() ) {
1457 return list.count();
1467 return f.absoluteFilePath();
1476 const int len = str.length();
1479 for(
int index = 0; index < len; index++) {
1480 if (delim.contains(str[index])) {
1484 token += str[index];
1487 if (!token.isEmpty()) {
1492 #ifndef KDE_NO_DEPRECATED
1495 return QString(QLatin1Char(
'%')) + QString::fromLatin1(type) + QLatin1Char(
'/');
1503 QMutexLocker lock(&d->m_cacheMutex);
1504 QString path = d->m_savelocations.value(type);
1508 if (dirs.isEmpty() && (
1509 (strcmp(type,
"socket") == 0) ||
1510 (strcmp(type,
"tmp") == 0) ||
1511 (strcmp(type,
"cache") == 0) ))
1514 dirs = d->m_relatives.value(type);
1516 if (!dirs.isEmpty())
1518 path = dirs.first();
1520 if (path.startsWith(QLatin1Char(
'%'))) {
1522 const int pos = path.indexOf(QLatin1Char(
'/'));
1523 QString rel = path.mid(1, pos - 1);
1524 QString rest = path.mid(pos + 1);
1526 path = basepath + rest;
1530 if (strncmp(type,
"xdgdata-", 8) == 0) {
1532 }
else if (strncmp(type,
"xdgconf-", 8) == 0) {
1539 dirs = d->m_absolutes.value(type);
1540 if (dirs.isEmpty()) {
1541 qFatal(
"KStandardDirs: The resource type %s is not registered", type);
1547 d->m_savelocations.insert(type, path.endsWith(QLatin1Char(
'/')) ? path : path + QLatin1Char(
'/'));
1549 QString fullPath = path + suffix;
1552 if (
KDE::stat(fullPath, &st) != 0 || !(S_ISDIR(st.st_mode))) {
1561 if(!
makeDir(fullPath, 0700)) {
1564 d->m_dircache.remove(type);
1566 if (!fullPath.endsWith(QLatin1Char(
'/')))
1567 fullPath += QLatin1Char(
'/');
1575 int i = absPath.lastIndexOf(QLatin1Char(
'/'));
1582 for (QStringList::ConstIterator it = candidates.begin();
1583 it != candidates.end(); ++it) {
1584 if (fullPath.startsWith(*it)) {
1585 return fullPath.mid((*it).length());
1595 if (QDir::isRelativePath(dir))
1599 return QDir().mkpath(dir);
1602 uint len = target.length();
1605 if (dir.at(len - 1) != QLatin1Char(
'/'))
1606 target += QLatin1Char(
'/');
1614 int pos = target.indexOf(QLatin1Char(
'/'), i);
1615 base += target.mid(i - 1, pos - i + 1);
1616 QByteArray baseEncoded = QFile::encodeName(base);
1618 if (KDE_stat(baseEncoded, &st) != 0)
1622 if (KDE_lstat(baseEncoded, &st) == 0)
1623 (void)unlink(baseEncoded);
1625 if (KDE_mkdir(baseEncoded, static_cast<mode_t>(mode)) != 0) {
1626 baseEncoded.prepend(
"trying to create local folder " );
1627 perror(baseEncoded.constData());
1641 c_path = qgetenv(env);
1642 if (c_path.isEmpty())
1650 c_path = retval.toAscii();
1653 return QDir::fromNativeSeparators(QFile::decodeName(c_path));
1657 static QString executablePrefix()
1659 char path_buffer[MAXPATHLEN + 1];
1660 path_buffer[MAXPATHLEN] = 0;
1661 int length = readlink (
"/proc/self/exe", path_buffer, MAXPATHLEN);
1665 path_buffer[length] =
'\0';
1667 QString path = QFile::decodeName(path_buffer);
1672 int pos = path.lastIndexOf(QLatin1Char(
'/'));
1675 pos = path.lastIndexOf(QLatin1Char(
'/'), pos - 1);
1679 return path.left(pos);
1683 void KStandardDirs::addResourcesFrom_krcdirs()
1685 QString localFile = QDir::currentPath() + QLatin1String(
"/.krcdirs");
1686 if (!QFile::exists(localFile))
1689 QSettings iniFile(localFile, QSettings::IniFormat);
1690 iniFile.beginGroup(QString::fromLatin1(
"KStandardDirs"));
1692 foreach(
const QString &key, resources)
1694 QDir path(iniFile.value(key).toString());
1698 if(path.makeAbsolute())
1703 void KStandardDirs::addKDEDefaults()
1705 addResourcesFrom_krcdirs();
1711 if (!kdedirs.isEmpty())
1717 QString execPrefix(QFile::decodeName(EXEC_INSTALL_PREFIX));
1718 if (!execPrefix.isEmpty() && !kdedirList.contains(execPrefix))
1719 kdedirList.append(execPrefix);
1721 const QString linuxExecPrefix = executablePrefix();
1722 if ( !linuxExecPrefix.isEmpty() )
1723 kdedirList.append( linuxExecPrefix );
1729 if (!localKdeDir.isEmpty()) {
1730 if (!localKdeDir.endsWith(QLatin1Char(
'/')))
1731 localKdeDir += QLatin1Char(
'/');
1737 #if defined(Q_WS_MACX)
1738 localKdeDir = QDir::homePath() + QLatin1String(
"/Library/Preferences/KDE/");
1739 #elif defined(Q_WS_WIN)
1741 WCHAR wPath[MAX_PATH+1];
1742 if ( SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, wPath) == S_OK) {
1743 localKdeDir = QDir::fromNativeSeparators(QString::fromUtf16((
const ushort *) wPath)) + QLatin1Char(
'/') + QString::fromLatin1(KDE_DEFAULT_HOME) + QLatin1Char(
'/');
1746 localKdeDir = QDir::homePath() + QLatin1Char(
'/') + QString::fromLatin1(KDE_DEFAULT_HOME) + QLatin1Char(
'/');
1751 localKdeDir = QDir::homePath() + QLatin1Char(
'/') + QString::fromLatin1(KDE_DEFAULT_HOME) + QLatin1Char(
'/');
1755 if (localKdeDir != QLatin1String(
"-/"))
1765 if (bundleDir.dirName() == QLatin1String(
"MacOS")) {
1773 QStringList::ConstIterator end(kdedirList.end());
1774 for (QStringList::ConstIterator it = kdedirList.constBegin();
1775 it != kdedirList.constEnd(); ++it)
1785 if (!xdgdirs.isEmpty())
1792 xdgdirList.append(QString::fromLatin1(
"/etc/xdg"));
1794 xdgdirList.append(
installPath(
"kdedir") + QString::fromLatin1(
"etc/xdg"));
1796 xdgdirList.append(QFile::decodeName(KDESYSCONFDIR
"/xdg"));
1801 if (!localXdgDir.isEmpty()) {
1802 if (!localXdgDir.endsWith(QLatin1Char(
'/')))
1803 localXdgDir += QLatin1Char(
'/');
1806 localXdgDir = QDir::homePath() + QString::fromLatin1(
"/Library/Preferences/XDG/");
1808 localXdgDir = QDir::homePath() + QString::fromLatin1(
"/.config/");
1815 for (QStringList::ConstIterator it = xdgdirList.constBegin();
1816 it != xdgdirList.constEnd(); ++it)
1825 for (QStringList::ConstIterator it = kdedirList.constBegin();
1826 it != kdedirList.constEnd(); ++it) {
1828 if (!dir.endsWith(QLatin1Char(
'/')))
1829 dir += QLatin1Char(
'/');
1830 kdedirDataDirs.append(dir + QLatin1String(
"share/"));
1834 if (!xdgdirs.isEmpty()) {
1839 Q_FOREACH(
const QString& dir, kdedirDataDirs) {
1840 if (!xdgdirList.contains(dir))
1841 xdgdirList.append(dir);
1844 xdgdirList = kdedirDataDirs;
1846 xdgdirList.append(QString::fromLatin1(
"/usr/local/share/"));
1847 xdgdirList.append(QString::fromLatin1(
"/usr/share/"));
1852 if (!localXdgDir.isEmpty())
1854 if (localXdgDir[localXdgDir.length()-1] != QLatin1Char(
'/'))
1855 localXdgDir += QLatin1Char(
'/');
1859 localXdgDir = QDir::homePath() + QLatin1String(
"/.local/share/");
1865 for (QStringList::ConstIterator it = xdgdirList.constBegin();
1866 it != xdgdirList.constEnd(); ++it)
1893 if (mapFile.isEmpty() || !QFile::exists(mapFile))
1895 profiles << QString::fromLatin1(
"default");
1899 struct passwd *pw = getpwuid(geteuid());
1902 profiles << QString::fromLatin1(
"default");
1906 QByteArray user = pw->pw_name;
1908 gid_t sup_gids[512];
1909 int sup_gids_nr = getgroups(512, sup_gids);
1913 if (mapCfg.
hasKey(user.constData()))
1924 for( QStringList::ConstIterator it = groups.begin();
1925 it != groups.end(); ++it )
1927 QByteArray grp = (*it).toUtf8();
1929 struct group *grp_ent = getgrnam(grp);
1930 if (!grp_ent)
continue;
1931 gid_t gid = grp_ent->gr_gid;
1932 if (pw->pw_gid == gid)
1939 for(
int i = 0; i < sup_gids_nr; i++)
1941 if (sup_gids[i] == gid)
1951 if (profiles.isEmpty())
1952 profiles << QString::fromLatin1(
"default");
1960 if (!d->m_checkRestrictions)
1976 int i = kioskAdmin.indexOf(QLatin1Char(
':'));
1977 QString user = kioskAdmin.left(i);
1978 QString host = kioskAdmin.mid(i+1);
1981 char hostname[ 256 ];
1982 hostname[ 0 ] =
'\0';
1983 if (!gethostname( hostname, 255 ))
1984 hostname[
sizeof(hostname)-1] =
'\0';
1987 (host.isEmpty() || (host == QLatin1String(hostname))))
1989 kde_kiosk_admin =
true;
1993 bool readProfiles =
true;
1995 if (kde_kiosk_admin && !qgetenv(
"KDE_KIOSK_NO_PROFILES").isEmpty())
1996 readProfiles =
false;
2000 if (!profileDirsPrefix.isEmpty() && !profileDirsPrefix.endsWith(QLatin1Char(
'/')))
2001 profileDirsPrefix.append(QLatin1Char(
'/'));
2008 bool priority =
false;
2013 for (QStringList::ConstIterator it = list.begin(); it != list.end(); ++it)
2021 if (list.isEmpty() && !profile.isEmpty() && !profileDirsPrefix.isEmpty())
2023 QString dir = profileDirsPrefix + profile;
2033 it2 != entries.end(); ++it2)
2035 const QString key = it2.key();
2036 if (key.startsWith(QLatin1String(
"dir_"))) {
2039 QStringList::ConstIterator sIt(dirs.begin());
2041 for (; sIt != dirs.end(); ++sIt)
2047 if (profiles.isEmpty())
2049 profile = profiles.back();
2050 group = QString::fromLatin1(
"Directories-%1").arg(profile);
2051 profiles.pop_back();
2057 if (!kde_kiosk_admin || qgetenv(
"KDE_KIOSK_NO_RESTRICTIONS").isEmpty())
2062 it2 != entries.end(); ++it2)
2064 const QString key = it2.key();
2067 d->m_restrictionsActive =
true;
2068 const QByteArray cKey = key.toLatin1();
2069 d->m_restrictions.insert(cKey,
true);
2070 d->m_dircache.remove(cKey);
2071 d->m_savelocations.remove(cKey);
2077 bool configDirsChanged = (
resourceDirs(
"config").count() != configdirs);
2079 d->m_checkRestrictions = configDirsChanged;
2081 return configDirsChanged;
2087 return d->m_prefixes.first();
2093 return d->xdgdata_prefixes.first();
2099 return d->xdgconf_prefixes.first();
2117 const QString& filename,
bool createDir,
2122 int slash = filename.lastIndexOf(QLatin1Char(
'/')) + 1;
2128 QString dir = filename.left(slash);
2129 QString file = filename.mid(slash);
2136 if ( accessOK == 0 )
2142 if ( (mode & W_OK) == 0 )
2151 int pos = dirName.lastIndexOf(QLatin1Char(
'/'));
2154 else if ( pos == 0 )
2157 dirName.truncate(pos);
2161 if ( accessOK == 0 )