github.com/alibaba/ilogtail/pkg@v0.0.0-20250526110833-c53b480d046c/helper/file_helper_darwin.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 "path/filepath" 23 "strconv" 24 "syscall" 25 ) 26 27 type StateOS struct { 28 Inode uint64 29 Device uint64 30 Size int64 31 ModifyTime uint64 32 } 33 34 // GetOSState returns the FileStateOS for non windows systems 35 func GetOSState(info os.FileInfo) StateOS { 36 stat := info.Sys().(*syscall.Stat_t) 37 38 // Convert inode and dev to uint64 to be cross platform compatible 39 fileState := StateOS{ 40 Inode: stat.Ino, 41 Device: uint64(stat.Dev), 42 Size: stat.Size, 43 ModifyTime: uint64(stat.Mtimespec.Nano()), 44 } 45 return fileState 46 } 47 48 // IsSame file checks if the files are identical 49 func (fs StateOS) IsSame(state StateOS) bool { 50 return fs.Inode == state.Inode && fs.Device == state.Device 51 } 52 53 // IsChange file checks if the files are changed 54 func (fs StateOS) IsChange(state StateOS) bool { 55 if !fs.IsSame(state) { 56 return true 57 } 58 return fs.Size != state.Size || fs.ModifyTime != state.ModifyTime 59 } 60 61 func (fs StateOS) IsEmpty() bool { 62 return fs.Device == 0 && fs.Inode == 0 63 } 64 65 func (fs StateOS) IsFileChange(state StateOS) bool { 66 return fs.Device != state.Device || fs.Inode != state.Inode 67 } 68 69 func (fs StateOS) String() string { 70 var buf [64]byte 71 current := strconv.AppendUint(buf[:0], fs.Inode, 10) 72 current = append(current, '-') 73 current = strconv.AppendUint(current, fs.Device, 10) 74 return string(current) 75 } 76 77 // ReadOpen opens a file for reading only 78 func ReadOpen(path string) (*os.File, error) { 79 return os.Open(filepath.Clean(path)) 80 }