Skip to content

Commit 2dff1e2

Browse files
committed
fix error handling and file writer management
1 parent bbd58bd commit 2dff1e2

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

cmd/cloudexec/push.go

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,7 @@ import (
1414
func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath string, destPath string) error {
1515
// Compute the path for the zipped archive of sourcePath
1616
zipFileName := "input.zip"
17-
zipFilePath, err := filepath.Abs(filepath.Join(filepath.Dir(sourcePath), zipFileName))
18-
if err != nil {
19-
return err
20-
}
17+
zipFilePath := filepath.Join(os.TempDir(), zipFileName)
2118

2219
// Create a file where we will write the zipped archive
2320
fmt.Printf("Creating zipped archive at %s\n", zipFilePath)
@@ -40,7 +37,7 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
4037

4138
// If it's a symbolic link, resolve the target
4239
if info.Mode()&os.ModeSymlink == os.ModeSymlink {
43-
target, err := os.Readlink(path)
40+
target, err = os.Readlink(path)
4441
fmt.Printf("Resolved link from %s to %s\n", path, target)
4542
if err != nil {
4643
return err
@@ -57,7 +54,7 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
5754
if targetInfo.IsDir() {
5855
cleanPath := filepath.Clean(path) + string(filepath.Separator)
5956
fmt.Printf("Creating directory %s in the zipped archive\n", cleanPath)
60-
_, err := zipWriter.Create(cleanPath)
57+
_, err = zipWriter.Create(cleanPath)
6158
if err != nil {
6259
return err
6360
}
@@ -82,14 +79,14 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
8279
if err != nil {
8380
return err
8481
}
85-
defer file.Close()
8682

8783
// Write this file to the zipped archive
8884
_, err = io.Copy(zipFileEntry, file)
8985
if err != nil {
9086
return err
9187
}
9288

89+
file.Close()
9390
return nil
9491
})
9592
if err != nil {
@@ -98,16 +95,18 @@ func UploadDirectoryToSpaces(config config.Config, bucket string, sourcePath str
9895
fmt.Printf("Successfully added all files from %s to zipped archive at %s\n", sourcePath, zipFilePath)
9996

10097
// Make sure all prior writes are sync'd to the filesystem
101-
// This is necessary because we're going to read the file immediately after writing it
98+
// This is necessary bc we're going to read the file right after writing it
10299
err = zipWriter.Flush()
103100
if err != nil {
104101
return err
105102
}
106-
zipWriter.Close()
107103
err = zipFile.Sync()
108104
if err != nil {
109105
return err
110106
}
107+
108+
// Manually Closing is necessary to prevent zip file corruption during upload
109+
zipWriter.Close()
111110
zipFile.Close()
112111

113112
// Read the zipped archive

0 commit comments

Comments
 (0)