ota服务层

This commit is contained in:
2025-12-05 16:31:12 +08:00
parent 2bb187071f
commit b4ecee6626
2 changed files with 71 additions and 16 deletions

View File

@@ -55,8 +55,8 @@ func ExecuteWithLock(action func() error, onRollback func(err error)) error {
// CreateTempDir 在指定的根目录下创建一个子目录。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func CreateTempDir(tempRoot, subDir string) (string, error) {
fullDirPath := filepath.Join(tempRoot, subDir)
func CreateTempDir(subDir string) (string, error) {
fullDirPath := filepath.Join(instance.tempRoot, subDir)
if err := os.MkdirAll(fullDirPath, 0755); err != nil {
return "", fmt.Errorf("创建临时目录 %s 失败: %w", fullDirPath, err)
}
@@ -65,8 +65,8 @@ func CreateTempDir(tempRoot, subDir string) (string, error) {
// PrepareTempFilePath 获取文件在指定子目录下的完整路径,并确保父目录存在。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func PrepareTempFilePath(tempRoot, subDir, fileName string) (string, error) {
fullDirPath := filepath.Join(tempRoot, subDir)
func PrepareTempFilePath(subDir, fileName string) (string, error) {
fullDirPath := filepath.Join(instance.tempRoot, subDir)
if err := os.MkdirAll(fullDirPath, 0755); err != nil {
return "", fmt.Errorf("创建临时文件父目录 %s 失败: %w", fullDirPath, err)
}
@@ -75,8 +75,8 @@ func PrepareTempFilePath(tempRoot, subDir, fileName string) (string, error) {
// RemoveTempDir 在指定的根目录下清理并删除一个子目录。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func RemoveTempDir(tempRoot, subDir string) error {
fullDirPath := filepath.Join(tempRoot, subDir)
func RemoveTempDir(subDir string) error {
fullDirPath := filepath.Join(instance.tempRoot, subDir)
return os.RemoveAll(fullDirPath)
}
@@ -107,8 +107,8 @@ func LoadYaml(path string, c interface{}) error {
// WriteTempFile 将数据写入到临时子目录的指定文件中。
// 它会自动创建所需的子目录。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func WriteTempFile(tempRoot, subDir, fileName string, data []byte) (string, error) {
fullDirPath := filepath.Join(tempRoot, subDir)
func WriteTempFile(subDir, fileName string, data []byte) (string, error) {
fullDirPath := filepath.Join(instance.tempRoot, subDir)
if err := os.MkdirAll(fullDirPath, 0755); err != nil {
return "", fmt.Errorf("为写入操作创建临时目录 %s 失败: %w", fullDirPath, err)
}
@@ -123,8 +123,8 @@ func WriteTempFile(tempRoot, subDir, fileName string, data []byte) (string, erro
// ReadTempFile 从临时子目录的指定文件中读取数据。
// 注意:此函数本身不是线程安全的,应在 ExecuteWithLock 的回调中使用。
func ReadTempFile(tempRoot, subDir, fileName string) ([]byte, error) {
filePath := filepath.Join(tempRoot, subDir, fileName)
func ReadTempFile(subDir, fileName string) ([]byte, error) {
filePath := filepath.Join(instance.tempRoot, subDir, fileName)
data, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("读取临时文件 %s 失败: %w", filePath, err)