mirror of
https://github.com/safing/portmaster
synced 2025-09-01 10:09:11 +00:00
* Move portbase into monorepo * Add new simple module mgr * [WIP] Switch to new simple module mgr * Add StateMgr and more worker variants * [WIP] Switch more modules * [WIP] Switch more modules * [WIP] swtich more modules * [WIP] switch all SPN modules * [WIP] switch all service modules * [WIP] Convert all workers to the new module system * [WIP] add new task system to module manager * [WIP] Add second take for scheduling workers * [WIP] Add FIXME for bugs in new scheduler * [WIP] Add minor improvements to scheduler * [WIP] Add new worker scheduler * [WIP] Fix more bug related to new module system * [WIP] Fix start handing of the new module system * [WIP] Improve startup process * [WIP] Fix minor issues * [WIP] Fix missing subsystem in settings * [WIP] Initialize managers in constructor * [WIP] Move module event initialization to constrictors * [WIP] Fix setting for enabling and disabling the SPN module * [WIP] Move API registeration into module construction * [WIP] Update states mgr for all modules * [WIP] Add CmdLine operation support * Add state helper methods to module group and instance * Add notification and module status handling to status package * Fix starting issues * Remove pilot widget and update security lock to new status data * Remove debug logs * Improve http server shutdown * Add workaround for cleanly shutting down firewall+netquery * Improve logging * Add syncing states with notifications for new module system * Improve starting, stopping, shutdown; resolve FIXMEs/TODOs * [WIP] Fix most unit tests * Review new module system and fix minor issues * Push shutdown and restart events again via API * Set sleep mode via interface * Update example/template module * [WIP] Fix spn/cabin unit test * Remove deprecated UI elements * Make log output more similar for the logging transition phase * Switch spn hub and observer cmds to new module system * Fix log sources * Make worker mgr less error prone * Fix tests and minor issues * Fix observation hub * Improve shutdown and restart handling * Split up big connection.go source file * Move varint and dsd packages to structures repo * Improve expansion test * Fix linter warnings * Fix interception module on windows * Fix linter errors --------- Co-authored-by: Vladimir Stoilov <vladimir@safing.io>
170 lines
4.9 KiB
Go
170 lines
4.9 KiB
Go
package renameio
|
|
|
|
import (
|
|
"errors"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// TempDir checks whether os.TempDir() can be used as a temporary directory for
|
|
// later atomically replacing files within dest. If no (os.TempDir() resides on
|
|
// a different mount point), dest is returned.
|
|
//
|
|
// Note that the returned value ceases to be valid once either os.TempDir()
|
|
// changes (e.g. on Linux, once the TMPDIR environment variable changes) or the
|
|
// file system is unmounted.
|
|
func TempDir(dest string) string {
|
|
return tempDir("", filepath.Join(dest, "renameio-TempDir"))
|
|
}
|
|
|
|
func tempDir(dir, dest string) string {
|
|
if dir != "" {
|
|
return dir // caller-specified directory always wins
|
|
}
|
|
|
|
// Chose the destination directory as temporary directory so that we
|
|
// definitely can rename the file, for which both temporary and destination
|
|
// file need to point to the same mount point.
|
|
fallback := filepath.Dir(dest)
|
|
|
|
// The user might have overridden the os.TempDir() return value by setting
|
|
// the TMPDIR environment variable.
|
|
tmpdir := os.TempDir()
|
|
|
|
testsrc, err := os.CreateTemp(tmpdir, "."+filepath.Base(dest))
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
cleanup := true
|
|
defer func() {
|
|
if cleanup {
|
|
_ = os.Remove(testsrc.Name())
|
|
}
|
|
}()
|
|
_ = testsrc.Close()
|
|
|
|
testdest, err := os.CreateTemp(filepath.Dir(dest), "."+filepath.Base(dest))
|
|
if err != nil {
|
|
return fallback
|
|
}
|
|
defer func() {
|
|
_ = os.Remove(testdest.Name())
|
|
}()
|
|
_ = testdest.Close()
|
|
|
|
if err := os.Rename(testsrc.Name(), testdest.Name()); err != nil {
|
|
return fallback
|
|
}
|
|
cleanup = false // testsrc no longer exists
|
|
return tmpdir
|
|
}
|
|
|
|
// PendingFile is a pending temporary file, waiting to replace the destination
|
|
// path in a call to CloseAtomicallyReplace.
|
|
type PendingFile struct {
|
|
*os.File
|
|
|
|
path string
|
|
done bool
|
|
closed bool
|
|
}
|
|
|
|
// Cleanup is a no-op if CloseAtomicallyReplace succeeded, and otherwise closes
|
|
// and removes the temporary file.
|
|
func (t *PendingFile) Cleanup() error {
|
|
if t.done {
|
|
return nil
|
|
}
|
|
// An error occurred. Close and remove the tempfile. Errors are returned for
|
|
// reporting, there is nothing the caller can recover here.
|
|
var closeErr error
|
|
if !t.closed {
|
|
closeErr = t.Close()
|
|
}
|
|
if err := os.Remove(t.Name()); err != nil {
|
|
return err
|
|
}
|
|
return closeErr
|
|
}
|
|
|
|
// CloseAtomicallyReplace closes the temporary file and atomically replaces
|
|
// the destination file with it, i.e., a concurrent open(2) call will either
|
|
// open the file previously located at the destination path (if any), or the
|
|
// just written file, but the file will always be present.
|
|
func (t *PendingFile) CloseAtomicallyReplace() error {
|
|
// Even on an ordered file system (e.g. ext4 with data=ordered) or file
|
|
// systems with write barriers, we cannot skip the fsync(2) call as per
|
|
// Theodore Ts'o (ext2/3/4 lead developer):
|
|
//
|
|
// > data=ordered only guarantees the avoidance of stale data (e.g., the previous
|
|
// > contents of a data block showing up after a crash, where the previous data
|
|
// > could be someone's love letters, medical records, etc.). Without the fsync(2)
|
|
// > a zero-length file is a valid and possible outcome after the rename.
|
|
if err := t.Sync(); err != nil {
|
|
return err
|
|
}
|
|
t.closed = true
|
|
if err := t.Close(); err != nil {
|
|
return err
|
|
}
|
|
if err := os.Rename(t.Name(), t.path); err != nil {
|
|
return err
|
|
}
|
|
t.done = true
|
|
return nil
|
|
}
|
|
|
|
// TempFile wraps os.CreateTemp for the use case of atomically creating or
|
|
// replacing the destination file at path.
|
|
//
|
|
// If dir is the empty string, TempDir(filepath.Base(path)) is used. If you are
|
|
// going to write a large number of files to the same file system, store the
|
|
// result of TempDir(filepath.Base(path)) and pass it instead of the empty
|
|
// string.
|
|
//
|
|
// The file's permissions will be 0600 by default. You can change these by
|
|
// explicitly calling Chmod on the returned PendingFile.
|
|
func TempFile(dir, path string) (*PendingFile, error) {
|
|
f, err := os.CreateTemp(tempDir(dir, path), "."+filepath.Base(path))
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &PendingFile{File: f, path: path}, nil
|
|
}
|
|
|
|
// Symlink wraps os.Symlink, replacing an existing symlink with the same name
|
|
// atomically (os.Symlink fails when newname already exists, at least on Linux).
|
|
func Symlink(oldname, newname string) error {
|
|
// Fast path: if newname does not exist yet, we can skip the whole dance
|
|
// below.
|
|
if err := os.Symlink(oldname, newname); err == nil || !errors.Is(err, fs.ErrExist) {
|
|
return err
|
|
}
|
|
|
|
// We need to use os.MkdirTemp, as we cannot overwrite a os.CreateTemp,
|
|
// and removing+symlinking creates a TOCTOU race.
|
|
d, err := os.MkdirTemp(filepath.Dir(newname), "."+filepath.Base(newname))
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cleanup := true
|
|
defer func() {
|
|
if cleanup {
|
|
_ = os.RemoveAll(d)
|
|
}
|
|
}()
|
|
|
|
symlink := filepath.Join(d, "tmp.symlink")
|
|
if err := os.Symlink(oldname, symlink); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := os.Rename(symlink, newname); err != nil {
|
|
return err
|
|
}
|
|
|
|
cleanup = false
|
|
return os.RemoveAll(d)
|
|
}
|