Skip to content

Commit 9c2c35f

Browse files
committed
Make index compatible with virtual drives on Windows
1 parent 0f20787 commit 9c2c35f

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ regex = { version = "1.5.5", default-features = false, features = ["std", "unico
2323
aho-corasick = "0.7"
2424
tantivy-fst = "0.4.0"
2525
memmap2 = { version = "0.5.3", optional = true }
26+
normpath = "1.0.0"
2627
lz4_flex = { version = "0.9.2", default-features = false, features = ["checked-decode"], optional = true }
2728
brotli = { version = "3.3.4", optional = true }
2829
zstd = { version = "0.12", optional = true, default-features = false }

src/directory/mmap_directory.rs

+13-15
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use std::{fmt, result};
99
use common::StableDeref;
1010
use fs2::FileExt;
1111
use memmap2::Mmap;
12+
use normpath::PathExt;
1213
use serde::{Deserialize, Serialize};
1314
use tempfile::TempDir;
1415

@@ -196,9 +197,9 @@ impl MmapDirectory {
196197
directory_path,
197198
)));
198199
}
199-
let canonical_path: PathBuf = directory_path.canonicalize().map_err(|io_err| {
200+
let canonical_path: PathBuf = directory_path.normalize().map_err(|io_err| {
200201
OpenDirectoryError::wrap_io_error(io_err, PathBuf::from(directory_path))
201-
})?;
202+
})?.into_path_buf();
202203
if !canonical_path.is_dir() {
203204
return Err(OpenDirectoryError::NotADirectory(PathBuf::from(
204205
directory_path,
@@ -443,26 +444,23 @@ impl Directory for MmapDirectory {
443444
Ok(self.inner.watch(watch_callback))
444445
}
445446

447+
#[cfg(windows)]
448+
fn sync_directory(&self) -> Result<(), io::Error> {
449+
// On Windows, it is not necessary to fsync the parent directory to
450+
// ensure that the directory entry containing the file has also reached
451+
// disk, and calling sync_data on a handle to directory is a no-op on
452+
// local disks, but will return an error on virtual drives.
453+
Ok(())
454+
}
455+
456+
#[cfg(not(windows))]
446457
fn sync_directory(&self) -> Result<(), io::Error> {
447458
let mut open_opts = OpenOptions::new();
448459

449460
// Linux needs read to be set, otherwise returns EINVAL
450461
// write must not be set, or it fails with EISDIR
451462
open_opts.read(true);
452463

453-
// On Windows, opening a directory requires FILE_FLAG_BACKUP_SEMANTICS
454-
// and calling sync_all() only works if write access is requested.
455-
#[cfg(windows)]
456-
{
457-
use std::os::windows::fs::OpenOptionsExt;
458-
459-
use winapi::um::winbase;
460-
461-
open_opts
462-
.write(true)
463-
.custom_flags(winbase::FILE_FLAG_BACKUP_SEMANTICS);
464-
}
465-
466464
let fd = open_opts.open(&self.inner.root_path)?;
467465
fd.sync_data()?;
468466
Ok(())

0 commit comments

Comments
 (0)