github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/externalresource/internal/bucket/file_path_util.go (about)

     1  // Copyright 2022 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package bucket
    15  
    16  import (
    17  	"fmt"
    18  	"strings"
    19  )
    20  
    21  type pathPredFunc func(path string) bool
    22  
    23  func getPathPredAlwaysTrue() pathPredFunc {
    24  	return func(_ string) bool {
    25  		return true
    26  	}
    27  }
    28  
    29  func getPathPredByName(target string, keepPlaceholder bool) pathPredFunc {
    30  	return func(path string) bool {
    31  		resName, fileName, ok := strings.Cut(path, "/")
    32  		if !ok {
    33  			return false
    34  		}
    35  
    36  		if keepPlaceholder && fileName == placeholderFileName {
    37  			return false
    38  		}
    39  
    40  		return resName == target
    41  	}
    42  }
    43  
    44  func getPathPredByPersistedResources(
    45  	resourcePaths map[string]struct{}, prefixCnt int,
    46  ) pathPredFunc {
    47  	return func(path string) bool {
    48  		resPath := ""
    49  		for i := 0; i < prefixCnt; i++ {
    50  			prefix, newPath, ok := strings.Cut(path, "/")
    51  			if !ok {
    52  				return false
    53  			}
    54  
    55  			if resPath == "" {
    56  				resPath = prefix
    57  			} else {
    58  				resPath = fmt.Sprintf("%s/%s", resPath, prefix)
    59  			}
    60  			path = newPath
    61  		}
    62  		_, ok := resourcePaths[resPath]
    63  		return !ok
    64  	}
    65  }