| 1 | package maincmd |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "os" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "unicode" |
| 9 | ) |
| 10 | |
| 11 | // parseHostspec returns the [USER@]HOST part of the string |
| 12 | // |
| 13 | // rsync/options.c:parse_hostspec |
| 14 | func parseHostspec(src string, parsingURL bool) (host, path string, port int, _ error) { |
| 15 | var userlen int |
| 16 | var hostlen int |
| 17 | var hoststart int |
| 18 | i := 0 |
| 19 | for ; i <= len(src); i++ { |
| 20 | if i == len(src) { |
| 21 | if !parsingURL { |
| 22 | return "", "", 0, fmt.Errorf("ran out of string") |
| 23 | } |
| 24 | if hostlen == 0 { |
| 25 | hostlen = len(src[hoststart:]) |
| 26 | } |
| 27 | break |
| 28 | } |
| 29 | |
| 30 | s := src[i] |
| 31 | if s == ':' || s == '/' { |
| 32 | if hostlen == 0 { |
| 33 | hostlen = len(src[hoststart:i]) |
| 34 | } |
| 35 | i++ |
| 36 | if s == '/' { |
| 37 | if !parsingURL { |
| 38 | return "", "", 0, fmt.Errorf("/, but not parsing URL") |
| 39 | } |
| 40 | } else if s == ':' && parsingURL { |
| 41 | rest := src[i:] |
| 42 | digits := "" |
| 43 | for _, s := range rest { |
| 44 | if !unicode.IsDigit(s) { |
| 45 | break |
| 46 | } |
| 47 | digits += string(s) |
| 48 | } |
| 49 | if digits != "" { |
| 50 | p, err := strconv.ParseInt(digits, 0, 64) |
| 51 | if err != nil { |
| 52 | return "", "", port, err |
| 53 | } |
| 54 | port = int(p) |
| 55 | i += len(digits) |
| 56 | } |
| 57 | if i < len(src) && src[i] != '/' { |
| 58 | return "", "", 0, fmt.Errorf("expected / or end, got %q", src[i:]) |
| 59 | } |
| 60 | if i < len(src) { |
| 61 | i++ |
| 62 | } |
| 63 | } |
| 64 | break |
| 65 | } |
| 66 | if s == '@' { |
| 67 | userlen = i + 1 |
| 68 | hoststart = i + 1 |
| 69 | } else if s == '[' { |
| 70 | if i != hoststart { |
| 71 | return "", "", 0, fmt.Errorf("brackets not at host position") |
| 72 | } |
| 73 | hoststart++ |
| 74 | for i < len(src) && src[i] != ']' && src[i] != '/' { |
| 75 | i++ |
| 76 | } |
| 77 | hostlen = len(src[hoststart : i+1]) |
| 78 | if i == len(src) || |
| 79 | src[i] != ']' || |
| 80 | (i < len(src)-1 && src[i+1] != '/' && src[i+1] != ':') || |
| 81 | hostlen == 0 { |
| 82 | return "", "", 0, fmt.Errorf("WTF") |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | if userlen > 0 { |
| 87 | host = src[:userlen] |
| 88 | hostlen += userlen |
| 89 | } |
| 90 | host += src[hoststart:hostlen] |
| 91 | |
| 92 | // On Windows, a local disk path like C:\rsync parses as |
| 93 | // host="C", path="\\rsync". Detect that and error out. |
| 94 | isDriveLetter := len(host) == 1 && |
| 95 | ((host[0] >= 'A' && host[0] <= 'Z') || |
| 96 | (host[0] >= 'a' && host[0] <= 'z')) |
| 97 | if isDriveLetter && src[i] == os.PathSeparator { |
| 98 | return "", "", 0, fmt.Errorf("local disk path detected") |
| 99 | } |
| 100 | |
| 101 | return host, src[i:], port, nil |
| 102 | } |
| 103 | |
| 104 | // rsync/options.c:check_for_hostspec |
| 105 | func checkForHostspec(src string) (host, path string, port int, _ error) { |
| 106 | if strings.HasPrefix(src, "rsync://") { |
| 107 | var err error |
| 108 | if host, path, port, err = parseHostspec(strings.TrimPrefix(src, "rsync://"), true); err == nil { |
| 109 | if port == 0 { |
| 110 | port = -1 |
| 111 | } |
| 112 | return host, path, port, nil |
| 113 | } |
| 114 | } |
| 115 | var err error |
| 116 | host, path, port, err = parseHostspec(src, false) |
| 117 | if err != nil { |
| 118 | return host, path, port, err |
| 119 | } |
| 120 | if strings.HasPrefix(path, ":") { |
| 121 | if port == 0 { |
| 122 | port = -1 |
| 123 | } |
| 124 | path = strings.TrimPrefix(path, ":") |
| 125 | return host, path, port, nil |
| 126 | } |
| 127 | port = 0 // not a daemon-accessing spec |
| 128 | return host, path, port, nil |
| 129 | } |