use crate::fs::{FollowSymlinks, OpenOptions, OpenOptionsExt};
use std::fs;
use windows_sys::Win32::Storage::FileSystem::{
FILE_FLAG_BACKUP_SEMANTICS, FILE_FLAG_OPEN_REPARSE_POINT, FILE_FLAG_WRITE_THROUGH,
FILE_SHARE_DELETE,
};
pub(in super::super) fn prepare_open_options_for_open(opts: &mut OpenOptions) -> bool {
let mut trunc = opts.truncate;
let mut manually_trunc = false;
let mut custom_flags = match opts.follow {
FollowSymlinks::Yes => opts.ext.custom_flags,
FollowSymlinks::No => {
if trunc && !opts.create_new && !opts.append && opts.write {
manually_trunc = true;
trunc = false;
}
opts.ext.custom_flags | FILE_FLAG_OPEN_REPARSE_POINT
}
};
let mut share_mode = opts.ext.share_mode;
if opts.maybe_dir {
custom_flags |= FILE_FLAG_BACKUP_SEMANTICS;
share_mode &= !FILE_SHARE_DELETE;
}
if opts.sync || opts.dsync {
custom_flags |= FILE_FLAG_WRITE_THROUGH;
}
opts.truncate(trunc)
.share_mode(share_mode)
.custom_flags(custom_flags);
manually_trunc
}
pub(in super::super) fn open_options_to_std(opts: &OpenOptions) -> (fs::OpenOptions, bool) {
use std::os::windows::fs::OpenOptionsExt;
let mut opts = opts.clone();
let manually_trunc = prepare_open_options_for_open(&mut opts);
let mut std_opts = fs::OpenOptions::new();
std_opts
.read(opts.read)
.write(opts.write)
.append(opts.append)
.truncate(opts.truncate)
.create(opts.create)
.create_new(opts.create_new)
.share_mode(opts.ext.share_mode)
.custom_flags(opts.ext.custom_flags)
.attributes(opts.ext.attributes);
if opts.ext.security_qos_flags != 0 {
std_opts.security_qos_flags(opts.ext.security_qos_flags);
}
if let Some(access_mode) = opts.ext.access_mode {
std_opts.access_mode(access_mode);
}
(std_opts, manually_trunc)
}