From 4f2d9fa808d2f991e13c7cda491ee356d32c9458 Mon Sep 17 00:00:00 2001 From: qydysky Date: Fri, 11 Jun 2021 23:46:30 +0800 Subject: [PATCH] filewr update --- CheckFile.go | 2 -- FileWR.go | 30 ++++++++++++++++++++++++------ Lock.go | 1 - 3 files changed, 24 insertions(+), 9 deletions(-) diff --git a/CheckFile.go b/CheckFile.go index 25aca5b..9c28bc4 100644 --- a/CheckFile.go +++ b/CheckFile.go @@ -26,7 +26,6 @@ func (this *checkfile) Build(checkFile,root,checkDir,SplitString string,usemd5 b v,_,_:=this.GetAllFile(checkDir) _checkFile := Filel { File:checkFile, - Write:true, Loc:0, } @@ -155,7 +154,6 @@ func (this *checkfile) CheckList(checkFile,root,SplitString string)bool{ }else{ var _checkFile = Filel { File:checkFile, - Write:false, Loc:0, } diff --git a/FileWR.go b/FileWR.go index 42123c9..be313ff 100644 --- a/FileWR.go +++ b/FileWR.go @@ -30,12 +30,23 @@ const ( type Filel struct { File string //src - Write bool //false:read Loc int64 //WriteOrRead loc ;0:rewrite Or read all;-1 write on end ReadNum int64 Context []interface{} //Write string + + // wrap with encoder/decoder + //https://pkg.go.dev/golang.org/x/text/encoding + WrapWriter func(io.Writer)(io.Writer) + // WrapReader func(io.Reader)(io.Reader) + + // read func(b []byte)(int,error) + write func(b []byte)(int,error) } +// func (t Filel) Read(b []byte)(int,error){return t.read(b)} +func (t Filel) Write(b []byte)(int,error){return t.write(b)} + + func File() *file{ return &file{} } @@ -48,9 +59,7 @@ func (this *file) FileWR(C Filel) string { if C.File == "" {returnVal="";return returnVal} - if C.Write { - if len(C.Context) == 0 {return ""} - + if len(C.Context) != 0 { switch C.Context[0].(type) { case io.Reader: if len(C.Context) != 1 { @@ -113,11 +122,20 @@ func (this *file) write(C Filel) string { Loc=this.locfix(Loc,File,fileObj) + var writer io.Writer + C.write = func(b []byte)(int, error){ + return fileObj.WriteAt(b, Loc) + } + writer = C + if C.WrapWriter != nil { + writer = C.WrapWriter(writer) + } + for _,v := range C.Context{ switch v.(type) { case []uint8: tmp := v.([]byte) - _, err = fileObj.WriteAt(tmp, Loc) + _, err = writer.Write(tmp) if err != nil { fmt.Println("Err:cant write file:",File,err); return "" @@ -125,7 +143,7 @@ func (this *file) write(C Filel) string { Loc += int64(len(tmp)) case string: tmp := []byte(v.(string)) - _, err = fileObj.WriteAt(tmp, Loc) + _, err = writer.Write(tmp) if err != nil { fmt.Println("Err:cant write file:",File,err); return "" diff --git a/Lock.go b/Lock.go index 7d39e45..81d4345 100644 --- a/Lock.go +++ b/Lock.go @@ -39,7 +39,6 @@ func (l *lock) Start(filePath string,timeout int64) error { for lock_file != "" { File().FileWR(Filel{ File:filePath, - Write:true, Loc:0, Context:[]interface{}{"still alive"}, }) -- 2.39.2