github.com/searKing/golang/go@v1.2.117/path/filepath/path.go (about)

     1  // Copyright 2020 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package filepath
     6  
     7  import (
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  )
    12  
    13  const (
    14  	// PrivateFileMode grants owner to read/write a file.
    15  	PrivateFileMode = 0600
    16  	// PrivateDirMode grants owner to make/remove files inside the directory.
    17  	PrivateDirMode = 0700
    18  )
    19  
    20  // Pathify Expand, Abs and Clean the path
    21  func Pathify(path string) string {
    22  	p := os.ExpandEnv(path)
    23  
    24  	if filepath.IsAbs(p) {
    25  		return filepath.Clean(p)
    26  	}
    27  
    28  	p, err := filepath.Abs(p)
    29  	if err == nil {
    30  		return filepath.Clean(p)
    31  	}
    32  	return ""
    33  }
    34  
    35  // ToDir returns the dir format ends with OS-specific path separator.
    36  func ToDir(path string) string {
    37  	sep := string(filepath.Separator)
    38  	if strings.HasSuffix(path, sep) {
    39  		return path
    40  	}
    41  	return path + sep
    42  }
    43  
    44  // Exist returns a boolean indicating whether the file is known to
    45  // report that a file or directory does exist.
    46  func Exist(name string) bool {
    47  	_, err := os.Stat(name)
    48  	if err == nil {
    49  		return true
    50  	}
    51  	return !os.IsNotExist(err)
    52  }
    53  
    54  // TouchAll creates a file or a directory only if it does not already exist.
    55  func TouchAll(path string, perm os.FileMode) error {
    56  	dir, file := filepath.Split(path)
    57  
    58  	fileInfo, err := os.Stat(path)
    59  	if err == nil {
    60  		// file type mismatched
    61  		if fileInfo.IsDir() && file != "" {
    62  			return os.ErrExist
    63  		}
    64  	} else {
    65  		// other errors
    66  		if !os.IsNotExist(err) {
    67  			return err
    68  		}
    69  		// Not Exist
    70  	}
    71  
    72  	// touch dir
    73  	if dir != "" && !Exist(dir) {
    74  		if err := os.MkdirAll(dir, perm); err != nil {
    75  			return err
    76  		}
    77  	}
    78  
    79  	// return if path is a dir
    80  	if file == "" {
    81  		return nil
    82  	}
    83  	// touch file
    84  	f, err := os.OpenFile(path, os.O_CREATE, perm)
    85  	if err != nil {
    86  		return err
    87  	}
    88  	_ = f.Close()
    89  	return nil
    90  }
    91  
    92  // ResolveReference resolves a path reference to a target base path from a base path.
    93  // If path_ is not under frombasepath, then ResolveReference ignores frombasepath and return a path under tobasepath.
    94  func ResolveReference(path_, frombasepath, tobasepath string) string {
    95  	relPath, err := filepath.Rel(frombasepath, path_)
    96  	if err != nil {
    97  		return filepath.Clean(filepath.Join(tobasepath, path_))
    98  	}
    99  	return filepath.Clean(filepath.Join(tobasepath, relPath))
   100  }