]> 127.0.0.1 Git - part/.git/commitdiff
filewr update v0.5.23
authorqydysky <qydysky@foxmail.com>
Fri, 11 Jun 2021 15:46:30 +0000 (23:46 +0800)
committerqydysky <qydysky@foxmail.com>
Fri, 11 Jun 2021 15:46:30 +0000 (23:46 +0800)
CheckFile.go
FileWR.go
Lock.go

index 25aca5b8a839da84c27dda5983a63cb5974ccb4b..9c28bc4d6f568907b6368668aa927af1f86e49d6 100644 (file)
@@ -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,
                }
                
index 42123c96812834c807b91ce789f3262f96cfb25a..be313ff67178c3806b5ea49ddbf6d2849f46a3ea 100644 (file)
--- 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 7d39e459a4c7bc8021b37a55f30a176f20a9f36e..81d43456bd7cd597495fbad6a109e55a5fccddac 100644 (file)
--- 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"},
                        })