hostfile
import "github.com/barevoidco/zelk/core/hostfile"Package hostfile provides the file opens, locks, replacing renames, directory syncs and private creation zelk hosts need to keep local state, the same on every platform. What a host keeps for the user alone is made private from the moment it exists and judged by its ACL on Windows, where permission bits say nothing.
Variables
Section titled “Variables”var ErrLocked = osfile.ErrLockedErrLocked reports that another open file already holds the lock.
var ErrNotPrivate = acl.ErrNotPrivateErrNotPrivate is wrapped by every refusal of a Windows file or directory another principal can reach. The error’s text says which principal, and whether the grant was inherited.
CheckACL
Section titled “CheckACL”func CheckACL(*os.File) errorCheckACL refuses a Windows file another principal can reach. Unix has no ACL to judge, so it returns nil; a host that checks Unix modes reads them from the file itself.
CheckDirACL
Section titled “CheckDirACL”func CheckDirACL(string) errorCheckDirACL refuses a Windows directory another principal can change, or whose entries give another principal access to the files made in it. Unix has no ACL to judge, so it returns nil.
CreateTemp
Section titled “CreateTemp”func CreateTemp(dir, pattern string) (*os.File, error)CreateTemp makes a new file in dir, as os.CreateTemp does, readable by the current user alone (mode 0o600). The caller owns the file.
MkdirAllPrivate
Section titled “MkdirAllPrivate”func MkdirAllPrivate(dir string) errorMkdirAllPrivate creates dir and any missing parents with mode 0o700. A directory that already exists is left as it is.
func Open(name string) (*os.File, error)Open opens name for reading, following links, so that another process can replace it with Rename while it is open. Go’s own os.Open on Windows holds the file against that; a reader of a file hosts replace uses Open. The caller owns the file.
OpenNoFollow
Section titled “OpenNoFollow”func OpenNoFollow(name string, flags int, mode fs.FileMode) (*os.File, error)OpenNoFollow opens a file with the given os.OpenFile flags and mode, refusing a symbolic link or junction at the final path component. Earlier path components may contain links. The caller owns the file.
OpenPrivate
Section titled “OpenPrivate”func OpenPrivate(name string, flag int) (*os.File, error)OpenPrivate opens name with the given os.OpenFile flags, refusing a link at the final component, and creates it with os.O_CREATE at mode 0o600. An existing file’s mode is not judged: a Unix host reads modes itself where it cares. On Windows the file is created with a descriptor naming only the current user, and one that exists is refused if others can reach it. The caller owns the file.
ReadFile
Section titled “ReadFile”func ReadFile(name string) ([]byte, error)ReadFile reads the whole of name as Open opens it, with os.ReadFile’s results.
Rename
Section titled “Rename”func Rename(oldpath, newpath string) errorRename renames oldpath to newpath, replacing newpath if it exists, even while it is open: that handle keeps the old contents, and every later open sees the new file. On Unix this is os.Rename. On Windows the handles open on newpath must have been opened with Open, OpenNoFollow or core’s own opens, which let a file be replaced; Go’s os.Open does not, and Rename then fails with a sharing violation. A host that publishes a file readers may hold open uses this, and reads it with Open.
SyncDir
Section titled “SyncDir”func SyncDir(dir string) errorSyncDir makes the names in dir durable: a file created, renamed or removed in it survives a crash once SyncDir returns. Use it in place of Sync on an opened directory, which Windows refuses.
TryLock
Section titled “TryLock”func TryLock(f *os.File) errorTryLock takes an exclusive lock without waiting. It returns ErrLocked wrapped in an error when the lock is held, including by another open of the same file in this process. Closing f releases the lock.
Example
dir, err := os.MkdirTemp("", "hostfile")if err != nil { panic(err)}defer os.RemoveAll(dir)
path := filepath.Join(dir, "state.lock")first, err := hostfile.OpenNoFollow(path, os.O_CREATE|os.O_RDWR, 0o600)if err != nil { panic(err)}defer first.Close()if err := hostfile.TryLock(first); err != nil { panic(err)}
second, err := hostfile.OpenNoFollow(path, os.O_RDWR, 0)if err != nil { panic(err)}defer second.Close()fmt.Println(errors.Is(hostfile.TryLock(second), hostfile.ErrLocked))// Output: trueOutput:
true