storj.io/minio@v0.0.0-20230509071714-0cbc90f649b1/cmd/metacache-marker.go (about)

     1  /*
     2   * MinIO Cloud Storage, (C) 2020 MinIO, Inc.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  package cmd
    18  
    19  import (
    20  	"context"
    21  	"fmt"
    22  	"strings"
    23  
    24  	"storj.io/minio/cmd/logger"
    25  )
    26  
    27  // markerTagVersion is the marker version.
    28  // Should not need to be updated unless a fundamental change is made to the marker format.
    29  const markerTagVersion = "v1"
    30  
    31  // parseMarker will parse a marker possibly encoded with encodeMarker
    32  func parseMarker(s string) (marker, uuid string) {
    33  	if !strings.Contains(s, "[minio_cache:"+markerTagVersion) {
    34  		return s, ""
    35  	}
    36  	start := strings.LastIndex(s, "[")
    37  	marker = s[:start]
    38  	end := strings.LastIndex(s, "]")
    39  	tag := strings.Trim(s[start:end], "[]")
    40  	tags := strings.Split(tag, ",")
    41  	for _, tag := range tags {
    42  		kv := strings.Split(tag, ":")
    43  		if len(kv) < 2 {
    44  			continue
    45  		}
    46  		switch kv[0] {
    47  		case "minio_cache":
    48  			if kv[1] != markerTagVersion {
    49  				break
    50  			}
    51  		case "id":
    52  			uuid = kv[1]
    53  		default:
    54  			// Ignore unknown
    55  		}
    56  	}
    57  	return
    58  }
    59  
    60  // encodeMarker will encode a uuid and return it as a marker.
    61  // uuid cannot contain '[', ':' or ','.
    62  func encodeMarker(marker, uuid string) string {
    63  	if uuid == "" {
    64  		return marker
    65  	}
    66  	if strings.ContainsAny(uuid, "[:,") {
    67  		logger.LogIf(context.Background(), fmt.Errorf("encodeMarker: uuid %s contained invalid characters", uuid))
    68  	}
    69  	return fmt.Sprintf("%s[minio_cache:%s,id:%s]", marker, markerTagVersion, uuid)
    70  }