2020-06-15 14:18:57 +00:00
|
|
|
/*-*- mode:c;indent-tabs-mode:nil;c-basic-offset:2;tab-width:8;coding:utf-8 -*-│
|
|
|
|
│ vi: set et ft=c ts=2 sts=2 sw=2 fenc=utf-8 :vi │
|
|
|
|
╞══════════════════════════════════════════════════════════════════════════════╡
|
|
|
|
│ Copyright 2020 Justine Alexandra Roberts Tunney │
|
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ Permission to use, copy, modify, and/or distribute this software for │
|
|
|
|
│ any purpose with or without fee is hereby granted, provided that the │
|
|
|
|
│ above copyright notice and this permission notice appear in all copies. │
|
2020-06-15 14:18:57 +00:00
|
|
|
│ │
|
2020-12-28 01:18:44 +00:00
|
|
|
│ THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL │
|
|
|
|
│ WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED │
|
|
|
|
│ WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE │
|
|
|
|
│ AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL │
|
|
|
|
│ DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR │
|
|
|
|
│ PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER │
|
|
|
|
│ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │
|
|
|
|
│ PERFORMANCE OF THIS SOFTWARE. │
|
2020-06-15 14:18:57 +00:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
|
|
|
#include "libc/calls/calls.h"
|
2021-09-04 05:19:41 +00:00
|
|
|
#include "libc/calls/struct/metastat.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/struct/stat.h"
|
2022-05-23 22:06:11 +00:00
|
|
|
#include "libc/calls/syscall-sysv.internal.h"
|
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/dce.h"
|
|
|
|
#include "libc/errno.h"
|
2022-09-13 06:10:38 +00:00
|
|
|
#include "libc/intrin/strace.h"
|
|
|
|
#include "libc/intrin/weaken.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/files.h"
|
2023-08-14 03:31:27 +00:00
|
|
|
#include "libc/runtime/zipos.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/str/str.h"
|
2021-08-18 21:21:30 +00:00
|
|
|
#include "libc/sysv/consts/at.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/sysv/errfuns.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if file exists at path.
|
|
|
|
*
|
2021-08-26 04:35:58 +00:00
|
|
|
* This function is equivalent to:
|
|
|
|
*
|
|
|
|
* struct stat st;
|
|
|
|
* return stat(path, &st) != -1;
|
|
|
|
*
|
2020-06-15 14:18:57 +00:00
|
|
|
* Please note that things which aren't strictly files, e.g. directories
|
|
|
|
* or sockets, could be considered files for the purposes of this
|
|
|
|
* function. The stat() function may be used to differentiate them.
|
|
|
|
*/
|
2023-08-14 03:31:27 +00:00
|
|
|
bool32 fileexists(const char *path) {
|
2021-08-26 04:35:58 +00:00
|
|
|
int e;
|
2021-10-15 02:36:49 +00:00
|
|
|
bool res;
|
2021-09-04 05:19:41 +00:00
|
|
|
union metastat st;
|
2021-08-26 04:35:58 +00:00
|
|
|
struct ZiposUri zipname;
|
2022-04-28 16:42:36 +00:00
|
|
|
uint16_t path16[PATH_MAX];
|
2021-10-15 02:36:49 +00:00
|
|
|
e = errno;
|
2024-06-22 12:45:49 +00:00
|
|
|
if (_weaken(__zipos_open) &&
|
|
|
|
_weaken(__zipos_parseuri)(path, &zipname) != -1) {
|
2022-09-13 06:10:38 +00:00
|
|
|
if (_weaken(__zipos_stat)(&zipname, &st.cosmo) != -1) {
|
2021-10-15 02:36:49 +00:00
|
|
|
res = true;
|
2021-08-26 04:35:58 +00:00
|
|
|
} else {
|
2021-10-15 02:36:49 +00:00
|
|
|
res = false;
|
2021-08-26 04:35:58 +00:00
|
|
|
}
|
|
|
|
} else if (IsMetal()) {
|
2021-10-15 02:36:49 +00:00
|
|
|
res = false;
|
2021-08-26 04:35:58 +00:00
|
|
|
} else if (!IsWindows()) {
|
|
|
|
if (__sys_fstatat(AT_FDCWD, path, &st, 0) != -1) {
|
2021-10-15 02:36:49 +00:00
|
|
|
res = true;
|
2021-08-26 04:35:58 +00:00
|
|
|
} else {
|
2021-10-15 02:36:49 +00:00
|
|
|
res = false;
|
2020-12-05 20:20:41 +00:00
|
|
|
}
|
2021-10-15 02:36:49 +00:00
|
|
|
} else if (__mkntpath(path, path16) != -1) {
|
|
|
|
res = GetFileAttributes(path16) != -1u;
|
2020-12-05 20:20:41 +00:00
|
|
|
} else {
|
2021-10-15 02:36:49 +00:00
|
|
|
res = false;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
2022-03-19 10:37:00 +00:00
|
|
|
STRACE("%s(%#s) → %hhhd% m", "fileexists", path, res);
|
2021-10-15 02:36:49 +00:00
|
|
|
if (!res && (errno == ENOENT || errno == ENOTDIR)) {
|
|
|
|
errno = e;
|
|
|
|
}
|
|
|
|
return res;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|