// Copyright (c) 2017 Intel Corporation // Copyright (c) 2018 HyperHQ Inc. // // SPDX-License-Identifier: Apache-2.0 // package katautils import ( "fmt" "io/ioutil" "os" "path/filepath" "syscall" ) // ResolvePath returns the fully resolved and expanded value of the // specified path. func ResolvePath(path string) (string, error) { if path == "" { return "", fmt.Errorf("path must be specified") } absolute, err := filepath.Abs(path) if err != nil { return "", err } resolved, err := filepath.EvalSymlinks(absolute) if err != nil { if os.IsNotExist(err) { // Make the error clearer than the default return "", fmt.Errorf("file %v does not exist", absolute) } return "", err } return resolved, nil } // fileSize returns the number of bytes in the specified file func fileSize(file string) (int64, error) { st := syscall.Stat_t{} err := syscall.Stat(file, &st) if err != nil { return 0, err } return st.Size, nil } // WriteFile write data into specified file func WriteFile(filePath string, data string, fileMode os.FileMode) error { // Normally dir should not be empty, one case is that cgroup subsystem // is not mounted, we will get empty dir, and we want it fail here. if filePath == "" { return fmt.Errorf("no such file for %s", filePath) } if err := ioutil.WriteFile(filePath, []byte(data), fileMode); err != nil { return fmt.Errorf("failed to write %v to %v: %v", data, filePath, err) } return nil } // GetFileContents return the file contents as a string. func GetFileContents(file string) (string, error) { bytes, err := ioutil.ReadFile(file) if err != nil { return "", err } return string(bytes), nil }