|  | 
|  | 1 | +//go:build !windows | 
|  | 2 | +// +build !windows | 
|  | 3 | + | 
|  | 4 | +/* | 
|  | 5 | +   Copyright © 2021 The CDI 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 cdi | 
|  | 21 | + | 
|  | 22 | +import ( | 
|  | 23 | +	runc "github.com/opencontainers/runc/libcontainer/devices" | 
|  | 24 | +	"github.com/pkg/errors" | 
|  | 25 | +) | 
|  | 26 | + | 
|  | 27 | +// fillMissingInfo fills in missing mandatory attributes from the host device. | 
|  | 28 | +func (d *DeviceNode) fillMissingInfo() error { | 
|  | 29 | +	if d.HostPath == "" { | 
|  | 30 | +		d.HostPath = d.Path | 
|  | 31 | +	} | 
|  | 32 | + | 
|  | 33 | +	if d.Type != "" && (d.Major != 0 || d.Type == "p") { | 
|  | 34 | +		return nil | 
|  | 35 | +	} | 
|  | 36 | + | 
|  | 37 | +	hostDev, err := runc.DeviceFromPath(d.HostPath, "rwm") | 
|  | 38 | +	if err != nil { | 
|  | 39 | +		return errors.Wrapf(err, "failed to stat CDI host device %q", d.HostPath) | 
|  | 40 | +	} | 
|  | 41 | + | 
|  | 42 | +	if d.Type == "" { | 
|  | 43 | +		d.Type = string(hostDev.Type) | 
|  | 44 | +	} else { | 
|  | 45 | +		if d.Type != string(hostDev.Type) { | 
|  | 46 | +			return errors.Errorf("CDI device (%q, %q), host type mismatch (%s, %s)", | 
|  | 47 | +				d.Path, d.HostPath, d.Type, string(hostDev.Type)) | 
|  | 48 | +		} | 
|  | 49 | +	} | 
|  | 50 | +	if d.Major == 0 && d.Type != "p" { | 
|  | 51 | +		d.Major = hostDev.Major | 
|  | 52 | +		d.Minor = hostDev.Minor | 
|  | 53 | +	} | 
|  | 54 | + | 
|  | 55 | +	return nil | 
|  | 56 | +} | 
0 commit comments