Skip to content

Commit ddec1b9

Browse files
Env var to control downloading to temp path (#1667)
* Env var to control downloading to temp path * Implement TempPath method in mockedLCM
1 parent 4360757 commit ddec1b9

File tree

4 files changed

+40
-12
lines changed

4 files changed

+40
-12
lines changed

cmd/zt_interceptors_for_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ type mockedLifecycleManager struct {
8282
outputFormat common.OutputFormat
8383
}
8484

85+
func (m *mockedLifecycleManager) DownloadToTempPath() bool {
86+
return false
87+
}
88+
8589
func (m *mockedLifecycleManager) Progress(o common.OutputBuilder) {
8690
select {
8791
case m.progressLog <- o(common.EOutputFormat.Text()):

common/environment.go

+8
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,11 @@ func (EnvironmentVariable) MimeMapping() EnvironmentVariable {
361361
Description: "Location of the file to override default OS mime mapping",
362362
}
363363
}
364+
365+
func (EnvironmentVariable) DownloadToTempPath() EnvironmentVariable {
366+
return EnvironmentVariable {
367+
Name: "AZCOPY_DOWNLOAD_TO_TEMP_PATH",
368+
DefaultValue: "true",
369+
Description: "Configures azcopy to download to a temp path before actual download. Allowed values are true/false",
370+
}
371+
}

common/lifecyleMgr.go

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type LifecycleMgr interface {
6868
RegisterCloseFunc(func())
6969
SetForceLogging()
7070
IsForceLoggingDisabled() bool
71+
DownloadToTempPath() bool
7172
}
7273

7374
func GetLifecycleMgr() LifecycleMgr {
@@ -590,6 +591,15 @@ func (lcm *lifecycleMgr) IsForceLoggingDisabled() bool {
590591
return lcm.disableSyslog
591592
}
592593

594+
func (lcm *lifecycleMgr) DownloadToTempPath() bool {
595+
ret, err := strconv.ParseBool(lcm.GetEnvironmentVariable(EEnvironmentVariable.DownloadToTempPath()))
596+
if err != nil {
597+
// By default we'll download to temp path
598+
ret = true
599+
}
600+
return ret
601+
}
602+
593603
// captures the common logic of exiting if there's an expected error
594604
func PanicIfErr(err error) {
595605
if err != nil {

ste/xfer-remoteToLocal-file.go

+18-12
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func remoteToLocal_file(jptm IJobPartTransferMgr, p pipeline.Pipeline, pacer pac
171171
// to correct name.
172172
pseudoId := common.NewPseudoChunkIDForWholeFile(info.Source)
173173
jptm.LogChunkStatus(pseudoId, common.EWaitReason.CreateLocalFile())
174-
dstFile, err = createDestinationFile(jptm, info.getTempDownloadPath(), fileSize, writeThrough)
174+
dstFile, err = createDestinationFile(jptm, info.getDownloadPath(), fileSize, writeThrough)
175175
jptm.LogChunkStatus(pseudoId, common.EWaitReason.ChunkDone()) // normal setting to done doesn't apply to these pseudo ids
176176
if err != nil {
177177
failFileCreation(err)
@@ -342,7 +342,7 @@ func epilogueWithCleanupDownload(jptm IJobPartTransferMgr, dl downloader, active
342342

343343
// check length if enabled (except for dev null and decompression case, where that's impossible)
344344
if info.DestLengthValidation && info.Destination != common.Dev_Null && !jptm.ShouldDecompress() {
345-
fi, err := common.OSStat(info.getTempDownloadPath())
345+
fi, err := common.OSStat(info.getDownloadPath())
346346

347347
if err != nil {
348348
jptm.FailActiveDownload("Download length check", err)
@@ -351,14 +351,16 @@ func epilogueWithCleanupDownload(jptm IJobPartTransferMgr, dl downloader, active
351351
}
352352
}
353353

354-
//Rename back to original name. At this point, we're sure the file is completely
354+
//check if we need to rename back to original name. At this point, we're sure the file is completely
355355
//downloaded and not corrupt. Infact, post this point we should only log errors and
356356
//not fail the transfer.
357-
if err == nil && !strings.EqualFold(info.Destination, common.Dev_Null) {
358-
renameErr := os.Rename(info.getTempDownloadPath(), info.Destination)
357+
renameNecessary := !strings.EqualFold(info.getDownloadPath(), info.Destination) &&
358+
!strings.EqualFold(info.Destination, common.Dev_Null)
359+
if err == nil && renameNecessary {
360+
renameErr := os.Rename(info.getDownloadPath(), info.Destination)
359361
if renameErr != nil {
360362
jptm.LogError(info.Destination, fmt.Sprintf(
361-
"Failed to rename. File at %s", info.getTempDownloadPath()), renameErr)
363+
"Failed to rename. File at %s", info.getDownloadPath()), renameErr)
362364
}
363365
}
364366
}
@@ -461,19 +463,23 @@ func tryDeleteFile(info TransferInfo, jptm IJobPartTransferMgr) {
461463
return
462464
}
463465

464-
err := deleteFile(info.getTempDownloadPath())
466+
err := deleteFile(info.getDownloadPath())
465467
if err != nil {
466468
// If there was an error deleting the file, log the error
467469
jptm.LogError(info.Destination, "Delete File Error ", err)
468470
}
469471
}
470472

471-
//Returns the path of temp file to be downloaded. The paths is in format
473+
// Returns the path of file to be downloaded. If we want to
474+
// download to a temp path we return a temp paht in format
472475
// /actual/parent/path/.azDownload-<jobID>-<actualFileName>
473-
func (info *TransferInfo) getTempDownloadPath() string {
474-
parent, fileName := filepath.Split(info.Destination)
475-
fileName = fmt.Sprintf(azcopyTempDownloadPrefix, info.JobID.String()) + fileName
476-
return filepath.Join(parent, fileName)
476+
func (info *TransferInfo) getDownloadPath() string {
477+
if common.GetLifecycleMgr().DownloadToTempPath() {
478+
parent, fileName := filepath.Split(info.Destination)
479+
fileName = fmt.Sprintf(azcopyTempDownloadPrefix, info.JobID.String()) + fileName
480+
return filepath.Join(parent, fileName)
481+
}
482+
return info.Destination
477483
}
478484

479485
// conforms to io.Writer and io.Closer

0 commit comments

Comments
 (0)