Skip to content

Commit

Permalink
Make index compatible with virtual drives on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
gyk committed Feb 9, 2023
1 parent 0f20787 commit 9c2c35f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ regex = { version = "1.5.5", default-features = false, features = ["std", "unico
aho-corasick = "0.7"
tantivy-fst = "0.4.0"
memmap2 = { version = "0.5.3", optional = true }
normpath = "1.0.0"
lz4_flex = { version = "0.9.2", default-features = false, features = ["checked-decode"], optional = true }
brotli = { version = "3.3.4", optional = true }
zstd = { version = "0.12", optional = true, default-features = false }
Expand Down
28 changes: 13 additions & 15 deletions src/directory/mmap_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{fmt, result};
use common::StableDeref;
use fs2::FileExt;
use memmap2::Mmap;
use normpath::PathExt;
use serde::{Deserialize, Serialize};
use tempfile::TempDir;

Expand Down Expand Up @@ -196,9 +197,9 @@ impl MmapDirectory {
directory_path,
)));
}
let canonical_path: PathBuf = directory_path.canonicalize().map_err(|io_err| {
let canonical_path: PathBuf = directory_path.normalize().map_err(|io_err| {
OpenDirectoryError::wrap_io_error(io_err, PathBuf::from(directory_path))
})?;
})?.into_path_buf();
if !canonical_path.is_dir() {
return Err(OpenDirectoryError::NotADirectory(PathBuf::from(
directory_path,
Expand Down Expand Up @@ -443,26 +444,23 @@ impl Directory for MmapDirectory {
Ok(self.inner.watch(watch_callback))
}

#[cfg(windows)]
fn sync_directory(&self) -> Result<(), io::Error> {
// On Windows, it is not necessary to fsync the parent directory to
// ensure that the directory entry containing the file has also reached
// disk, and calling sync_data on a handle to directory is a no-op on
// local disks, but will return an error on virtual drives.
Ok(())
}

#[cfg(not(windows))]
fn sync_directory(&self) -> Result<(), io::Error> {
let mut open_opts = OpenOptions::new();

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

// On Windows, opening a directory requires FILE_FLAG_BACKUP_SEMANTICS
// and calling sync_all() only works if write access is requested.
#[cfg(windows)]
{
use std::os::windows::fs::OpenOptionsExt;

use winapi::um::winbase;

open_opts
.write(true)
.custom_flags(winbase::FILE_FLAG_BACKUP_SEMANTICS);
}

let fd = open_opts.open(&self.inner.root_path)?;
fd.sync_data()?;
Ok(())
Expand Down

0 comments on commit 9c2c35f

Please sign in to comment.