Visual Studio Code, rebuilt in Rust
Rust
//! platform/userData/common/fileUserDataProvider.ts: the `vscode-userdata` scheme over the disk provider. A user data
//! resource is the disk path under the `file` scheme; the profiles' settings, keybindings, tasks and extensions files
//! are written atomically, and disk changes reach the watchers of user data resources as user data changes.
use std::path::{Path, PathBuf};
use crate::disk::{self, Disk};
use crate::watcher::Change;
use crate::{FileSystem, Resource, Result, Stat};
/// `Schemas.vscodeUserData`.
pub const SCHEME: &str = "vscode-userdata";
pub struct FileUserDataProvider {
/// `environmentService.userRoamingDataHome` on disk: `<user data>/User`.
user_home: PathBuf,
/// `watchResources`.
watched: Vec<Resource>,
}
impl FileUserDataProvider {
pub fn new(user_home: &Path) -> Self {
FileUserDataProvider { user_home: user_home.to_path_buf(), watched: Vec::new() }
}
/// `atomicReadWriteResources` (`updateAtomicReadWritesResources`): every profile's settings, keybindings, tasks
/// and extensions files. The default profile keeps the first three in the user home and its extensions.json in
/// the extensions folder; every other profile keeps all four in `profiles/<id>` (userDataProfile.ts `profilesHome`).
pub fn enforce_atomic_write_file(&self, resource: &Resource) -> bool {
let path = Path::new(&resource.path);
let (Some(name), Some(location)) = (path.file_name().and_then(|name| name.to_str()), path.parent()) else { return false };
let key = |path: &Path| path.to_string_lossy().to_lowercase();
let in_default_profile = key(location) == key(&self.user_home) && ["settings.json", "keybindings.json", "tasks.json"].contains(&name);
let in_profile = location.parent().is_some_and(|profiles| key(profiles) == key(&self.user_home.join("profiles")))
&& ["settings.json", "keybindings.json", "tasks.json", "extensions.json"].contains(&name);
in_default_profile || in_profile
}
pub fn watch(&mut self, resource: &Resource) {
self.watched.push(resource.clone());
}
pub fn unwatch(&mut self, resource: &Resource) {
if let Some(index) = self.watched.iter().position(|watched| watched.is_equal(resource, true)) {
self.watched.remove(index);
}
}
/// `handleFileChanges`: the disk changes at or below a watched user data resource, as user data changes.
pub fn handle_file_changes(&self, changes: &[Change]) -> Vec<Change> {
let below_watched = |change: &Change| {
let resource = to_user_data_resource(&Resource::file(change.path.to_string_lossy()));
self.watched.iter().any(|watched| resource.is_equal_or_below(watched, true))
};
changes.iter().filter(|change| below_watched(change)).cloned().collect()
}
}
/// `toFileSystemResource`.
fn to_file_system_resource(resource: &Resource) -> Resource {
Resource { scheme: "file".into(), ..resource.clone() }
}
/// `toUserDataResource`.
pub fn to_user_data_resource(resource: &Resource) -> Resource {
Resource { scheme: SCHEME.into(), ..resource.clone() }
}
impl FileSystem for FileUserDataProvider {
fn capabilities(&mut self, scheme: &str) -> Option<u32> {
(scheme == SCHEME).then_some(disk::CAPABILITIES)
}
fn stat(&mut self, resource: &Resource) -> Result<Stat> {
Disk.stat(&to_file_system_resource(resource))
}
fn read_dir(&mut self, resource: &Resource) -> Result<Vec<(String, u32)>> {
Disk.read_dir(&to_file_system_resource(resource))
}
fn read_file(&mut self, resource: &Resource) -> Result<Vec<u8>> {
Disk.read_file(&to_file_system_resource(resource))
}
/// `enforceAtomicWriteFile` gives the `.vsctmp` postfix diskFileSystemProvider.ts `doWriteFileAtomic` renames over
/// the file.
fn write_file(&mut self, resource: &Resource, content: &[u8], create: bool, overwrite: bool) -> Result<()> {
let file = to_file_system_resource(resource);
if !self.enforce_atomic_write_file(resource) {
return Disk.write_file(&file, content, create, overwrite);
}
let path = Path::new(&file.path);
disk::validate_write(path, create, overwrite)?;
config::service::write_user_data_file(path, content).map_err(|err| disk::io_error(err, "open", path))
}
fn mkdir(&mut self, resource: &Resource) -> Result<()> {
Disk.mkdir(&to_file_system_resource(resource))
}
fn delete(&mut self, resource: &Resource, recursive: bool, use_trash: bool) -> Result<()> {
Disk.delete(&to_file_system_resource(resource), recursive, use_trash)
}
fn rename(&mut self, from: &Resource, to: &Resource, overwrite: bool) -> Result<()> {
Disk.rename(&to_file_system_resource(from), &to_file_system_resource(to), overwrite)
}
fn copy(&mut self, from: &Resource, to: &Resource, overwrite: bool) -> Result<()> {
Disk.copy(&to_file_system_resource(from), &to_file_system_resource(to), overwrite)
}
}
#[cfg(test)]
mod tests;