2020-06-15 07:18:57 -07: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-27 17:18:44 -08: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 07:18:57 -07:00
|
|
|
│ │
|
2020-12-27 17:18:44 -08: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 07:18:57 -07:00
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2022-05-23 15:06:11 -07:00
|
|
|
#include "libc/calls/syscall_support-nt.internal.h"
|
2021-01-30 01:49:43 -08:00
|
|
|
#include "libc/errno.h"
|
2022-03-24 00:05:59 -07:00
|
|
|
#include "libc/nt/createfile.h"
|
|
|
|
#include "libc/nt/enum/accessmask.h"
|
|
|
|
#include "libc/nt/enum/creationdisposition.h"
|
2021-09-27 22:58:51 -07:00
|
|
|
#include "libc/nt/enum/fileflagandattributes.h"
|
2022-03-24 00:05:59 -07:00
|
|
|
#include "libc/nt/enum/filesharemode.h"
|
2021-09-27 22:58:51 -07:00
|
|
|
#include "libc/nt/enum/io.h"
|
2022-10-14 08:25:47 -07:00
|
|
|
#include "libc/nt/enum/movefileexflags.h"
|
2021-01-29 21:46:23 -08:00
|
|
|
#include "libc/nt/errors.h"
|
2020-06-15 07:18:57 -07:00
|
|
|
#include "libc/nt/files.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
2021-09-27 22:58:51 -07:00
|
|
|
#include "libc/nt/struct/win32fileattributedata.h"
|
|
|
|
#include "libc/nt/struct/win32finddata.h"
|
2021-01-29 21:46:23 -08:00
|
|
|
#include "libc/nt/synchronization.h"
|
2022-05-23 15:06:11 -07:00
|
|
|
#include "libc/str/str.h"
|
2021-01-30 01:49:43 -08:00
|
|
|
#include "libc/sysv/consts/at.h"
|
2023-08-21 02:28:24 -07:00
|
|
|
#include "libc/sysv/errfuns.h"
|
2022-03-24 00:05:59 -07:00
|
|
|
|
2021-09-27 22:58:51 -07:00
|
|
|
static textwindows bool IsDirectorySymlink(const char16_t *path) {
|
2022-04-07 20:30:04 -07:00
|
|
|
int e;
|
2021-09-27 22:58:51 -07:00
|
|
|
int64_t h;
|
|
|
|
struct NtWin32FindData data;
|
|
|
|
struct NtWin32FileAttributeData info;
|
2022-04-07 20:30:04 -07:00
|
|
|
e = errno;
|
2021-09-27 22:58:51 -07:00
|
|
|
if (GetFileAttributesEx(path, 0, &info) &&
|
|
|
|
((info.dwFileAttributes & kNtFileAttributeDirectory) &&
|
|
|
|
(info.dwFileAttributes & kNtFileAttributeReparsePoint)) &&
|
|
|
|
(h = FindFirstFile(path, &data)) != -1) {
|
|
|
|
FindClose(h);
|
|
|
|
return data.dwReserved0 == kNtIoReparseTagSymlink ||
|
|
|
|
data.dwReserved0 == kNtIoReparseTagMountPoint;
|
2021-01-30 01:49:43 -08:00
|
|
|
} else {
|
2022-04-07 20:30:04 -07:00
|
|
|
errno = e;
|
2021-09-27 22:58:51 -07:00
|
|
|
return false;
|
2021-01-30 01:49:43 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:35:29 -08:00
|
|
|
static textwindows int sys_rmdir_nt(const char16_t *path) {
|
2023-10-11 14:54:42 -07:00
|
|
|
int ms;
|
2021-03-08 11:42:53 -08:00
|
|
|
for (ms = 1;; ms *= 2) {
|
2024-09-10 21:21:52 -07:00
|
|
|
if (RemoveDirectory(path))
|
2022-04-12 22:11:00 -07:00
|
|
|
return 0;
|
2023-08-21 02:28:24 -07:00
|
|
|
// Files can linger, for absolutely no reason.
|
|
|
|
// Possibly some Windows Defender bug on Win7.
|
|
|
|
// Sleep for up to one second w/ expo backoff.
|
|
|
|
// Alternative is use Microsoft internal APIs.
|
|
|
|
// Never could have imagined it'd be this bad.
|
2024-09-10 21:21:52 -07:00
|
|
|
if (GetLastError() == kNtErrorDirNotEmpty && ms <= 1024) {
|
2021-03-08 11:42:53 -08:00
|
|
|
Sleep(ms);
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2020-06-15 07:18:57 -07:00
|
|
|
}
|
2023-10-11 14:54:42 -07:00
|
|
|
return __winerr();
|
2020-06-15 07:18:57 -07:00
|
|
|
}
|
2021-01-30 01:49:43 -08:00
|
|
|
|
2021-09-27 22:58:51 -07:00
|
|
|
static textwindows int sys_unlink_nt(const char16_t *path) {
|
2022-03-24 00:05:59 -07:00
|
|
|
if (IsDirectorySymlink(path)) {
|
|
|
|
return sys_rmdir_nt(path);
|
|
|
|
} else if (DeleteFile(path)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2022-04-12 22:11:00 -07:00
|
|
|
return -1;
|
2022-03-24 00:05:59 -07:00
|
|
|
}
|
2021-09-27 22:58:51 -07:00
|
|
|
}
|
|
|
|
|
2023-08-21 02:28:24 -07:00
|
|
|
textwindows int sys_unlinkat_nt_impl(const char16_t *path, int flags) {
|
|
|
|
if (flags & AT_REMOVEDIR) {
|
|
|
|
return sys_rmdir_nt(path);
|
|
|
|
} else {
|
|
|
|
return sys_unlink_nt(path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 19:35:29 -08:00
|
|
|
textwindows int sys_unlinkat_nt(int dirfd, const char *path, int flags) {
|
2022-04-28 09:42:36 -07:00
|
|
|
char16_t path16[PATH_MAX];
|
2023-08-21 02:28:24 -07:00
|
|
|
|
|
|
|
// check validity of flags
|
|
|
|
if (flags & ~AT_REMOVEDIR) {
|
|
|
|
return einval();
|
|
|
|
}
|
|
|
|
|
|
|
|
// translate unix to windows path
|
|
|
|
int n;
|
2022-03-24 00:05:59 -07:00
|
|
|
if ((n = __mkntpathat(dirfd, path, 0, path16)) == -1) {
|
2023-08-21 02:28:24 -07:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// optimistic first attempt
|
|
|
|
int e = errno;
|
|
|
|
int rc = sys_unlinkat_nt_impl(path16, flags);
|
|
|
|
|
|
|
|
// reactively ensure unlink() deletes read-only files
|
|
|
|
if (rc == -1 && errno == kNtErrorAccessDenied) {
|
|
|
|
uint32_t attr;
|
|
|
|
if ((attr = GetFileAttributes(path16)) != -1u &&
|
|
|
|
(attr & kNtFileAttributeReadonly) &&
|
|
|
|
SetFileAttributes(path16, attr & ~kNtFileAttributeReadonly)) {
|
|
|
|
errno = e;
|
|
|
|
rc = sys_unlinkat_nt_impl(path16, flags);
|
|
|
|
} else {
|
|
|
|
errno = kNtErrorAccessDenied;
|
2022-03-24 00:05:59 -07:00
|
|
|
}
|
2021-01-30 01:49:43 -08:00
|
|
|
}
|
2023-08-21 02:28:24 -07:00
|
|
|
|
|
|
|
// return status
|
2022-04-20 09:56:53 -07:00
|
|
|
return __fix_enotdir(rc, path16);
|
2021-01-30 01:49:43 -08:00
|
|
|
}
|