]> 127.0.0.1 Git - part/.git/commitdiff
52
authorqydysky <qydysky@foxmail.com>
Thu, 20 Aug 2020 06:45:54 +0000 (14:45 +0800)
committerqydysky <qydysky@foxmail.com>
Thu, 20 Aug 2020 06:45:54 +0000 (14:45 +0800)
CheckFile.go
FileWR.go
Limit.go
Lock.go

index ce629c10e809d81ce13c9549f533390468cbe06c..be039e4b57773c77a04bd5bc3350f45c54b1b00d 100644 (file)
@@ -30,18 +30,18 @@ func (this *checkfile) Build(checkFile,root,checkDir,SplitString string,usemd5 b
 
        Logf().I("checkFile Build:begin")
 
-       if usemd5 {_checkFile.Context += "UseMd5"
+       if usemd5 {_checkFile.Context = append(_checkFile.Context, "UseMd5")
 
-       _checkFile.Context += SplitString
+       _checkFile.Context = append(_checkFile.Context, SplitString)
 
        for _,value := range v {
                if usemd5 { 
                        md5, e := Md5().Md5File(value)
                        if e != nil {md5 = "00000000000000000000000000000000"}
-                       _checkFile.Context += md5 
+                       _checkFile.Context = append(_checkFile.Context, md5)
                }
-               _checkFile.Context += value[len(root):]
-               _checkFile.Context += SplitString
+               _checkFile.Context = append(_checkFile.Context, value[len(root):])
+               _checkFile.Context = append(_checkFile.Context, SplitString)
        }
        
        File().FileWR(_checkFile)
index 3c086c4972ccd096b72bb1fd1129fcbdb25c8314..3a59655d5e5f6e4e3fe121f16f0c8de5cc8901bc 100644 (file)
--- a/FileWR.go
+++ b/FileWR.go
@@ -5,11 +5,15 @@ import (
        "strings"
        "fmt"
        "os"
+       "io"
        "io/ioutil"
        "syscall"
 )
 
-type file struct {sync.Mutex}
+type file struct {
+       sync.Mutex
+       F Filel
+}
 
 const (
        o_RDONLY int = syscall.O_RDONLY // 只读打开文件和os.Open()同义
@@ -27,7 +31,7 @@ type Filel struct {
        Write bool //false:read
        Loc int64 //WriteOrRead loc ;0:rewrite Or read all;-1 write on end
        ReadNum int64
-       Context string //Write string
+       Context []interface{} //Write string
 }
 
 func File() *file{
@@ -43,7 +47,18 @@ func (this *file) FileWR(C Filel) string {
        if C.File == "" {returnVal="";return returnVal}
        
        if C.Write {
-               returnVal=this.write(C)
+               if len(C.Context) == 0 {return ""}
+
+               switch C.Context[0].(type) {
+               case io.Reader:
+                       if len(C.Context) != 1 {
+                               fmt.Println("Err:copy only allow one context")
+                               return ""
+                       }
+                       returnVal=this.copy(C)
+               default:
+                       returnVal=this.write(C)
+               }
        }else{
                returnVal=this.read(C)
        }
@@ -51,12 +66,32 @@ func (this *file) FileWR(C Filel) string {
        return returnVal
 }
 
+func (this *file) copy(C Filel) string {
+       var (
+               File string=C.File
+       )
+
+       this.NewPath(File)
+
+       fileObj,err := os.OpenFile(File,os.O_RDWR|os.O_EXCL|os.O_TRUNC,0644)
+       if err != nil {
+               fmt.Println("Err:cant open file:",File,err);
+               return ""
+       }
+       defer fileObj.Close()
+
+       if _, err := io.Copy(fileObj, C.Context[0].(io.Reader)); err != nil {
+               fmt.Println("Err:cant copy file:",File,err);
+               return ""
+       }
+       return "ok"
+}
+
 func (this *file) write(C Filel) string {
 
        var (
                File string=C.File
                Loc int64=C.Loc
-               Context string=C.Context
        )
 
        this.NewPath(File)
@@ -76,12 +111,30 @@ func (this *file) write(C Filel) string {
 
        Loc=this.locfix(Loc,File,fileObj)
 
-       _, err = fileObj.WriteAt([]byte(Context), Loc)
-       if err != nil {
-               fmt.Println("Err:cant write file:",File,err);
-               return ""
+       for _,v := range C.Context{
+               switch v.(type) {
+               case []uint8:
+                       tmp := v.([]byte)
+                       _, err = fileObj.WriteAt(tmp, Loc)
+                       if err != nil {
+                               fmt.Println("Err:cant write file:",File,err);
+                               return ""
+                       }
+                       Loc += int64(len(tmp))
+               case string:
+                       tmp := []byte(v.(string))
+                       _, err = fileObj.WriteAt(tmp, Loc)
+                       if err != nil {
+                               fmt.Println("Err:cant write file:",File,err);
+                               return ""
+                       }
+                       Loc += int64(len(tmp))
+               default:
+                       fmt.Println("Err:need context type string or []byte");
+                       return ""
+               }
        }
-       
+
        return "ok"
 }
 
index 80957b7e79a8f762eb074e34d4c826f555f7d980..4f4c49f17a444c0581eec08eea92006c8102a33b 100644 (file)
--- a/Limit.go
+++ b/Limit.go
@@ -32,14 +32,12 @@ func Limit(Max,Second,TimeOut int) (*Limitl) {
                return &returnVal
        }
 
-       returnVal = Limitl{
-               Second:Second,
-               TimeOut:TimeOut,
-       }
+       returnVal.Second=Second
+       returnVal.TimeOut=TimeOut
 
        go func(returnVal *Limitl){
                for !returnVal.Stop {
-                       for i:=1;i<=Max;i++{
+                       for i:=1;i<=returnVal.Max;i++{
                                returnVal.Channl <- true
                        }
                        time.Sleep(time.Duration(Second)*time.Millisecond)
diff --git a/Lock.go b/Lock.go
index cac9e0577fb3f50c84fcaca714acebf022055bab..7d39e459a4c7bc8021b37a55f30a176f20a9f36e 100644 (file)
--- a/Lock.go
+++ b/Lock.go
@@ -41,7 +41,7 @@ func (l *lock) Start(filePath string,timeout int64) error {
                                File:filePath,
                                Write:true,
                                Loc:0,
-                               Context:"still alive",
+                               Context:[]interface{}{"still alive"},
                        })
                        Sys().Timeoutf(int(lock_timeout))
                }