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

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. // Copyright 2019 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package repository
  4. import (
  5. "fmt"
  6. "os"
  7. "path"
  8. "path/filepath"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. // LocalCopyPath returns the local repository temporary copy path.
  14. func LocalCopyPath() string {
  15. if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
  16. return setting.Repository.Local.LocalCopyPath
  17. }
  18. return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath)
  19. }
  20. // CreateTemporaryPath creates a temporary path
  21. func CreateTemporaryPath(prefix string) (string, error) {
  22. if err := os.MkdirAll(LocalCopyPath(), os.ModePerm); err != nil {
  23. log.Error("Unable to create localcopypath directory: %s (%v)", LocalCopyPath(), err)
  24. return "", fmt.Errorf("Failed to create localcopypath directory %s: %w", LocalCopyPath(), err)
  25. }
  26. basePath, err := os.MkdirTemp(LocalCopyPath(), prefix+".git")
  27. if err != nil {
  28. log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
  29. return "", fmt.Errorf("Failed to create dir %s-*.git: %w", prefix, err)
  30. }
  31. return basePath, nil
  32. }
  33. // RemoveTemporaryPath removes the temporary path
  34. func RemoveTemporaryPath(basePath string) error {
  35. if _, err := os.Stat(basePath); !os.IsNotExist(err) {
  36. return util.RemoveAll(basePath)
  37. }
  38. return nil
  39. }