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.

47 lines
1.4 KiB

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