2011-08-20 03:59:46 -07:00
|
|
|
/**
|
|
|
|
* OpenAL cross platform audio library
|
|
|
|
* Copyright (C) 2011 by authors.
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
2014-08-18 14:11:03 +02:00
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2011-08-20 03:59:46 -07:00
|
|
|
* Or go to http://www.gnu.org/copyleft/lgpl.html
|
|
|
|
*/
|
|
|
|
|
2014-02-27 01:49:23 -08:00
|
|
|
#ifdef _WIN32
|
|
|
|
#ifdef __MINGW32__
|
|
|
|
#define _WIN32_IE 0x501
|
|
|
|
#else
|
|
|
|
#define _WIN32_IE 0x400
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2011-08-20 03:59:46 -07:00
|
|
|
#include "config.h"
|
|
|
|
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <algorithm>
|
2019-01-09 19:42:40 +01:00
|
|
|
#include <cerrno>
|
|
|
|
#include <cstdarg>
|
2019-07-28 11:28:36 -07:00
|
|
|
#include <cstdlib>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
|
|
|
#include <mutex>
|
|
|
|
#include <string>
|
|
|
|
|
2015-10-03 20:41:18 -07:00
|
|
|
#ifdef HAVE_DIRENT_H
|
|
|
|
#include <dirent.h>
|
|
|
|
#endif
|
2014-01-18 19:16:46 -08:00
|
|
|
#ifdef HAVE_SYS_SYSCONF_H
|
|
|
|
#include <sys/sysconf.h>
|
|
|
|
#endif
|
2012-10-06 21:30:03 -07:00
|
|
|
|
2019-08-12 14:56:17 -07:00
|
|
|
#ifdef HAVE_PROC_PIDPATH
|
|
|
|
#include <libproc.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef __FreeBSD__
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#endif
|
|
|
|
|
2015-10-06 01:26:15 -07:00
|
|
|
#ifndef _WIN32
|
|
|
|
#include <unistd.h>
|
|
|
|
#elif defined(_WIN32_IE)
|
2014-02-27 01:49:23 -08:00
|
|
|
#include <shlobj.h>
|
|
|
|
#endif
|
|
|
|
|
2019-07-28 18:33:29 -07:00
|
|
|
#include "alcmain.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "almalloc.h"
|
2019-09-23 19:23:33 -07:00
|
|
|
#include "alfstream.h"
|
2019-10-24 15:10:37 -07:00
|
|
|
#include "alspan.h"
|
2019-09-16 15:10:36 -07:00
|
|
|
#include "alstring.h"
|
2019-07-28 11:28:36 -07:00
|
|
|
#include "compat.h"
|
|
|
|
#include "logging.h"
|
2019-08-11 18:50:07 -07:00
|
|
|
#include "strutils.h"
|
2019-08-05 18:36:39 -07:00
|
|
|
#include "vector.h"
|
2011-08-20 03:59:46 -07:00
|
|
|
|
2013-11-04 12:12:31 -08:00
|
|
|
|
2011-08-20 03:59:46 -07:00
|
|
|
#ifdef _WIN32
|
2014-04-16 05:19:34 -07:00
|
|
|
|
2018-12-25 11:27:22 -08:00
|
|
|
const PathNamePair &GetProcBinary()
|
2016-06-04 10:45:44 -07:00
|
|
|
{
|
2018-12-25 11:27:22 -08:00
|
|
|
static PathNamePair ret;
|
|
|
|
if(!ret.fname.empty() || !ret.path.empty())
|
|
|
|
return ret;
|
2016-06-04 10:45:44 -07:00
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<WCHAR> fullpath(256);
|
2016-06-04 10:45:44 -07:00
|
|
|
DWORD len;
|
2018-12-12 21:58:41 -08:00
|
|
|
while((len=GetModuleFileNameW(nullptr, fullpath.data(), static_cast<DWORD>(fullpath.size()))) == fullpath.size())
|
2018-11-11 16:09:24 -08:00
|
|
|
fullpath.resize(fullpath.size() << 1);
|
2016-06-04 10:45:44 -07:00
|
|
|
if(len == 0)
|
|
|
|
{
|
|
|
|
ERR("Failed to get process name: error %lu\n", GetLastError());
|
2018-11-11 16:09:24 -08:00
|
|
|
return ret;
|
2016-06-04 10:45:44 -07:00
|
|
|
}
|
|
|
|
|
2018-11-11 16:09:24 -08:00
|
|
|
fullpath.resize(len);
|
|
|
|
if(fullpath.back() != 0)
|
|
|
|
fullpath.push_back(0);
|
2018-01-13 02:59:21 -08:00
|
|
|
|
2018-11-11 16:09:24 -08:00
|
|
|
auto sep = std::find(fullpath.rbegin()+1, fullpath.rend(), '\\');
|
|
|
|
sep = std::find(fullpath.rbegin()+1, sep, '/');
|
|
|
|
if(sep != fullpath.rend())
|
2018-01-13 02:59:21 -08:00
|
|
|
{
|
2018-11-11 16:09:24 -08:00
|
|
|
*sep = 0;
|
|
|
|
ret.fname = wstr_to_utf8(&*sep + 1);
|
|
|
|
ret.path = wstr_to_utf8(fullpath.data());
|
2018-01-13 02:59:21 -08:00
|
|
|
}
|
|
|
|
else
|
2018-11-11 16:09:24 -08:00
|
|
|
ret.fname = wstr_to_utf8(fullpath.data());
|
2016-06-04 10:45:44 -07:00
|
|
|
|
2019-06-04 01:37:36 -07:00
|
|
|
TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
|
2018-11-11 16:09:24 -08:00
|
|
|
return ret;
|
2016-06-04 10:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-18 22:57:48 -07:00
|
|
|
void al_print(FILE *logfile, const char *fmt, ...)
|
2011-08-20 03:59:46 -07:00
|
|
|
{
|
2019-04-09 22:11:11 -07:00
|
|
|
al::vector<char> dynmsg;
|
|
|
|
char stcmsg[256];
|
|
|
|
char *str{stcmsg};
|
|
|
|
|
|
|
|
va_list args, args2;
|
|
|
|
va_start(args, fmt);
|
|
|
|
va_copy(args2, args);
|
|
|
|
int msglen{std::vsnprintf(str, sizeof(stcmsg), fmt, args)};
|
2019-08-04 11:59:14 -07:00
|
|
|
if UNLIKELY(msglen >= 0 && static_cast<size_t>(msglen) >= sizeof(stcmsg))
|
2019-04-09 22:11:11 -07:00
|
|
|
{
|
|
|
|
dynmsg.resize(static_cast<size_t>(msglen) + 1u);
|
|
|
|
str = dynmsg.data();
|
|
|
|
msglen = std::vsnprintf(str, dynmsg.size(), fmt, args2);
|
|
|
|
}
|
|
|
|
va_end(args2);
|
|
|
|
va_end(args);
|
2018-11-11 19:17:40 -08:00
|
|
|
|
|
|
|
std::wstring wstr{utf8_to_wstr(str)};
|
2019-10-08 00:21:21 -07:00
|
|
|
fputws(wstr.c_str(), logfile);
|
2019-06-18 22:57:48 -07:00
|
|
|
fflush(logfile);
|
2011-08-20 03:59:46 -07:00
|
|
|
}
|
|
|
|
|
2014-12-19 10:14:02 -08:00
|
|
|
|
2014-04-13 21:44:13 -07:00
|
|
|
static inline int is_slash(int c)
|
|
|
|
{ return (c == '\\' || c == '/'); }
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
|
2015-10-04 03:17:52 -07:00
|
|
|
{
|
2018-11-11 19:17:40 -08:00
|
|
|
std::string pathstr{path};
|
|
|
|
pathstr += "\\*";
|
|
|
|
pathstr += ext;
|
|
|
|
TRACE("Searching %s\n", pathstr.c_str());
|
2015-10-04 03:17:52 -07:00
|
|
|
|
2018-11-11 19:17:40 -08:00
|
|
|
std::wstring wpath{utf8_to_wstr(pathstr.c_str())};
|
|
|
|
WIN32_FIND_DATAW fdata;
|
|
|
|
HANDLE hdl{FindFirstFileW(wpath.c_str(), &fdata)};
|
2019-10-24 21:58:35 -07:00
|
|
|
if(hdl == INVALID_HANDLE_VALUE) return;
|
|
|
|
|
|
|
|
const auto base = results->size();
|
|
|
|
|
|
|
|
do {
|
|
|
|
results->emplace_back();
|
|
|
|
std::string &str = results->back();
|
|
|
|
str = path;
|
|
|
|
str += '\\';
|
|
|
|
str += wstr_to_utf8(fdata.cFileName);
|
|
|
|
} while(FindNextFileW(hdl, &fdata));
|
|
|
|
FindClose(hdl);
|
|
|
|
|
|
|
|
const al::span<std::string> newlist{results->data()+base, results->size()-base};
|
|
|
|
std::sort(newlist.begin(), newlist.end());
|
|
|
|
for(const auto &name : newlist)
|
|
|
|
TRACE(" got %s\n", name.c_str());
|
2015-10-04 03:17:52 -07:00
|
|
|
}
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
|
2015-10-03 20:41:18 -07:00
|
|
|
{
|
2018-11-11 23:05:21 -08:00
|
|
|
static std::mutex search_lock;
|
|
|
|
std::lock_guard<std::mutex> _{search_lock};
|
2015-10-03 20:41:18 -07:00
|
|
|
|
2015-10-04 03:17:52 -07:00
|
|
|
/* If the path is absolute, use it directly. */
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<std::string> results;
|
2016-02-23 10:54:42 -08:00
|
|
|
if(isalpha(subdir[0]) && subdir[1] == ':' && is_slash(subdir[2]))
|
2015-10-04 03:17:52 -07:00
|
|
|
{
|
2018-11-11 19:17:40 -08:00
|
|
|
std::string path{subdir};
|
|
|
|
std::replace(path.begin(), path.end(), '/', '\\');
|
|
|
|
DirectorySearch(path.c_str(), ext, &results);
|
2018-11-11 23:05:21 -08:00
|
|
|
return results;
|
2015-10-04 03:17:52 -07:00
|
|
|
}
|
2018-11-11 23:05:21 -08:00
|
|
|
if(subdir[0] == '\\' && subdir[1] == '\\' && subdir[2] == '?' && subdir[3] == '\\')
|
|
|
|
{
|
2016-02-23 10:54:42 -08:00
|
|
|
DirectorySearch(subdir, ext, &results);
|
2018-11-11 23:05:21 -08:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string path;
|
|
|
|
|
|
|
|
/* Search the app-local directory. */
|
2019-08-12 03:59:52 -07:00
|
|
|
if(auto localpath = al::getenv(L"ALSOFT_LOCAL_PATH"))
|
2018-11-11 23:05:21 -08:00
|
|
|
{
|
2019-08-12 03:59:52 -07:00
|
|
|
path = wstr_to_utf8(localpath->c_str());
|
2018-11-11 23:05:21 -08:00
|
|
|
if(is_slash(path.back()))
|
|
|
|
path.pop_back();
|
|
|
|
}
|
2019-08-12 03:59:52 -07:00
|
|
|
else if(WCHAR *cwdbuf{_wgetcwd(nullptr, 0)})
|
2015-10-04 03:17:52 -07:00
|
|
|
{
|
2018-11-11 23:05:21 -08:00
|
|
|
path = wstr_to_utf8(cwdbuf);
|
|
|
|
if(is_slash(path.back()))
|
|
|
|
path.pop_back();
|
|
|
|
free(cwdbuf);
|
|
|
|
}
|
2019-08-12 03:59:52 -07:00
|
|
|
else
|
|
|
|
path = ".";
|
2018-11-11 23:05:21 -08:00
|
|
|
std::replace(path.begin(), path.end(), '/', '\\');
|
|
|
|
DirectorySearch(path.c_str(), ext, &results);
|
2015-10-06 01:26:15 -07:00
|
|
|
|
2018-11-11 23:05:21 -08:00
|
|
|
/* Search the local and global data dirs. */
|
2019-10-25 01:43:23 -07:00
|
|
|
static const int ids[2]{ CSIDL_APPDATA, CSIDL_COMMON_APPDATA };
|
2018-11-11 23:05:21 -08:00
|
|
|
for(int id : ids)
|
|
|
|
{
|
|
|
|
WCHAR buffer[MAX_PATH];
|
|
|
|
if(SHGetSpecialFolderPathW(nullptr, buffer, id, FALSE) == FALSE)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
path = wstr_to_utf8(buffer);
|
|
|
|
if(!is_slash(path.back()))
|
|
|
|
path += '\\';
|
|
|
|
path += subdir;
|
2018-11-11 19:17:40 -08:00
|
|
|
std::replace(path.begin(), path.end(), '/', '\\');
|
2015-10-06 01:26:15 -07:00
|
|
|
|
2018-11-11 23:05:21 -08:00
|
|
|
DirectorySearch(path.c_str(), ext, &results);
|
2018-11-11 19:17:40 -08:00
|
|
|
}
|
2015-10-03 20:41:18 -07:00
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2018-11-12 18:05:16 -08:00
|
|
|
void SetRTPriority(void)
|
|
|
|
{
|
|
|
|
bool failed = false;
|
|
|
|
if(RTPrioLevel > 0)
|
|
|
|
failed = !SetThreadPriority(GetCurrentThread(), THREAD_PRIORITY_TIME_CRITICAL);
|
|
|
|
if(failed) ERR("Failed to set priority level for thread\n");
|
|
|
|
}
|
|
|
|
|
2014-02-27 01:49:23 -08:00
|
|
|
#else
|
2014-12-19 10:14:02 -08:00
|
|
|
|
2019-08-05 18:36:39 -07:00
|
|
|
#if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
|
|
|
|
#include <pthread.h>
|
|
|
|
#include <sched.h>
|
|
|
|
#endif
|
|
|
|
|
2018-12-25 11:27:22 -08:00
|
|
|
const PathNamePair &GetProcBinary()
|
2016-06-04 10:45:44 -07:00
|
|
|
{
|
2018-12-25 11:27:22 -08:00
|
|
|
static PathNamePair ret;
|
|
|
|
if(!ret.fname.empty() || !ret.path.empty())
|
|
|
|
return ret;
|
2017-06-09 15:08:47 +02:00
|
|
|
|
2018-12-25 11:27:22 -08:00
|
|
|
al::vector<char> pathname;
|
2017-06-09 15:08:47 +02:00
|
|
|
#ifdef __FreeBSD__
|
2018-11-11 16:09:24 -08:00
|
|
|
size_t pathlen;
|
2018-06-08 19:49:13 -07:00
|
|
|
int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
|
2018-11-11 19:17:40 -08:00
|
|
|
if(sysctl(mib, 4, nullptr, &pathlen, nullptr, 0) == -1)
|
2018-06-08 19:49:13 -07:00
|
|
|
WARN("Failed to sysctl kern.proc.pathname: %s\n", strerror(errno));
|
2018-01-13 03:22:41 -08:00
|
|
|
else
|
2016-06-04 10:45:44 -07:00
|
|
|
{
|
2018-11-11 16:09:24 -08:00
|
|
|
pathname.resize(pathlen + 1);
|
|
|
|
sysctl(mib, 4, pathname.data(), &pathlen, nullptr, 0);
|
2018-11-11 22:19:32 -08:00
|
|
|
pathname.resize(pathlen);
|
2016-06-04 10:45:44 -07:00
|
|
|
}
|
2018-03-24 08:25:58 -07:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_PROC_PIDPATH
|
2018-11-11 16:09:24 -08:00
|
|
|
if(pathname.empty())
|
2018-03-24 08:25:58 -07:00
|
|
|
{
|
2018-11-11 22:19:32 -08:00
|
|
|
char procpath[PROC_PIDPATHINFO_MAXSIZE]{};
|
|
|
|
const pid_t pid{getpid()};
|
2018-11-11 23:05:21 -08:00
|
|
|
if(proc_pidpath(pid, procpath, sizeof(procpath)) < 1)
|
2018-11-11 22:19:32 -08:00
|
|
|
ERR("proc_pidpath(%d, ...) failed: %s\n", pid, strerror(errno));
|
2018-03-24 08:25:58 -07:00
|
|
|
else
|
2018-11-11 22:19:32 -08:00
|
|
|
pathname.insert(pathname.end(), procpath, procpath+strlen(procpath));
|
2018-03-24 08:25:58 -07:00
|
|
|
}
|
2018-01-13 03:22:41 -08:00
|
|
|
#endif
|
2018-11-11 16:09:24 -08:00
|
|
|
if(pathname.empty())
|
2016-06-04 10:45:44 -07:00
|
|
|
{
|
2018-11-11 16:09:24 -08:00
|
|
|
pathname.resize(256);
|
2018-01-13 03:22:41 -08:00
|
|
|
|
2018-11-11 16:09:24 -08:00
|
|
|
const char *selfname{"/proc/self/exe"};
|
|
|
|
ssize_t len{readlink(selfname, pathname.data(), pathname.size())};
|
2018-01-13 03:22:41 -08:00
|
|
|
if(len == -1 && errno == ENOENT)
|
|
|
|
{
|
|
|
|
selfname = "/proc/self/file";
|
2018-11-11 16:09:24 -08:00
|
|
|
len = readlink(selfname, pathname.data(), pathname.size());
|
2018-01-13 03:22:41 -08:00
|
|
|
}
|
|
|
|
if(len == -1 && errno == ENOENT)
|
|
|
|
{
|
|
|
|
selfname = "/proc/curproc/exe";
|
2018-11-11 16:09:24 -08:00
|
|
|
len = readlink(selfname, pathname.data(), pathname.size());
|
2018-01-13 03:22:41 -08:00
|
|
|
}
|
|
|
|
if(len == -1 && errno == ENOENT)
|
|
|
|
{
|
|
|
|
selfname = "/proc/curproc/file";
|
2018-11-11 16:09:24 -08:00
|
|
|
len = readlink(selfname, pathname.data(), pathname.size());
|
2018-01-13 03:22:41 -08:00
|
|
|
}
|
2016-06-04 10:45:44 -07:00
|
|
|
|
2019-01-08 19:42:44 +01:00
|
|
|
while(len > 0 && static_cast<size_t>(len) == pathname.size())
|
2018-01-13 03:22:41 -08:00
|
|
|
{
|
2018-11-11 16:09:24 -08:00
|
|
|
pathname.resize(pathname.size() << 1);
|
|
|
|
len = readlink(selfname, pathname.data(), pathname.size());
|
2018-01-13 03:22:41 -08:00
|
|
|
}
|
|
|
|
if(len <= 0)
|
|
|
|
{
|
|
|
|
WARN("Failed to readlink %s: %s\n", selfname, strerror(errno));
|
2018-11-11 16:09:24 -08:00
|
|
|
return ret;
|
2018-01-13 03:22:41 -08:00
|
|
|
}
|
|
|
|
|
2019-09-13 09:38:35 -07:00
|
|
|
pathname.resize(static_cast<size_t>(len));
|
2018-01-13 03:22:41 -08:00
|
|
|
}
|
2018-11-11 16:09:24 -08:00
|
|
|
while(!pathname.empty() && pathname.back() == 0)
|
|
|
|
pathname.pop_back();
|
2017-06-09 15:08:47 +02:00
|
|
|
|
2018-11-11 16:09:24 -08:00
|
|
|
auto sep = std::find(pathname.crbegin(), pathname.crend(), '/');
|
|
|
|
if(sep != pathname.crend())
|
2018-01-13 02:59:21 -08:00
|
|
|
{
|
2018-11-11 16:09:24 -08:00
|
|
|
ret.path = std::string(pathname.cbegin(), sep.base()-1);
|
|
|
|
ret.fname = std::string(sep.base(), pathname.cend());
|
2018-01-13 02:59:21 -08:00
|
|
|
}
|
2016-06-04 10:45:44 -07:00
|
|
|
else
|
2018-11-11 16:09:24 -08:00
|
|
|
ret.fname = std::string(pathname.cbegin(), pathname.cend());
|
2016-06-04 10:45:44 -07:00
|
|
|
|
2019-06-04 01:37:36 -07:00
|
|
|
TRACE("Got binary: %s, %s\n", ret.path.c_str(), ret.fname.c_str());
|
2018-11-11 16:09:24 -08:00
|
|
|
return ret;
|
2016-06-04 10:45:44 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-06-18 22:57:48 -07:00
|
|
|
void al_print(FILE *logfile, const char *fmt, ...)
|
2014-12-19 10:14:02 -08:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
2019-06-18 22:57:48 -07:00
|
|
|
vfprintf(logfile, fmt, ap);
|
2014-12-19 10:14:02 -08:00
|
|
|
va_end(ap);
|
|
|
|
|
2019-06-18 22:57:48 -07:00
|
|
|
fflush(logfile);
|
2014-12-19 10:14:02 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
static void DirectorySearch(const char *path, const char *ext, al::vector<std::string> *const results)
|
2015-10-03 20:41:18 -07:00
|
|
|
{
|
2016-02-23 10:54:42 -08:00
|
|
|
TRACE("Searching %s for *%s\n", path, ext);
|
2018-11-11 19:17:40 -08:00
|
|
|
DIR *dir{opendir(path)};
|
2019-10-24 21:58:35 -07:00
|
|
|
if(!dir) return;
|
|
|
|
|
|
|
|
const auto base = results->size();
|
|
|
|
const size_t extlen{strlen(ext)};
|
|
|
|
|
|
|
|
struct dirent *dirent;
|
|
|
|
while((dirent=readdir(dir)) != nullptr)
|
2015-10-03 20:41:18 -07:00
|
|
|
{
|
2019-10-24 21:58:35 -07:00
|
|
|
if(strcmp(dirent->d_name, ".") == 0 || strcmp(dirent->d_name, "..") == 0)
|
|
|
|
continue;
|
2018-11-11 19:17:40 -08:00
|
|
|
|
2019-10-24 21:58:35 -07:00
|
|
|
const size_t len{strlen(dirent->d_name)};
|
|
|
|
if(len <= extlen) continue;
|
|
|
|
if(al::strcasecmp(dirent->d_name+len-extlen, ext) != 0)
|
|
|
|
continue;
|
2015-10-04 03:17:52 -07:00
|
|
|
|
2019-10-24 21:58:35 -07:00
|
|
|
results->emplace_back();
|
|
|
|
std::string &str = results->back();
|
|
|
|
str = path;
|
|
|
|
if(str.back() != '/')
|
|
|
|
str.push_back('/');
|
|
|
|
str += dirent->d_name;
|
2015-10-03 20:41:18 -07:00
|
|
|
}
|
2019-10-24 21:58:35 -07:00
|
|
|
closedir(dir);
|
|
|
|
|
|
|
|
const al::span<std::string> newlist{results->data()+base, results->size()-base};
|
|
|
|
std::sort(newlist.begin(), newlist.end());
|
|
|
|
for(const auto &name : newlist)
|
|
|
|
TRACE(" got %s\n", name.c_str());
|
2015-10-03 20:41:18 -07:00
|
|
|
}
|
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<std::string> SearchDataFiles(const char *ext, const char *subdir)
|
2015-10-03 20:41:18 -07:00
|
|
|
{
|
2018-11-11 23:05:21 -08:00
|
|
|
static std::mutex search_lock;
|
|
|
|
std::lock_guard<std::mutex> _{search_lock};
|
2015-10-03 20:41:18 -07:00
|
|
|
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<std::string> results;
|
2016-02-23 10:54:42 -08:00
|
|
|
if(subdir[0] == '/')
|
2018-11-11 23:05:21 -08:00
|
|
|
{
|
2016-02-23 10:54:42 -08:00
|
|
|
DirectorySearch(subdir, ext, &results);
|
2018-11-11 23:05:21 -08:00
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Search the app-local directory. */
|
2019-08-12 03:59:52 -07:00
|
|
|
if(auto localpath = al::getenv("ALSOFT_LOCAL_PATH"))
|
|
|
|
DirectorySearch(localpath->c_str(), ext, &results);
|
2015-10-03 20:41:18 -07:00
|
|
|
else
|
|
|
|
{
|
2018-11-24 16:58:49 -08:00
|
|
|
al::vector<char> cwdbuf(256);
|
2018-11-11 23:05:21 -08:00
|
|
|
while(!getcwd(cwdbuf.data(), cwdbuf.size()))
|
2018-01-13 04:40:20 -08:00
|
|
|
{
|
2018-11-11 23:05:21 -08:00
|
|
|
if(errno != ERANGE)
|
2018-01-13 04:40:20 -08:00
|
|
|
{
|
2018-11-11 19:17:40 -08:00
|
|
|
cwdbuf.clear();
|
2018-11-11 23:05:21 -08:00
|
|
|
break;
|
2018-01-13 04:40:20 -08:00
|
|
|
}
|
2018-11-11 23:05:21 -08:00
|
|
|
cwdbuf.resize(cwdbuf.size() << 1);
|
2018-01-13 04:40:20 -08:00
|
|
|
}
|
2018-11-11 23:05:21 -08:00
|
|
|
if(cwdbuf.empty())
|
|
|
|
DirectorySearch(".", ext, &results);
|
|
|
|
else
|
2015-10-03 20:41:18 -07:00
|
|
|
{
|
2018-11-11 23:05:21 -08:00
|
|
|
DirectorySearch(cwdbuf.data(), ext, &results);
|
|
|
|
cwdbuf.clear();
|
2015-10-03 20:41:18 -07:00
|
|
|
}
|
2018-11-11 23:05:21 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Search local data dir
|
2019-08-12 03:59:52 -07:00
|
|
|
if(auto datapath = al::getenv("XDG_DATA_HOME"))
|
2018-11-11 23:05:21 -08:00
|
|
|
{
|
2019-08-12 03:59:52 -07:00
|
|
|
std::string &path = *datapath;
|
2018-11-11 23:05:21 -08:00
|
|
|
if(path.back() != '/')
|
|
|
|
path += '/';
|
|
|
|
path += subdir;
|
|
|
|
DirectorySearch(path.c_str(), ext, &results);
|
|
|
|
}
|
2019-08-12 03:59:52 -07:00
|
|
|
else if(auto homepath = al::getenv("HOME"))
|
2018-11-11 23:05:21 -08:00
|
|
|
{
|
2019-08-12 03:59:52 -07:00
|
|
|
std::string &path = *homepath;
|
2018-11-11 23:05:21 -08:00
|
|
|
if(path.back() == '/')
|
|
|
|
path.pop_back();
|
|
|
|
path += "/.local/share/";
|
|
|
|
path += subdir;
|
|
|
|
DirectorySearch(path.c_str(), ext, &results);
|
|
|
|
}
|
2015-10-03 20:41:18 -07:00
|
|
|
|
2018-11-11 23:05:21 -08:00
|
|
|
// Search global data dirs
|
2019-08-12 03:59:52 -07:00
|
|
|
std::string datadirs{al::getenv("XDG_DATA_DIRS").value_or("/usr/local/share/:/usr/share/")};
|
2015-10-03 20:41:18 -07:00
|
|
|
|
2019-08-12 03:59:52 -07:00
|
|
|
size_t curpos{0u};
|
|
|
|
while(curpos < datadirs.size())
|
2018-11-11 23:05:21 -08:00
|
|
|
{
|
2019-08-12 03:59:52 -07:00
|
|
|
size_t nextpos{datadirs.find(':', curpos)};
|
2015-10-03 20:41:18 -07:00
|
|
|
|
2019-08-12 03:59:52 -07:00
|
|
|
std::string path{(nextpos != std::string::npos) ?
|
|
|
|
datadirs.substr(curpos, nextpos++ - curpos) : datadirs.substr(curpos)};
|
|
|
|
curpos = nextpos;
|
2018-11-11 23:05:21 -08:00
|
|
|
|
2019-08-12 03:59:52 -07:00
|
|
|
if(path.empty()) continue;
|
2018-11-11 23:05:21 -08:00
|
|
|
if(path.back() != '/')
|
|
|
|
path += '/';
|
|
|
|
path += subdir;
|
|
|
|
|
|
|
|
DirectorySearch(path.c_str(), ext, &results);
|
2015-10-03 20:41:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2019-01-09 19:43:54 +01:00
|
|
|
void SetRTPriority()
|
2011-08-20 03:59:46 -07:00
|
|
|
{
|
2018-11-12 18:05:16 -08:00
|
|
|
bool failed = false;
|
|
|
|
#if defined(HAVE_PTHREAD_SETSCHEDPARAM) && !defined(__OpenBSD__)
|
2011-08-20 03:59:46 -07:00
|
|
|
if(RTPrioLevel > 0)
|
|
|
|
{
|
2011-08-31 01:05:01 -07:00
|
|
|
struct sched_param param;
|
2011-08-20 03:59:46 -07:00
|
|
|
/* Use the minimum real-time priority possible for now (on Linux this
|
|
|
|
* should be 1 for SCHED_RR) */
|
|
|
|
param.sched_priority = sched_get_priority_min(SCHED_RR);
|
|
|
|
failed = !!pthread_setschedparam(pthread_self(), SCHED_RR, ¶m);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/* Real-time priority not available */
|
|
|
|
failed = (RTPrioLevel>0);
|
|
|
|
#endif
|
|
|
|
if(failed)
|
|
|
|
ERR("Failed to set priority level for thread\n");
|
|
|
|
}
|
|
|
|
|
2018-11-12 18:05:16 -08:00
|
|
|
#endif
|