mirror of
https://gitea.wildfiregames.com/0ad/0ad
synced 2026-06-16 05:13:58 -07:00
Make include-what-you-use happy with some files in source/lib and fix what needs to be fixed. Ref: #8086 Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
171 lines
3.9 KiB
C++
171 lines
3.9 KiB
C++
/* Copyright (C) 2025 Wildfire Games.
|
|
*
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
* a copy of this software and associated documentation files (the
|
|
* "Software"), to deal in the Software without restriction, including
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
* the following conditions:
|
|
*
|
|
* The above copyright notice and this permission notice shall be included
|
|
* in all copies or substantial portions of the Software.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
*/
|
|
|
|
/*
|
|
* Unix implementation of wchar_t versions of POSIX filesystem functions
|
|
*/
|
|
|
|
#include "precompiled.h"
|
|
|
|
#include "lib/sysdep/filesystem.h"
|
|
|
|
#include "lib/code_annotation.h"
|
|
#include "lib/debug.h"
|
|
#include "lib/os_path.h"
|
|
#include "lib/posix/posix_filesystem.h"
|
|
#include "lib/posix/posix_types.h"
|
|
#include "lib/secure_crt.h"
|
|
#include "lib/sysdep/os.h"
|
|
|
|
#include <climits>
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
#include <fcntl.h>
|
|
#include <string>
|
|
|
|
struct WDIR
|
|
{
|
|
DIR* d;
|
|
wchar_t name[PATH_MAX];
|
|
wdirent ent;
|
|
};
|
|
|
|
#if OS_ANDROID
|
|
|
|
// The Crystax NDK seems to do weird things with opendir etc.
|
|
// To avoid that, load the symbols directly from the real libc
|
|
// and use them instead.
|
|
|
|
#include <dlfcn.h>
|
|
|
|
static void* libc;
|
|
static DIR* (*libc_opendir)(const char*);
|
|
static dirent* (*libc_readdir)(DIR*);
|
|
static int (*libc_closedir)(DIR*);
|
|
|
|
void init_libc()
|
|
{
|
|
if (libc)
|
|
return;
|
|
libc = dlopen("/system/lib/libc.so", RTLD_LAZY);
|
|
ENSURE(libc);
|
|
libc_opendir = (DIR*(*)(const char*))dlsym(libc, "opendir");
|
|
libc_readdir = (dirent*(*)(DIR*))dlsym(libc, "readdir");
|
|
libc_closedir = (int(*)(DIR*))dlsym(libc, "closedir");
|
|
ENSURE(libc_opendir && libc_readdir && libc_closedir);
|
|
}
|
|
|
|
#define opendir libc_opendir
|
|
#define readdir libc_readdir
|
|
#define closedir libc_closedir
|
|
|
|
#else
|
|
|
|
void init_libc() { }
|
|
|
|
#endif
|
|
|
|
WDIR* wopendir(const OsPath& path)
|
|
{
|
|
init_libc();
|
|
DIR* d = opendir(OsString(path).c_str());
|
|
if(!d)
|
|
return 0;
|
|
WDIR* wd = new WDIR;
|
|
wd->d = d;
|
|
wd->name[0] = '\0';
|
|
wd->ent.d_name = wd->name;
|
|
return wd;
|
|
}
|
|
|
|
struct wdirent* wreaddir(WDIR* wd)
|
|
{
|
|
dirent* ent = readdir(wd->d);
|
|
if(!ent)
|
|
return 0;
|
|
wcscpy_s(wd->name, ARRAY_SIZE(wd->name), OsPath(ent->d_name).string().c_str());
|
|
return &wd->ent;
|
|
}
|
|
|
|
int wclosedir(WDIR* wd)
|
|
{
|
|
int ret = closedir(wd->d);
|
|
delete wd;
|
|
return ret;
|
|
}
|
|
|
|
|
|
int wopen(const OsPath& pathname, int oflag)
|
|
{
|
|
ENSURE(!(oflag & O_CREAT));
|
|
return open(OsString(pathname).c_str(), oflag);
|
|
}
|
|
|
|
int wopen(const OsPath& pathname, int oflag, mode_t mode)
|
|
{
|
|
return open(OsString(pathname).c_str(), oflag, mode);
|
|
}
|
|
|
|
int wclose(int fd)
|
|
{
|
|
return close(fd);
|
|
}
|
|
|
|
|
|
int wtruncate(const OsPath& pathname, off_t length)
|
|
{
|
|
return truncate(OsString(pathname).c_str(), length);
|
|
}
|
|
|
|
int wunlink(const OsPath& pathname)
|
|
{
|
|
return unlink(OsString(pathname).c_str());
|
|
}
|
|
|
|
int wrmdir(const OsPath& path)
|
|
{
|
|
return rmdir(OsString(path).c_str());
|
|
}
|
|
|
|
int wrename(const OsPath& pathnameOld, const OsPath& pathnameNew)
|
|
{
|
|
return rename(OsString(pathnameOld).c_str(), OsString(pathnameNew).c_str());
|
|
}
|
|
|
|
OsPath wrealpath(const OsPath& pathname)
|
|
{
|
|
char resolvedBuf[PATH_MAX];
|
|
const char* resolved = realpath(OsString(pathname).c_str(), resolvedBuf);
|
|
if(!resolved)
|
|
return OsPath();
|
|
return resolved;
|
|
}
|
|
|
|
int wstat(const OsPath& pathname, struct stat* buf)
|
|
{
|
|
return stat(OsString(pathname).c_str(), buf);
|
|
}
|
|
|
|
int wmkdir(const OsPath& path, mode_t mode)
|
|
{
|
|
return mkdir(OsString(path).c_str(), mode);
|
|
}
|