mirror of
https://github.com/AdguardTeam/AdGuardHome.git
synced 2024-12-14 09:43:57 +03:00
e77de2e67d
Closes #7314. Squashed commit of the following: commit f8b6ffeec2f0f96c947cf896c75d05efaca77caf Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Tue Oct 29 14:14:41 2024 +0300 all: fix chlog commit9417b7dc51
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 19:41:30 2024 +0300 aghos: imp doc commitb91f0e72a7
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 19:26:15 2024 +0300 all: rm bin commit9008ee93b1
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 18:23:54 2024 +0300 all: revert permcheck commitbcc85d50f5
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 17:48:55 2024 +0300 all: use aghos more commit993e351712
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Mon Oct 28 16:24:56 2024 +0300 all: fix more bugs commita22b0d265e
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 18:30:52 2024 +0300 all: fix bugs commita2309f812a
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 17:05:08 2024 +0300 all: fix chlog, imp api commit42c3f8e91c
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 16:04:47 2024 +0300 scripts: fix docs commit9e781ff18d
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 16:03:19 2024 +0300 scripts: imp docs commit1dbc784982
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 15:55:16 2024 +0300 all: use new functions, add tests commitdcbabaf4e3
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Fri Oct 25 13:23:50 2024 +0300 aghos: add stat commit72d7c0f881
Author: Eugene Burkov <E.Burkov@AdGuard.COM> Date: Thu Oct 24 17:10:30 2024 +0300 aghos: add windows functions
76 lines
1.8 KiB
Go
76 lines
1.8 KiB
Go
//go:build windows
|
|
|
|
package aghrenameio
|
|
|
|
import (
|
|
"fmt"
|
|
"io/fs"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"github.com/AdguardTeam/AdGuardHome/internal/aghos"
|
|
"github.com/AdguardTeam/golibs/errors"
|
|
)
|
|
|
|
// pendingFile is a wrapper around [*os.File] calling [os.Rename] in its Close
|
|
// method.
|
|
type pendingFile struct {
|
|
file *os.File
|
|
targetPath string
|
|
}
|
|
|
|
// type check
|
|
var _ PendingFile = (*pendingFile)(nil)
|
|
|
|
// Cleanup implements the [PendingFile] interface for *pendingFile.
|
|
func (f *pendingFile) Cleanup() (err error) {
|
|
closeErr := f.file.Close()
|
|
err = os.Remove(f.file.Name())
|
|
|
|
// Put closeErr into the deferred error because that's where it is usually
|
|
// expected.
|
|
return errors.WithDeferred(err, closeErr)
|
|
}
|
|
|
|
// CloseReplace implements the [PendingFile] interface for *pendingFile.
|
|
func (f *pendingFile) CloseReplace() (err error) {
|
|
err = f.file.Close()
|
|
if err != nil {
|
|
return fmt.Errorf("closing: %w", err)
|
|
}
|
|
|
|
err = os.Rename(f.file.Name(), f.targetPath)
|
|
if err != nil {
|
|
return fmt.Errorf("renaming: %w", err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// Write implements the [PendingFile] interface for *pendingFile.
|
|
func (f *pendingFile) Write(b []byte) (n int, err error) {
|
|
return f.file.Write(b)
|
|
}
|
|
|
|
// NewPendingFile is a wrapper around [os.CreateTemp].
|
|
//
|
|
// f.Close must be called to finish the renaming.
|
|
func newPendingFile(filePath string, mode fs.FileMode) (f PendingFile, err error) {
|
|
// Use the same directory as the file itself, because moves across
|
|
// filesystems can be especially problematic.
|
|
file, err := os.CreateTemp(filepath.Dir(filePath), "")
|
|
if err != nil {
|
|
return nil, fmt.Errorf("opening pending file: %w", err)
|
|
}
|
|
|
|
err = aghos.Chmod(file.Name(), mode)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("preparing pending file: %w", err)
|
|
}
|
|
|
|
return &pendingFile{
|
|
file: file,
|
|
targetPath: filePath,
|
|
}, nil
|
|
}
|