* Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
* rubik licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
* http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
* Author: Xiang Li
* Create: 2021-04-17
* Description: filepath related common functions
*
* Original file: https://gitee.com/openeuler/rubik/blob/master/pkg/common/util/file.go
*/
package utils
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"syscall"
"openfuyao.com/colocation-management/pkg/common"
)
const (
fileMaxSize = 10 * 1024 * 1024
)
func CreateFile(path string) (*os.File, error) {
path = filepath.Clean(path)
if err := os.MkdirAll(filepath.Dir(path), common.DefaultDirMode); err != nil {
return nil, err
}
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_TRUNC, common.DefaultFileMode)
if err != nil {
return nil, err
}
return f, nil
}
func IsDir(path string) bool {
fi, err := os.Lstat(path)
if err != nil {
return false
}
return fi.IsDir()
}
func ReadSmallFile(path string) ([]byte, error) {
size, err := FileSize(path)
if err != nil {
return nil, err
}
if size > fileMaxSize {
return nil, fmt.Errorf("file too big")
}
return ReadFile(path)
}
func FileSize(path string) (int64, error) {
if !PathExist(path) {
return 0, fmt.Errorf("%v: No such file or directory", path)
}
st, err := os.Lstat(path)
if err != nil {
return 0, err
}
return st.Size(), nil
}
func PathExist(path string) bool {
if _, err := os.Lstat(path); err != nil {
return false
}
return true
}
func LockFile(path string) (*os.File, error) {
lock, err := CreateFile(path)
if err != nil {
return nil, err
}
if err := syscall.Flock(int(lock.Fd()), syscall.LOCK_EX|syscall.LOCK_NB); err != nil {
return lock, err
}
return lock, nil
}
func UnlockFile(lock *os.File) error {
return syscall.Flock(int(lock.Fd()), syscall.LOCK_UN)
}
func ReadFile(path string) ([]byte, error) {
path = filepath.Clean(path)
if IsDir(path) {
return nil, fmt.Errorf("%v is not a file", path)
}
return ioutil.ReadFile(path)
}
func WriteFile(path, content string) error {
if IsDir(path) {
return fmt.Errorf("%v is not a file", path)
}
dirPath := filepath.Dir(path)
if !PathExist(dirPath) {
if err := os.MkdirAll(dirPath, common.DefaultDirMode); err != nil {
return fmt.Errorf("error creating dir %v: %v", dirPath, err)
}
}
return ioutil.WriteFile(path, []byte(content), common.DefaultFileMode)
}
func AppendFile(path, content string) error {
path = filepath.Clean(path)
if !PathExist(path) {
return fmt.Errorf("%v: No such file or directory", path)
}
if IsDir(path) {
return fmt.Errorf("%v is not a file", path)
}
f, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, common.DefaultFileMode)
if err != nil {
return fmt.Errorf("error opening file: %v", err)
}
defer f.Close()
if _, err := f.WriteString(content); err != nil {
return fmt.Errorf("error writing file: %v", err)
}
return nil
}