Fix #17581 - Fix append to file on Windows (#17584)

* Fix append to file on Windows

* Test from @ret2libc
This commit is contained in:
GustavoLCR 2020-09-03 19:13:42 -03:00 committed by GitHub
parent a08337f7c7
commit f46b1749b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 2 deletions

View File

@ -369,7 +369,7 @@ R_API int r_sandbox_open(const char *path, int perm, int mode) {
if (perm & O_EXCL) {
creation = CREATE_NEW;
} else {
creation = CREATE_ALWAYS;
creation = OPEN_ALWAYS;
}
if (mode & S_IREAD && !(mode & S_IWRITE)) {
flags = FILE_ATTRIBUTE_READONLY;
@ -389,6 +389,9 @@ R_API int r_sandbox_open(const char *path, int perm, int mode) {
} else {
permission = GENERIC_READ;
}
if (perm & O_APPEND) {
permission |= FILE_APPEND_DATA;
}
wchar_t *wepath = r_utf8_to_utf16 (epath);
if (!wepath) {
@ -397,7 +400,7 @@ R_API int r_sandbox_open(const char *path, int perm, int mode) {
}
HANDLE h = CreateFileW (wepath, permission, FILE_SHARE_READ | (read_only ? 0 : FILE_SHARE_WRITE), NULL, creation, flags, NULL);
if (h != INVALID_HANDLE_VALUE) {
ret = _open_osfhandle ((intptr_t)h, 0);
ret = _open_osfhandle ((intptr_t)h, perm);
}
free (wepath);
}

View File

@ -24,3 +24,16 @@ EXPECT=<<EOF
EOF
RUN
NAME=append to file
FILE=-
CMDS=<<EOF
echo Hello > .r2-append-to-file.test
echo World >> .r2-append-to-file.test
cat .r2-append-to-file.test
rm -f .r2-append-to-file.test
EOF
EXPECT=<<EOF
Hello
World
EOF
RUN