...

Source file src/github.com/containerd/continuity/driver/driver_unix.go

Documentation: github.com/containerd/continuity/driver

     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 driver
    21  
    22  import (
    23  	"errors"
    24  	"fmt"
    25  	"os"
    26  	"sort"
    27  
    28  	"github.com/containerd/continuity/devices"
    29  	"github.com/containerd/continuity/sysx"
    30  )
    31  
    32  func (d *driver) Mknod(path string, mode os.FileMode, major, minor int) error {
    33  	err := devices.Mknod(path, mode, major, minor)
    34  	if err != nil {
    35  		err = &os.PathError{Op: "mknod", Path: path, Err: err}
    36  	}
    37  	return err
    38  }
    39  
    40  func (d *driver) Mkfifo(path string, mode os.FileMode) error {
    41  	if mode&os.ModeNamedPipe == 0 {
    42  		return errors.New("mode passed to Mkfifo does not have the named pipe bit set")
    43  	}
    44  	// mknod with a mode that has ModeNamedPipe set creates a fifo, not a
    45  	// device.
    46  	err := devices.Mknod(path, mode, 0, 0)
    47  	if err != nil {
    48  		err = &os.PathError{Op: "mkfifo", Path: path, Err: err}
    49  	}
    50  	return err
    51  }
    52  
    53  // Getxattr returns all of the extended attributes for the file at path p.
    54  func (d *driver) Getxattr(p string) (map[string][]byte, error) {
    55  	xattrs, err := sysx.Listxattr(p)
    56  	if err != nil {
    57  		return nil, fmt.Errorf("listing %s xattrs: %w", p, err)
    58  	}
    59  
    60  	sort.Strings(xattrs)
    61  	m := make(map[string][]byte, len(xattrs))
    62  
    63  	for _, attr := range xattrs {
    64  		value, err := sysx.Getxattr(p, attr)
    65  		if err != nil {
    66  			return nil, fmt.Errorf("getting %q xattr on %s: %w", attr, p, err)
    67  		}
    68  
    69  		// NOTE(stevvooe): This append/copy tricky relies on unique
    70  		// xattrs. Break this out into an alloc/copy if xattrs are no
    71  		// longer unique.
    72  		m[attr] = append(m[attr], value...)
    73  	}
    74  
    75  	return m, nil
    76  }
    77  
    78  // Setxattr sets all of the extended attributes on file at path, following
    79  // any symbolic links, if necessary. All attributes on the target are
    80  // replaced by the values from attr. If the operation fails to set any
    81  // attribute, those already applied will not be rolled back.
    82  func (d *driver) Setxattr(path string, attrMap map[string][]byte) error {
    83  	for attr, value := range attrMap {
    84  		if err := sysx.Setxattr(path, attr, value, 0); err != nil {
    85  			return fmt.Errorf("error setting xattr %q on %s: %w", attr, path, err)
    86  		}
    87  	}
    88  
    89  	return nil
    90  }
    91  
    92  // LGetxattr returns all of the extended attributes for the file at path p
    93  // not following symbolic links.
    94  func (d *driver) LGetxattr(p string) (map[string][]byte, error) {
    95  	xattrs, err := sysx.LListxattr(p)
    96  	if err != nil {
    97  		return nil, fmt.Errorf("listing %s xattrs: %w", p, err)
    98  	}
    99  
   100  	sort.Strings(xattrs)
   101  	m := make(map[string][]byte, len(xattrs))
   102  
   103  	for _, attr := range xattrs {
   104  		value, err := sysx.LGetxattr(p, attr)
   105  		if err != nil {
   106  			return nil, fmt.Errorf("getting %q xattr on %s: %w", attr, p, err)
   107  		}
   108  
   109  		// NOTE(stevvooe): This append/copy tricky relies on unique
   110  		// xattrs. Break this out into an alloc/copy if xattrs are no
   111  		// longer unique.
   112  		m[attr] = append(m[attr], value...)
   113  	}
   114  
   115  	return m, nil
   116  }
   117  
   118  // LSetxattr sets all of the extended attributes on file at path, not
   119  // following any symbolic links. All attributes on the target are
   120  // replaced by the values from attr. If the operation fails to set any
   121  // attribute, those already applied will not be rolled back.
   122  func (d *driver) LSetxattr(path string, attrMap map[string][]byte) error {
   123  	for attr, value := range attrMap {
   124  		if err := sysx.LSetxattr(path, attr, value, 0); err != nil {
   125  			return fmt.Errorf("error setting xattr %q on %s: %w", attr, path, err)
   126  		}
   127  	}
   128  
   129  	return nil
   130  }
   131  
   132  func (d *driver) DeviceInfo(fi os.FileInfo) (maj uint64, min uint64, err error) {
   133  	return devices.DeviceInfo(fi)
   134  }
   135  

View as plain text