github.com/cobalt77/jfrog-client-go@v0.14.5/utils/io/fileutils/temp.go (about) 1 package fileutils 2 3 import ( 4 "errors" 5 "io/ioutil" 6 "os" 7 "path" 8 "strconv" 9 "strings" 10 "time" 11 12 "github.com/cobalt77/jfrog-client-go/utils/errorutils" 13 "github.com/cobalt77/jfrog-client-go/utils/log" 14 ) 15 16 const ( 17 tempPrefix = "jfrog.cli.temp." 18 ) 19 20 // Max temp file age in hours 21 var maxFileAge = 24.0 22 23 // Path to the root temp dir 24 var tempDirBase string 25 26 func init() { 27 tempDirBase = os.TempDir() 28 } 29 30 // Creates the temp dir at tempDirBase. 31 // Set tempDirPath to the created directory path. 32 func CreateTempDir() (string, error) { 33 if tempDirBase == "" { 34 return "", errorutils.CheckError(errors.New("Temp dir cannot be created in an empty base dir.")) 35 } 36 timestamp := strconv.FormatInt(time.Now().Unix(), 10) 37 path, err := ioutil.TempDir(tempDirBase, tempPrefix+"-"+timestamp+"-") 38 if err != nil { 39 return "", errorutils.CheckError(err) 40 } 41 42 return path, nil 43 } 44 45 // Change the containing directory of temp dir. 46 func SetTempDirBase(dirPath string) { 47 tempDirBase = dirPath 48 } 49 50 func RemoveTempDir(dirPath string) error { 51 exists, err := IsDirExists(dirPath, false) 52 if err != nil { 53 return err 54 } 55 if exists { 56 return os.RemoveAll(dirPath) 57 } 58 return nil 59 } 60 61 // Create a new temp file named "tempPrefix+timeStamp". 62 func CreateTempFile() (*os.File, error) { 63 if tempDirBase == "" { 64 return nil, errorutils.CheckError(errors.New("Temp File cannot be created in an empty base dir.")) 65 } 66 timestamp := strconv.FormatInt(time.Now().Unix(), 10) 67 fd, err := ioutil.TempFile(tempDirBase, tempPrefix+"-"+timestamp+"-") 68 return fd, err 69 } 70 71 // Old runs/tests may leave junk at temp dir. 72 // Each temp file/Dir is named with prefix+timestamp, search for all temp files/dirs that match the common prefix and validate their timestamp. 73 func CleanOldDirs() error { 74 // Get all files at temp dir 75 files, err := ioutil.ReadDir(tempDirBase) 76 if err != nil { 77 log.Error(err) 78 return errorutils.CheckError(err) 79 } 80 now := time.Now() 81 // Search for files/dirs that match the template. 82 for _, file := range files { 83 if strings.HasPrefix(file.Name(), tempPrefix) { 84 timeStamp, err := extractTimestamp(file.Name()) 85 if err != nil { 86 return err 87 } 88 // Delete old file/dirs. 89 if now.Sub(timeStamp).Hours() > maxFileAge { 90 if err := os.RemoveAll(path.Join(tempDirBase, file.Name())); err != nil { 91 return errorutils.CheckError(err) 92 } 93 } 94 } 95 } 96 return nil 97 } 98 99 func extractTimestamp(item string) (time.Time, error) { 100 // Get timestamp from file/dir. 101 endTimestampIdx := strings.LastIndex(item, "-") 102 beginingTimestampIdx := strings.LastIndex(item[:endTimestampIdx], "-") 103 timestampStr := item[beginingTimestampIdx+1 : endTimestampIdx] 104 // Convert to int. 105 timeStampint, err := strconv.ParseInt(timestampStr, 10, 64) 106 if err != nil { 107 return time.Time{}, errorutils.CheckError(err) 108 } 109 // Convert to time type. 110 return time.Unix(timeStampint, 0), nil 111 }