Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
taylormonacelli committed Jul 25, 2024
1 parent df0b005 commit f3d9d5b
Showing 1 changed file with 16 additions and 29 deletions.
45 changes: 16 additions & 29 deletions core/file.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//go:build !windows
// +build !windows

package core

import (
Expand All @@ -7,41 +10,25 @@ import (
"fmt"
"io"
"os"
"runtime"
"syscall"

"github.com/go-logr/logr"
)

var (
lockFile func(*os.File) error
unlockFile func(*os.File) error
)
func lockFile(f *os.File) error {
err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
if err != nil {
return fmt.Errorf("failed to lock file: %w", err)
}
return nil
}

func init() {
if runtime.GOOS == "windows" {
lockFile = func(f *os.File) error {
return nil
}
unlockFile = func(f *os.File) error {
return nil
}
} else {
lockFile = func(f *os.File) error {
err := syscall.Flock(int(f.Fd()), syscall.LOCK_EX)
if err != nil {
return fmt.Errorf("failed to lock file: %w", err)
}
return nil
}
unlockFile = func(f *os.File) error {
err := syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
if err != nil {
return fmt.Errorf("failed to unlock file: %w", err)
}
return nil
}
func unlockFile(f *os.File) error {
err := syscall.Flock(int(f.Fd()), syscall.LOCK_UN)
if err != nil {
return fmt.Errorf("failed to unlock file: %w", err)
}
return nil
}

func ProcessFile(ctx context.Context, path string, transform func(io.Reader, io.Writer) error) error {
Expand Down Expand Up @@ -88,7 +75,7 @@ func ProcessFile(ctx context.Context, path string, transform func(io.Reader, io.

err = originalFile.Sync()
if err != nil {
return fmt.Errorf("failed to s.V(1)ync file: %w", err)
return fmt.Errorf("failed to sync file: %w", err)
}

logger.V(1).Info("Successfully processed and updated file")
Expand Down

0 comments on commit f3d9d5b

Please sign in to comment.