os.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. // Copyright 2021 EMQ Technologies Co., Ltd.
  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. package server
  15. import (
  16. "bufio"
  17. "bytes"
  18. "errors"
  19. "os"
  20. "strings"
  21. )
  22. const (
  23. EtcOsRelease string = "/etc/os-release"
  24. UsrLibOsRelease string = "/usr/lib/os-release"
  25. )
  26. // Read and return os-release, trying EtcOsRelease, followed by UsrLibOsRelease.
  27. // err will contain an error message if neither file exists or failed to parse
  28. func Read() (osrelease map[string]string, err error) {
  29. osrelease, err = ReadFile(EtcOsRelease)
  30. if err != nil {
  31. osrelease, err = ReadFile(UsrLibOsRelease)
  32. }
  33. return
  34. }
  35. // Similar to Read(), but takes the name of a file to load instead
  36. func ReadFile(filename string) (osrelease map[string]string, err error) {
  37. osrelease = make(map[string]string)
  38. err = nil
  39. lines, err := parseFile(filename)
  40. if err != nil {
  41. return
  42. }
  43. for _, v := range lines {
  44. key, value, err := parseLine(v)
  45. if err == nil {
  46. osrelease[key] = value
  47. }
  48. }
  49. return
  50. }
  51. // ReadString is similar to Read(), but takes a string to load instead
  52. func ReadString(content string) (osrelease map[string]string, err error) {
  53. osrelease = make(map[string]string)
  54. err = nil
  55. lines, err := parseString(content)
  56. if err != nil {
  57. return
  58. }
  59. for _, v := range lines {
  60. key, value, err := parseLine(v)
  61. if err == nil {
  62. osrelease[key] = value
  63. }
  64. }
  65. return
  66. }
  67. func parseFile(filename string) (lines []string, err error) {
  68. file, err := os.Open(filename)
  69. if err != nil {
  70. return nil, err
  71. }
  72. defer file.Close()
  73. scanner := bufio.NewScanner(file)
  74. for scanner.Scan() {
  75. lines = append(lines, scanner.Text())
  76. }
  77. return lines, scanner.Err()
  78. }
  79. func parseString(content string) (lines []string, err error) {
  80. in := bytes.NewBufferString(content)
  81. reader := bufio.NewReader(in)
  82. scanner := bufio.NewScanner(reader)
  83. for scanner.Scan() {
  84. lines = append(lines, scanner.Text())
  85. }
  86. return lines, scanner.Err()
  87. }
  88. func parseLine(line string) (key string, value string, err error) {
  89. err = nil
  90. // skip empty lines
  91. if len(line) == 0 {
  92. err = errors.New("Skipping: zero-length")
  93. return
  94. }
  95. // skip comments
  96. if line[0] == '#' {
  97. err = errors.New("Skipping: comment")
  98. return
  99. }
  100. // try to split string at the first '='
  101. splitString := strings.SplitN(line, "=", 2)
  102. if len(splitString) != 2 {
  103. err = errors.New("Can not extract key=value")
  104. return
  105. }
  106. // trim white space from key and value
  107. key = splitString[0]
  108. key = strings.Trim(key, " ")
  109. value = splitString[1]
  110. value = strings.Trim(value, " ")
  111. // Handle double quotes
  112. if strings.ContainsAny(value, `"`) {
  113. first := value[0:1]
  114. last := value[len(value)-1:]
  115. if first == last && strings.ContainsAny(first, `"'`) {
  116. value = strings.TrimPrefix(value, `'`)
  117. value = strings.TrimPrefix(value, `"`)
  118. value = strings.TrimSuffix(value, `'`)
  119. value = strings.TrimSuffix(value, `"`)
  120. }
  121. }
  122. // expand anything else that could be escaped
  123. value = strings.Replace(value, `\"`, `"`, -1)
  124. value = strings.Replace(value, `\$`, `$`, -1)
  125. value = strings.Replace(value, `\\`, `\`, -1)
  126. value = strings.Replace(value, "\\`", "`", -1)
  127. return
  128. }