github.com/PandaGoAdmin/utils@v0.0.0-20211208134815-d5461603a00f/file_unix.go (about)

     1  // +build linux darwin
     2  
     3  package kgo
     4  
     5  import (
     6  	"golang.org/x/sys/unix"
     7  	"path/filepath"
     8  	"strings"
     9  )
    10  
    11  // IsReadable 路径是否可读.
    12  func (kf *LkkFile) IsReadable(fpath string) bool {
    13  	err := unix.Access(fpath, unix.R_OK)
    14  	if err != nil {
    15  		return false
    16  	}
    17  	return true
    18  }
    19  
    20  // IsWritable 路径是否可写.
    21  func (kf *LkkFile) IsWritable(fpath string) bool {
    22  	err := unix.Access(fpath, unix.W_OK)
    23  	if err != nil {
    24  		return false
    25  	}
    26  	return true
    27  }
    28  
    29  // IsExecutable 是否可执行文件.
    30  func (kf *LkkFile) IsExecutable(fpath string) bool {
    31  	err := unix.Access(fpath, unix.X_OK)
    32  	if err != nil {
    33  		return false
    34  	}
    35  	return true
    36  }
    37  
    38  // FormatPath 格式化路径.
    39  func (kf *LkkFile) FormatPath(fpath string) string {
    40  	if fpath == "" {
    41  		return ""
    42  	}
    43  
    44  	fpath = formatPath(fpath)
    45  	dir := filepath.Dir(fpath)
    46  
    47  	if dir == `.` {
    48  		return fpath
    49  	}
    50  
    51  	return strings.TrimRight(dir, "/") + "/" + filepath.Base(fpath)
    52  }