Skip to content

Commit

Permalink
Don't set FILE_ATTRIBUTE_HIDDEN
Browse files Browse the repository at this point in the history
If this attribute is set, we cannot close and reopen the file and then
write to it. Attempting to open the file a second time in write mode
results in `ERROR_ACCESS_DENIED`.

For reference, see the CreateFile MSDN docs:

    If CREATE_ALWAYS and FILE_ATTRIBUTE_NORMAL are specified, CreateFile
    fails and sets the last error to ERROR_ACCESS_DENIED if the file
    exists and has the FILE_ATTRIBUTE_HIDDEN or FILE_ATTRIBUTE_SYSTEM
    attribute. To avoid the error, specify the same attributes as the
    existing file.
  • Loading branch information
jasonwhite authored and Stebalien committed Dec 28, 2018
1 parent 3d7ca36 commit 221480b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
5 changes: 2 additions & 3 deletions src/file/imp/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use winapi::um::fileapi::{CreateFileW, SetFileAttributesW, CREATE_NEW};
use winapi::um::handleapi::INVALID_HANDLE_VALUE;
use winapi::um::winbase::{FILE_FLAG_DELETE_ON_CLOSE, MOVEFILE_REPLACE_EXISTING};
use winapi::um::winbase::{MoveFileExW, ReOpenFile};
use winapi::um::winnt::{FILE_ATTRIBUTE_HIDDEN, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_TEMPORARY};
use winapi::um::winnt::{FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_TEMPORARY};
use winapi::um::winnt::{FILE_GENERIC_READ, FILE_GENERIC_WRITE, HANDLE};
use winapi::um::winnt::{FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE};

Expand All @@ -24,8 +24,7 @@ const SHARE_MODE: DWORD = FILE_SHARE_DELETE
| FILE_SHARE_READ
| FILE_SHARE_WRITE;
#[cfg_attr(irustfmt, rustfmt_skip)]
const FLAGS: DWORD = FILE_ATTRIBUTE_HIDDEN
| FILE_ATTRIBUTE_TEMPORARY;
const FLAGS: DWORD = FILE_ATTRIBUTE_TEMPORARY;

fn to_utf16(s: &Path) -> Vec<u16> {
s.as_os_str()
Expand Down
6 changes: 6 additions & 0 deletions tests/namedtempfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,9 @@ fn test_temppath_persist_noclobber() {
assert_eq!("abcde", buf);
std::fs::remove_file(&persist_path).unwrap();
}

#[test]
fn test_write_after_close() {
let path = NamedTempFile::new().unwrap().into_temp_path();
File::create(path).unwrap().write_all(b"test").unwrap();
}

0 comments on commit 221480b

Please sign in to comment.