Debian Codegraph @

.cargo/vendor/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 None,
49 |path, _permissions| {
50 OpenOptions::new()
51 .create_new(true)
52 .read(true)
53 .write(true)
54 .share_mode(0)
55 .custom_flags(FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE)
56 .open(path)
57 },
58 )
59 }
60
61 pub fn reopen(file: &File, _path: &Path) -> io::Result<File> {
62 let handle = file.as_raw_handle();
63 unsafe {
64 let handle = ReOpenFile(
65 handle as HANDLE,
66 FILE_GENERIC_READ | FILE_GENERIC_WRITE,
67 FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
68 0,
69 );
70 if handle == INVALID_HANDLE_VALUE {
71 Err(io::Error::last_os_error())
72 } else {
73 Ok(FromRawHandle::from_raw_handle(handle as RawHandle))
74 }
75 }
76 }
77
78 pub fn keep(path: &Path) -> io::Result<()> {
79 unsafe {
80 let path_w = to_utf16(path);
81 if SetFileAttributesW(path_w.as_ptr(), FILE_ATTRIBUTE_NORMAL) == 0 {
82 Err(io::Error::last_os_error())
83 } else {
84 Ok(())
85 }
86 }
87 }
88
89 pub fn persist(old_path: &Path, new_path: &Path, overwrite: bool) -> io::Result<()> {
90 unsafe {
91 let old_path_w = to_utf16(old_path);
92 let new_path_w = to_utf16(new_path);
93
94 // Don't succeed if this fails. We don't want to claim to have successfully persisted a file
95 // still marked as temporary because this file won't have the same consistency guarantees.
96 if SetFileAttributesW(old_path_w.as_ptr(), FILE_ATTRIBUTE_NORMAL) == 0 {
97 return Err(io::Error::last_os_error());
98 }
99
100 let mut flags = 0;
101
102 if overwrite {
103 flags |= MOVEFILE_REPLACE_EXISTING;
104 }
105
106 if MoveFileExW(old_path_w.as_ptr(), new_path_w.as_ptr(), flags) == 0 {
107 let e = io::Error::last_os_error();
108 // If this fails, the temporary file is now un-hidden and no longer marked temporary
109 // (slightly less efficient) but it will still work.
110 let _ = SetFileAttributesW(old_path_w.as_ptr(), FILE_ATTRIBUTE_TEMPORARY);
111 Err(e)
112 } else {
113 Ok(())
114 }
115 }
116 }