mirror of
https://github.com/tailscale/tailscale.git
synced 2024-11-29 13:05:46 +00:00
71029cea2d
This updates all source files to use a new standard header for copyright and license declaration. Notably, copyright no longer includes a date, and we now use the standard SPDX-License-Identifier header. This commit was done almost entirely mechanically with perl, and then some minimal manual fixes. Updates #6865 Signed-off-by: Will Norris <will@tailscale.com>
46 lines
1.0 KiB
Go
46 lines
1.0 KiB
Go
// Copyright (c) Tailscale Inc & AUTHORS
|
|
// SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
// Package atomicfile contains code related to writing to filesystems
|
|
// atomically.
|
|
//
|
|
// This package should be considered internal; its API is not stable.
|
|
package atomicfile // import "tailscale.com/atomicfile"
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
)
|
|
|
|
// WriteFile writes data to filename+some suffix, then renames it
|
|
// into filename. The perm argument is ignored on Windows.
|
|
func WriteFile(filename string, data []byte, perm os.FileMode) (err error) {
|
|
f, err := os.CreateTemp(filepath.Dir(filename), filepath.Base(filename)+".tmp")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tmpName := f.Name()
|
|
defer func() {
|
|
if err != nil {
|
|
f.Close()
|
|
os.Remove(tmpName)
|
|
}
|
|
}()
|
|
if _, err := f.Write(data); err != nil {
|
|
return err
|
|
}
|
|
if runtime.GOOS != "windows" {
|
|
if err := f.Chmod(perm); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
if err := f.Sync(); err != nil {
|
|
return err
|
|
}
|
|
if err := f.Close(); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tmpName, filename)
|
|
}
|