...

Source file src/github.com/containerd/continuity/hardlinks.go

Documentation: github.com/containerd/continuity

     1  /*
     2     Copyright The containerd Authors.
     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 continuity
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  )
    23  
    24  var errNotAHardLink = fmt.Errorf("invalid hardlink")
    25  
    26  type hardlinkManager struct {
    27  	hardlinks map[hardlinkKey][]Resource
    28  }
    29  
    30  func newHardlinkManager() *hardlinkManager {
    31  	return &hardlinkManager{
    32  		hardlinks: map[hardlinkKey][]Resource{},
    33  	}
    34  }
    35  
    36  // Add attempts to add the resource to the hardlink manager. If the resource
    37  // cannot be considered as a hardlink candidate, errNotAHardLink is returned.
    38  func (hlm *hardlinkManager) Add(fi os.FileInfo, resource Resource) error {
    39  	if _, ok := resource.(Hardlinkable); !ok {
    40  		return errNotAHardLink
    41  	}
    42  
    43  	key, err := newHardlinkKey(fi)
    44  	if err != nil {
    45  		return err
    46  	}
    47  
    48  	hlm.hardlinks[key] = append(hlm.hardlinks[key], resource)
    49  
    50  	return nil
    51  }
    52  
    53  // Merge processes the current state of the hardlink manager and merges any
    54  // shared nodes into hard linked resources.
    55  func (hlm *hardlinkManager) Merge() ([]Resource, error) {
    56  	var resources []Resource
    57  	for key, linked := range hlm.hardlinks {
    58  		if len(linked) < 1 {
    59  			return nil, fmt.Errorf("no hardlink entrys for dev, inode pair: %#v", key)
    60  		}
    61  
    62  		merged, err := Merge(linked...)
    63  		if err != nil {
    64  			return nil, fmt.Errorf("error merging hardlink: %w", err)
    65  		}
    66  
    67  		resources = append(resources, merged)
    68  	}
    69  
    70  	return resources, nil
    71  }
    72  

View as plain text