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 net 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
|
|
|
╚─────────────────────────────────────────────────────────────────────────────*/
|
2021-03-01 07:42:35 +00:00
|
|
|
#include "libc/bits/safemacros.internal.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/calls/internal.h"
|
|
|
|
#include "libc/calls/struct/stat.h"
|
2020-12-09 23:04:54 +00:00
|
|
|
#include "libc/fmt/conv.h"
|
2020-06-15 14:18:57 +00:00
|
|
|
#include "libc/nt/enum/fileflagandattributes.h"
|
|
|
|
#include "libc/nt/enum/fileinfobyhandleclass.h"
|
|
|
|
#include "libc/nt/enum/filetype.h"
|
|
|
|
#include "libc/nt/files.h"
|
|
|
|
#include "libc/nt/runtime.h"
|
|
|
|
#include "libc/nt/struct/byhandlefileinformation.h"
|
|
|
|
#include "libc/nt/struct/filecompressioninfo.h"
|
|
|
|
#include "libc/str/str.h"
|
|
|
|
#include "libc/sysv/consts/s.h"
|
|
|
|
|
2021-02-04 03:35:29 +00:00
|
|
|
textwindows int sys_fstat_nt(int64_t handle, struct stat *st) {
|
2020-11-25 16:19:00 +00:00
|
|
|
int filetype;
|
2020-06-15 14:18:57 +00:00
|
|
|
uint64_t actualsize;
|
|
|
|
struct NtFileCompressionInfo fci;
|
2020-12-05 20:20:41 +00:00
|
|
|
struct NtByHandleFileInformation wst;
|
2021-01-17 01:52:15 +00:00
|
|
|
if ((filetype = GetFileType(handle))) {
|
2020-06-15 14:18:57 +00:00
|
|
|
memset(st, 0, sizeof(*st));
|
2021-01-17 01:52:15 +00:00
|
|
|
switch (filetype) {
|
|
|
|
case kNtFileTypeChar:
|
2021-04-21 02:14:21 +00:00
|
|
|
st->st_mode = S_IFCHR | 0644;
|
2021-01-17 01:52:15 +00:00
|
|
|
break;
|
|
|
|
case kNtFileTypePipe:
|
2021-04-21 02:14:21 +00:00
|
|
|
st->st_mode = S_IFIFO | 0644;
|
2021-01-17 01:52:15 +00:00
|
|
|
break;
|
|
|
|
case kNtFileTypeDisk:
|
|
|
|
if (GetFileInformationByHandle(handle, &wst)) {
|
2021-04-21 02:14:21 +00:00
|
|
|
st->st_mode = 0555;
|
|
|
|
if (!(wst.dwFileAttributes & kNtFileAttributeReadonly)) {
|
|
|
|
st->st_mode |= 0200;
|
|
|
|
}
|
|
|
|
if (wst.dwFileAttributes & kNtFileAttributeDirectory) {
|
|
|
|
st->st_mode |= S_IFDIR;
|
|
|
|
} else if (wst.dwFileAttributes & kNtFileFlagOpenReparsePoint) {
|
|
|
|
st->st_mode |= S_IFLNK;
|
|
|
|
} else {
|
|
|
|
st->st_mode |= S_IFREG;
|
|
|
|
}
|
2021-01-17 01:52:15 +00:00
|
|
|
st->st_atim = FileTimeToTimeSpec(wst.ftLastAccessFileTime);
|
|
|
|
st->st_mtim = FileTimeToTimeSpec(wst.ftLastWriteFileTime);
|
|
|
|
st->st_ctim = FileTimeToTimeSpec(wst.ftCreationFileTime);
|
|
|
|
st->st_size = (uint64_t)wst.nFileSizeHigh << 32 | wst.nFileSizeLow;
|
|
|
|
st->st_blksize = PAGESIZE;
|
|
|
|
st->st_dev = wst.dwVolumeSerialNumber;
|
2021-08-12 07:42:14 +00:00
|
|
|
st->st_rdev = wst.dwVolumeSerialNumber;
|
2021-01-17 01:52:15 +00:00
|
|
|
st->st_ino = (uint64_t)wst.nFileIndexHigh << 32 | wst.nFileIndexLow;
|
|
|
|
st->st_nlink = wst.nNumberOfLinks;
|
|
|
|
if (GetFileInformationByHandleEx(handle, kNtFileCompressionInfo, &fci,
|
|
|
|
sizeof(fci))) {
|
|
|
|
actualsize = fci.CompressedFileSize;
|
|
|
|
} else {
|
|
|
|
actualsize = st->st_size;
|
|
|
|
}
|
|
|
|
st->st_blocks = roundup(actualsize, PAGESIZE) / 512;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
} else {
|
2020-11-28 20:01:51 +00:00
|
|
|
return __winerr();
|
2020-06-15 14:18:57 +00:00
|
|
|
}
|
|
|
|
}
|