...

Source file src/github.com/coreos/go-systemd/v22/unit/escape.go

Documentation: github.com/coreos/go-systemd/v22/unit

     1  // Copyright 2015 CoreOS, 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  // 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  // Implements systemd-escape [--unescape] [--path]
    16  
    17  package unit
    18  
    19  import (
    20  	"fmt"
    21  	"strconv"
    22  	"strings"
    23  )
    24  
    25  const (
    26  	allowed = `:_.abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789`
    27  )
    28  
    29  // If isPath is true:
    30  //
    31  //	We remove redundant '/'s, the leading '/', and trailing '/'.
    32  //	If the result is empty, a '/' is inserted.
    33  //
    34  // We always:
    35  //
    36  //	Replace the following characters with `\x%x`: Leading `.`,
    37  //	 `-`, `\`, and anything not in this set: `:-_.\[0-9a-zA-Z]`
    38  //	Replace '/' with '-'.
    39  func escape(unescaped string, isPath bool) string {
    40  	e := []byte{}
    41  	inSlashes := false
    42  	start := true
    43  	for i := 0; i < len(unescaped); i++ {
    44  		c := unescaped[i]
    45  		if isPath {
    46  			if c == '/' {
    47  				inSlashes = true
    48  				continue
    49  			} else if inSlashes {
    50  				inSlashes = false
    51  				if !start {
    52  					e = append(e, '-')
    53  				}
    54  			}
    55  		}
    56  
    57  		if c == '/' {
    58  			e = append(e, '-')
    59  		} else if start && c == '.' || strings.IndexByte(allowed, c) == -1 {
    60  			e = append(e, []byte(fmt.Sprintf(`\x%x`, c))...)
    61  		} else {
    62  			e = append(e, c)
    63  		}
    64  		start = false
    65  	}
    66  	if isPath && len(e) == 0 {
    67  		e = append(e, '-')
    68  	}
    69  	return string(e)
    70  }
    71  
    72  // If isPath is true:
    73  //
    74  //	We always return a string beginning with '/'.
    75  //
    76  // We always:
    77  //
    78  //	Replace '-' with '/'.
    79  //	Replace `\x%x` with the value represented in hex.
    80  func unescape(escaped string, isPath bool) string {
    81  	u := []byte{}
    82  	for i := 0; i < len(escaped); i++ {
    83  		c := escaped[i]
    84  		if c == '-' {
    85  			c = '/'
    86  		} else if c == '\\' && len(escaped)-i >= 4 && escaped[i+1] == 'x' {
    87  			n, err := strconv.ParseInt(escaped[i+2:i+4], 16, 8)
    88  			if err == nil {
    89  				c = byte(n)
    90  				i += 3
    91  			}
    92  		}
    93  		u = append(u, c)
    94  	}
    95  	if isPath && (len(u) == 0 || u[0] != '/') {
    96  		u = append([]byte("/"), u...)
    97  	}
    98  	return string(u)
    99  }
   100  
   101  // UnitNameEscape escapes a string as `systemd-escape` would
   102  func UnitNameEscape(unescaped string) string {
   103  	return escape(unescaped, false)
   104  }
   105  
   106  // UnitNameUnescape unescapes a string as `systemd-escape --unescape` would
   107  func UnitNameUnescape(escaped string) string {
   108  	return unescape(escaped, false)
   109  }
   110  
   111  // UnitNamePathEscape escapes a string as `systemd-escape --path` would
   112  func UnitNamePathEscape(unescaped string) string {
   113  	return escape(unescaped, true)
   114  }
   115  
   116  // UnitNamePathUnescape unescapes a string as `systemd-escape --path --unescape` would
   117  func UnitNamePathUnescape(escaped string) string {
   118  	return unescape(escaped, true)
   119  }
   120  

View as plain text