github.com/matrixorigin/matrixone@v1.2.0/pkg/fileservice/path.go (about)

     1  // Copyright 2022 Matrix Origin
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package fileservice
    16  
    17  import (
    18  	"encoding/csv"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  	"unicode"
    23  
    24  	"github.com/matrixorigin/matrixone/pkg/common/moerr"
    25  )
    26  
    27  const ServiceNameSeparator = ":"
    28  
    29  type Path struct {
    30  	Service          string
    31  	ServiceArguments []string
    32  	File             string
    33  }
    34  
    35  func (p Path) String() string {
    36  	return JoinPath(
    37  		p.ServiceString(),
    38  		p.File,
    39  	)
    40  }
    41  
    42  func (p Path) ServiceString() string {
    43  	return strings.Join(append([]string{p.Service}, p.ServiceArguments...), ",")
    44  }
    45  
    46  func ParsePath(s string) (path Path, err error) {
    47  	// split
    48  	i := strings.LastIndex(s, ServiceNameSeparator)
    49  	if i == -1 {
    50  		// no service part
    51  		path.File = s
    52  	} else {
    53  		// with service part
    54  		path.Service, path.ServiceArguments, err = parseService(s[:i])
    55  		if err != nil {
    56  			return
    57  		}
    58  		path.File = s[i+1:]
    59  	}
    60  
    61  	// validate
    62  	for _, r := range path.File {
    63  		// most common patterns first
    64  		if r >= '0' && r <= '9' ||
    65  			r >= 'a' && r <= 'z' ||
    66  			r >= 'A' && r <= 'Z' {
    67  			continue
    68  		}
    69  		switch r {
    70  		case '!', '-', '_', '.', '*', '\'', '(', ')', '@', '/':
    71  			continue
    72  		}
    73  		// printable non-ASCII characters
    74  		if r > unicode.MaxASCII && unicode.IsPrint(r) {
    75  			continue
    76  		}
    77  		err = moerr.NewInvalidPathNoCtx(path.File)
    78  		return
    79  	}
    80  
    81  	path.File = strings.TrimLeft(path.File, "/") // trim leading /
    82  
    83  	return
    84  }
    85  
    86  func parseService(str string) (service string, arguments []string, err error) {
    87  	r := csv.NewReader(strings.NewReader(str))
    88  	records, err := r.ReadAll()
    89  	if err != nil {
    90  		return
    91  	}
    92  	if len(records) != 1 {
    93  		err = moerr.NewInvalidInputNoCtx("bad service: %v", str)
    94  		return
    95  	}
    96  	service = records[0][0]
    97  	arguments = records[0][1:]
    98  	return
    99  }
   100  
   101  func ParsePathAtService(s string, serviceStr string) (path Path, err error) {
   102  	path, err = ParsePath(s)
   103  	if err != nil {
   104  		return
   105  	}
   106  	if serviceStr != "" &&
   107  		path.Service != "" &&
   108  		!strings.EqualFold(path.ServiceString(), serviceStr) {
   109  		err = moerr.NewWrongServiceNoCtx(serviceStr, path.Service)
   110  		return
   111  	}
   112  	return
   113  }
   114  
   115  func JoinPath(serviceName string, path string) string {
   116  	if len(serviceName) == 0 {
   117  		return path
   118  	}
   119  	buf := new(strings.Builder)
   120  	buf.WriteString(serviceName)
   121  	buf.WriteString(ServiceNameSeparator)
   122  	buf.WriteString(path)
   123  	return buf.String()
   124  }
   125  
   126  var osPathSeparatorStr = string([]rune{os.PathSeparator})
   127  
   128  func toOSPath(filePath string) string {
   129  	if os.PathSeparator == '/' {
   130  		return filePath
   131  	}
   132  	return strings.ReplaceAll(filePath, "/", osPathSeparatorStr)
   133  }
   134  
   135  func fromOSPath(diskPath string) string {
   136  	diskPath = filepath.Clean(diskPath)
   137  	if os.PathSeparator == '/' {
   138  		return diskPath
   139  	}
   140  	return strings.ReplaceAll(diskPath, osPathSeparatorStr, "/")
   141  }