code.gitea.io/gitea@v1.19.3/modules/repository/temp.go (about)

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