Skip to content

Commit 4c85e5b

Browse files
Bump go directive and modernize stdlib usage
Raise the go directive to 1.21.0 so it is consistent with the toolchain go1.26.0 directive; the previous go 1.13 caused the vet inlining analyzer to report spurious diagnostics under a modern toolchain. With a current language version the deprecated APIs are now flagged, so replace them with their supported equivalents: - io/ioutil -> os (ReadDir, ReadFile, WriteFile) - reflect.Ptr -> reflect.Pointer Also fold in small cleanups surfaced along the way: use strings.TrimSuffix, add //go:build constraints, drop an unused test helper, and tighten test file permissions to 0600. Signed-off-by: Antonin Bas <antonin.bas@broadcom.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 8bb1ead commit 4c85e5b

7 files changed

Lines changed: 36 additions & 39 deletions

File tree

chown.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build !linux
12
// +build !linux
23

34
package lumberjack

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
module gopkg.in/natefinch/lumberjack.v2
22

3-
go 1.13
3+
go 1.21.0
44

55
toolchain go1.26.0

linux_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
package lumberjack

lumberjack.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// Note that this is v2.0 of lumberjack, and should be imported using gopkg.in
44
// thusly:
55
//
6-
// import "gopkg.in/natefinch/lumberjack.v2"
6+
// import "gopkg.in/natefinch/lumberjack.v2"
77
//
88
// The package name remains simply lumberjack, and the code resides at
99
// https://github.com/natefinch/lumberjack under the v2.0 branch.
@@ -26,7 +26,6 @@ import (
2626
"errors"
2727
"fmt"
2828
"io"
29-
"io/ioutil"
3029
"os"
3130
"path/filepath"
3231
"sort"
@@ -66,7 +65,7 @@ var _ io.WriteCloser = (*Logger)(nil)
6665
// `/var/log/foo/server.log`, a backup created at 6:30pm on Nov 11 2016 would
6766
// use the filename `/var/log/foo/server-2016-11-04T18-30-00.000.log`
6867
//
69-
// Cleaning Up Old Log Files
68+
// # Cleaning Up Old Log Files
7069
//
7170
// Whenever a new logfile gets created, old log files may be deleted. The most
7271
// recent files according to the encoded timestamp will be retained, up to a
@@ -331,10 +330,7 @@ func (l *Logger) millRunOnce() error {
331330
for _, f := range files {
332331
// Only count the uncompressed log file or the
333332
// compressed log file, not both.
334-
fn := f.Name()
335-
if strings.HasSuffix(fn, compressSuffix) {
336-
fn = fn[:len(fn)-len(compressSuffix)]
337-
}
333+
fn := strings.TrimSuffix(f.Name(), compressSuffix)
338334
preserved[fn] = true
339335

340336
if len(preserved) > l.MaxBackups {
@@ -450,24 +446,29 @@ func (l *Logger) stopMill() {
450446
// oldLogFiles returns the list of backup log files stored in the same
451447
// directory as the current log file, sorted by ModTime
452448
func (l *Logger) oldLogFiles() ([]logInfo, error) {
453-
files, err := ioutil.ReadDir(l.dir())
449+
entries, err := os.ReadDir(l.dir())
454450
if err != nil {
455451
return nil, fmt.Errorf("can't read log file directory: %s", err)
456452
}
457453
logFiles := []logInfo{}
458454

459455
prefix, ext := l.prefixAndExt()
460456

461-
for _, f := range files {
462-
if f.IsDir() {
457+
for _, e := range entries {
458+
if e.IsDir() {
459+
continue
460+
}
461+
info, err := e.Info()
462+
if err != nil {
463+
// The file may have been removed since ReadDir listed it.
463464
continue
464465
}
465-
if t, err := l.timeFromName(f.Name(), prefix, ext); err == nil {
466-
logFiles = append(logFiles, logInfo{t, f})
466+
if t, err := l.timeFromName(info.Name(), prefix, ext); err == nil {
467+
logFiles = append(logFiles, logInfo{t, info})
467468
continue
468469
}
469-
if t, err := l.timeFromName(f.Name(), prefix, ext+compressSuffix); err == nil {
470-
logFiles = append(logFiles, logInfo{t, f})
470+
if t, err := l.timeFromName(info.Name(), prefix, ext+compressSuffix); err == nil {
471+
logFiles = append(logFiles, logInfo{t, info})
471472
continue
472473
}
473474
// error parsing means that the suffix at the end was not generated

lumberjack_test.go

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"compress/gzip"
66
"encoding/json"
77
"fmt"
8-
"io/ioutil"
98
"os"
109
"path/filepath"
1110
"runtime"
@@ -63,7 +62,7 @@ func TestOpenExisting(t *testing.T) {
6362

6463
filename := logFile(dir)
6564
data := []byte("foo!")
66-
err := ioutil.WriteFile(filename, data, 0644)
65+
err := os.WriteFile(filename, data, 0600)
6766
isNil(err, t)
6867
existsWithContent(filename, data, t)
6968

@@ -188,7 +187,7 @@ func TestFirstWriteRotate(t *testing.T) {
188187
defer l.Close()
189188

190189
start := []byte("boooooo!")
191-
err := ioutil.WriteFile(filename, start, 0600)
190+
err := os.WriteFile(filename, start, 0600)
192191
isNil(err, t)
193192

194193
newFakeTime()
@@ -277,7 +276,7 @@ func TestMaxBackups(t *testing.T) {
277276
// create a file that is close to but different from the logfile name.
278277
// It shouldn't get caught by our deletion filters.
279278
notlogfile := logFile(dir) + ".foo"
280-
err = ioutil.WriteFile(notlogfile, []byte("data"), 0644)
279+
err = os.WriteFile(notlogfile, []byte("data"), 0600)
281280
isNil(err, t)
282281

283282
// Make a directory that exactly matches our log file filters... it still
@@ -295,7 +294,7 @@ func TestMaxBackups(t *testing.T) {
295294
// not be counted since both the compressed and the uncompressed
296295
// log files still exist.
297296
compLogFile := fourthFilename + compressSuffix
298-
err = ioutil.WriteFile(compLogFile, []byte("compress"), 0644)
297+
err = os.WriteFile(compLogFile, []byte("compress"), 0600)
299298
isNil(err, t)
300299

301300
// this will make us rotate again
@@ -344,24 +343,24 @@ func TestCleanupExistingBackups(t *testing.T) {
344343

345344
data := []byte("data")
346345
backup := backupFile(dir)
347-
err := ioutil.WriteFile(backup, data, 0644)
346+
err := os.WriteFile(backup, data, 0600)
348347
isNil(err, t)
349348

350349
newFakeTime()
351350

352351
backup = backupFile(dir)
353-
err = ioutil.WriteFile(backup+compressSuffix, data, 0644)
352+
err = os.WriteFile(backup+compressSuffix, data, 0600)
354353
isNil(err, t)
355354

356355
newFakeTime()
357356

358357
backup = backupFile(dir)
359-
err = ioutil.WriteFile(backup, data, 0644)
358+
err = os.WriteFile(backup, data, 0600)
360359
isNil(err, t)
361360

362361
// now create a primary log file with some data
363362
filename := logFile(dir)
364-
err = ioutil.WriteFile(filename, data, 0644)
363+
err = os.WriteFile(filename, data, 0600)
365364
isNil(err, t)
366365

367366
l := &Logger{
@@ -462,7 +461,7 @@ func TestOldLogFiles(t *testing.T) {
462461

463462
filename := logFile(dir)
464463
data := []byte("data")
465-
err := ioutil.WriteFile(filename, data, 07)
464+
err := os.WriteFile(filename, data, 0600)
466465
isNil(err, t)
467466

468467
// This gives us a time with the same precision as the time we get from the
@@ -471,7 +470,7 @@ func TestOldLogFiles(t *testing.T) {
471470
isNil(err, t)
472471

473472
backup := backupFile(dir)
474-
err = ioutil.WriteFile(backup, data, 07)
473+
err = os.WriteFile(backup, data, 0600)
475474
isNil(err, t)
476475

477476
newFakeTime()
@@ -480,7 +479,7 @@ func TestOldLogFiles(t *testing.T) {
480479
isNil(err, t)
481480

482481
backup2 := backupFile(dir)
483-
err = ioutil.WriteFile(backup2, data, 07)
482+
err = os.WriteFile(backup2, data, 0600)
484483
isNil(err, t)
485484

486485
l := &Logger{Filename: filename}
@@ -666,9 +665,9 @@ func TestCompressOnResume(t *testing.T) {
666665
// Create a backup file and empty "compressed" file.
667666
filename2 := backupFile(dir)
668667
b := []byte("foo!")
669-
err := ioutil.WriteFile(filename2, b, 0644)
668+
err := os.WriteFile(filename2, b, 0600)
670669
isNil(err, t)
671-
err = ioutil.WriteFile(filename2+compressSuffix, []byte{}, 0644)
670+
err = os.WriteFile(filename2+compressSuffix, []byte{}, 0600)
672671
isNil(err, t)
673672

674673
newFakeTime()
@@ -814,7 +813,7 @@ func TestMillRunDrainsPendingRequestBeforeStop(t *testing.T) {
814813
}
815814
// Pre-create a backup that the mill is expected to compress.
816815
backup := backupFile(dir)
817-
err := ioutil.WriteFile(backup, []byte("content"), 0644)
816+
err := os.WriteFile(backup, []byte("content"), 0600)
818817
isNil(err, t)
819818

820819
l.millCh = make(chan bool, 1)
@@ -848,7 +847,7 @@ func existsWithContent(path string, content []byte, t testing.TB) {
848847
isNilUp(err, t, 1)
849848
equalsUp(int64(len(content)), info.Size(), t, 1)
850849

851-
b, err := ioutil.ReadFile(path)
850+
b, err := os.ReadFile(path)
852851
isNilUp(err, t, 1)
853852
equalsUp(content, b, t, 1)
854853
}
@@ -867,15 +866,9 @@ func backupFileLocal(dir string) string {
867866
return filepath.Join(dir, "foobar-"+fakeTime().Format(backupTimeFormat)+".log")
868867
}
869868

870-
// logFileLocal returns the log file name in the given directory for the current
871-
// fake time using the local timezone.
872-
func logFileLocal(dir string) string {
873-
return filepath.Join(dir, fakeTime().Format(backupTimeFormat))
874-
}
875-
876869
// fileCount checks that the number of files in the directory is exp.
877870
func fileCount(dir string, exp int, t testing.TB) {
878-
files, err := ioutil.ReadDir(dir)
871+
files, err := os.ReadDir(dir)
879872
isNilUp(err, t, 1)
880873
// Make sure no other files were created.
881874
equalsUp(exp, len(files), t, 1)

rotate_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//go:build linux
12
// +build linux
23

34
package lumberjack

testing_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func _isNil(obtained interface{}) bool {
8383
}
8484

8585
switch v := reflect.ValueOf(obtained); v.Kind() {
86-
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Ptr, reflect.Slice:
86+
case reflect.Chan, reflect.Func, reflect.Interface, reflect.Map, reflect.Pointer, reflect.Slice:
8787
return v.IsNil()
8888
}
8989

0 commit comments

Comments
 (0)