rsync

package
v0.0.0-...-0a3bb19 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 4, 2024 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	END_FLAG      = 0
	TOKEN_LONG    = 0x20
	TOKENRUN_LONG = 0x21
	DEFLATED_DATA = 0x40
	TOKEN_REL     = 0x80
	TOKENRUN_REL  = 0xc0
)
View Source
const (
	RSYNC_VERSION  = "@RSYNCD: 27.0\n"
	RSYNC_AUTHREQD = "@RSYNCD: AUTHREQD"
	RSYNCD_OK      = "@RSYNCD: OK"
	RSYNC_EXIT     = "@RSYNCD: EXIT"

	K = 1 << 10
	M = 1 << 20
	G = 1 << 30

	MAXPATHLEN    = 1024
	INDEX_END     = int32(-1)
	EXCLUSION_END = int32(0)
	END1          = '\n'
	END2          = '\x00'
	PHASE_END     = int32(-1)

	// ARGUMENTS
	ARG_SERVER       = "--server"
	ARG_SENDER       = "--sender"
	ARG_SYMLINK      = "-l"
	ARG_RECURSIVE    = "-r"
	ARG_PERMS        = "-p"
	SAMPLE_ARGS      = "--server\n--sender\n-l\n-p\n-r\n-t\n.\n"
	SAMPLE_LIST_ARGS = "--server\n--sender\n--list-only\n-l\n-p\n-r\n-t\n.\n"

	// For Multiplex(1 byte)
	MUX_BASE       = 7
	MSG_DATA       = 0
	MSG_ERROR_XFER = 1
	MSG_INFO       = 2
	MSG_ERROR      = 3
	MSG_WARNING    = 4
	MSG_IO_ERROR   = 22
	MSG_NOOP       = 42
	MSG_SUCCESS    = 100
	MSG_DELETED    = 101
	MSG_NO_SEND    = 102

	// For FILE LIST(1 byte)
	FLIST_END       = 0x00
	FLIST_TOP_LEVEL = 0x01 /* needed for remote --delete */
	FLIST_MODE_SAME = 0x02 /* mode is repeat */
	FLIST_RDEV_SAME = 0x04 /* rdev is repeat */
	FLIST_UID_SAME  = 0x08 /* uid is repeat */
	FLIST_GID_SAME  = 0x10 /* gid is repeat */
	FLIST_NAME_SAME = 0x20 /* name is repeat */
	FLIST_NAME_LONG = 0x40 /* name >255 bytes */
	FLIST_TIME_SAME = 0x80 /* time is repeat */

	// File type
	S_IFMT   = 0o170000 /* Type of file */
	S_IFREG  = 0o100000 /* Regular file.  */
	S_IFDIR  = 0o040000 /* Directory.  */
	S_IFLNK  = 0o120000 /* Symbolic link.  */
	S_IFCHR  = 0o020000 /* Character device.  */
	S_IFBLK  = 0o060000 /* Block device.  */
	S_IFIFO  = 0o010000 /* FIFO.  */
	S_IFSOCK = 0o140000 /* Socket.  */
)

Variables

This section is empty.

Functions

func SplitURI

func SplitURI(uri string) (string, string, string, error)

For rsync

func SplitURIS

func SplitURIS(uri string) (string, int, string, string, error)

func SplitURL

func SplitURL(url *url.URL) (string, string, string, error)

For rsync

func TrimPrepath

func TrimPrepath(prepath string) string

The path always has a trailing slash appended

Types

type Attribs

type Attribs struct {
	Sender     bool // --sender
	Server     bool // --server
	Recursive  bool // -r
	DryRun     bool // -n
	HasModTime bool // -t
	HasPerms   bool // -p
	HasLinks   bool // -l
	HasGID     bool // -g
	HasUID     bool // -u

}

func (*Attribs) Marshal

func (a *Attribs) Marshal() []byte

type ClientAuth

type ClientAuth struct {
	Username string
	Password string
}

type ClientOption

type ClientOption func(*clientOptions)

func WithClientAuth

func WithClientAuth(username, password string) ClientOption

func WithExclusionList

func WithExclusionList(exclusionList ExclusionList) ClientOption

func WithLogger

func WithLogger(logger *slog.Logger) ClientOption

type Conn

type Conn struct {
	Writer    io.WriteCloser // Write only
	Reader    io.ReadCloser  // Read only
	Bytespool []byte         // Anti memory-wasted, default size: 8 bytes
}

io.ReadWriteCloser This struct has two main attributes, both of them can be used for a plain socket or an SSH

func (*Conn) Close

func (conn *Conn) Close() error

TODO: If both writer and reader are based on a same Connection (socket, SSH), how to close them twice?

func (*Conn) Read

func (conn *Conn) Read(p []byte) (n int, err error)

func (*Conn) ReadByte

func (conn *Conn) ReadByte() (byte, error)
Encoding: little endian

size of: int: 4, long: 8, varint: 4 or 8

func (*Conn) ReadInt

func (conn *Conn) ReadInt() (int32, error)

func (*Conn) ReadLong

func (conn *Conn) ReadLong() (int64, error)

func (*Conn) ReadShort

func (conn *Conn) ReadShort() (int16, error)

func (*Conn) ReadVarint

func (conn *Conn) ReadVarint() (int64, error)

func (*Conn) Write

func (conn *Conn) Write(p []byte) (n int, err error)

func (*Conn) WriteByte

func (conn *Conn) WriteByte(data byte) error

func (*Conn) WriteInt

func (conn *Conn) WriteInt(data int32) error

func (*Conn) WriteLong

func (conn *Conn) WriteLong(data int64) error

func (*Conn) WriteShort

func (conn *Conn) WriteShort(data int16) error

type ExclusionList

type ExclusionList []string

Filter List

func (ExclusionList) SendExlusionList

func (e ExclusionList) SendExlusionList(conn *Conn) error

This is only called by the client

type FS

type FS interface {
	Put(fileName string, content io.Reader, fileSize int64, metadata FileMetadata) (written int64, err error)
	// Get(fileName string, metadata FileMetadata) (File, error)
	Delete(fileName string, mode FileMode) error
	List() (FileList, error)
}

File System: need to handle all type of files: regular, folder, symlink, etc

type File

type File interface {
	io.Reader
	io.ReaderAt
	io.Seeker
	io.Closer
}

Interface: Read, ReadAt, Seek, Close

type FileInfo

type FileInfo struct {
	Path  []byte
	Size  int64
	Mtime int32
	Mode  FileMode
}

type FileList

type FileList []FileInfo

func (FileList) Diff

func (L FileList) Diff(R FileList) (newitems []int, olditems []int)
Diff two sorted rsync file list, return their difference

list NEW: only R has. list OLD: only L has.

func (FileList) Len

func (L FileList) Len() int

func (FileList) Less

func (L FileList) Less(i, j int) bool

func (FileList) Swap

func (L FileList) Swap(i, j int)

type FileMetadata

type FileMetadata struct {
	Mtime int32
	Mode  FileMode
}

type FileMode

type FileMode uint32

For unix/linux

func NewFileMode

func NewFileMode(mode os.FileMode) FileMode

reference: os/types.go

func (FileMode) Convert

func (m FileMode) Convert() os.FileMode

Convert to os.FileMode

func (FileMode) IsBLK

func (m FileMode) IsBLK() bool

func (FileMode) IsDIR

func (m FileMode) IsDIR() bool

func (FileMode) IsFIFO

func (m FileMode) IsFIFO() bool

func (FileMode) IsLNK

func (m FileMode) IsLNK() bool

func (FileMode) IsREG

func (m FileMode) IsREG() bool

func (FileMode) IsSOCK

func (m FileMode) IsSOCK() bool

func (FileMode) Perm

func (m FileMode) Perm() FileMode

Return only unix permission bits

func (FileMode) String

func (m FileMode) String() string

strmode

type FlatedtokenReader

type FlatedtokenReader struct {
	In           Conn
	Flatedwraper *flatedWraper
	Decompressor io.ReadCloser
	Savedflag    byte
	Flag         byte
	Remains      uint32
}

RFC 1951: https://tools.ietf.org/html/rfc1951

func NewflatedtokenReader

func NewflatedtokenReader(reader Conn) *FlatedtokenReader

func (*FlatedtokenReader) Close

func (f *FlatedtokenReader) Close() error

func (*FlatedtokenReader) Read

func (f *FlatedtokenReader) Read(p []byte) (n int, err error)

func (*FlatedtokenReader) ReadFlag

func (f *FlatedtokenReader) ReadFlag() error

Update flag & len of remain data

type MuxReader

type MuxReader struct {
	In     io.ReadCloser
	Remain uint32 // Default value: 0
	Header []byte // Size: 4 bytes
	Logger *slog.Logger
}

func NewMuxReader

func NewMuxReader(reader io.ReadCloser, logger *slog.Logger) *MuxReader

func (*MuxReader) Close

func (r *MuxReader) Close() error

func (*MuxReader) Read

func (r *MuxReader) Read(p []byte) (n int, err error)

type Receiver

type Receiver struct {
	Conn    *Conn
	Module  string
	Path    string
	Seed    int32
	LVer    int32
	RVer    int32
	Storage FS
	Logger  *slog.Logger
}
Receiver:

1. Receive File list 2. Request files by sending files' index 3. Receive Files, pass the files to Storage

func (*Receiver) FileCleaner

func (r *Receiver) FileCleaner(localList FileList, deleteList []int) error

Clean up local files

func (*Receiver) FileDownloader

func (r *Receiver) FileDownloader(localList FileList) error

TODO: It is better to update files in goroutine

func (*Receiver) FinalPhase

func (r *Receiver) FinalPhase() error

func (*Receiver) Generator

func (r *Receiver) Generator(remoteList FileList, downloadList []int, symlinks map[string][]byte) error

Generator: handle files: if it's a regular file, send its index. Otherwise, put them to Storage

func (*Receiver) GetSyncPlan

func (r *Receiver) GetSyncPlan() (*SyncPlan, error)

func (*Receiver) List

func (r *Receiver) List() error

func (*Receiver) RecvFileList

func (r *Receiver) RecvFileList() (FileList, map[string][]byte, error)

Return a filelist from remote

func (*Receiver) Sync

func (r *Receiver) Sync() error

type SSH

type SSH struct {
	// contains filtered or unexported fields
}

func NewSSH

func NewSSH(address string, _ string, _ string, cmd string) (*SSH, error)

func (*SSH) Close

func (s *SSH) Close() error

func (*SSH) Read

func (s *SSH) Read(p []byte) (n int, err error)

func (*SSH) Write

func (s *SSH) Write(p []byte) (n int, err error)

type SendReceiver

type SendReceiver interface {
	Sync() error
	GetSyncPlan() (*SyncPlan, error)
	List() error
}

func SSHClient

func SSHClient(storage FS, address string, module string, path string, _ map[string]string) (SendReceiver, error)

Connect to sshd, and start a rsync server on remote

func SocketClient

func SocketClient(storage FS, address string, module string, path string, opts ...ClientOption) (SendReceiver, error)

TODO: passes more arguments: cmd Connect to rsync daemon

type Sender

type Sender struct {
	Conn    *Conn
	Module  string
	Path    string
	Seed    int32
	LVer    int32
	RVer    int32
	Storage FS
}

func (*Sender) FileUploader

func (s *Sender) FileUploader() error

func (*Sender) FinalPhase

func (s *Sender) FinalPhase() error

func (*Sender) Generator

func (s *Sender) Generator(_ FileList) error

func (*Sender) SendFileList

func (s *Sender) SendFileList() error

func (*Sender) Sync

func (s *Sender) Sync() error

type SumChunk

type SumChunk struct {
	FileOffset int64
	ChunkLen   uint
	Sum1       uint32
	Sum2       []byte
}

type SumStruct

type SumStruct struct {
	FileLen   uint64     // totol file length
	Count     uint64     // how many chunks
	Remainder uint64     // fileLen % blockLen
	BlockLen  uint64     // block length
	Sum2Len   uint64     // sum2 length
	SumList   []SumChunk // chunks
}

type SyncPlan

type SyncPlan struct {
	RemoteFiles      FileList
	LocalFiles       FileList
	AddRemoteFiles   []int
	DeleteLocalFiles []int
	Symlinks         map[string][]byte
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL