From ccfe92a58cac6f3d8dd286c357ba8fbdd7a0eab6 Mon Sep 17 00:00:00 2001 From: Achille Roussel Date: Mon, 1 May 2023 11:07:29 -0700 Subject: [PATCH] os: add os.(*File).Sync Signed-off-by: Achille Roussel --- src/os/file.go | 12 ++++++++++++ src/os/file_anyos_test.go | 3 +++ src/os/file_unix.go | 5 +++++ src/os/file_windows.go | 4 ++++ src/os/filesystem.go | 3 +++ src/os/stat_other.go | 5 ----- src/os/stat_unix.go | 5 ----- src/os/stat_windows.go | 5 ----- 8 files changed, 27 insertions(+), 15 deletions(-) diff --git a/src/os/file.go b/src/os/file.go index f26bf3ce..19dd9a96 100644 --- a/src/os/file.go +++ b/src/os/file.go @@ -247,6 +247,18 @@ func (f *File) Fd() uintptr { return ^uintptr(0) } +// Sync commits the current contents of the file to stable storage. +// Typically, this means flushing the file system's in-memory copy of recently +// written data to disk. +func (f *File) Sync() (err error) { + if f.handle == nil { + err = ErrClosed + } else { + err = f.handle.Sync() + } + return +} + // Truncate is a stub, not yet implemented func (f *File) Truncate(size int64) (err error) { if f.handle == nil { diff --git a/src/os/file_anyos_test.go b/src/os/file_anyos_test.go index a1e8425f..36a28814 100644 --- a/src/os/file_anyos_test.go +++ b/src/os/file_anyos_test.go @@ -140,6 +140,9 @@ var closeTests = map[string]func(*File) error{ _, err := f.Seek(0, 0) return err }, + "Sync": func(f *File) error { + return f.Sync() + }, "SyscallConn": func(f *File) error { _, err := f.SyscallConn() return err diff --git a/src/os/file_unix.go b/src/os/file_unix.go index 6cb6f7f0..fd31e530 100644 --- a/src/os/file_unix.go +++ b/src/os/file_unix.go @@ -130,6 +130,11 @@ func (f unixFileHandle) Seek(offset int64, whence int) (int64, error) { return newoffset, handleSyscallError(err) } +func (f unixFileHandle) Sync() error { + err := syscall.Fsync(syscallFd(f)) + return handleSyscallError(err) +} + type unixDirent struct { parent string name string diff --git a/src/os/file_windows.go b/src/os/file_windows.go index 572a82f2..f882ea21 100644 --- a/src/os/file_windows.go +++ b/src/os/file_windows.go @@ -90,6 +90,10 @@ func (f unixFileHandle) Seek(offset int64, whence int) (int64, error) { return newoffset, handleSyscallError(err) } +func (f unixFileHandle) Sync() error { + return ErrNotImplemented +} + // isWindowsNulName reports whether name is os.DevNull ('NUL') on Windows. // True is returned if name is 'NUL' whatever the case. func isWindowsNulName(name string) bool { diff --git a/src/os/filesystem.go b/src/os/filesystem.go index 63780236..c14a1021 100644 --- a/src/os/filesystem.go +++ b/src/os/filesystem.go @@ -52,6 +52,9 @@ type FileHandle interface { // Seek resets the file pointer relative to start, current position, or end Seek(offset int64, whence int) (newoffset int64, err error) + // Sync blocks until buffered writes have been written to persistent storage + Sync() (err error) + // Write writes up to len(b) bytes to the file. Write(b []byte) (n int, err error) diff --git a/src/os/stat_other.go b/src/os/stat_other.go index 57be74be..162313b1 100644 --- a/src/os/stat_other.go +++ b/src/os/stat_other.go @@ -6,11 +6,6 @@ package os -// Sync is a stub, not yet implemented -func (f *File) Sync() error { - return ErrNotImplemented -} - // Stat is a stub, not yet implemented func (f *File) Stat() (FileInfo, error) { return nil, ErrNotImplemented diff --git a/src/os/stat_unix.go b/src/os/stat_unix.go index 74507c77..4667d96a 100644 --- a/src/os/stat_unix.go +++ b/src/os/stat_unix.go @@ -10,11 +10,6 @@ import ( "syscall" ) -// Sync is a stub, not yet implemented -func (f *File) Sync() error { - return ErrNotImplemented -} - // Stat returns the FileInfo structure describing file. // If there is an error, it will be of type *PathError. func (f *File) Stat() (FileInfo, error) { diff --git a/src/os/stat_windows.go b/src/os/stat_windows.go index 8515c7ab..203ed6f4 100644 --- a/src/os/stat_windows.go +++ b/src/os/stat_windows.go @@ -10,11 +10,6 @@ import ( "unsafe" ) -// Sync is a stub, not yet implemented -func (f *File) Sync() error { - return ErrNotImplemented -} - // Stat returns the FileInfo structure describing file. // If there is an error, it will be of type *PathError. func (file *File) Stat() (FileInfo, error) {