9
9
"net/http/httputil"
10
10
"os"
11
11
"path/filepath"
12
- "runtime"
13
12
14
- "github.com/gofrs/flock"
15
13
"github.com/taskcluster/httpbackoff/v3"
16
14
"github.com/taskcluster/taskcluster/v83/clients/client-go/tcqueue"
17
15
"github.com/taskcluster/taskcluster/v83/internal/mocktc/tc"
@@ -31,40 +29,11 @@ type S3Artifact struct {
31
29
ContentType string
32
30
}
33
31
34
- // createTempFileForPUTBody first tries to put a file lock on
35
- // the artifact to prevent further writes to it (posix only).
36
- // The file path of the locked file is returned. If this is unsuccessful,
37
- // it will fall back to copying to a temporary file.
38
- // This method will also gzip-compress the file at Path and writes
39
- // it to a temporary file in the same directory. The file path of the
40
- // generated temporary file is returned.
41
- // A callback function is also returned that should be used to
42
- // cleanup the resources (e.g. unlock the file lock or delete the
43
- // temp file). It is the responsibility of the caller to
44
- // use this cleanup function.
45
- func (s3Artifact * S3Artifact ) createTempFileForPUTBody () (string , func ()) {
46
- var fileLock * flock.Flock
47
- removeFileLock := func () {
48
- log .Printf ("Removing exclusive file lock on %v..." , s3Artifact .ContentPath )
49
- _ = fileLock .Unlock ()
50
- }
51
-
52
- if runtime .GOOS != "windows" {
53
- fileLock = flock .New (s3Artifact .ContentPath )
54
- log .Printf ("Attempting to get exclusive file lock on %v..." , s3Artifact .ContentPath )
55
- locked , _ := fileLock .TryLock ()
56
- if locked {
57
- log .Printf ("Got exclusive file lock on %v" , s3Artifact .ContentPath )
58
- if s3Artifact .ContentEncoding != "gzip" {
59
- log .Printf ("No need to copy %v to temp file" , s3Artifact .ContentPath )
60
- return s3Artifact .ContentPath , removeFileLock
61
- }
62
- log .Printf ("Need to gzip-compress %v..." , s3Artifact .ContentPath )
63
- } else {
64
- log .Printf ("Failed to get exclusive file lock on %v" , s3Artifact .ContentPath )
65
- }
66
- }
67
-
32
+ // createTempFileForPUTBody gzip-compresses the file at Path and
33
+ // writes it to a temporary file in the same directory. The file path of the
34
+ // generated temporary file is returned. It is the responsibility of the
35
+ // caller to delete the temporary file.
36
+ func (s3Artifact * S3Artifact ) createTempFileForPUTBody () string {
68
37
baseName := filepath .Base (s3Artifact .Path )
69
38
tmpFile , err := os .CreateTemp ("" , baseName )
70
39
if err != nil {
@@ -84,28 +53,30 @@ func (s3Artifact *S3Artifact) createTempFileForPUTBody() (string, func()) {
84
53
}
85
54
defer source .Close ()
86
55
_ , _ = io .Copy (target , source )
87
- return tmpFile .Name (), func () {
88
- if runtime .GOOS != "windows" {
89
- removeFileLock ()
90
- }
91
- os .Remove (tmpFile .Name ())
92
- }
56
+ return tmpFile .Name ()
93
57
}
94
58
95
59
func (s3Artifact * S3Artifact ) ProcessResponse (resp any , logger Logger , serviceFactory tc.ServiceFactory , config * gwconfig.Config ) (err error ) {
96
60
response := resp .(* tcqueue.S3ArtifactResponse )
97
61
98
62
logger .Infof ("Uploading artifact %v from file %v with content encoding %q, mime type %q and expiry %v" , s3Artifact .Name , s3Artifact .Path , s3Artifact .ContentEncoding , s3Artifact .ContentType , s3Artifact .Expires )
99
63
100
- transferContentFile , cleanup := s3Artifact .createTempFileForPUTBody ()
101
- defer cleanup ()
64
+ // task user copies artifact at Path to ContentPath for task artifacts
65
+ // and this should be cleaned up after artifact upload
66
+ tempFileCreated := s3Artifact .Path != s3Artifact .ContentPath
67
+ if tempFileCreated {
68
+ defer os .Remove (s3Artifact .ContentPath )
69
+ }
102
70
103
- defer func () {
104
- if s3Artifact .Path != s3Artifact .ContentPath {
105
- // If we created a temporary file, delete it.
106
- os .Remove (s3Artifact .ContentPath )
107
- }
108
- }()
71
+ var transferContentFile string
72
+ if tempFileCreated && s3Artifact .ContentEncoding != "gzip" {
73
+ log .Printf ("Not copying %v to temp file" , s3Artifact .ContentPath )
74
+ transferContentFile = s3Artifact .ContentPath
75
+ } else {
76
+ log .Printf ("Copying %v to temp file..." , s3Artifact .ContentPath )
77
+ transferContentFile = s3Artifact .createTempFileForPUTBody ()
78
+ defer os .Remove (transferContentFile )
79
+ }
109
80
110
81
// perform http PUT to upload to S3...
111
82
httpClient := & http.Client {}
0 commit comments