Skip to content

Commit 729e688

Browse files
committed
fix: cleaning up the cache folder caused permission issues
1 parent 9ea4857 commit 729e688

File tree

3 files changed

+11
-33
lines changed

3 files changed

+11
-33
lines changed

bot/bot.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package bot
33
import (
44
"context"
55
"net/url"
6-
"os"
76
"time"
87

98
"github.com/celestix/gotgproto"
@@ -90,12 +89,10 @@ func Init() {
9089

9190
select {
9291
case <-ctx.Done():
93-
common.Log.Fatal("初始化客户端失败: 超时")
94-
os.Exit(1)
92+
common.Log.Panic("初始化客户端失败: 超时")
9593
case result := <-resultChan:
9694
if result.err != nil {
97-
common.Log.Fatalf("初始化客户端失败: %s", result.err)
98-
os.Exit(1)
95+
common.Log.Panicf("初始化客户端失败: %s", result.err)
9996
}
10097
Client = result.client
10198
RegisterHandlers(Client.Dispatcher)

cmd/run.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func Run(_ *cobra.Command, _ []string) {
4747
return
4848
}
4949
common.Log.Info("正在清理缓存文件夹: ", cachePath)
50-
if err := os.RemoveAll(cachePath); err != nil {
50+
if err := common.RemoveAllInDir(cachePath); err != nil {
5151
common.Log.Error("清理缓存失败: ", err)
5252
}
5353
}

common/os.go

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,11 @@
11
package common
22

33
import (
4-
"errors"
54
"os"
65
"path/filepath"
76
"time"
87
)
98

10-
// 创建文件, 自动创建目录
11-
func MkFile(path string, data []byte) error {
12-
err := os.MkdirAll(filepath.Dir(path), os.ModePerm)
13-
if err != nil {
14-
return err
15-
}
16-
return os.WriteFile(path, data, os.ModePerm)
17-
}
18-
19-
// 删除文件, 并清理空目录. 如果文件不存在则返回 nil
20-
func PurgeFile(path string) error {
21-
if err := os.Remove(path); err != nil {
22-
if !errors.Is(err, os.ErrNotExist) {
23-
return err
24-
}
25-
}
26-
return RemoveEmptyDirectories(filepath.Dir(path))
27-
}
28-
299
func RmFileAfter(path string, td time.Duration) {
3010
_, err := os.Stat(path)
3111
if err != nil {
@@ -34,22 +14,23 @@ func RmFileAfter(path string, td time.Duration) {
3414
}
3515
Log.Debugf("Remove file after %s: %s", td, path)
3616
time.AfterFunc(td, func() {
37-
PurgeFile(path)
17+
if err := os.Remove(path); err != nil {
18+
Log.Errorf("Failed to remove file %s: %s", path, err)
19+
}
3820
})
3921
}
4022

41-
// 递归删除空目录
42-
func RemoveEmptyDirectories(dirPath string) error {
23+
// 删除目录下的所有内容, 但不删除目录本身
24+
func RemoveAllInDir(dirPath string) error {
4325
entries, err := os.ReadDir(dirPath)
4426
if err != nil {
4527
return err
4628
}
47-
if len(entries) == 0 {
48-
err := os.Remove(dirPath)
49-
if err != nil {
29+
for _, entry := range entries {
30+
entryPath := filepath.Join(dirPath, entry.Name())
31+
if err := os.RemoveAll(entryPath); err != nil {
5032
return err
5133
}
52-
return RemoveEmptyDirectories(filepath.Dir(dirPath))
5334
}
5435
return nil
5536
}

0 commit comments

Comments
 (0)