localname

package
v0.60.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 12, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package localname is the one rule for what a name becomes on the disk of the computer a file is written to: whether that operating system can hold it at all (Problem), what it is called there when it cannot (Safe, Alias, Numbered), and when two different names are ONE file there (Key).

filex keeps the name it is given (internal/leafname says which names a door accepts), so a server holds names a person's own computer cannot: `report.` (Windows drops the trailing dot), `CON` and `aux.txt` (device names), `a:b.txt` (a forbidden character), a 300-byte Chinese name (ext4 and APFS hold 255 bytes), and `Rapor.docx` beside `rapor.docx` (NTFS and APFS compare names without case, APFS also without the normalisation form). Every program that writes server names onto a local disk asks this package: the sync engine (internal/filesync, which writes such an item under its Alias and remembers the pairing), `filex decrypt` (internal/e2edecrypt, which renames and warns). The desktop app's drag-out applies the same rules in TypeScript (desktop/src/local-name.ts, tested against the same hostile names).

The operating system is a parameter, never runtime.GOOS read here, so the rules of all three are tested on the one the suite runs on.

Leaf package: it imports nothing from filex.

Index

Constants

View Source
const (
	// Reserved: a Windows device name (CON, PRN, AUX, NUL, COM1-9, LPT1-9),
	// with or without an extension - `aux.txt` included, which Windows 10
	// still opens as the device.
	Reserved = "reserved"
	// Forbidden: a character Windows refuses in a name - < > : " | ? * \ /
	// or a control character.
	Forbidden = "forbidden"
	// Trailing: the name ends in a dot or a space, which Windows removes, so
	// the file it writes has another name.
	Trailing = "trailing"
	// TooLong: over MaxLen - bytes of UTF-8 on Linux and macOS, UTF-16
	// units on Windows.
	TooLong = "too_long"
)

Why a name cannot exist on a computer, as a stable code.

View Source
const MaxLen = 255

MaxLen is the longest one name may be: 255 bytes of UTF-8 on ext4, XFS and APFS, 255 UTF-16 units on NTFS.

Variables

This section is empty.

Functions

func Alias

func Alias(name, goos string) string

Alias is the name a copy of a server item is written under on goos when its own name cannot be (Problem != ""): Safe, shortened to fit (Fit), and made safe again - a cut can leave a trailing space. A name goos holds comes back as it is.

func Fit

func Fit(name, goos string) string

Fit shortens a name to MaxLen for goos, taking whole characters off the end of its stem and keeping its extension (when the extension is a short one). A name that fits is returned as it is.

func Fold

func Fold(name string) string

Fold compares names the way the strictest target does (case-insensitive and Unicode-normalised): a key for output that may land on any computer.

func Folds

func Folds(goos string) bool

Folds reports whether goos's usual file system calls two different names one: case-insensitive NTFS, case- and normalisation-insensitive APFS.

func Image

func Image(name, goos string) (image string, ok bool)

Image is the name goos actually writes when a program asks for name and does not check: Windows drops trailing dots and spaces (`report.` is written as `report`) and reads a backslash as a separator (`a\b.txt` is a folder `a`). ok is false when the name is written as it is, or when nothing recognisable is written at all.

func Key

func Key(name, goos string) string

Key is the name as goos's file system compares it: two names with one Key are one file there. NTFS ignores case and keeps both normalisation forms apart; APFS ignores both; everything else compares the bytes.

func Numbered

func Numbered(name string, n int, goos string) string

Numbered is name with " (n)" before its extension, shortened to fit: the next candidate when an Alias is already one name here with another item's.

func Problem

func Problem(name, goos string) string

Problem is why name cannot be a file or folder name on goos ("windows", "darwin", "linux", ...), or "" when it can.

func Safe

func Safe(name, goos string) string

Safe adapts a name to goos's rules: on Windows every forbidden character and every trailing dot or space becomes `_`, and a device name gets a `_` in front; elsewhere the name is returned as it is. It does not shorten a name (Problem says TooLong).

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL