本站源代码
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.

46 lines
1.3KB

  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. "os"
  8. "path"
  9. "path/filepath"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/setting"
  13. "github.com/unknwon/com"
  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. timeStr := com.ToStr(time.Now().Nanosecond()) // SHOULD USE SOMETHING UNIQUE
  25. basePath := path.Join(LocalCopyPath(), prefix+"-"+timeStr+".git")
  26. if err := os.MkdirAll(filepath.Dir(basePath), os.ModePerm); err != nil {
  27. log.Error("Unable to create temporary directory: %s (%v)", basePath, err)
  28. return "", fmt.Errorf("Failed to create dir %s: %v", basePath, err)
  29. }
  30. return basePath, nil
  31. }
  32. // RemoveTemporaryPath removes the temporary path
  33. func RemoveTemporaryPath(basePath string) error {
  34. if _, err := os.Stat(basePath); !os.IsNotExist(err) {
  35. return os.RemoveAll(basePath)
  36. }
  37. return nil
  38. }
上海开阖软件有限公司 沪ICP备12045867号-1