From 3c3a883667390c2f03dffb3fdcb4fa4172cb23e6 Mon Sep 17 00:00:00 2001
From: Tobias Klauser <tklauser@distanz.ch>
Date: Mon, 22 May 2023 20:37:05 +0200
Subject: [PATCH] os: avoid second fcntl syscall in NewFile on unix

CL 494915 introduced an additional fcntl(F_GETFL) syscall to determine
whether the file is in append-only mode. The existing unix.IsNonblock
call also issues an fcntl(F_GETFL) syscall. The two can be combined and
both the append-only mode and the non-blocking flags can be determined
from that syscall's result.

Change-Id: I915589ed94e079f6abaa2fd0032ef01f78698f7f
Reviewed-on: https://go-review.googlesource.com/c/go/+/497075
Reviewed-by: Ian Lance Taylor <iant@google.com>
Reviewed-by: Bryan Mills <bcmills@google.com>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
---
 src/os/file_unix.go | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/src/os/file_unix.go b/src/os/file_unix.go
index 25ce83bf9dc..a34de8333da 100644
--- a/src/os/file_unix.go
+++ b/src/os/file_unix.go
@@ -104,13 +104,15 @@ func (f *File) Fd() uintptr {
 // constraints apply.
 func NewFile(fd uintptr, name string) *File {
 	kind := kindNewFile
-	if nb, err := unix.IsNonblock(int(fd)); err == nil && nb {
-		kind = kindNonBlock
-	}
-	f := newFile(fd, name, kind)
+	appendMode := false
 	if flags, err := unix.Fcntl(int(fd), syscall.F_GETFL, 0); err == nil {
-		f.appendMode = flags&syscall.O_APPEND != 0
+		if unix.HasNonblockFlag(flags) {
+			kind = kindNonBlock
+		}
+		appendMode = flags&syscall.O_APPEND != 0
 	}
+	f := newFile(fd, name, kind)
+	f.appendMode = appendMode
 	return f
 }
 
-- 
GitLab