in file/rotator.go [243:279]
func (r *Rotator) openNew() error {
err := os.MkdirAll(r.dir(), r.dirMode())
if err != nil {
return fmt.Errorf("failed to make directories for new file: %w", err)
}
stat, err := os.Lstat(r.rot.ActiveFile())
if err == nil {
isSymlink := stat.Mode()&fs.ModeSymlink != 0
// check if the file has to be rotated before writing to it
reason, t := r.isRotationTriggered(0)
if reason == rotateReasonNoRotate {
// To avoid symlink following attacks, if the active file is a symlink
// we need to rotate it to avoid writing to the symlink target, which
// could be a sensitive or protected file not owned by us.
if isSymlink {
if r.log != nil {
r.log.Debugw("Active file is a symlink, forcing rotation", "filename", r.rot.ActiveFile())
}
reason = rotateReasonInitializing
t = r.clock.Now()
} else {
return r.appendToFile()
}
}
if err = r.rot.Rotate(reason, t); err != nil {
return fmt.Errorf("failed to rotate backups: %w", err)
}
if err = r.purge(); err != nil {
return fmt.Errorf("failed to purge unnecessary rotated files: %w", err)
}
}
return r.openFile()
}