Skip to content

Commit 7ebbe1a

Browse files
mergify[bot]narph
andauthored
Improve sync handling and errors (#312) (#327)
* add error message * rename * rewrite to tempfile * update reference (cherry picked from commit 1a76514) Co-authored-by: Mariana Dima <mariana@elastic.co>
1 parent d5bdaae commit 7ebbe1a

File tree

5 files changed

+75
-27
lines changed

5 files changed

+75
-27
lines changed

NOTICE.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -641,11 +641,11 @@ SOFTWARE
641641

642642
--------------------------------------------------------------------------------
643643
Dependency : github.com/elastic/elastic-agent-libs
644-
Version: v0.1.1
644+
Version: v0.1.2
645645
Licence type (autodetected): Apache-2.0
646646
--------------------------------------------------------------------------------
647647

648-
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.1.1/LICENSE:
648+
Contents of probable licence file $GOMODCACHE/github.com/elastic/elastic-agent-libs@v0.1.2/LICENSE:
649649

650650
Apache License
651651
Version 2.0, January 2004
@@ -4643,11 +4643,11 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
46434643

46444644
--------------------------------------------------------------------------------
46454645
Dependency : golang.org/x/tools
4646-
Version: v0.1.8
4646+
Version: v0.1.9
46474647
Licence type (autodetected): BSD-3-Clause
46484648
--------------------------------------------------------------------------------
46494649

4650-
Contents of probable licence file $GOMODCACHE/golang.org/x/tools@v0.1.8/LICENSE:
4650+
Contents of probable licence file $GOMODCACHE/golang.org/x/tools@v0.1.9/LICENSE:
46514651

46524652
Copyright (c) 2009 The Go Authors. All rights reserved.
46534653

@@ -15573,11 +15573,11 @@ limitations under the License.
1557315573

1557415574
--------------------------------------------------------------------------------
1557515575
Dependency : howett.net/plist
15576-
Version: v0.0.0-20201203080718-1454fab16a06
15576+
Version: v1.0.0
1557715577
Licence type (autodetected): BSD-2-Clause
1557815578
--------------------------------------------------------------------------------
1557915579

15580-
Contents of probable licence file $GOMODCACHE/howett.net/plist@v0.0.0-20201203080718-1454fab16a06/LICENSE:
15580+
Contents of probable licence file $GOMODCACHE/howett.net/plist@v1.0.0/LICENSE:
1558115581

1558215582
Copyright (c) 2013, Dustin L. Howett. All rights reserved.
1558315583

go.mod

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ require (
1010
github.com/coreos/go-systemd/v22 v22.3.2
1111
github.com/elastic/e2e-testing v1.99.2-0.20220117192005-d3365c99b9c4
1212
github.com/elastic/elastic-agent-client/v7 v7.0.0-20210727140539-f0905d9377f6
13-
github.com/elastic/elastic-agent-libs v0.1.1
13+
github.com/elastic/elastic-agent-libs v0.1.2
1414
github.com/elastic/go-licenser v0.4.0
1515
github.com/elastic/go-sysinfo v1.7.1
1616
github.com/elastic/go-ucfg v0.8.4
@@ -43,7 +43,7 @@ require (
4343
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5
4444
golang.org/x/lint v0.0.0-20210508222113-6edffad5e616
4545
golang.org/x/sys v0.0.0-20220209214540-3681064d5158
46-
golang.org/x/tools v0.1.8
46+
golang.org/x/tools v0.1.9
4747
google.golang.org/grpc v1.42.0
4848
google.golang.org/protobuf v1.27.1
4949
gopkg.in/yaml.v2 v2.4.0
@@ -130,7 +130,7 @@ require (
130130
google.golang.org/grpc/examples v0.0.0-20220304170021-431ea809a767 // indirect
131131
gopkg.in/inf.v0 v0.9.1 // indirect
132132
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
133-
howett.net/plist v0.0.0-20201203080718-1454fab16a06 // indirect
133+
howett.net/plist v1.0.0 // indirect
134134
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
135135
k8s.io/utils v0.0.0-20210930125809-cb0fa318a74b // indirect
136136
sigs.k8s.io/json v0.0.0-20211020170558-c049b76a60c6 // indirect

go.sum

+9-4
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,8 @@ github.com/elastic/e2e-testing v1.99.2-0.20220117192005-d3365c99b9c4 h1:uYT+Krd8
529529
github.com/elastic/e2e-testing v1.99.2-0.20220117192005-d3365c99b9c4/go.mod h1:UcNuf4pX/qDVNQr0zybm1NL2YoWik+jKBaINZqQCA40=
530530
github.com/elastic/elastic-agent-client/v7 v7.0.0-20210727140539-f0905d9377f6 h1:nFvXHBjYK3e9+xF0WKDeAKK4aOO51uC28s+L9rBmilo=
531531
github.com/elastic/elastic-agent-client/v7 v7.0.0-20210727140539-f0905d9377f6/go.mod h1:uh/Gj9a0XEbYoM4NYz4LvaBVARz3QXLmlNjsrKY9fTc=
532-
github.com/elastic/elastic-agent-libs v0.1.1 h1:13Y268vfgkZK99Ql1X2WdT8jjpd01stln2wLvlewkbo=
533-
github.com/elastic/elastic-agent-libs v0.1.1/go.mod h1:h8K/f7RcdxM2f19VahcS1jeco170ItqV9N7HyYsn9Ss=
532+
github.com/elastic/elastic-agent-libs v0.1.2 h1:dcHNpyRITPkYs2gv9HPkMvNuOGkBjP32dcQy6GJwsVs=
533+
github.com/elastic/elastic-agent-libs v0.1.2/go.mod h1:xG0zNuqE4rDl/rM2B0OTh4SN+Q5+VxqtC27mcwVUNys=
534534
github.com/elastic/elastic-package v0.32.1/go.mod h1:l1fEnF52XRBL6a5h6uAemtdViz2bjtjUtgdQcuRhEAY=
535535
github.com/elastic/go-concert v0.2.0/go.mod h1:HWjpO3IAEJUxOeaJOWXWEp7imKd27foxz9V5vegC/38=
536536
github.com/elastic/go-elasticsearch/v7 v7.16.0/go.mod h1:OJ4wdbtDNk5g503kvlHLyErCgQwwzmDtaFC4XyOxXA4=
@@ -1701,6 +1701,8 @@ go.elastic.co/apm/module/apmhttp v1.7.2/go.mod h1:sTFWiWejnhSdZv6+dMgxGec2Nxe/ZK
17011701
go.elastic.co/apm/module/apmhttp v1.10.0/go.mod h1:Y4timwcJ8sQWbWpcw3Y7Mat1OssNpGhpwyfUnpqIDew=
17021702
go.elastic.co/apm/module/apmhttp v1.15.0 h1:Le/DhI0Cqpr9wG/NIGOkbz7+rOMqJrfE4MRG6q/+leU=
17031703
go.elastic.co/apm/module/apmhttp v1.15.0/go.mod h1:NruY6Jq8ALLzWUVUQ7t4wIzn+onKoiP5woJJdTV7GMg=
1704+
go.elastic.co/apm/module/apmhttp/v2 v2.0.0/go.mod h1:5KmxcNN7hkJh8sVW3Ggl/pYgnwiNenygE46bZoUb9RE=
1705+
go.elastic.co/apm/v2 v2.0.0/go.mod h1:KGQn56LtRmkQjt2qw4+c1Jz8gv9rCBUU/m21uxrqcps=
17041706
go.elastic.co/ecszap v0.3.0/go.mod h1:HTUi+QRmr3EuZMqxPX+5fyOdMNfUu5iPebgfhgsTJYQ=
17051707
go.elastic.co/ecszap v1.0.0 h1:PdQkRUeraR3XHJ14T7JMa+ncU0XXrVrcEN/BoRa2nMI=
17061708
go.elastic.co/ecszap v1.0.0/go.mod h1:HTUi+QRmr3EuZMqxPX+5fyOdMNfUu5iPebgfhgsTJYQ=
@@ -2243,8 +2245,9 @@ golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
22432245
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
22442246
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
22452247
golang.org/x/tools v0.1.7/go.mod h1:LGqMHiF4EqQNHR1JncWGqT5BVaXmza+X+BDGol+dOxo=
2246-
golang.org/x/tools v0.1.8 h1:P1HhGGuLW4aAclzjtmJdf0mJOjVUZUzOTqkAkWL+l6w=
22472248
golang.org/x/tools v0.1.8/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
2249+
golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8=
2250+
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
22482251
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
22492252
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
22502253
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
@@ -2476,6 +2479,7 @@ gopkg.in/src-d/go-git.v4 v4.13.1/go.mod h1:nx5NYcxdKxq5fpltdHnPa2Exj4Sx0EclMWZQb
24762479
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
24772480
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
24782481
gopkg.in/warnings.v0 v0.1.2/go.mod h1:jksf8JmL6Qr/oQM2OXTHunEvvTAsrWBLb6OOjuVWRNI=
2482+
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0/go.mod h1:WDnlLJ4WF5VGsH/HVa3CI79GS0ol3YnhVnKP89i0kNg=
24792483
gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bli9HhUf9+ttbYbLASfIpnQbh74=
24802484
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
24812485
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
@@ -2512,8 +2516,9 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
25122516
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
25132517
honnef.co/go/tools v0.2.2/go.mod h1:lPVVZ2BS5TfnjLyizF7o7hv7j9/L+8cZY2hLyjP9cGY=
25142518
howett.net/plist v0.0.0-20181124034731-591f970eefbb/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
2515-
howett.net/plist v0.0.0-20201203080718-1454fab16a06 h1:QDxUo/w2COstK1wIBYpzQlHX/NqaQTcf9jyz347nI58=
25162519
howett.net/plist v0.0.0-20201203080718-1454fab16a06/go.mod h1:vMygbs4qMhSZSc4lCUl2OEE+rDiIIJAIdR4m7MiMcm0=
2520+
howett.net/plist v1.0.0 h1:7CrbWYbPPO/PyNy38b2EB/+gYbjCe2DXBxgtOOZbSQM=
2521+
howett.net/plist v1.0.0/go.mod h1:lqaXoTrLY4hg8tnEzNru53gicrbv7rrk+2xJA/7hw9g=
25172522
k8s.io/api v0.17.5/go.mod h1:0zV5/ungglgy2Rlm3QK8fbxkXVs+BSJWpJP/+8gUVLY=
25182523
k8s.io/api v0.20.1/go.mod h1:KqwcCVogGxQY3nBlRpwt+wpAMF/KjaCc7RpywacvqUo=
25192524
k8s.io/api v0.20.4/go.mod h1:++lNL1AJMkDymriNniQsWRkMDzRaX2Y/POTUi8yvqYQ=

internal/pkg/agent/storage/disk_store.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,23 @@ func (d *DiskStore) Save(in io.Reader) error {
5555
defer os.Remove(tmpFile)
5656

5757
if _, err := io.Copy(fd, in); err != nil {
58-
_ = fd.Close()
58+
if err := fd.Close(); err != nil {
59+
return errors.New(err, "could not close temporary file",
60+
errors.TypeFilesystem,
61+
errors.M(errors.MetaKeyPath, tmpFile))
62+
}
5963

6064
return errors.New(err, "could not save content on disk",
6165
errors.TypeFilesystem,
6266
errors.M(errors.MetaKeyPath, tmpFile))
6367
}
6468

65-
_ = fd.Sync()
69+
if err := fd.Sync(); err != nil {
70+
return errors.New(err,
71+
fmt.Sprintf("could not sync temporary file %s", d.target),
72+
errors.TypeFilesystem,
73+
errors.M(errors.MetaKeyPath, tmpFile))
74+
}
6675

6776
if err := fd.Close(); err != nil {
6877
return errors.New(err, "could not close temporary file",

internal/pkg/agent/storage/replace_store.go

+46-12
Original file line numberDiff line numberDiff line change
@@ -85,30 +85,64 @@ func (r *ReplaceOnSuccessStore) Save(in io.Reader) error {
8585
errors.M("backup_path", backFilename))
8686
}
8787
}
88-
defer fd.Close()
88+
if err := fd.Close(); err != nil {
89+
return errors.New(err, fmt.Sprintf("could not close target file after checking for access %s", r.target),
90+
errors.TypeFilesystem,
91+
errors.M(errors.MetaKeyPath, r.target))
92+
}
8993

90-
if _, err := fd.Write(r.replaceWith); err != nil {
91-
if err := file.SafeFileRotate(r.target, backFilename); err != nil {
92-
return errors.New(err,
93-
fmt.Sprintf("could not rollback %s to %s", backFilename, r.target),
94+
// create a temporary file with the new changes, if successful, will replace the target file.
95+
tmpFile := r.target + ".tmp"
96+
97+
// Always clean up the temporary file and ignore errors.
98+
defer os.Remove(tmpFile)
99+
100+
fdt, err := os.OpenFile(tmpFile, os.O_CREATE|os.O_TRUNC|os.O_WRONLY, perms)
101+
if err != nil {
102+
return errors.New(err,
103+
fmt.Sprintf("could not save to %s", tmpFile),
104+
errors.TypeFilesystem,
105+
errors.M(errors.MetaKeyPath, tmpFile))
106+
}
107+
108+
if _, err := fdt.Write(r.replaceWith); err != nil {
109+
if err := fdt.Close(); err != nil {
110+
return errors.New(err, fmt.Sprintf("could not close temporary file %s", tmpFile),
94111
errors.TypeFilesystem,
95-
errors.M(errors.MetaKeyPath, r.target),
96-
errors.M("backup_path", backFilename))
112+
errors.M(errors.MetaKeyPath, tmpFile))
97113
}
114+
return errors.New(err, fmt.Sprintf("could not succefully write new changes in temporary file %s", tmpFile),
115+
errors.TypeFilesystem,
116+
errors.M(errors.MetaKeyPath, tmpFile))
98117
}
99118

100-
if err := acl.Chmod(r.target, perms); err != nil {
119+
if err := fdt.Sync(); err != nil {
101120
return errors.New(err,
102-
fmt.Sprintf("could not set permissions target file %s", r.target),
121+
fmt.Sprintf("could not sync temporary file %s", tmpFile),
103122
errors.TypeFilesystem,
104-
errors.M(errors.MetaKeyPath, r.target))
123+
errors.M(errors.MetaKeyPath, tmpFile))
124+
}
125+
126+
if err := fdt.Close(); err != nil {
127+
return errors.New(err, fmt.Sprintf("could not close temporary file %s", tmpFile),
128+
errors.TypeFilesystem,
129+
errors.M(errors.MetaKeyPath, tmpFile))
105130
}
106131

107-
if err := fd.Sync(); err != nil {
132+
if err := file.SafeFileRotate(r.target, tmpFile); err != nil {
108133
return errors.New(err,
109-
fmt.Sprintf("could not sync target file %s", r.target),
134+
fmt.Sprintf("could not replace target file %s with %s", r.target, tmpFile),
135+
errors.TypeFilesystem,
136+
errors.M(errors.MetaKeyPath, r.target),
137+
errors.M("backup_path", backFilename))
138+
}
139+
140+
if err := acl.Chmod(r.target, perms); err != nil {
141+
return errors.New(err,
142+
fmt.Sprintf("could not set permissions target file %s", r.target),
110143
errors.TypeFilesystem,
111144
errors.M(errors.MetaKeyPath, r.target))
112145
}
146+
113147
return nil
114148
}

0 commit comments

Comments
 (0)