From 1acf90e8cf39cd75e90bd61b538bf1371684d653 Mon Sep 17 00:00:00 2001 From: Lynx <141365347+iLynxcat@users.noreply.github.com> Date: Mon, 18 Mar 2024 07:28:54 -0500 Subject: [PATCH] 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> --- apps/desktop/crates/windows/Cargo.toml | 1 - core/src/location/metadata.rs | 34 ++++++++++++++++++++------ 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/desktop/crates/windows/Cargo.toml b/apps/desktop/crates/windows/Cargo.toml index 7a9d717b3..f9a4ba616 100644 --- a/apps/desktop/crates/windows/Cargo.toml +++ b/apps/desktop/crates/windows/Cargo.toml @@ -8,7 +8,6 @@ edition = { workspace = true } [dependencies] normpath = { workspace = true } thiserror = { workspace = true } - libc = "0.2" [target.'cfg(target_os = "windows")'.dependencies.windows] diff --git a/core/src/location/metadata.rs b/core/src/location/metadata.rs index 048685f01..7a8ffade5 100644 --- a/core/src/location/metadata.rs +++ b/core/src/location/metadata.rs @@ -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())) } }