Skip to content

Commit 5e21fec

Browse files
committed
* clean up code
1 parent 76114a7 commit 5e21fec

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

decompression.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ package embeddedpostgres
22

33
import (
44
"archive/tar"
5-
"errors"
65
"fmt"
76
"io"
87
"os"
98
"path/filepath"
10-
"syscall"
119

1210
"github.com/xi2/xz"
1311
)
@@ -58,16 +56,11 @@ func decompressTarXz(tarReader func(*xz.Reader) (func() (*tar.Header, error), fu
5856
}
5957

6058
targetPath := filepath.Join(tempExtractPath, header.Name)
61-
finalPath := filepath.Join(extractPath, header.Name)
6259

6360
if err := os.MkdirAll(filepath.Dir(targetPath), os.ModePerm); err != nil {
6461
return errorExtractingPostgres(err)
6562
}
6663

67-
if err := os.MkdirAll(filepath.Dir(finalPath), os.ModePerm); err != nil {
68-
return errorExtractingPostgres(err)
69-
}
70-
7164
switch header.Typeflag {
7265
case tar.TypeReg:
7366
outFile, err := os.OpenFile(targetPath, os.O_CREATE|os.O_RDWR, os.FileMode(header.Mode))
@@ -90,25 +83,11 @@ func decompressTarXz(tarReader func(*xz.Reader) (func() (*tar.Header, error), fu
9083
if err := os.Symlink(header.Linkname, targetPath); err != nil {
9184
return errorExtractingPostgres(err)
9285
}
93-
94-
case tar.TypeDir:
95-
if err := os.MkdirAll(finalPath, os.FileMode(header.Mode)); err != nil {
96-
return errorExtractingPostgres(err)
97-
}
98-
continue
99-
}
100-
101-
if err := os.Rename(targetPath, finalPath); err != nil {
102-
// if the error is due to syscall.EEXIST then this is most likely windows, and a race condition with
103-
// multiple downloads of the file. We assume that the existing file is the correct one and ignore the
104-
// error
105-
if errors.Is(err, syscall.EEXIST) {
106-
return nil
107-
}
108-
109-
return errorExtractingPostgres(err)
11086
}
87+
}
11188

89+
if err := renameOrIgnore(tempExtractPath, extractPath); err != nil {
90+
return errorExtractingPostgres(err)
11291
}
11392

11493
return nil

remote_fetch.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import (
1313
"os"
1414
"path/filepath"
1515
"strings"
16-
"syscall"
1716
)
1817

1918
// RemoteFetchStrategy provides a strategy to fetch a Postgres binary so that it is available for use.
@@ -93,7 +92,8 @@ func decompressResponse(bodyBytes []byte, contentLength int64, cacheLocator Cach
9392
return errorExtractingPostgres(err)
9493
}
9594

96-
// to prevent file corruption when multiple processes attempt to extract at the same time we download
95+
// if multiple processes attempt to extract
96+
// to prevent file corruption when multiple processes attempt to extract at the same time
9797
// first to a cache location, and then move the file into place.
9898
tmp, err := os.CreateTemp("", "embedded_postgres")
9999
if err != nil {
@@ -114,14 +114,7 @@ func decompressResponse(bodyBytes []byte, contentLength int64, cacheLocator Cach
114114
return errorExtractingPostgres(err)
115115
}
116116

117-
if err := os.Rename(tmp.Name(), cacheLocation); err != nil {
118-
// if the error is due to syscall.EEXIST then this is most likely windows, and a race condition with
119-
// multiple downloads of the file. We assume that the existing file is the correct one and ignore the
120-
// error
121-
if errors.Is(err, syscall.EEXIST) {
122-
return nil
123-
}
124-
117+
if err := renameOrIgnore(tmp.Name(), cacheLocation); err != nil {
125118
return errorExtractingPostgres(err)
126119
}
127120

rename.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package embeddedpostgres
2+
3+
import (
4+
"errors"
5+
"os"
6+
"syscall"
7+
)
8+
9+
// renameOrIgnore will rename the oldpath to the newpath.
10+
//
11+
// On Unix this will be a safe atomic operation.
12+
// On Windows this will do nothing if the new path already exists.
13+
//
14+
// This is only safe to use if you can be sure that the newpath is either missing, or contains the same data as the
15+
// old path.
16+
func renameOrIgnore(oldpath, newpath string) error {
17+
err := os.Rename(oldpath, newpath)
18+
19+
// if the error is due to syscall.EEXIST then this is most likely windows, and a race condition with
20+
// multiple downloads of the file. We can assume that the existing file is the correct one and ignore
21+
// the error
22+
if errors.Is(err, syscall.EEXIST) {
23+
return nil
24+
}
25+
26+
return err
27+
}

0 commit comments

Comments
 (0)