github.com/jfrog/jfrog-cli-core/v2@v2.52.0/artifactory/utils/transfersettings.go (about) 1 package utils 2 3 import ( 4 "bytes" 5 "encoding/json" 6 "errors" 7 "os" 8 "path/filepath" 9 10 "github.com/jfrog/jfrog-cli-core/v2/utils/coreutils" 11 "github.com/jfrog/jfrog-cli-core/v2/utils/lock" 12 "github.com/jfrog/jfrog-client-go/utils/errorutils" 13 "github.com/jfrog/jfrog-client-go/utils/io/fileutils" 14 ) 15 16 const ( 17 // DefaultThreads is the default number of threads working while transferring Artifactory's data 18 DefaultThreads = 8 19 // Maximum working threads allowed to execute the AQL queries and upload chunks for build-info repositories 20 MaxBuildInfoThreads = 8 21 // Maximum working threads allowed to execute the AQL queries 22 MaxChunkBuilderThreads = 16 23 24 transferSettingsFile = "transfer.conf" 25 transferSettingsLockFile = "transfer-settings" 26 ) 27 28 type TransferSettings struct { 29 ThreadsNumber int `json:"threadsNumber,omitempty"` 30 } 31 32 func (ts *TransferSettings) CalcNumberOfThreads(buildInfoRepo bool) (chunkBuilderThreads, chunkUploaderThreads int) { 33 chunkBuilderThreads = ts.ThreadsNumber 34 chunkUploaderThreads = ts.ThreadsNumber 35 if buildInfoRepo && MaxBuildInfoThreads < ts.ThreadsNumber { 36 chunkBuilderThreads = MaxBuildInfoThreads 37 chunkUploaderThreads = MaxBuildInfoThreads 38 } 39 if MaxChunkBuilderThreads < chunkBuilderThreads { 40 chunkBuilderThreads = MaxChunkBuilderThreads 41 } 42 return 43 } 44 45 func LoadTransferSettings() (settings *TransferSettings, err error) { 46 filePath, err := getSettingsFilePath() 47 if err != nil { 48 return 49 } 50 51 locksDirPath, err := coreutils.GetJfrogLocksDir() 52 if err != nil { 53 return nil, err 54 } 55 unlockFunc, err := lock.CreateLock(filepath.Join(locksDirPath, transferSettingsLockFile)) 56 // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. 57 defer func() { 58 err = errors.Join(err, unlockFunc()) 59 }() 60 if err != nil { 61 return 62 } 63 64 exists, err := fileutils.IsFileExists(filePath, false) 65 if err != nil || !exists { 66 return 67 } 68 content, err := fileutils.ReadFile(filePath) 69 if err != nil { 70 return 71 } 72 err = errorutils.CheckError(json.Unmarshal(content, &settings)) 73 return 74 } 75 76 func SaveTransferSettings(settings *TransferSettings) (err error) { 77 b, err := json.Marshal(&settings) 78 if err != nil { 79 err = errorutils.CheckError(err) 80 return 81 } 82 var contentBuffer bytes.Buffer 83 err = errorutils.CheckError(json.Indent(&contentBuffer, b, "", " ")) 84 if err != nil { 85 return 86 } 87 bytesContent := contentBuffer.Bytes() 88 filePath, err := getSettingsFilePath() 89 if err != nil { 90 return 91 } 92 93 locksDirPath, err := coreutils.GetJfrogLocksDir() 94 if err != nil { 95 return 96 } 97 unlockFunc, err := lock.CreateLock(filepath.Join(locksDirPath, transferSettingsLockFile)) 98 // Defer the lockFile.Unlock() function before throwing a possible error to avoid deadlock situations. 99 defer func() { 100 err = errors.Join(err, unlockFunc()) 101 }() 102 if err != nil { 103 return 104 } 105 106 err = errorutils.CheckError(os.WriteFile(filePath, bytesContent, 0600)) 107 return 108 } 109 110 func getSettingsFilePath() (string, error) { 111 filePath, err := coreutils.GetJfrogTransferDir() 112 if err != nil { 113 return "", err 114 } 115 err = os.MkdirAll(filePath, 0777) 116 if err != nil { 117 return "", errorutils.CheckError(err) 118 } 119 120 filePath = filepath.Join(filePath, transferSettingsFile) 121 return filePath, nil 122 }