Skip to content

Commit a69bacb

Browse files
committed
fix pre-push zip logic
1 parent 60f438f commit a69bacb

File tree

1 file changed

+48
-20
lines changed

1 file changed

+48
-20
lines changed

cmd/cloudexec/push.go

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,69 +11,97 @@ import (
1111
"github.com/crytic/cloudexec/pkg/s3"
1212
)
1313

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
1723
zipFile, err := os.Create(zipFilePath)
1824
if err != nil {
1925
return err
2026
}
2127
defer zipFile.Close()
2228

23-
archive := zip.NewWriter(zipFile)
24-
defer archive.Close()
29+
// Create a new zip writer
30+
zipWriter := zip.NewWriter(zipFile)
31+
defer zipWriter.Close()
2532

26-
// Walk the directory and recursively add files to the zip archive
33+
// Walk the directory and recursively add files to the zipped archive
2734
err = filepath.Walk(sourcePath, func(path string, info os.FileInfo, err error) error {
2835
if err != nil {
2936
return err
3037
}
31-
fmt.Printf("Adding %s to the zipped archive", path)
3238

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
4041
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)
4148
return nil
4249
}
4350

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
4458
file, err := os.Open(path)
4559
if err != nil {
4660
return err
4761
}
4862
defer file.Close()
4963

64+
// Write this file to the zipped archive
5065
_, err = io.Copy(zipFileEntry, file)
5166
if err != nil {
5267
return err
5368
}
5469

70+
fmt.Printf("Added %s to the zipped archive\n", path)
5571
return nil
5672
})
5773
if err != nil {
5874
return err
5975
}
6076
fmt.Printf("Successfully added all files from %s to zipped archive at %s\n", sourcePath, zipFilePath)
6177

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+
6289
// Read the zipped archive
6390
fileBytes, err := os.ReadFile(zipFilePath)
6491
if err != nil {
6592
return fmt.Errorf("Failed to read zipped archive %s: %w", zipFilePath, err)
6693
}
94+
if len(fileBytes) == 0 {
95+
return fmt.Errorf("Failed to read zipped archive at %s: read zero bytes of data", zipFilePath)
96+
}
6797

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)
73102
if err != nil {
74103
return err
75104
}
76105

77-
fmt.Printf("Successfully uploaded %s to %s/%s\n", zipFilePath, bucketName, destinationKey)
78106
return nil
79107
}

0 commit comments

Comments
 (0)