@@ -10,6 +10,8 @@ import (
1010 "math"
1111 "os"
1212 "path/filepath"
13+
14+ "golang.org/x/sys/unix"
1315)
1416
1517// SelfExtractArchiveComment is the comment present in the End Of Central Directory Record.
@@ -74,6 +76,23 @@ func Unzip(dir string, zr *zip.ReadCloser) ([]fs.FileInfo, error) {
7476 if err := os .MkdirAll (dir , 0o755 ); err != nil {
7577 return nil , err
7678 }
79+
80+ dirFile , err := os .Open (dir )
81+ if err != nil {
82+ return nil , err
83+ }
84+ defer dirFile .Close () //nolint:errcheck
85+
86+ if err = flock (dirFile , unix .LOCK_EX ); err != nil {
87+ slog .Warn ("failed to lock dir" , "path" , dir , "error" , err )
88+ } else {
89+ defer func () {
90+ if err = flock (dirFile , unix .LOCK_UN ); err != nil {
91+ slog .Warn ("failed to unlock dir" , "path" , dir , "error" , err )
92+ }
93+ }()
94+ }
95+
7796 res := make ([]fs.FileInfo , len (zr .File ))
7897 for i , f := range zr .File {
7998 if err := unzip1 (dir , f ); err != nil {
@@ -135,3 +154,13 @@ func unzip1(dir string, f *zip.File) error {
135154 }
136155 return nil
137156}
157+
158+ func flock (f * os.File , flags int ) error {
159+ fd := int (f .Fd ())
160+ for {
161+ err := unix .Flock (fd , flags )
162+ if err == nil || err != unix .EINTR {
163+ return err
164+ }
165+ }
166+ }
0 commit comments