Hide .spacedrive file on Windows (#2216)

* Hide `.spacedrive` file on Windows

* Use hard-coded hidden file attribute

* Remove winapi crate

* fix: use `?` operator instead of `unwrap`

---------

Co-authored-by: jake <77554505+brxken128@users.noreply.github.com>
This commit is contained in:
Lynx
2024-03-18 07:28:54 -05:00
committed by GitHub
parent 3afc3bd34f
commit 1acf90e8cf
2 changed files with 26 additions and 9 deletions

View File

@@ -8,7 +8,10 @@ use std::{
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::{fs, io};
use tokio::{
fs::{self, OpenOptions},
io::{self, AsyncWriteExt},
};
use tracing::error;
use uuid::Uuid;
@@ -247,13 +250,28 @@ impl SpacedriveLocationMetadataFile {
}
async fn write_metadata(&self) -> Result<(), LocationMetadataError> {
fs::write(
&self.path,
serde_json::to_vec(&self.metadata)
.map_err(|e| LocationMetadataError::Serialize(e, self.path.clone()))?,
)
.await
.map_err(|e| LocationMetadataError::Write(e, self.path.clone()))
let mut file_options = OpenOptions::new();
// we want to write the file if it exists, otherwise create it
file_options.create(true).write(true);
#[cfg(target_os = "windows")]
{
let FILE_ATTRIBUTE_HIDDEN = 0x00000002; // Windows file attribute byte indicating hidden file
use std::os::windows::fs::OpenOptionsExt;
file_options.attributes(FILE_ATTRIBUTE_HIDDEN);
}
let metadata_contents = serde_json::to_vec(&self.metadata)
.map_err(|e| LocationMetadataError::Serialize(e, self.path.clone()))?;
file_options
.open(&self.path)
.await
.map_err(|e| LocationMetadataError::Write(e, self.path.clone()))?
.write_all(&metadata_contents)
.await
.map_err(|e| LocationMetadataError::Write(e, self.path.clone()))
}
}