d2b09d854f
Implement a new error handling library based on pkg/errors. It provides stack saving on wrapping and exports some function to add stack saving also to external errors. It also implements custom zerolog error formatting without adding too much verbosity by just printing the chain error file:line without a full stack trace of every error. * Add a --detailed-errors options to print error with they full chain * Wrap all error returns. Use errors.WithStack to wrap without adding a new messsage and error.Wrap[f] to add a message. * Add golangci-lint wrapcheck to check that external packages errors are wrapped. This won't check that internal packages error are wrapped. But we want also to ensure this case so we'll have to find something else to check also these.
89 lines
2.1 KiB
Go
89 lines
2.1 KiB
Go
// Copyright 2019 Sorint.lab
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package objectstorage
|
|
|
|
import (
|
|
"io"
|
|
"io/ioutil"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
|
|
"agola.io/agola/internal/errors"
|
|
)
|
|
|
|
// writeFileAtomicFunc atomically writes a file, it achieves this by creating a
|
|
// temporary file and then moving it. writeFunc is the func that will write
|
|
// data to the file.
|
|
// TODO(sgotti) remove left over tmp files if process crashes before calling
|
|
// os.Remove
|
|
func writeFileAtomicFunc(p, baseDir, tmpDir string, perm os.FileMode, persist bool, writeFunc func(f io.Writer) error) error {
|
|
f, err := ioutil.TempFile(tmpDir, "tmpfile")
|
|
if err != nil {
|
|
return errors.WithStack(err)
|
|
}
|
|
err = writeFunc(f)
|
|
if persist && err == nil {
|
|
err = f.Sync()
|
|
}
|
|
if closeErr := f.Close(); err == nil {
|
|
err = closeErr
|
|
}
|
|
if permErr := os.Chmod(f.Name(), perm); err == nil {
|
|
err = permErr
|
|
}
|
|
if err == nil {
|
|
err = os.Rename(f.Name(), p)
|
|
}
|
|
if err != nil {
|
|
os.Remove(f.Name())
|
|
return errors.WithStack(err)
|
|
}
|
|
|
|
if !persist {
|
|
return nil
|
|
}
|
|
// sync parent dirs
|
|
pdir := filepath.Dir(p)
|
|
for {
|
|
if !strings.HasPrefix(pdir, baseDir) {
|
|
break
|
|
}
|
|
f, err := os.Open(pdir)
|
|
if err != nil {
|
|
f.Close()
|
|
return nil
|
|
}
|
|
if err := f.Sync(); err != nil {
|
|
f.Close()
|
|
return nil
|
|
}
|
|
f.Close()
|
|
|
|
pdir = filepath.Dir(pdir)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
/*
|
|
func writeFileAtomic(filename, baseDir, tmpDir string, perm os.FileMode, persist bool, data []byte) error {
|
|
return writeFileAtomicFunc(filename, baseDir, tmpDir, perm, persist,
|
|
func(f io.Writer) error {
|
|
_, err := f.Write(data)
|
|
return errors.WithStack(err)
|
|
})
|
|
}
|
|
*/
|