os.go 3.5 KB

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