path.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright 2021-2023 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 conf
  15. import (
  16. "fmt"
  17. "os"
  18. "path"
  19. "path/filepath"
  20. "strings"
  21. )
  22. const (
  23. etcDir = "etc"
  24. dataDir = "data"
  25. logDir = "log"
  26. pluginsDir = "plugins"
  27. KuiperBaseKey = "KuiperBaseKey"
  28. KuiperSyslogKey = "KuiperSyslogKey"
  29. )
  30. var LoadFileType = "relative"
  31. var AbsoluteMapping = map[string]string{
  32. etcDir: "/etc/kuiper",
  33. dataDir: "/var/lib/kuiper/data",
  34. logDir: "/var/log/kuiper",
  35. pluginsDir: "/var/lib/kuiper/plugins",
  36. }
  37. func GetConfLoc() (string, error) {
  38. return GetLoc(etcDir)
  39. }
  40. func GetDataLoc() (string, error) {
  41. if IsTesting {
  42. dataDir, err := GetLoc(dataDir)
  43. if err != nil {
  44. return "", err
  45. }
  46. d := path.Join(dataDir, "test")
  47. if _, err := os.Stat(d); os.IsNotExist(err) {
  48. err = os.MkdirAll(d, 0755)
  49. if err != nil {
  50. return "", err
  51. }
  52. }
  53. return d, nil
  54. }
  55. return GetLoc(dataDir)
  56. }
  57. func GetPluginsLoc() (string, error) {
  58. return GetLoc(pluginsDir)
  59. }
  60. func absolutePath(loc string) (dir string, err error) {
  61. for relDir, absoluteDir := range AbsoluteMapping {
  62. if strings.HasPrefix(loc, relDir) {
  63. dir = strings.Replace(loc, relDir, absoluteDir, 1)
  64. break
  65. }
  66. }
  67. if 0 == len(dir) {
  68. return "", fmt.Errorf("location %s is not allowed for absolute mode", loc)
  69. }
  70. return dir, nil
  71. }
  72. // GetLoc subdir must be a relative path
  73. func GetLoc(subdir string) (string, error) {
  74. if "relative" == LoadFileType {
  75. return relativePath(subdir)
  76. }
  77. if "absolute" == LoadFileType {
  78. return absolutePath(subdir)
  79. }
  80. return "", fmt.Errorf("Unrecognized loading method.")
  81. }
  82. func relativePath(subdir string) (dir string, err error) {
  83. dir, err = os.Getwd()
  84. if err != nil {
  85. return "", err
  86. }
  87. if base := os.Getenv(KuiperBaseKey); base != "" {
  88. Log.Infof("Specified Kuiper base folder at location %s.\n", base)
  89. dir = base
  90. }
  91. confDir := path.Join(dir, subdir)
  92. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  93. lastdir := dir
  94. for len(dir) > 0 {
  95. dir = filepath.Dir(dir)
  96. if lastdir == dir {
  97. break
  98. }
  99. confDir = path.Join(dir, subdir)
  100. if _, err := os.Stat(confDir); os.IsNotExist(err) {
  101. lastdir = dir
  102. continue
  103. } else {
  104. //Log.Printf("Trying to load file from %s", confDir)
  105. return confDir, nil
  106. }
  107. }
  108. } else {
  109. //Log.Printf("Trying to load file from %s", confDir)
  110. return confDir, nil
  111. }
  112. return "", fmt.Errorf("dir %s not found, please make sure it is created.", confDir)
  113. }
  114. func ProcessPath(p string) (string, error) {
  115. if abs, err := filepath.Abs(p); err != nil {
  116. return "", nil
  117. } else {
  118. if _, err := os.Stat(abs); os.IsNotExist(err) {
  119. return "", err
  120. }
  121. return abs, nil
  122. }
  123. }