github.com/hanwen/go-fuse@v1.0.0/fuse/pathfs/copy.go (about)

     1  // Copyright 2016 the Go-FUSE Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package pathfs
     6  
     7  import (
     8  	"os"
     9  
    10  	"github.com/hanwen/go-fuse/fuse"
    11  )
    12  
    13  func CopyFile(srcFs, destFs FileSystem, srcFile, destFile string, context *fuse.Context) fuse.Status {
    14  	src, code := srcFs.Open(srcFile, uint32(os.O_RDONLY), context)
    15  	if !code.Ok() {
    16  		return code
    17  	}
    18  	defer src.Release()
    19  	defer src.Flush()
    20  
    21  	attr, code := srcFs.GetAttr(srcFile, context)
    22  	if !code.Ok() {
    23  		return code
    24  	}
    25  
    26  	dst, code := destFs.Create(destFile, uint32(os.O_WRONLY|os.O_CREATE|os.O_TRUNC), attr.Mode, context)
    27  	if !code.Ok() {
    28  		return code
    29  	}
    30  	defer dst.Release()
    31  	defer dst.Flush()
    32  
    33  	buf := make([]byte, 128*(1<<10))
    34  	off := int64(0)
    35  	for {
    36  		res, code := src.Read(buf, off)
    37  		if !code.Ok() {
    38  			return code
    39  		}
    40  		data, code := res.Bytes(buf)
    41  		if !code.Ok() {
    42  			return code
    43  		}
    44  
    45  		if len(data) == 0 {
    46  			break
    47  		}
    48  		n, code := dst.Write(data, off)
    49  		if !code.Ok() {
    50  			return code
    51  		}
    52  		if int(n) < len(data) {
    53  			return fuse.EIO
    54  		}
    55  		if len(data) < len(buf) {
    56  			break
    57  		}
    58  		off += int64(len(data))
    59  	}
    60  	return fuse.OK
    61  }