font-store: name macOS copies by family, recheck before publishing, drop gone system files

Flat-layout copies are named Craft-<family folder>-<file>, so two families with the
same file name don't collide. A Present entry whose system file is gone is dropped by
settle and shows as pending. copy_in publishes through a hard link (no replace) and
rechecks the destination first; a file another app put there is a conflict. Ledger
copies count as the store's in the conflict check only while unchanged. A no-op
install reshares, and withdraw finds families the ledger alone still lists.
This commit is contained in:
aiongg
2026-11-25 01:43:38 +00:00
parent 07dff4f8c0
commit 92e4b97eac
4 changed files with 54 additions and 12 deletions
+2 -1
View File
@@ -429,7 +429,8 @@ impl Installer for SuiteStore {
}
fn withdraw(&self, family: &str, files: &[PathBuf]) -> Result<Vec<PathBuf>, StoreError> {
let (source, name) = self.found(family, files)?;
// A family an unsupported build removed keeps its ledger entry and copies.
let (source, name) = self.found(family, files).or_else(|e| self.store.available_family_holding(family, files).ok_or(e))?;
self.store.withdraw(&source, &name).map_err(err)
}
}
+1 -1
View File
@@ -200,7 +200,7 @@ The user's `remove_family` and `remove_files` withdraw the copies first, so the
| System | Folder (`OsFontFolder::for_env`) | Copies |
|---|---|---|
| macOS | `~/Library/Fonts` | `Craft-<file>`, side by side. macOS activates them for every app of the user. |
| macOS | `~/Library/Fonts` | `Craft-<family folder>-<file>`, side by side. macOS activates them for every app of the user. |
| Linux and the BSDs | `$XDG_DATA_HOME/fonts/Craft` (`~/.local/share/fonts/Craft`) | `<store family folder>/<file>`. fontconfig reads the folder with its subfolders; `fc-cache` refreshes the cache afterwards when it is on `PATH`, off the calling thread. |
| Flatpak | the host's `$HOST_XDG_DATA_HOME/fonts/Craft`, else `~/.local/share/fonts/Craft` | Only when `/.flatpak-info` grants write access to that folder (`--filesystem=xdg-data/fonts:create`; its entries apply in order, `!name` revokes and `:ro` grants no write). No `fc-cache`: it would refresh the sandbox's cache. |
| Windows 10 1809 and later | `%LOCALAPPDATA%\Microsoft\Windows\Fonts\Craft` | `<store family folder>/<file>`, each listed as `<full name> (TrueType)` (or `(OpenType)` for CFF outlines) under `HKCU\Software\Microsoft\Windows NT\CurrentVersion\Fonts`, loaded into the running session with `AddFontResourceW`, then `WM_FONTCHANGE` is broadcast off the calling thread. A registry value that already names another file is a conflict. Withdrawing unloads the font, deletes the value, then deletes the file; a file another program still holds can't be deleted, so the withdrawal fails and the ledger keeps the copy for the next try. The Win32 calls are in `craft-font-os-windows`. |
+1
View File
@@ -724,6 +724,7 @@ impl FontStore {
let wanted: Vec<&FontFile> = files.iter().copied().filter(|f| !have(f) || !folder.join(f.file_name()).is_file()).collect();
let license_missing = family.license_file_name().is_some_and(|name| !folder.join(name).is_file());
if wanted.is_empty() && !license_missing {
self.reshare(&family.source, &family.family);
return installed.ok_or_else(|| Error::NotInstalled(family.family.clone()));
}
self.require_network()?;
+50 -10
View File
@@ -41,7 +41,7 @@ const MAX_LEDGER_BYTES: u64 = 16 << 20;
/// How the OS user font folder is laid out.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum OsFontLayout {
/// One folder of files, each named `Craft-<file>`: `~/Library/Fonts`.
/// One folder of files, each named `Craft-<family folder>-<file>`: `~/Library/Fonts`.
Flat,
/// A folder per family, `<folder>/<store family folder>/<file>`: `~/.local/share/fonts/Craft`,
/// which fontconfig reads with its subfolders.
@@ -137,7 +137,7 @@ impl OsFontFolder {
/// Where the copy of `file` of the store family in folder `family_dir` goes.
fn path_for(&self, family_dir: &str, file: &str) -> PathBuf {
match self.layout {
OsFontLayout::Flat => self.dir.join(format!("Craft-{file}")),
OsFontLayout::Flat => self.dir.join(format!("Craft-{family_dir}-{file}")),
OsFontLayout::Folders => self.dir.join(family_dir).join(file),
}
}
@@ -366,6 +366,10 @@ impl Ledger {
if owned {
restore_removing(&f.path);
}
if f.mechanism == Mechanism::Present {
// The system copy is gone: the next make-available copies the file.
return std::fs::metadata(&f.path).is_ok();
}
if f.state == State::Active {
return true;
}
@@ -513,12 +517,23 @@ impl FontStore {
/// The store files of an available family that have no copy in the OS folder yet, by their
/// names in its folder: styles installed since (an agent's install doesn't copy them), or
/// files whose copy is older than the store's. Empty for a family that isn't available.
/// files whose copy is older than the store's, or whose identical system file is gone. Empty
/// for a family that isn't available.
pub fn pending_files(&self, source: &str, name: &str) -> Vec<String> {
let Some(ledger) = self.cached_ledger() else { return Vec::new() };
let Some(listed) = ledger.family(source, name) else { return Vec::new() };
let Some(family) = self.installed_family(source, name) else { return Vec::new() };
family.files.iter().filter(|f| !listed.files.iter().any(|x| x.store_file == f.name && x.sha256 == f.sha256)).map(|f| f.name.clone()).collect()
family
.files
.iter()
.filter(|f| {
!listed
.files
.iter()
.any(|x| x.store_file == f.name && x.sha256 == f.sha256 && (x.mechanism == Mechanism::Copy || std::fs::metadata(&x.path).is_ok()))
})
.map(|f| f.name.clone())
.collect()
}
/// Makes the installed family available to all apps, from the user's own action: copies its
@@ -607,7 +622,7 @@ impl FontStore {
let ours: HashSet<FileId> = ledger
.families
.iter()
.flat_map(|f| f.copies().map(|x| &x.path))
.flat_map(|f| f.copies().filter(|x| file_sha256(&x.path).as_ref() == Some(&x.sha256)).map(|x| &x.path))
.chain(planned.iter().map(|p| &p.path))
.filter_map(|p| file_id(p))
.collect();
@@ -656,7 +671,7 @@ impl FontStore {
let state = if disk.as_ref() == Some(&p.sha256) {
State::Active
} else {
to_copy.push(p);
to_copy.push((p, disk.clone()));
State::Adding
};
files.push(LedgerFile {
@@ -685,7 +700,7 @@ impl FontStore {
ledger.families.sort_by_key(|f| (f.family.to_lowercase(), f.source.clone()));
self.write_ledger(&ledger)?;
if let Err(e) = to_copy.into_iter().try_for_each(|p| copy_in(folder, p)) {
if let Err(e) = to_copy.into_iter().try_for_each(|(p, found)| copy_in(folder, p, found.as_deref())) {
ledger.settle(folder);
let _ = self.write_ledger(&ledger);
return Err(e);
@@ -856,8 +871,9 @@ impl FontStore {
}
}
/// Copies a store file to its place through a dot-file, checking the copy's sha256.
fn copy_in(folder: &OsFontFolder, p: &Planned) -> Result<(), Error> {
/// Copies a store file to its place through a dot-file, checking the copy's sha256. `found` is
/// the sha256 of the file planning found at the place (the family's earlier copy), if any.
fn copy_in(folder: &OsFontFolder, p: &Planned, found: Option<&str>) -> Result<(), Error> {
let what = || format!("copying {} to {}", p.store_file, p.path.display());
if let Some(parent) = p.path.parent() {
std::fs::create_dir_all(parent).map_err(io(what()))?;
@@ -876,13 +892,37 @@ fn copy_in(folder: &OsFontFolder, p: &Planned) -> Result<(), Error> {
Err(Error::Verify { file: p.store_file.clone(), error: VerifyError::Sha256 })
}
});
if let Err(e) = checked.and_then(|()| std::fs::rename(&part, &p.path).map_err(io(what()))) {
if let Err(e) = checked.and_then(|()| publish(&part, p, found)) {
let _ = std::fs::remove_file(&part);
return Err(e);
}
Ok(())
}
/// Moves the checked copy `part` to `p.path`, only over what planning found there: nothing, or
/// the file with sha256 `found`. A file another app put there since is a conflict.
fn publish(part: &Path, p: &Planned, found: Option<&str>) -> Result<(), Error> {
let conflict = || Error::Conflict(vec![Conflict { postscript: None, path: p.path.clone() }]);
let absent = || std::fs::symlink_metadata(&p.path).is_err();
if absent() {
// A hard link never replaces a file that appeared since the check.
match std::fs::hard_link(part, &p.path) {
Ok(()) => {
let _ = std::fs::remove_file(part);
return Ok(());
}
// A file system without hard links falls through to the rename below.
Err(e) if e.kind() != std::io::ErrorKind::AlreadyExists && absent() => {}
Err(_) => return Err(conflict()),
}
} else if found.is_none() || file_sha256(&p.path).as_deref() != found {
return Err(conflict());
}
// Over the earlier copy, or without hard links, a file another app writes between the check
// above and this rename is replaced: std has no rename that refuses to replace.
std::fs::rename(part, &p.path).map_err(io(format!("copying {} to {}", p.store_file, p.path.display())))
}
/// Deletes a listed copy of the family in store folder `dir` when it is the store's and
/// unchanged: `Ok(true)` when it is gone (or already was), `Ok(false)` when it is left in place.
/// The file is moved aside first and checked there, so one changed in the meantime goes back.