11package buffer
22
33import (
4+ "errors"
5+ "fmt"
46 "io"
57 "io/fs"
68 "os"
@@ -12,6 +14,15 @@ import (
1214 "github.com/binzume/dkango"
1315)
1416
17+ // 错误定义
18+ var (
19+ ErrSourcePathNotExist = errors .New ("源路径不存在或无法访问" )
20+ ErrSourcePathNotDir = errors .New ("源路径不是有效的目录" )
21+ ErrMountPointInvalid = errors .New ("挂载点无效" )
22+ ErrMountFailed = errors .New ("挂载失败" )
23+ ErrAlreadyMounted = errors .New ("已经存在挂载实例,请先卸载" )
24+ )
25+
1526type BufferConfig struct {
1627 SourcePath string
1728 MemoryLimit int64
@@ -27,7 +38,14 @@ type BufferFS struct {
2738 readCache * cache.LRU
2839}
2940
30- func NewBufferFS (config * BufferConfig ) * BufferFS {
41+ func NewBufferFS (config * BufferConfig ) (* BufferFS , error ) {
42+ if config .SourcePath == "" {
43+ config .SourcePath = "."
44+ }
45+ if err := validateSourcePath (config .SourcePath ); err != nil {
46+ return nil , err
47+ }
48+
3149 fs := & BufferFS {config : config }
3250 if config .EnableWriteBuffer {
3351 strategy := bufioutil .GetPresetStrategy (bufioutil .UseCase (config .Strategy ))
@@ -39,10 +57,21 @@ func NewBufferFS(config *BufferConfig) *BufferFS {
3957 if config .EnableReadCache {
4058 fs .readCache = cache .NewLRU (config .MemoryLimit )
4159 }
42- if config .SourcePath == "" {
43- config .SourcePath = "."
60+ return fs , nil
61+ }
62+
63+ func validateSourcePath (sourcePath string ) error {
64+ info , err := os .Stat (sourcePath )
65+ if err != nil {
66+ if os .IsNotExist (err ) {
67+ return fmt .Errorf ("%w: %s" , ErrSourcePathNotExist , sourcePath )
68+ }
69+ return fmt .Errorf ("%w: %s" , ErrSourcePathNotExist , err .Error ())
4470 }
45- return fs
71+ if ! info .IsDir () {
72+ return fmt .Errorf ("%w: %s" , ErrSourcePathNotDir , sourcePath )
73+ }
74+ return nil
4675}
4776
4877func (b * BufferFS ) Open (name string ) (fs.File , error ) {
@@ -63,7 +92,17 @@ func (b *BufferFS) OpenWriter(name string, flag int) (io.WriteCloser, error) {
6392 if ! fs .ValidPath (name ) {
6493 return nil , & fs.PathError {Op : "open" , Path : name , Err : fs .ErrInvalid }
6594 }
66- return os .OpenFile (path .Join (b .config .SourcePath , name ), flag , fs .ModePerm )
95+ f , err := os .OpenFile (path .Join (b .config .SourcePath , name ), flag , 0644 )
96+ if err != nil {
97+ if os .IsPermission (err ) {
98+ return nil , fmt .Errorf ("文件写入失败,权限不足: %s, 错误: %w" , name , err )
99+ }
100+ if os .IsNotExist (err ) {
101+ return nil , fmt .Errorf ("文件写入失败,父目录不存在: %s, 错误: %w" , name , err )
102+ }
103+ return nil , fmt .Errorf ("文件写入失败: %s, 错误: %w" , name , err )
104+ }
105+ return f , nil
67106}
68107
69108func (b * BufferFS ) Remove (name string ) error {
@@ -97,10 +136,21 @@ func (b *BufferFS) Truncate(name string, size int64) error {
97136var mountInstance interface { Close () error }
98137
99138func Mount (drive string , config * BufferConfig ) error {
100- fs := NewBufferFS (config )
139+ if drive == "" {
140+ return fmt .Errorf ("%w: 挂载点不能为空" , ErrMountPointInvalid )
141+ }
142+ if mountInstance != nil {
143+ return fmt .Errorf ("%w" , ErrAlreadyMounted )
144+ }
145+
146+ fs , err := NewBufferFS (config )
147+ if err != nil {
148+ return fmt .Errorf ("初始化BufferFS失败: %w" , err )
149+ }
150+
101151 mount , err := dkango .MountFS (drive , fs , nil )
102152 if err != nil {
103- return err
153+ return fmt . Errorf ( "%w: %v" , ErrMountFailed , err )
104154 }
105155 mountInstance = mount
106156 return nil
0 commit comments