diff --git a/pkg/yqlib/chown_linux.go b/pkg/yqlib/chown_linux.go new file mode 100644 index 00000000..3913662b --- /dev/null +++ b/pkg/yqlib/chown_linux.go @@ -0,0 +1,18 @@ +//go:build linux + +package yqlib + +import ( + "io/fs" + "os" + "syscall" +) + +func changeOwner(info fs.FileInfo, file *os.File) error { + if stat, ok := info.Sys().(*syscall.Stat_t); ok { + uid := int(stat.Uid) + gid := int(stat.Gid) + return os.Chown(file.Name(), uid, gid) + } + return nil +} diff --git a/pkg/yqlib/chown_not_linux_os.go b/pkg/yqlib/chown_not_linux_os.go new file mode 100644 index 00000000..3675791a --- /dev/null +++ b/pkg/yqlib/chown_not_linux_os.go @@ -0,0 +1,12 @@ +//go:build !linux + +package yqlib + +import ( + "io/fs" + "os" +) + +func changeOwner(info fs.FileInfo, file *os.File) error { + return nil +} diff --git a/pkg/yqlib/write_in_place_handler.go b/pkg/yqlib/write_in_place_handler.go index d3bbb487..d49b2963 100644 --- a/pkg/yqlib/write_in_place_handler.go +++ b/pkg/yqlib/write_in_place_handler.go @@ -34,6 +34,10 @@ func (w *writeInPlaceHandlerImpl) CreateTempFile() (*os.File, error) { if err != nil { return nil, err } + + if err = changeOwner(info, file); err != nil { + return nil, err + } log.Debug("WriteInPlaceHandler: writing to tempfile: %v", file.Name()) w.tempFile = file return file, err