You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.go 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. Copyright 2021 The KubeEdge Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package util
  14. import (
  15. "fmt"
  16. "io"
  17. "math/rand"
  18. "os"
  19. "path"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "time"
  24. "k8s.io/klog/v2"
  25. )
  26. // IsExists checks whether the file or folder exists
  27. func IsExists(path string) bool {
  28. _, err := os.Stat(path)
  29. if err != nil {
  30. return os.IsExist(err)
  31. }
  32. return true
  33. }
  34. // IsFile checks whether the specified path is a file
  35. func IsFile(path string) bool {
  36. return !IsDir(path)
  37. }
  38. // IsDir checks whether the given path is a folder
  39. func IsDir(path string) bool {
  40. s, err := os.Stat(path)
  41. if err != nil {
  42. return false
  43. }
  44. return s.IsDir()
  45. }
  46. // CopyFile copies a file to other
  47. func CopyFile(srcName, dstName string) (written int64, err error) {
  48. src, err := os.Open(srcName)
  49. if err != nil {
  50. klog.Errorf("open file %s failed: %v", srcName, err)
  51. return -1, err
  52. }
  53. defer func() {
  54. _ = src.Close()
  55. }()
  56. dst, err := os.OpenFile(dstName, os.O_WRONLY|os.O_CREATE, 0644)
  57. if err != nil {
  58. return -1, err
  59. }
  60. defer func() {
  61. _ = dst.Close()
  62. }()
  63. return io.Copy(dst, src)
  64. }
  65. // CreateFolder creates a fold
  66. func CreateFolder(path string) error {
  67. s, err := os.Stat(path)
  68. if os.IsNotExist(err) {
  69. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  70. return err
  71. }
  72. return nil
  73. }
  74. if !s.IsDir() {
  75. if err := os.MkdirAll(path, os.ModePerm); err != nil {
  76. return err
  77. }
  78. }
  79. return nil
  80. }
  81. // TrimPrefixPath gets path without the provided leading prefix path. If path doesn't start with prefix, path is returned.
  82. func TrimPrefixPath(prefix string, path string) string {
  83. return strings.TrimPrefix(path, prefix)
  84. }
  85. // AddPrefixPath gets path that adds the provided the prefix.
  86. func AddPrefixPath(prefix string, path string) string {
  87. return filepath.Join(prefix, path)
  88. }
  89. // GetUniqueIdentifier get unique identifier
  90. func GetUniqueIdentifier(namespace string, name string, kind string) string {
  91. return fmt.Sprintf("%s/%s/%s", namespace, kind, name)
  92. }
  93. // CreateTemporaryDir creates a temporary dir
  94. func CreateTemporaryDir() (string, error) {
  95. var src = rand.NewSource(time.Now().UnixNano())
  96. dir := path.Join("/tmp/", strconv.FormatInt(src.Int63(), 10), "/")
  97. err := CreateFolder(dir)
  98. return dir, err
  99. }
  100. // ParsingDatasetIndex parses index file of dataset and adds the prefix to abs url of sample
  101. // first line is the prefix, the next lines are abs url of sample
  102. func ParsingDatasetIndex(samples []string, prefix string) []string {
  103. var l []string
  104. l = append(l, prefix)
  105. for _, v := range samples {
  106. absURL := strings.Split(v, " ")[0]
  107. l = append(l, absURL)
  108. }
  109. return l
  110. }