Make improvements

- Invent openatemp() API
- Invent O_UNLINK open flag
- Introduce getenv_secure() API
- Remove `git pull` from cosmocc
- Fix utimes() when path is NULL
- Fix mktemp() to never return NULL
- Fix utimensat() UTIME_OMIT on XNU
- Improve utimensat() code for RHEL5
- Turn `argv[0]` C:/ to /C/ on Windows
- Introduce tmpnam() and tmpnam_r() APIs
- Fix more const issues with internal APIs
- Permit utimes() on WIN32 in O_RDONLY mode
- Fix fdopendir() to check fd is a directory
- Fix recent crash regression in landlock make
- Fix futimens(AT_FDCWD, NULL) to return EBADF
- Use workaround so `make -j` doesn't fork bomb
- Rename dontdiscard to __wur (just like glibc)
- Fix st_size for WIN32 symlinks containing UTF-8
- Introduce stdio ext APIs needed by GNU coreutils
- Fix lstat() on WIN32 for symlinks to directories
- Move some constants from normalize.inc to limits.h
- Fix segv with memchr() and memcmp() overlapping page
- Implement POSIX fflush() behavior for reader streams
- Implement AT_SYMLINK_NOFOLLOW for utimensat() on WIN32
- Don't change read-only status of existing files on WIN32
- Correctly handle `0x[^[:xdigit:]]` case in strtol() functions
This commit is contained in:
Justine Tunney 2023-09-06 03:54:42 -07:00
parent 8596e83cce
commit f531acc8f9
No known key found for this signature in database
GPG key ID: BE714B4575D6E328
297 changed files with 1920 additions and 1681 deletions

View file

@ -33,7 +33,7 @@
#define _O_EXCL 0x00000080 // kNtCreateNew
#define _O_TRUNC 0x00000200 // kNtCreateAlways
#define _O_DIRECTORY 0x00010000 // kNtFileFlagBackupSemantics
#define _O_TMPFILE 0x00410000 // AttributeTemporary|FlagDeleteOnClose
#define _O_UNLINK 0x04000100 // kNtFileAttributeTemporary|DeleteOnClose
#define _O_DIRECT 0x00004000 // kNtFileFlagNoBuffering
#define _O_NONBLOCK 0x00000800 // kNtFileFlagWriteThrough (not sent to win32)
#define _O_RANDOM 0x80000000 // kNtFileFlagRandomAccess
@ -51,11 +51,18 @@ textwindows int GetNtOpenFlags(int flags, int mode, uint32_t *out_perm,
if (flags &
~(O_ACCMODE | _O_APPEND | _O_CREAT | _O_EXCL | _O_TRUNC | _O_DIRECTORY |
_O_TMPFILE | _O_NONBLOCK | _O_RANDOM | _O_SEQUENTIAL | _O_COMPRESSED |
_O_UNLINK | _O_NONBLOCK | _O_RANDOM | _O_SEQUENTIAL | _O_COMPRESSED |
_O_INDEXED | _O_CLOEXEC | _O_DIRECT)) {
return einval();
}
// "Some of these flags should not be combined. For instance,
// combining kNtFileFlagRandomAccess with kNtFileFlagSequentialScan
// is self-defeating." -Quoth MSDN § CreateFileW
if ((flags & _O_SEQUENTIAL) && (flags & _O_RANDOM)) {
return einval();
}
switch (flags & O_ACCMODE) {
case O_RDONLY:
perm = kNtFileGenericRead;
@ -80,7 +87,7 @@ textwindows int GetNtOpenFlags(int flags, int mode, uint32_t *out_perm,
}
attr = 0;
is_creating_file = (flags & _O_CREAT) || (flags & _O_TMPFILE) == _O_TMPFILE;
is_creating_file = !!(flags & _O_CREAT);
// POSIX O_EXEC doesn't mean the same thing as kNtGenericExecute. We
// request execute access when we can determine it from mode's bits.
@ -125,33 +132,64 @@ textwindows int GetNtOpenFlags(int flags, int mode, uint32_t *out_perm,
disp = kNtOpenExisting;
}
// Please use tmpfd() or tmpfile() instead of O_TMPFILE.
if ((flags & _O_TMPFILE) == _O_TMPFILE) {
attr |= kNtFileAttributeTemporary | kNtFileFlagDeleteOnClose;
} else {
attr |= kNtFileAttributeNormal;
if (flags & _O_DIRECTORY) {
attr |= kNtFileFlagBackupSemantics;
}
}
// The Windows content indexing service ravages performance similar to
// Windows Defender. Please pass O_INDEXED to openat() to enable this.
if (~flags & _O_INDEXED) {
// The Windows content indexing service ravages performance similar to
// Windows Defender. Please pass O_INDEXED to openat() to enable this.
attr |= kNtFileAttributeNotContentIndexed;
}
// Windows' transparent filesystem compression is really cool, as such
// we've introduced a nonstandard O_COMPRESSED flag to help you use it
if (flags & _O_COMPRESSED) {
// Windows' transparent filesystem compression is really cool, as such
// we've introduced a nonstandard O_COMPRESSED flag to help you use it
attr |= kNtFileAttributeCompressed;
}
if (flags & kNtFileAttributeTemporary) { // subset of _O_UNLINK
// "Specifying the kNtFileAttributeTemporary attribute causes file
// systems to avoid writing data back to mass storage if sufficient
// cache memory is available, because an application deletes a
// temporary file after a handle is closed. In that case, the
// system can entirely avoid writing the data. Although it does not
// directly control data caching in the same way as the
// [kNtFileFlagNoBuffering, kNtFileFlagWriteThrough,
// kNtFileFlagSequentialScan, and kNtFileFlagRandomAccess] flags,
// the kNtFileAttributeTemporary attribute does tell the system to
// hold as much as possible in the system cache without writing and
// therefore may be of concern for certain applications." -MSDN
attr |= kNtFileAttributeTemporary;
}
if (!attr) {
// "All other file attributes override kNtFileAttributeNormal [...]
// [which] is valid only if used alone." -Quoth MSDN § CreateFileW
attr |= kNtFileAttributeNormal;
}
attr |= flags & kNtFileFlagDeleteOnClose; // subset of _O_UNLINK
if (flags & _O_DIRECTORY) {
// "You must set this flag to obtain a handle to a directory."
// -Quoth MSDN § CreateFileW
attr |= kNtFileFlagBackupSemantics;
}
// Not certain yet what benefit these flags offer.
if (flags & _O_SEQUENTIAL) attr |= kNtFileFlagSequentialScan;
if (flags & _O_RANDOM) attr |= kNtFileFlagRandomAccess;
if (flags & _O_DIRECT) attr |= kNtFileFlagNoBuffering;
// TODO(jart): Should we *always* open with write permission if the
// kernel will give it to us? We'd then deny write access
// in libc system call wrappers.
//
// "When an application creates a file across a network, it is better
// to use kNtGenericRead | kNtGenericWrite for dwDesiredAccess than
// to use kNtGenericWrite alone. The resulting code is faster,
// because the redirector can use the cache manager and send fewer
// SMBs with more data. This combination also avoids an issue where
// writing to a file across a network can occasionally return
// kNtErrorAccessDenied." -Quoth MSDN
if (out_perm) *out_perm = perm;
if (out_share) *out_share = share;
if (out_disp) *out_disp = disp;