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.

helper_directory.go 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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. "code.gitea.io/gitea/modules/util"
  14. )
  15. // LocalCopyPath returns the local repository temporary copy path.
  16. func LocalCopyPath() string {
  17. if filepath.IsAbs(setting.Repository.Local.LocalCopyPath) {
  18. return setting.Repository.Local.LocalCopyPath
  19. }
  20. return path.Join(setting.AppDataPath, setting.Repository.Local.LocalCopyPath)
  21. }
  22. // CreateTemporaryPath creates a temporary path
  23. func CreateTemporaryPath(prefix string) (string, error) {
  24. if err := os.MkdirAll(LocalCopyPath(), os.ModePerm); err != nil {
  25. log.Error("Unable to create localcopypath directory: %s (%v)", LocalCopyPath(), err)
  26. return "", fmt.Errorf("Failed to create localcopypath directory %s: %v", LocalCopyPath(), err)
  27. }
  28. basePath, err := ioutil.TempDir(LocalCopyPath(), prefix+".git")
  29. if err != nil {
  30. log.Error("Unable to create temporary directory: %s-*.git (%v)", prefix, err)
  31. return "", fmt.Errorf("Failed to create dir %s-*.git: %v", prefix, err)
  32. }
  33. return basePath, nil
  34. }
  35. // RemoveTemporaryPath removes the temporary path
  36. func RemoveTemporaryPath(basePath string) error {
  37. if _, err := os.Stat(basePath); !os.IsNotExist(err) {
  38. return util.RemoveAll(basePath)
  39. }
  40. return nil
  41. }