Files
ClaudeandClaude Opus 5 7ee270e128 Add printer and scanner driver support
A new "driver" provider installs Windows printer and scanner drivers through
the same catalog, UI and profiles as everything else: stage the package into the
driver store with pnputil, then register it with the print spooler so it turns
up in Add Printer.

Driver names are matched by PREFIX against the names read out of the package's
own INF files. Vendors stamp the version into the name -- "HP Universal Printing
PCL 6 (v7.1.0)" -- so an exact name in a catalog goes stale at the next release,
and a wrong one is unrecoverable without knowing what the package contains. When
a prefix matches nothing, the error lists the names the package actually
provides, which turns a dead end into a one-line fix.

INF parsing handles what real files are, not what the spec suggests: Windows
ships its own printer INFs as UTF-16LE and vendor packages follow suit, so
reading them as UTF-8 finds nothing at all. Verified against every prn*.inf on
a stock Windows install -- that test is what caught the encoding bug, since the
hand-written sample parsed fine.

Scanners are TWAIN/WIA rather than spooler drivers, so there is nothing to
register: leaving driver_match unset means stage-only. Detection falls back to
the uninstall entry, because a TWAIN data source has no spooler record.

Packages come from a driver share rather than the web. Vendor URLs are
version-pinned and several are EULA-gated, so nothing stable can be shipped in a
catalog; a UNC path or local folder is used in place with no download at all,
which is faster and is what a shop wants anyway. $DEPLOYR_DRIVERS names the
share, and an unset variable is reported by name rather than surfacing three
layers down as "no such file".

Ships universal drivers for HP, Xerox, Ricoh, Sharp, Canon, Kyocera, Konica
Minolta, Lexmark, Brother, Toshiba, Epson, Muratec and Zebra, plus scanner
packages for the fi series, ScanSnap, Canon DR, Epson, Brother, Visioneer and
Kodak Alaris. Profiles: @printers, @printers-all, @scanners.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-26 17:47:24 -05:00

195 lines
5.1 KiB
Go

// Package platform answers the "where am I running and with what rights"
// questions: OS/arch, elevation, and the well-known directories deployr uses
// for logs, downloads and catalog overlays.
package platform
import (
"os"
"path/filepath"
"regexp"
"runtime"
"strings"
)
// OS returns the normalized operating system name: windows, darwin or linux.
func OS() string { return runtime.GOOS }
// Arch returns the normalized CPU architecture: amd64, arm64, 386, arm.
func Arch() string { return runtime.GOARCH }
// Supported reports whether deployr knows how to operate on this OS.
func Supported() bool {
switch runtime.GOOS {
case "windows", "darwin", "linux":
return true
}
return false
}
// Hostname is the machine name, or "unknown" when it cannot be determined.
func Hostname() string {
h, err := os.Hostname()
if err != nil || h == "" {
return "unknown"
}
return h
}
// DataDir is the machine-wide state directory. It falls back to a per-user
// directory when the system location is not writable, so an unelevated run
// still works for user-scope package managers.
func DataDir() string {
return firstWritable(systemDataDir(), userDir("deployr"))
}
// LogDir is where run logs are written.
func LogDir() string {
return firstWritable(systemLogDir(), filepath.Join(userDir("deployr"), "logs"))
}
// CacheDir holds downloaded installers.
func CacheDir() string {
return firstWritable(filepath.Join(systemDataDir(), "cache"), filepath.Join(userDir("deployr"), "cache"))
}
// CatalogDirs lists the directories scanned for catalog overlay files
// (*.yaml / *.yml), in increasing order of precedence.
func CatalogDirs() []string {
dirs := []string{filepath.Join(systemDataDir(), "catalog.d")}
if runtime.GOOS != "windows" {
dirs = append(dirs, "/etc/deployr/catalog.d")
}
dirs = append(dirs, filepath.Join(userDir("deployr"), "catalog.d"))
return dirs
}
func systemDataDir() string {
switch runtime.GOOS {
case "windows":
pd := os.Getenv("ProgramData")
if pd == "" {
pd = `C:\ProgramData`
}
return filepath.Join(pd, "Deployr")
case "darwin":
return "/Library/Application Support/Deployr"
default:
return "/var/lib/deployr"
}
}
func systemLogDir() string {
switch runtime.GOOS {
case "windows":
return filepath.Join(systemDataDir(), "logs")
case "darwin":
return "/Library/Logs/Deployr"
default:
return "/var/log/deployr"
}
}
func userDir(name string) string {
base, err := os.UserCacheDir()
if err != nil || base == "" {
base = os.TempDir()
}
return filepath.Join(base, name)
}
// firstWritable returns the first candidate directory that exists or can be
// created, falling back to the last candidate unconditionally.
func firstWritable(candidates ...string) string {
for i, dir := range candidates {
if dir == "" {
continue
}
if err := os.MkdirAll(dir, 0o755); err == nil {
return dir
}
if i == len(candidates)-1 {
return dir
}
}
return os.TempDir()
}
var winVar = regexp.MustCompile(`%([A-Za-z_][A-Za-z0-9_()]*)%`)
// ExpandVars resolves $VAR, ${VAR} and Windows-style %VAR% in a string, without
// touching path separators. Use this for anything that is not a filesystem path
// -- a URL in particular, which filepath.Clean would mangle into "https:/host".
func ExpandVars(s string) string {
if s == "" {
return ""
}
s = winVar.ReplaceAllStringFunc(s, func(m string) string {
return os.Getenv(strings.Trim(m, "%"))
})
return os.ExpandEnv(s)
}
var unixVar = regexp.MustCompile(`\$\{?([A-Za-z_][A-Za-z0-9_]*)\}?`)
// MissingVars returns the environment variables a string references that are
// unset or empty. Expansion turns those into nothing, which produces a baffling
// "no such file" three layers down; naming the variable up front does not.
func MissingVars(s string) []string {
var missing []string
seen := map[string]bool{}
note := func(name string) {
if name == "" || seen[name] {
return
}
seen[name] = true
if os.Getenv(name) == "" {
missing = append(missing, name)
}
}
for _, m := range winVar.FindAllStringSubmatch(s, -1) {
note(m[1])
}
for _, m := range unixVar.FindAllStringSubmatch(s, -1) {
note(m[1])
}
return missing
}
// ExpandPath resolves ~, $VAR/${VAR} and Windows-style %VAR% in a path.
// Unset variables expand to empty, matching shell behaviour.
func ExpandPath(p string) string {
if p == "" {
return ""
}
if strings.HasPrefix(p, "~") {
if home, err := os.UserHomeDir(); err == nil {
p = filepath.Join(home, strings.TrimPrefix(p, "~"))
}
}
p = ExpandVars(p)
if p == "" {
return ""
}
// A UNC path must keep its leading double slash, which Clean would eat.
if strings.HasPrefix(p, `\\`) || strings.HasPrefix(p, "//") {
return `\\` + filepath.Clean(strings.TrimLeft(p, `\/`))
}
return filepath.Clean(p)
}
// PathExists reports whether a file or directory exists at the expanded path.
// Glob metacharacters are honoured so a catalog can match e.g.
// "%ProgramFiles%/Mozilla Firefox/firefox.exe".
func PathExists(p string) bool {
p = ExpandPath(p)
if p == "" {
return false
}
if strings.ContainsAny(p, "*?[") {
matches, err := filepath.Glob(p)
return err == nil && len(matches) > 0
}
_, err := os.Stat(p)
return err == nil
}