Debian Codegraph @

debian/missing-sources/tempfile/src/file/imp/windows.rs

raw
1 use std::ffi::OsStr;
2 use std::fs::{File, OpenOptions};
3 use std::os::windows::ffi::OsStrExt;
4 use std::os::windows::fs::OpenOptionsExt;
5 use std::os::windows::io::{AsRawHandle, FromRawHandle, RawHandle};
6 use std::path::Path;
7 use std::{io, iter};
8
9 use windows_sys::Win32::Foundation::{HANDLE, INVALID_HANDLE_VALUE};
10 use windows_sys::Win32::Storage::FileSystem::{
11 MoveFileExW, ReOpenFile, SetFileAttributesW, FILE_ATTRIBUTE_NORMAL, FILE_ATTRIBUTE_TEMPORARY,
12 FILE_FLAG_DELETE_ON_CLOSE, FILE_GENERIC_READ, FILE_GENERIC_WRITE, FILE_SHARE_DELETE,
13 FILE_SHARE_READ, FILE_SHARE_WRITE, MOVEFILE_REPLACE_EXISTING,
14 };
15
16 use crate::util;
17
18 fn to_utf16(s: &Path) -> Vec<u16> {
19 s.as_os_str().encode_wide().chain(iter::once(0)).collect()
20 }
21
22 fn not_supported<T>(msg: &str) -> io::Result<T> {
23 Err(io::Error::new(io::ErrorKind::Other, msg))
24 }
25
26 pub fn create_named(
27 path: &Path,
28 open_options: &mut OpenOptions,
29 permissions: Option<&std::fs::Permissions>,
30 ) -> io::Result<File> {
31 if permissions.map_or(false, |p| p.readonly()) {
32 return not_supported("changing permissions is not supported on this platform");
33 }
34 open_options
35 .create_new(true)
36 .read(true)
37 .write(true)
38 .custom_flags(FILE_ATTRIBUTE_TEMPORARY)
39 .open(path)
40 }
41
42 pub fn create(dir: &Path) -> io::Result<File> {
43 util::create_helper(
44 dir,
45 OsStr::new(".tmp"),
46 OsStr::new(""),
47 crate::NUM_RAND_CHARS,
48 |path| {
49 let f = OpenOptions::new()
50 .create_new(true)
51 .read(true)
52 .write(true)
53 .share_mode(0)
54 .custom_flags(FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE)
55 .open(path)?;
56 // NOTE: in theory, we could delete the file immediately (we open the file in "unix
57 // semantics" mode) but this seemed to corrupt something in Windows at scale (see #339).
58 // So we just rely on `FILE_FLAG_DELETE_ON_CLOSE`.
59 Ok(f)
60 },
61 )
62 }
63
64 pub fn reopen(file: &File, _path: &Path) -> io::Result<File> {
65 let handle = file.as_raw_handle();
66 unsafe {
67 let handle = ReOpenFile(
68 handle as HANDLE,
69 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
70 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
71 0,
72 );
73 if handle == INVALID_HANDLE_VALUE {
74 Err(io::Error::last_os_error())
75 } else {
76 Ok(FromRawHandle::from_raw_handle(handle as RawHandle))
77 }
78 }
79 }
80
81 pub fn keep(path: &Path) -> io::Result<()> {
82 unsafe {
83 let path_w = to_utf16(path);
84 if SetFileAttributesW(path_w.as_ptr(), FILE_ATTRIBUTE_NORMAL) == 0 {
85 Err(io::Error::last_os_error())
86 } else {
87 Ok(())
88 }
89 }
90 }
91
92 pub fn persist(old_path: &Path, new_path: &Path, overwrite: bool) -> io::Result<()> {
93 unsafe {
94 let old_path_w = to_utf16(old_path);
95 let new_path_w = to_utf16(new_path);
96
97 // Don't succeed if this fails. We don't want to claim to have successfully persisted a file
98 // still marked as temporary because this file won't have the same consistency guarantees.
99 if SetFileAttributesW(old_path_w.as_ptr(), FILE_ATTRIBUTE_NORMAL) == 0 {
100 return Err(io::Error::last_os_error());
101 }
102
103 let mut flags = 0;
104
105 if overwrite {
106 flags |= MOVEFILE_REPLACE_EXISTING;
107 }
108
109 if MoveFileExW(old_path_w.as_ptr(), new_path_w.as_ptr(), flags) == 0 {
110 let e = io::Error::last_os_error();
111 // If this fails, the temporary file is now un-hidden and no longer marked temporary
112 // (slightly less efficient) but it will still work.
113 let _ = SetFileAttributesW(old_path_w.as_ptr(), FILE_ATTRIBUTE_TEMPORARY);
114 Err(e)
115 } else {
116 Ok(())
117 }
118 }
119 }