Skip to content

Commit 8f77776

Browse files
committed
close open files
1 parent e47b76f commit 8f77776

File tree

2 files changed

+10
-9
lines changed

2 files changed

+10
-9
lines changed

pkg/rotate/roate.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ const (
2626
suffix = ".0"
2727
)
2828

29-
var (
30-
defaultRotator = Rotator{
31-
maxsize: 20 * Megabytes,
32-
tailsize: 10 * Megabytes,
33-
suffix: suffix,
34-
}
35-
)
29+
var defaultRotator = Rotator{
30+
maxsize: 20 * Megabytes,
31+
tailsize: 10 * Megabytes,
32+
suffix: suffix,
33+
}
3634

3735
type Size int64
3836

@@ -111,8 +109,8 @@ func (r *Rotator) Rotate(file string) error {
111109
} else if err != nil {
112110
return fmt.Errorf("failed to open file '%s': %w", file, err)
113111
}
114-
115112
defer fd.Close()
113+
116114
stat, err := fd.Stat()
117115
if err != nil {
118116
return fmt.Errorf("failed to get file stat: %w", err)
@@ -122,7 +120,7 @@ func (r *Rotator) Rotate(file string) error {
122120
return nil
123121
}
124122

125-
//otherwise we move seek to the end of the file
123+
// otherwise we move seek to the end of the file
126124
if _, err := fd.Seek(-int64(r.tailsize), 2); err != nil {
127125
return fmt.Errorf("failed to seek to truncate position: %w", err)
128126
}
@@ -132,6 +130,7 @@ func (r *Rotator) Rotate(file string) error {
132130
if err != nil {
133131
return fmt.Errorf("failed to create tail file '%s': %w", tail, err)
134132
}
133+
defer tailFd.Close()
135134

136135
if _, err := io.Copy(tailFd, fd); err != nil {
137136
return fmt.Errorf("failed to copy log tail: %w", err)

pkg/zinit/commands.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,8 @@ func (c *Client) Get(service string) (InitService, error) {
292292
if err != nil {
293293
return result, errors.Wrap(err, "couldn't open service file")
294294
}
295+
defer f.Close()
296+
295297
err = yaml.NewDecoder(f).Decode(&result)
296298
if err != nil {
297299
return result, errors.Wrap(err, "couldn't decode service file")

0 commit comments

Comments
 (0)