1 //go:build !windows 2 // +build !windows 3 4 /* 5 Copyright The containerd Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 */ 19 20 package continuity 21 22 import ( 23 "fmt" 24 "os" 25 "syscall" 26 ) 27 28 // hardlinkKey provides a tuple-key for managing hardlinks. This is system- 29 // specific. 30 type hardlinkKey struct { 31 dev uint64 32 inode uint64 33 } 34 35 // newHardlinkKey returns a hardlink key for the provided file info. If the 36 // resource does not represent a possible hardlink, errNotAHardLink will be 37 // returned. 38 func newHardlinkKey(fi os.FileInfo) (hardlinkKey, error) { 39 sys, ok := fi.Sys().(*syscall.Stat_t) 40 if !ok { 41 return hardlinkKey{}, fmt.Errorf("cannot resolve (*syscall.Stat_t) from os.FileInfo") 42 } 43 44 if sys.Nlink < 2 { 45 // NOTE(stevvooe): This is not always true for all filesystems. We 46 // should somehow detect this and provided a slow "polyfill" that 47 // leverages os.SameFile if we detect a filesystem where link counts 48 // is not really supported. 49 return hardlinkKey{}, errNotAHardLink 50 } 51 52 //nolint:unconvert 53 return hardlinkKey{dev: uint64(sys.Dev), inode: uint64(sys.Ino)}, nil 54 } 55