github.com/zxy12/go_duplicate_112_new@v0.0.0-20200807091221-747231827200/src/os/error_plan9.go (about)

     1  // Copyright 2011 The Go Authors. 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 os
     6  
     7  func isExist(err error) bool {
     8  	return checkErrMessageContent(err, "exists", "is a directory")
     9  }
    10  
    11  func isNotExist(err error) bool {
    12  	return checkErrMessageContent(err, "does not exist", "not found",
    13  		"has been removed", "no parent")
    14  }
    15  
    16  func isPermission(err error) bool {
    17  	return checkErrMessageContent(err, "permission denied")
    18  }
    19  
    20  // checkErrMessageContent checks if err message contains one of msgs.
    21  func checkErrMessageContent(err error, msgs ...string) bool {
    22  	if err == nil {
    23  		return false
    24  	}
    25  	err = underlyingError(err)
    26  	for _, msg := range msgs {
    27  		if contains(err.Error(), msg) {
    28  			return true
    29  		}
    30  	}
    31  	return false
    32  }
    33  
    34  // contains is a local version of strings.Contains. It knows len(sep) > 1.
    35  func contains(s, sep string) bool {
    36  	n := len(sep)
    37  	c := sep[0]
    38  	for i := 0; i+n <= len(s); i++ {
    39  		if s[i] == c && s[i:i+n] == sep {
    40  			return true
    41  		}
    42  	}
    43  	return false
    44  }