github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/blob/ssutils/ssutils.go (about) 1 /* 2 Copyright 2023. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package ssutils 18 19 import ( 20 "os" 21 "time" 22 23 "github.com/siglens/siglens/pkg/segment/structs" 24 log "github.com/sirupsen/logrus" 25 ) 26 27 func NewSegSetData(fileName string, size uint64) *structs.SegSetData { 28 return &structs.SegSetData{ 29 AccessTime: time.Now().Unix(), 30 Size: size, 31 SegSetFileName: fileName, 32 } 33 } 34 35 func GetFileNameFromSegSetFile(segSetFile structs.SegSetFile) string { 36 fileName := segSetFile.SegKey 37 switch segSetFile.FileType { 38 case structs.Cmi: 39 if segSetFile.Identifier != "" { 40 fileName = fileName + "_" + segSetFile.Identifier + ".cmi" 41 } 42 case structs.Csg: 43 if segSetFile.Identifier != "" { 44 fileName = fileName + "_" + segSetFile.Identifier + ".csg" 45 } 46 case structs.Bsu: 47 fileName = fileName + ".bsu" 48 case structs.Sid: 49 fileName = fileName + ".sid" 50 case structs.Pqmr: 51 fileName = fileName + "/pqmr/" + segSetFile.Identifier + ".pqmr" 52 case structs.Rollup: 53 fileName = fileName + "/rups/" + segSetFile.Identifier + ".crup" 54 default: 55 log.Errorf("GetFileNameFromSegSetFile: unknown seg set file type! %+v", segSetFile.FileType) 56 } 57 return fileName 58 } 59 60 /* 61 Return size of file and bool if file was found 62 Gets the file size using an os.Stat. If no file is found, returns 0 63 */ 64 func GetFileSizeFromDisk(filePath string) (uint64, bool) { 65 fi, err := os.Stat(filePath) 66 if err != nil { 67 return 0, false 68 } 69 return uint64(fi.Size()), true 70 }