mirror of
https://github.com/opencloud-eu/opencloud.git
synced 2026-04-22 19:19:18 -05:00
committed by
Ralf Haferkamp
parent
90e4127227
commit
e85d8effc1
+21
-2
@@ -1,5 +1,24 @@
|
||||
version: "2"
|
||||
linters:
|
||||
disable:
|
||||
- errcheck
|
||||
- errcheck
|
||||
exclusions:
|
||||
generated: lax
|
||||
presets:
|
||||
- comments
|
||||
- common-false-positives
|
||||
- legacy
|
||||
- std-error-handling
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
formatters:
|
||||
enable:
|
||||
- gofmt
|
||||
- gofmt
|
||||
exclusions:
|
||||
generated: lax
|
||||
paths:
|
||||
- third_party$
|
||||
- builtin$
|
||||
- examples$
|
||||
|
||||
+2
-2
@@ -1,6 +1,6 @@
|
||||
[](https://github.com/google/renameio/actions?query=workflow%3ATest)
|
||||
[](https://pkg.go.dev/github.com/google/renameio)
|
||||
[](https://goreportcard.com/report/github.com/google/renameio)
|
||||
[](https://pkg.go.dev/github.com/google/renameio/v2)
|
||||
[](https://goreportcard.com/report/github.com/google/renameio/v2)
|
||||
|
||||
The `renameio` Go package provides a way to atomically create or replace a file or
|
||||
symbolic link.
|
||||
|
||||
+9
@@ -77,3 +77,12 @@ func WithExistingPermissions() Option {
|
||||
c.attemptPermCopy = true
|
||||
})
|
||||
}
|
||||
|
||||
// WithReplaceOnClose causes PendingFile.Close() to actually call
|
||||
// CloseAtomicallyReplace(). This means PendingFile implements io.Closer while
|
||||
// maintaining atomicity per default.
|
||||
func WithReplaceOnClose() Option {
|
||||
return optionFunc(func(c *config) {
|
||||
c.renameOnClose = true
|
||||
})
|
||||
}
|
||||
|
||||
+17
-6
@@ -114,9 +114,10 @@ func tempDir(dir, dest string) string {
|
||||
type PendingFile struct {
|
||||
*os.File
|
||||
|
||||
path string
|
||||
done bool
|
||||
closed bool
|
||||
path string
|
||||
done bool
|
||||
closed bool
|
||||
replaceOnClose bool
|
||||
}
|
||||
|
||||
// Cleanup is a no-op if CloseAtomicallyReplace succeeded, and otherwise closes
|
||||
@@ -131,7 +132,7 @@ func (t *PendingFile) Cleanup() error {
|
||||
// reporting, there is nothing the caller can recover here.
|
||||
var closeErr error
|
||||
if !t.closed {
|
||||
closeErr = t.Close()
|
||||
closeErr = t.File.Close()
|
||||
}
|
||||
if err := os.Remove(t.Name()); err != nil {
|
||||
return err
|
||||
@@ -159,7 +160,7 @@ func (t *PendingFile) CloseAtomicallyReplace() error {
|
||||
return err
|
||||
}
|
||||
t.closed = true
|
||||
if err := t.Close(); err != nil {
|
||||
if err := t.File.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(t.Name(), t.path); err != nil {
|
||||
@@ -169,6 +170,15 @@ func (t *PendingFile) CloseAtomicallyReplace() error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Close closes the file. By default it just calls Close() on the underlying file. For PendingFiles created with
|
||||
// WithReplaceOnClose it calls CloseAtomicallyReplace() instead.
|
||||
func (t *PendingFile) Close() error {
|
||||
if t.replaceOnClose {
|
||||
return t.CloseAtomicallyReplace()
|
||||
}
|
||||
return t.File.Close()
|
||||
}
|
||||
|
||||
// TempFile creates a temporary file destined to atomically creating or
|
||||
// replacing the destination file at path.
|
||||
//
|
||||
@@ -189,6 +199,7 @@ type config struct {
|
||||
attemptPermCopy bool
|
||||
ignoreUmask bool
|
||||
chmod *os.FileMode
|
||||
renameOnClose bool
|
||||
}
|
||||
|
||||
// NewPendingFile creates a temporary file destined to atomically creating or
|
||||
@@ -244,7 +255,7 @@ func NewPendingFile(path string, opts ...Option) (*PendingFile, error) {
|
||||
}
|
||||
}
|
||||
|
||||
return &PendingFile{File: f, path: cfg.path}, nil
|
||||
return &PendingFile{File: f, path: cfg.path, replaceOnClose: cfg.renameOnClose}, nil
|
||||
}
|
||||
|
||||
// Symlink wraps os.Symlink, replacing an existing symlink with the same name
|
||||
|
||||
Reference in New Issue
Block a user