github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/file_helper_linux.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package helper 19 20 import ( 21 "os" 22 "strconv" 23 "syscall" 24 ) 25 26 type StateOS struct { 27 Inode uint64 28 Device uint64 29 Size int64 30 ModifyTime uint64 31 } 32 33 // GetOSState returns the FileStateOS for non windows systems 34 func GetOSState(info os.FileInfo) StateOS { 35 stat := info.Sys().(*syscall.Stat_t) 36 37 // Convert inode and dev to uint64 to be cross platform compatible 38 fileState := StateOS{ 39 Inode: stat.Ino, 40 Device: stat.Dev, 41 Size: stat.Size, 42 ModifyTime: uint64(stat.Mtim.Nano()), 43 } 44 return fileState 45 } 46 47 // IsSame file checks if the files are identical 48 func (fs StateOS) IsSame(state StateOS) bool { 49 return fs.Inode == state.Inode && fs.Device == state.Device 50 } 51 52 // IsChange file checks if the files are changed 53 func (fs StateOS) IsChange(state StateOS) bool { 54 if !fs.IsSame(state) { 55 return true 56 } 57 return fs.Size != state.Size || fs.ModifyTime != state.ModifyTime 58 } 59 60 func (fs StateOS) IsEmpty() bool { 61 return fs.Device == 0 && fs.Inode == 0 62 } 63 64 func (fs StateOS) IsFileChange(state StateOS) bool { 65 return fs.Device != state.Device || fs.Inode != state.Inode 66 } 67 68 func (fs StateOS) String() string { 69 var buf [64]byte 70 current := strconv.AppendUint(buf[:0], fs.Inode, 10) 71 current = append(current, '-') 72 current = strconv.AppendUint(current, fs.Device, 10) 73 current = append(current, '-') 74 current = strconv.AppendInt(current, fs.Size, 10) 75 return string(current) 76 } 77 78 // ReadOpen opens a file for reading only 79 func ReadOpen(path string) (*os.File, error) { 80 flag := os.O_RDONLY 81 perm := os.FileMode(0) 82 return os.OpenFile(path, flag, perm) //nolint:gosec 83 }