| 1 | /* Copyright (C) 2010 Wildfire Games.
|
|---|
| 2 | *
|
|---|
| 3 | * Permission is hereby granted, free of charge, to any person obtaining
|
|---|
| 4 | * a copy of this software and associated documentation files (the
|
|---|
| 5 | * "Software"), to deal in the Software without restriction, including
|
|---|
| 6 | * without limitation the rights to use, copy, modify, merge, publish,
|
|---|
| 7 | * distribute, sublicense, and/or sell copies of the Software, and to
|
|---|
| 8 | * permit persons to whom the Software is furnished to do so, subject to
|
|---|
| 9 | * the following conditions:
|
|---|
| 10 | *
|
|---|
| 11 | * The above copyright notice and this permission notice shall be included
|
|---|
| 12 | * in all copies or substantial portions of the Software.
|
|---|
| 13 | *
|
|---|
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|---|
| 15 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|---|
| 16 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|---|
| 17 | * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|---|
| 18 | * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|---|
| 19 | * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|---|
| 20 | * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|---|
| 21 | */
|
|---|
| 22 |
|
|---|
| 23 | /*
|
|---|
| 24 | * Unix implementation of wchar_t versions of POSIX filesystem functions
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 | #include "precompiled.h"
|
|---|
| 28 | #include "lib/sysdep/filesystem.h"
|
|---|
| 29 |
|
|---|
| 30 | #include "lib/path.h"
|
|---|
| 31 |
|
|---|
| 32 | #include <cstdio>
|
|---|
| 33 |
|
|---|
| 34 | struct WDIR
|
|---|
| 35 | {
|
|---|
| 36 | DIR* d;
|
|---|
| 37 | wchar_t name[PATH_MAX];
|
|---|
| 38 | wdirent ent;
|
|---|
| 39 | };
|
|---|
| 40 |
|
|---|
| 41 | #if OS_ANDROID
|
|---|
| 42 |
|
|---|
| 43 | // The Crystax NDK seems to do weird things with opendir etc.
|
|---|
| 44 | // To avoid that, load the symbols directly from the real libc
|
|---|
| 45 | // and use them instead.
|
|---|
| 46 |
|
|---|
| 47 | #include <dlfcn.h>
|
|---|
| 48 |
|
|---|
| 49 | static void* libc;
|
|---|
| 50 | static DIR* (*libc_opendir)(const char*);
|
|---|
| 51 | static dirent* (*libc_readdir)(DIR*);
|
|---|
| 52 | static int (*libc_closedir)(DIR*);
|
|---|
| 53 |
|
|---|
| 54 | void init_libc()
|
|---|
| 55 | {
|
|---|
| 56 | if (libc)
|
|---|
| 57 | return;
|
|---|
| 58 | libc = dlopen("/system/lib/libc.so", RTLD_LAZY);
|
|---|
| 59 | ENSURE(libc);
|
|---|
| 60 | libc_opendir = (DIR*(*)(const char*))dlsym(libc, "opendir");
|
|---|
| 61 | libc_readdir = (dirent*(*)(DIR*))dlsym(libc, "readdir");
|
|---|
| 62 | libc_closedir = (int(*)(DIR*))dlsym(libc, "closedir");
|
|---|
| 63 | ENSURE(libc_opendir && libc_readdir && libc_closedir);
|
|---|
| 64 | }
|
|---|
| 65 |
|
|---|
| 66 | #define opendir libc_opendir
|
|---|
| 67 | #define readdir libc_readdir
|
|---|
| 68 | #define closedir libc_closedir
|
|---|
| 69 |
|
|---|
| 70 | #else
|
|---|
| 71 |
|
|---|
| 72 | void init_libc() { }
|
|---|
| 73 |
|
|---|
| 74 | #endif
|
|---|
| 75 |
|
|---|
| 76 | WDIR* wopendir(const OsPath& path)
|
|---|
| 77 | {
|
|---|
| 78 | init_libc();
|
|---|
| 79 | DIR* d = opendir(OsString(path).c_str());
|
|---|
| 80 | if(!d)
|
|---|
| 81 | return 0;
|
|---|
| 82 | WDIR* wd = new WDIR;
|
|---|
| 83 | wd->d = d;
|
|---|
| 84 | wd->name[0] = '\0';
|
|---|
| 85 | wd->ent.d_name = wd->name;
|
|---|
| 86 | return wd;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | struct wdirent* wreaddir(WDIR* wd)
|
|---|
| 90 | {
|
|---|
| 91 | dirent* ent = readdir(wd->d);
|
|---|
| 92 | if(!ent)
|
|---|
| 93 | return 0;
|
|---|
| 94 | wcscpy_s(wd->name, ARRAY_SIZE(wd->name), OsPath(ent->d_name).string().c_str());
|
|---|
| 95 | return &wd->ent;
|
|---|
| 96 | }
|
|---|
| 97 |
|
|---|
| 98 | int wclosedir(WDIR* wd)
|
|---|
| 99 | {
|
|---|
| 100 | int ret = closedir(wd->d);
|
|---|
| 101 | delete wd;
|
|---|
| 102 | return ret;
|
|---|
| 103 | }
|
|---|
| 104 |
|
|---|
| 105 |
|
|---|
| 106 | int wopen(const OsPath& pathname, int oflag)
|
|---|
| 107 | {
|
|---|
| 108 | ENSURE(!(oflag & O_CREAT));
|
|---|
| 109 | return open(OsString(pathname).c_str(), oflag);
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | int wopen(const OsPath& pathname, int oflag, mode_t mode)
|
|---|
| 113 | {
|
|---|
| 114 | return open(OsString(pathname).c_str(), oflag, mode);
|
|---|
| 115 | }
|
|---|
| 116 |
|
|---|
| 117 | int wclose(int fd)
|
|---|
| 118 | {
|
|---|
| 119 | return close(fd);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 |
|
|---|
| 123 | int wtruncate(const OsPath& pathname, off_t length)
|
|---|
| 124 | {
|
|---|
| 125 | return truncate(OsString(pathname).c_str(), length);
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | int wunlink(const OsPath& pathname)
|
|---|
| 129 | {
|
|---|
| 130 | return unlink(OsString(pathname).c_str());
|
|---|
| 131 | }
|
|---|
| 132 |
|
|---|
| 133 | int wrmdir(const OsPath& path)
|
|---|
| 134 | {
|
|---|
| 135 | return rmdir(OsString(path).c_str());
|
|---|
| 136 | }
|
|---|
| 137 |
|
|---|
| 138 | int wrename(const OsPath& pathnameOld, const OsPath& pathnameNew)
|
|---|
| 139 | {
|
|---|
| 140 | return rename(OsString(pathnameOld).c_str(), OsString(pathnameNew).c_str());
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | OsPath wrealpath(const OsPath& pathname)
|
|---|
| 144 | {
|
|---|
| 145 | char resolvedBuf[PATH_MAX];
|
|---|
| 146 | const char* resolved = realpath(OsString(pathname).c_str(), resolvedBuf);
|
|---|
| 147 | if(!resolved)
|
|---|
| 148 | return OsPath();
|
|---|
| 149 | return resolved;
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | int wstat(const OsPath& pathname, struct stat* buf)
|
|---|
| 153 | {
|
|---|
| 154 | return stat(OsString(pathname).c_str(), buf);
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | int wmkdir(const OsPath& path, mode_t mode)
|
|---|
| 158 | {
|
|---|
| 159 | return mkdir(OsString(path).c_str(), mode);
|
|---|
| 160 | }
|
|---|