Skip to content

Package copyfilerange wraps the copy_file_range system call.

License

Notifications You must be signed in to change notification settings

hslam/copyfilerange

Repository files navigation

copyfilerange

PkgGoDev Build Status codecov Go Report Card LICENSE

Package copyfilerange wraps the copy_file_range system call.

Get started

Install

go get github.com/hslam/copyfilerange

Import

import "github.com/hslam/copyfilerange"

Usage

Example

package main

import (
	"fmt"
	"github.com/hslam/copyfilerange"
	"os"
)

func main() {
	srcName, dstName := "srcFile", "dstFile"
	srcFile, _ := os.Create(srcName)
	defer os.Remove(srcName)
	defer srcFile.Close()
	dstFile, _ := os.Create(dstName)
	defer os.Remove(dstName)
	defer dstFile.Close()

	content := []byte("Hello world")
	srcOffset, dstOffset := int64(64), int64(32)
	srcFile.Truncate(srcOffset)
	srcFile.WriteAt(content, srcOffset)
	dstFile.Truncate(dstOffset + int64(len(content)))

	roff, woff := srcOffset, dstOffset
	copyfilerange.CopyFileRange(int(srcFile.Fd()), &roff, int(dstFile.Fd()), &woff, len(content), 0)

	buf := make([]byte, len(content))
	dstFile.ReadAt(buf, dstOffset)
	fmt.Println(string(buf))
}

Output

Hello world

License

This package is licensed under a MIT license (Copyright (c) 2023 Meng Huang)

Author

copyfilerange was written by Meng Huang.

About

Package copyfilerange wraps the copy_file_range system call.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages