...

Source file src/github.com/Microsoft/hcsshim/cmd/runhcs/utils.go

Documentation: github.com/Microsoft/hcsshim/cmd/runhcs

     1  //go:build windows
     2  
     3  package main
     4  
     5  import (
     6  	"fmt"
     7  	"net"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  
    12  	"github.com/Microsoft/hcsshim/internal/appargs"
    13  	"github.com/Microsoft/hcsshim/internal/runhcs"
    14  )
    15  
    16  var argID = appargs.NonEmptyString
    17  
    18  func absPathOrEmpty(path string) (string, error) {
    19  	if path == "" {
    20  		return "", nil
    21  	}
    22  	if strings.HasPrefix(path, runhcs.SafePipePrefix) {
    23  		if len(path) > len(runhcs.SafePipePrefix) {
    24  			return runhcs.SafePipePath(path[len(runhcs.SafePipePrefix):]), nil
    25  		}
    26  	}
    27  	return filepath.Abs(path)
    28  }
    29  
    30  // createPidFile creates a file with the processes pid inside it atomically
    31  // it creates a temp file with the paths filename + '.' infront of it
    32  // then renames the file
    33  func createPidFile(path string, pid int) error {
    34  	var (
    35  		tmpDir  = filepath.Dir(path)
    36  		tmpName = filepath.Join(tmpDir, fmt.Sprintf(".%s", filepath.Base(path)))
    37  	)
    38  	f, err := os.OpenFile(tmpName, os.O_RDWR|os.O_CREATE|os.O_EXCL|os.O_SYNC, 0666)
    39  	if err != nil {
    40  		return err
    41  	}
    42  	_, err = fmt.Fprintf(f, "%d", pid)
    43  	f.Close()
    44  	if err != nil {
    45  		return err
    46  	}
    47  	return os.Rename(tmpName, path)
    48  }
    49  
    50  func closeWritePipe(pipe net.Conn) error {
    51  	return pipe.(interface {
    52  		CloseWrite() error
    53  	}).CloseWrite()
    54  }
    55  

View as plain text