@@ -11,69 +11,97 @@ import (
11
11
"github.com/crytic/cloudexec/pkg/s3"
12
12
)
13
13
14
- func UploadDirectoryToSpaces (config config.Config , bucketName string , sourcePath string , destPath string ) error {
15
- // Create a file to store the compressed archive of the input sourcePath
16
- zipFilePath := filepath .Join (filepath .Dir (sourcePath ), filepath .Base (sourcePath ) + ".zip" )
14
+ func UploadDirectoryToSpaces (config config.Config , bucket string , sourcePath string , destPath string ) error {
15
+ // Compute the path for the zipped archive of sourcePath
16
+ zipFileName := "input.zip"
17
+ zipFilePath , err := filepath .Abs (filepath .Join (filepath .Dir (sourcePath ), zipFileName ))
18
+ if err != nil {
19
+ return err
20
+ }
21
+
22
+ // Create a file where we will write the zipped archive
17
23
zipFile , err := os .Create (zipFilePath )
18
24
if err != nil {
19
25
return err
20
26
}
21
27
defer zipFile .Close ()
22
28
23
- archive := zip .NewWriter (zipFile )
24
- defer archive .Close ()
29
+ // Create a new zip writer
30
+ zipWriter := zip .NewWriter (zipFile )
31
+ defer zipWriter .Close ()
25
32
26
- // Walk the directory and recursively add files to the zip archive
33
+ // Walk the directory and recursively add files to the zipped archive
27
34
err = filepath .Walk (sourcePath , func (path string , info os.FileInfo , err error ) error {
28
35
if err != nil {
29
36
return err
30
37
}
31
- fmt .Printf ("Adding %s to the zipped archive" , path )
32
38
33
- // Create a new ZIP file entry
34
- zipFileEntry , err := archive .Create (path [len (sourcePath ):])
35
- if err != nil {
36
- return err
37
- }
38
-
39
- // If this is a subdirectory, we're done. Move on.
39
+ // If this is a subdirectory, make sure the path ends with a trailing slash before we create it
40
+ // See https://pkg.go.dev/archive/zip#Writer.Create for details
40
41
if info .IsDir () {
42
+ cleanPath := filepath .Clean (path ) + string (filepath .Separator )
43
+ _ , err := zipWriter .Create (cleanPath )
44
+ if err != nil {
45
+ return err
46
+ }
47
+ fmt .Printf ("Created directory %s in the zipped archive\n " , cleanPath )
41
48
return nil
42
49
}
43
50
51
+ // Create a new file entry in the zipped archive
52
+ zipFileEntry , err := zipWriter .Create (path )
53
+ if err != nil {
54
+ return err
55
+ }
56
+
57
+ // Open the file we're adding to the zipped archive
44
58
file , err := os .Open (path )
45
59
if err != nil {
46
60
return err
47
61
}
48
62
defer file .Close ()
49
63
64
+ // Write this file to the zipped archive
50
65
_ , err = io .Copy (zipFileEntry , file )
51
66
if err != nil {
52
67
return err
53
68
}
54
69
70
+ fmt .Printf ("Added %s to the zipped archive\n " , path )
55
71
return nil
56
72
})
57
73
if err != nil {
58
74
return err
59
75
}
60
76
fmt .Printf ("Successfully added all files from %s to zipped archive at %s\n " , sourcePath , zipFilePath )
61
77
78
+ // Make sure all prior writes are sync'd to the filesystem
79
+ // This is necessary because we're going to read the file immediately after writing it
80
+ err = zipWriter .Flush ()
81
+ if err != nil {
82
+ return err
83
+ }
84
+ err = zipFile .Sync ()
85
+ if err != nil {
86
+ return err
87
+ }
88
+
62
89
// Read the zipped archive
63
90
fileBytes , err := os .ReadFile (zipFilePath )
64
91
if err != nil {
65
92
return fmt .Errorf ("Failed to read zipped archive %s: %w" , zipFilePath , err )
66
93
}
94
+ if len (fileBytes ) == 0 {
95
+ return fmt .Errorf ("Failed to read zipped archive at %s: read zero bytes of data" , zipFilePath )
96
+ }
67
97
68
- // Compute the destination key in the bucket
69
- relativePath , _ := filepath .Rel (sourcePath , zipFilePath )
70
- destinationKey := filepath .Join (destPath , "input" , relativePath )
71
-
72
- err = s3 .PutObject (config , bucketName , destinationKey , fileBytes )
98
+ // Upload the zipped archive
99
+ destKey := filepath .Join (destPath , "input.zip" )
100
+ fmt .Printf ("Uploading archive (%v bytes) to %s\n " , len (fileBytes ), destKey )
101
+ err = s3 .PutObject (config , bucket , destKey , fileBytes )
73
102
if err != nil {
74
103
return err
75
104
}
76
105
77
- fmt .Printf ("Successfully uploaded %s to %s/%s\n " , zipFilePath , bucketName , destinationKey )
78
106
return nil
79
107
}
0 commit comments