github.com/dubbogo/gost@v1.14.0/path/filepath/path.go (about)

     1  /*
     2   * Licensed to the Apache Software Foundation (ASF) under one or more
     3   * contributor license agreements.  See the NOTICE file distributed with
     4   * this work for additional information regarding copyright ownership.
     5   * The ASF licenses this file to You under the Apache License, Version 2.0
     6   * (the "License"); you may not use this file except in compliance with
     7   * the License.  You may obtain a copy of the License at
     8   *
     9   *     http://www.apache.org/licenses/LICENSE-2.0
    10   *
    11   * Unless required by applicable law or agreed to in writing, software
    12   * distributed under the License is distributed on an "AS IS" BASIS,
    13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14   * See the License for the specific language governing permissions and
    15   * limitations under the License.
    16   */
    17  
    18  package gxfilepath
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  )
    24  
    25  // Exists returns whether the given file or directory exists
    26  func Exists(path string) (bool, error) {
    27  	_, err := os.Lstat(path)
    28  	if err == nil {
    29  		return true, nil
    30  	}
    31  	if os.IsNotExist(err) {
    32  		return false, nil
    33  	}
    34  
    35  	return false, err
    36  }
    37  
    38  // FileExists checks whether a file exists in the given path. It also fails if
    39  // the path points to a directory or there is an error when trying to check the file.
    40  func FileExists(path string) (bool, error) {
    41  	info, err := os.Lstat(path)
    42  	if err != nil {
    43  		return false, err
    44  	}
    45  	if info.IsDir() {
    46  		return false, fmt.Errorf("%q is a directory", path)
    47  	}
    48  
    49  	return true, nil
    50  }
    51  
    52  // DirExists checks whether a directory exists in the given path. It also fails
    53  // if the path is a file rather a directory or there is an error checking whether it exists.
    54  func DirExists(path string) (bool, error) {
    55  	info, err := os.Lstat(path)
    56  	if err != nil {
    57  		return false, err
    58  	}
    59  	if !info.IsDir() {
    60  		return false, fmt.Errorf("%q is a file", path)
    61  	}
    62  
    63  	return true, nil
    64  }