Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "fmt"
- "net"
- "os"
- "github.com/mistifyio/go-zfs"
- )
- func main() {
- // Listen for incoming connections.
- l, err := net.Listen("tcp", "192.168.99.5:9977")
- if err != nil ...
- // Close the listener when the application closes.
- defer l.Close()
- fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
- for {
- // Listen for an incoming connection.
- conn, err := l.Accept()
- if err != nil ...
- //Handle connections in a new goroutine.
- go handleRequest(conn)
- }
- }
- // Handles incoming requests.
- func handleRequest(conn net.Conn) {
- // Make a buffer to hold incoming data.
- buff := make([]byte, 1024)
- // Read the incoming connection into the buffer.
- _, err := conn.Read(buff)
- if err != nil {
- fmt.Printf("Error reading: %s.n", err.Error())
- }
- // ReceiveSnapshot
- ds, err := zfs.ReceiveSnapshot(buff, "tank/replication")
- if err != nil {
- fmt.Printf("Error receiving: %s.n", err.Error())
- }
- fmt.Printf("%s... done!n", ds)
- // Send a response back to person contacting us.
- conn.Write([]byte("Received!"))
- // Close the connection when you're done with it.
- conn.Close()
- }
- type command struct {
- Command string
- Stdin io.Reader
- Stdout io.Writer
- }
- func ReceiveSnapshot(input io.Reader, name string) (*Dataset, error) {
- c := command{Command: "zfs", Stdin: input}
- _, err := c.Run("receive", name)
- if err != nil {
- return nil, err
- }
- return GetDataset(name)
- }
- type Reader interface {
- Read(p []byte) (n int, err error)
- }
- ReceiveSnapshot( input io.Reader ...
- Read(p []byte) (n int, err error)
- func (T) Read(b []byte) (n int, err error)
- type MyReader struct {
- src []byte
- pos int
- }
- func (r *MyReader) Read(dst []byte) (n int, err error) {
- n = copy(dst, r.src[r.pos:])
- r.pos += n
- if r.pos == len(r.src) {
- return n, io.EOF
- }
- return
- }
- func NewMyReader(b []byte) *MyReader { return &MyReader{b, 0} }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement