github.com/zhyoulun/cilium@v1.6.12/common/utils.go (about)

     1  // Copyright 2016-2019 Authors of Cilium
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package common
    16  
    17  import (
    18  	"bufio"
    19  	"fmt"
    20  	"io"
    21  	"io/ioutil"
    22  	"os"
    23  	"path/filepath"
    24  	"strconv"
    25  	"strings"
    26  )
    27  
    28  // C2GoArray transforms an hexadecimal string representation into a byte slice.
    29  // Example:
    30  // str := "0x12, 0xff, 0x0, 0x1"
    31  // fmt.Print(C2GoArray(str)) //`{0x12, 0xFF, 0x0, 0x01}`"
    32  func C2GoArray(str string) []byte {
    33  	ret := []byte{}
    34  
    35  	if str == "" {
    36  		return ret
    37  	}
    38  
    39  	hexStr := strings.Split(str, ", ")
    40  	for _, hexDigit := range hexStr {
    41  		strDigit := strings.TrimPrefix(hexDigit, "0x")
    42  		digit, err := strconv.ParseInt(strDigit, 16, 9)
    43  		if err != nil {
    44  			return nil
    45  		}
    46  		ret = append(ret, byte(digit))
    47  	}
    48  	return ret
    49  }
    50  
    51  // FindEPConfigCHeader returns the full path of the file that is the CHeaderFileName from
    52  // the slice of files
    53  func FindEPConfigCHeader(basePath string, epFiles []os.FileInfo) string {
    54  	for _, epFile := range epFiles {
    55  		if epFile.Name() == CHeaderFileName {
    56  			return filepath.Join(basePath, epFile.Name())
    57  		}
    58  	}
    59  	return ""
    60  }
    61  
    62  // GetCiliumVersionString returns the first line containing CiliumCHeaderPrefix.
    63  func GetCiliumVersionString(epCHeaderFilePath string) (string, error) {
    64  	f, err := os.Open(epCHeaderFilePath)
    65  	if err != nil {
    66  		return "", err
    67  	}
    68  	br := bufio.NewReader(f)
    69  	defer f.Close()
    70  	for {
    71  		s, err := br.ReadString('\n')
    72  		if err == io.EOF {
    73  			return "", nil
    74  		}
    75  		if err != nil {
    76  			return "", err
    77  		}
    78  		if strings.Contains(s, CiliumCHeaderPrefix) {
    79  			return s, nil
    80  		}
    81  	}
    82  }
    83  
    84  // RequireRootPrivilege checks if the user running cmd is root. If not, it exits the program
    85  func RequireRootPrivilege(cmd string) {
    86  	if os.Getuid() != 0 {
    87  		fmt.Fprintf(os.Stderr, "Please run %q command(s) with root privileges.\n", cmd)
    88  		os.Exit(1)
    89  	}
    90  }
    91  
    92  // MoveNewFilesTo copies all files, that do not exist in newDir, from oldDir.
    93  func MoveNewFilesTo(oldDir, newDir string) error {
    94  	oldFiles, err := ioutil.ReadDir(oldDir)
    95  	if err != nil {
    96  		return err
    97  	}
    98  	newFiles, err := ioutil.ReadDir(newDir)
    99  	if err != nil {
   100  		return err
   101  	}
   102  
   103  	for _, oldFile := range oldFiles {
   104  		exists := false
   105  		for _, newFile := range newFiles {
   106  			if oldFile.Name() == newFile.Name() {
   107  				exists = true
   108  				break
   109  			}
   110  		}
   111  		if !exists {
   112  			os.Rename(filepath.Join(oldDir, oldFile.Name()), filepath.Join(newDir, oldFile.Name()))
   113  		}
   114  	}
   115  	return nil
   116  }
   117  
   118  // MapStringStructToSlice returns a slice with all keys of the given
   119  // map[string]struct{}
   120  func MapStringStructToSlice(m map[string]struct{}) []string {
   121  	s := make([]string, 0, len(m))
   122  	for k := range m {
   123  		s = append(s, k)
   124  	}
   125  	return s
   126  }