Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package main
  2.  
  3. import (
  4. "fmt"
  5. "net"
  6. "os"
  7. "github.com/mistifyio/go-zfs"
  8. )
  9.  
  10. func main() {
  11. // Listen for incoming connections.
  12. l, err := net.Listen("tcp", "192.168.99.5:9977")
  13. if err != nil ...
  14. // Close the listener when the application closes.
  15. defer l.Close()
  16. fmt.Println("Listening on " + CONN_HOST + ":" + CONN_PORT)
  17. for {
  18. // Listen for an incoming connection.
  19. conn, err := l.Accept()
  20. if err != nil ...
  21.  
  22. //Handle connections in a new goroutine.
  23. go handleRequest(conn)
  24. }
  25. }
  26.  
  27. // Handles incoming requests.
  28. func handleRequest(conn net.Conn) {
  29. // Make a buffer to hold incoming data.
  30. buff := make([]byte, 1024)
  31. // Read the incoming connection into the buffer.
  32. _, err := conn.Read(buff)
  33. if err != nil {
  34. fmt.Printf("Error reading: %s.n", err.Error())
  35. }
  36. // ReceiveSnapshot
  37. ds, err := zfs.ReceiveSnapshot(buff, "tank/replication")
  38. if err != nil {
  39. fmt.Printf("Error receiving: %s.n", err.Error())
  40. }
  41. fmt.Printf("%s... done!n", ds)
  42. // Send a response back to person contacting us.
  43. conn.Write([]byte("Received!"))
  44. // Close the connection when you're done with it.
  45. conn.Close()
  46. }
  47.  
  48. type command struct {
  49. Command string
  50. Stdin io.Reader
  51. Stdout io.Writer
  52. }
  53.  
  54. func ReceiveSnapshot(input io.Reader, name string) (*Dataset, error) {
  55. c := command{Command: "zfs", Stdin: input}
  56. _, err := c.Run("receive", name)
  57. if err != nil {
  58. return nil, err
  59. }
  60. return GetDataset(name)
  61. }
  62.  
  63. type Reader interface {
  64. Read(p []byte) (n int, err error)
  65. }
  66.  
  67. ReceiveSnapshot( input io.Reader ...
  68.  
  69. Read(p []byte) (n int, err error)
  70.  
  71. func (T) Read(b []byte) (n int, err error)
  72.  
  73. type MyReader struct {
  74. src []byte
  75. pos int
  76. }
  77.  
  78. func (r *MyReader) Read(dst []byte) (n int, err error) {
  79. n = copy(dst, r.src[r.pos:])
  80. r.pos += n
  81. if r.pos == len(r.src) {
  82. return n, io.EOF
  83. }
  84. return
  85. }
  86.  
  87. func NewMyReader(b []byte) *MyReader { return &MyReader{b, 0} }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement