0ad/source/lib/sysdep/os/unix/ufilesystem.cpp
Ralph Sennhauser 71400e8045
Drop unused lowlevel functions
and associated types. Aka posix dirent.h abstraction.

Signed-off-by: Ralph Sennhauser <ralph.sennhauser@gmail.com>
2026-06-14 21:58:59 +02:00

134 lines
3.4 KiB
C++

/* Copyright (C) 2026 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>
#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
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);
}