From: qydysky Date: Thu, 20 Aug 2020 06:45:54 +0000 (+0800) Subject: 52 X-Git-Tag: v0.0.2~34 X-Git-Url: http://127.0.0.1:8081/?a=commitdiff_plain;h=6e54580b1fc5bc049fbf38b40b3d6a7524193df0;p=part%2F.git 52 --- diff --git a/CheckFile.go b/CheckFile.go index ce629c1..be039e4 100644 --- a/CheckFile.go +++ b/CheckFile.go @@ -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) diff --git a/FileWR.go b/FileWR.go index 3c086c4..3a59655 100644 --- 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" } diff --git a/Limit.go b/Limit.go index 80957b7..4f4c49f 100644 --- 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 cac9e05..7d39e45 100644 --- 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)) }