shared

package module
v0.0.0-...-26c207c Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2015 License: MIT Imports: 14 Imported by: 8

README

Shared

Shared code of the various Tinzenite packages.

Documentation

Index

Constants

View Source
const (
	/*RANDOMSEEDLENGTH is the amount of bytes used as cryptographic hash seed.*/
	RANDOMSEEDLENGTH = 32
	/*IDMAXLENGTH is the length in chars of new random identification hashes.*/
	IDMAXLENGTH = 16
	/*KEYLENGTH is the length of the encryption key used for challenges and file encryption.*/
	KEYLENGTH = 256
	/*FILEPERMISSIONMODE used for all file operations.*/
	FILEPERMISSIONMODE = 0777
	/*FILEFLAGCREATEAPPEND is the flag required to create a file or append to it if it already exists.*/
	FILEFLAGCREATEAPPEND = os.O_CREATE | os.O_RDWR | os.O_APPEND
	/*CHUNKSIZE for hashing and encryption.*/
	CHUNKSIZE = 8 * 1024
)

constant value here

View Source
const (
	TINZENITEDIR   = ".tinzenite"
	TINIGNORE      = ".tinignore" // correct valid name of .tinignore files
	DIRECTORYLIST  = "directory.list"
	LOCALDIR       = "local" // info to the local peer is stored here
	TEMPDIR        = "temp"
	RECEIVINGDIR   = "receiving" // dir for receiving transfers
	SENDINGDIR     = "sending"   // send dir for now only used in encrypted
	REMOVEDIR      = "removed"   // remove
	REMOVECHECKDIR = "check"     // remove
	REMOVEDONEDIR  = "done"      // remove
	REMOVESTOREDIR = "rmstore"   // local remove check dir
	ORGDIR         = "org"       // only directory that IS synchronized as normal
	PEERSDIR       = "peers"
	ENDING         = ".json"
	AUTHJSON       = "auth" + ENDING
	MODELJSON      = "model" + ENDING
	SELFPEERJSON   = "self" + ENDING
	BOOTJSON       = "boot" + ENDING
)

Path constants here

View Source
const (
	STOREPEERDIR    = TINZENITEDIR + "/" + ORGDIR + "/" + PEERSDIR
	STORETOXDUMPDIR = TINZENITEDIR + "/" + LOCALDIR
	STOREAUTHDIR    = TINZENITEDIR + "/" + ORGDIR
	STOREMODELDIR   = TINZENITEDIR + "/" + LOCALDIR
)

Special path sets here. Mostly used to make writing to sub directories easier.

TODO aren't these all for core? Then move them there.

View Source
const (
	/*OpUnknown operation.*/
	OpUnknown = iota
	/*OpCreate operation.*/
	OpCreate
	/*OpModify operation.*/
	OpModify
	/*OpRemove operation.*/
	OpRemove
)
View Source
const IDMODEL = "MODEL"

IDMODEL is the model identification used to differentiate models from files.

View Source
const TINDIRIGNORE = "# DO NOT MODIFY!\n/" + LOCALDIR + "\n/" +
	TEMPDIR + "\n/" + RECEIVINGDIR + "\n/" + SENDINGDIR

.tinignore content for .tinzenite directory

Variables

View Source
var (
	ErrIllegalParameters = errors.New("illegal parameters given")
	ErrUnsupported       = errors.New("feature currently unsupported")
	ErrIsTinzenite       = errors.New("already a Tinzenite directory")
	ErrNotTinzenite      = errors.New("path is not valid Tinzenite directory")
	ErrNoTinIgnore       = errors.New("no .tinignore file found")
	ErrUntracked         = errors.New("object is not tracked in the model")
	ErrNilInternalState  = errors.New("internal state has illegal NIL values")
	ErrConflict          = errors.New("conflict, can not apply")
	ErrIllegalFileState  = errors.New("illegal file state detected")
)

Errors of Tinzenite.

Functions

func Contains

func Contains(s []string, value string) bool

Contains check whether the string slice contains the given string value.

func ContentHash

func ContentHash(path string) (string, error)

ContentHash generates the hash of the content of the given file at path.

func Difference

func Difference(start, target map[string]bool) (created, modified, removed []string)

Difference takes the starting and target path lists and returns the operations required for each path to get from the starting to the target maps.

NOTE: Extra care has been taken that Difference does NOT modify the passed map references.

func DirectoryExists

func DirectoryExists(path string) (bool, error)

DirectoryExists checks whether a directory at that location exists.

func FileExists

func FileExists(path string) (bool, error)

FileExists checks whether a file at that location exists.

func GetInt

func GetInt(request string) int

GetInt poses a request to the user and returns his entry as an integer.

func GetString

func GetString(request string) string

GetString poses a request to the user and returns his entry as a string.

func IsDirectoryEmpty

func IsDirectoryEmpty(path string) (bool, error)

IsDirectoryEmpty checks whether the given directory is empty.

func IsEncrypted

func IsEncrypted(dirpath string) bool

IsEncrypted checks whether a given path is indeed a valid directory for an encrypted peer.

TODO detect incomplete dir (no connected peers, etc) or write a validate method

func IsTinzenite

func IsTinzenite(dirpath string) bool

IsTinzenite checks whether a given path is indeed a valid directory

TODO detect incomplete dir (no connected peers, etc) or write a validate method

func LoadPeers

func LoadPeers(root string) (map[string]*Peer, error)

LoadPeers loads all peers for the given tinzenite root path.

func MakeDirectories

func MakeDirectories(root string, subdirs ...string) error

MakeDirectories creates a number of directories in the given root path. Useful if a complete directory tree has to be built at once.

func MakeDirectory

func MakeDirectory(path string) error

MakeDirectory creates the path.

func MakeEncryptedDir

func MakeEncryptedDir(root string) error

MakeEncryptedDir builds the directory structure for an encrypted peer. The given path is the path to the directory.

func MakeTinzeniteDir

func MakeTinzeniteDir(root string) error

MakeTinzeniteDir creates the directory structure for the .tinzenite directory including the .tinignore file required for it. The given path is the path to the directory (NOT the .TINZENITEDIR!).

func NewIdentifier

func NewIdentifier() (string, error)

NewIdentifier creates a new random hash that is intended as identification strings for all manner of different objects. Length is IDMAXLENGTH.

func ObjectExists

func ObjectExists(path string) (bool, error)

ObjectExists combines FileExists and DirectoryExists.

func ReadDirectoryList

func ReadDirectoryList() ([]string, error)

ReadDirectoryList reads all registered Tinzenite directories in the system. May return an empty listing if none found!

func RemoveDirContents

func RemoveDirContents(path string) error

RemoveDirContents removes all files within the given path, leaving the directory as is.

func RemoveDotTinzenite

func RemoveDotTinzenite(path string) error

RemoveDotTinzenite directory. Specifically leaves all user files but removes all Tinzenite specific items.

func SortString

func SortString(list []string) []string

SortString sorts an array of strings representing paths by length.

func WriteDirectoryList

func WriteDirectoryList(path string) error

WriteDirectoryList adds the given path to the DIRECTORYLIST file. Will try to avoid writing the same path multiple times.

Types

type Answer

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

Answer is a possible requested answer to a question.

type AuthenticationMessage

type AuthenticationMessage struct {
	Type      MsgType
	Encrypted []byte
}

AuthenticationMessage is the message used to authenticate trusted peers.

func CreateAuthenticationMessage

func CreateAuthenticationMessage(encrypted []byte) AuthenticationMessage

CreateAuthenticationMessage is a convenience method for building an instance of the message.

func (*AuthenticationMessage) JSON

func (am *AuthenticationMessage) JSON() string

JSON representation of this message.

func (*AuthenticationMessage) String

func (am *AuthenticationMessage) String() string

type Cmd

type Cmd int

Cmd is the enum for which operation the program should execute. Satisfies the Value interface so that it can be used in flag.

const (
	/*CmdNone is the default empty command.*/
	CmdNone Cmd = iota
	/*CmdCreate is the create command.*/
	CmdCreate
	/*CmdLoad is the load command.*/
	CmdLoad
	/*CmdBootstrap is the bootstrap command.*/
	CmdBootstrap
)

func CmdParse

func CmdParse(value string) Cmd

CmdParse parses a string to cmd. If illegal or can not be matched will simply return cmdNone.

func (Cmd) String

func (c Cmd) String() string

type Communication

type Communication int

Communication is an enumeration for the available communication methods of Tinzenite peers.

const (
	/*CmNone method.*/
	CmNone Communication = iota
	/*CmTox protocol.*/
	CmTox
)

func (Communication) String

func (cm Communication) String() string

type LockAction

type LockAction int

LockAction defines what action a LockMessage is.

const (
	/*LoNone is the default empty action.*/
	LoNone LockAction = iota
	/*LoRequest is a request for a lock.*/
	LoRequest
	/*LoRelease is a release request for a lock.*/
	LoRelease
	/*LoAccept is used to notify of a successful previous operation.*/
	LoAccept
)

func (*LockAction) MarshalJSON

func (lock *LockAction) MarshalJSON() ([]byte, error)

MarshalJSON overrides json.Marshal for this type.

func (LockAction) String

func (lock LockAction) String() string

func (*LockAction) UnmarshalJSON

func (lock *LockAction) UnmarshalJSON(data []byte) error

UnmarshalJSON overrides json.Unmarshal for this type.

type LockMessage

type LockMessage struct {
	Type   MsgType
	Action LockAction
}

LockMessage is the message used to lock an encrypted peer.

func CreateLockMessage

func CreateLockMessage(action LockAction) LockMessage

CreateLockMessage is a convenience method for building an instance of the message.

func (*LockMessage) JSON

func (lm *LockMessage) JSON() string

JSON representation of this message.

func (*LockMessage) String

func (lm *LockMessage) String() string

type Message

type Message struct {
	Type MsgType
}

Message is a base type for only reading out the operation to define the message type.

type MsgType

type MsgType int

MsgType is used to define a message type.

const (
	/*MsgNone default.*/
	MsgNone MsgType = iota
	/*MsgUpdate is an UpdateMessage.*/
	MsgUpdate
	/*MsgRequest is a RequestMessage.*/
	MsgRequest
	/*MsgNotify is a NotifyMessage.*/
	MsgNotify
	/*MsgLock is a LockMessage.*/
	MsgLock
	/*MsgPush is a PushMessage.*/
	MsgPush
	/*MsgChallenge is a ChallengeMessage.*/
	MsgChallenge
)

func (*MsgType) MarshalJSON

func (msg *MsgType) MarshalJSON() ([]byte, error)

MarshalJSON overrides json.Marshal for this type.

func (MsgType) String

func (msg MsgType) String() string

func (*MsgType) UnmarshalJSON

func (msg *MsgType) UnmarshalJSON(data []byte) error

UnmarshalJSON overrides json.Unmarshal for this type.

type NotifyMessage

type NotifyMessage struct {
	Type           MsgType
	Notify         NotifyType
	Identification string
	ObjType        ObjectType
}

NotifyMessage is used to notify another peer of special cases.

func CreateNotifyMessage

func CreateNotifyMessage(notify NotifyType, identification string, objType ObjectType) NotifyMessage

CreateNotifyMessage is a convenience method for building an instance of the message.

func (*NotifyMessage) JSON

func (nm *NotifyMessage) JSON() string

JSON representation of this message.

func (*NotifyMessage) String

func (nm *NotifyMessage) String() string

type NotifyType

type NotifyType int

NotifyType defines what notify action a NotifyMessage is to be.

const (
	/*NoNone is the default empty notify.*/
	NoNone NotifyType = iota
	/*NoRemoved is the removed notification.*/
	NoRemoved
	/*NoMissing is the missing notification.*/
	NoMissing
)

func (*NotifyType) MarshalJSON

func (n *NotifyType) MarshalJSON() ([]byte, error)

MarshalJSON overrides json.Marshal for this type.

func (NotifyType) String

func (n NotifyType) String() string

func (*NotifyType) UnmarshalJSON

func (n *NotifyType) UnmarshalJSON(data []byte) error

UnmarshalJSON overrides json.Unmarshal for this type.

type ObjectInfo

type ObjectInfo struct {
	Directory      bool
	Identification string
	Name           string
	Path           string
	Shadow         bool
	Version        Version
	Content        string        `json:",omitempty"`
	Objects        []*ObjectInfo `json:",omitempty"`
}

ObjectInfo represents the in model object fully.

func CreateObjectInfo

func CreateObjectInfo(root string, subpath string, selfid string) (*ObjectInfo, error)

CreateObjectInfo is a TEST function for creating an object for the specified parameters.

func (*ObjectInfo) Equal

func (o *ObjectInfo) Equal(that *ObjectInfo) bool

Equal checks wether the given pointer points to the same object based on pointer and identification. NOTE: Does not compare any other properties!

func (*ObjectInfo) ForEach

func (o *ObjectInfo) ForEach(f onEach)

ForEach is a helper function that applies the given function to the object and all its sub Objects.

func (*ObjectInfo) JSON

func (o *ObjectInfo) JSON() string

JSON returns a json representation of this object.

func (*ObjectInfo) String

func (o *ObjectInfo) String() string

type ObjectType

type ObjectType int

ObjectType defines the type of Request or Push.

const (
	/*OtNone is default empty request.*/
	OtNone ObjectType = iota
	/*OtObject requests an object.*/
	OtObject
	/*OtModel requests the model.*/
	OtModel
	/*OtPeer requests the connected peers peer file.*/
	OtPeer
	/*OtAuth requests the authentication file.*/
	OtAuth
)

func (*ObjectType) MarshalJSON

func (ot *ObjectType) MarshalJSON() ([]byte, error)

MarshalJSON overrides json.Marshal for this type.

func (ObjectType) String

func (ot ObjectType) String() string

func (*ObjectType) UnmarshalJSON

func (ot *ObjectType) UnmarshalJSON(data []byte) error

UnmarshalJSON overrides json.Unmarshal for this type.

type Operation

type Operation int

Operation is the enumeration for the possible protocol operations.

func (*Operation) MarshalJSON

func (op *Operation) MarshalJSON() ([]byte, error)

MarshalJSON overrides json.Marshal for this type.

func (Operation) String

func (op Operation) String() string

func (*Operation) UnmarshalJSON

func (op *Operation) UnmarshalJSON(data []byte) error

UnmarshalJSON overrides json.Unmarshal for this type.

type Peer

type Peer struct {
	Name           string        // user defined name for the peer
	Address        string        // tox address of the peer
	Protocol       Communication // for now always Tox
	Trusted        bool          // if trusted peer (meaning it must satisfy a challenge)
	Identification string        // internal ID of peer
	// contains filtered or unexported fields
}

Peer is the communication representation of a Tinzenite peer.

func CreatePeer

func CreatePeer(name string, address string, trusted bool) (*Peer, error)

CreatePeer returns a peer object for the given parameters.

func (*Peer) IsAuthenticated

func (p *Peer) IsAuthenticated() bool

IsAuthenticated returns the set value whether the Peer has been set as authenticated.

func (*Peer) IsLocked

func (p *Peer) IsLocked() bool

IsLocked returns the value of p.locked.

func (*Peer) SetAuthenticated

func (p *Peer) SetAuthenticated(value bool)

SetAuthenticated allows to set whether a peer has been authenticated.

func (*Peer) SetLocked

func (p *Peer) SetLocked(value bool)

SetLocked sets the value of p.locked.

func (*Peer) StoreTo

func (p *Peer) StoreTo(path string) error

StoreTo the given path a JSON representation of peer.

type PushMessage

type PushMessage struct {
	Type           MsgType
	Identification string
	ObjType        ObjectType
}

PushMessage is the message used to notify an encrypted peer of an incomming file transfer.

func CreatePushMessage

func CreatePushMessage(identification string, ot ObjectType) PushMessage

CreatePushMessage is a convenience method for building an instance of the message.

func (*PushMessage) JSON

func (pm *PushMessage) JSON() string

JSON representation of this message.

func (*PushMessage) String

func (pm *PushMessage) String() string

type Question

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

Question is a struct for variable user input.

func CreateQuestion

func CreateQuestion(questionText string) *Question

CreateQuestion creates a new question object with the given question. Per default the answers are not case sensitive.

func CreateYesNo

func CreateYesNo(questionText string) *Question

CreateYesNo creates a question with predeclared Yes and No answer options. The Option for No is negative, for Yes positive.

func (*Question) Ask

func (q *Question) Ask() int

Ask the question and returns the option value of the chosen answer.

func (*Question) CreateAnswer

func (q *Question) CreateAnswer(option int, valid ...string)

CreateAnswer creates an answer for the given question.

type RelativePath

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

RelativePath implements a path consisting of a base path and any subpath that lies beneath it.

func CreatePath

func CreatePath(root string, subpath string) *RelativePath

CreatePath creates a path directly with a subpath selected. Note that the subpath is NOT checked if it is relative or absolute: to do that use CreatePathRoot directly followed by Apply, which will check.

func CreatePathRoot

func CreatePathRoot(root string) *RelativePath

CreatePathRoot creates a RelativePath with the given root path.

func (*RelativePath) Apply

func (r *RelativePath) Apply(path string) *RelativePath

Apply does two different things depending on the path given. If path begins with "/" it is considered an absolute path and must match the root of the calling path. Otherwise the path is applied as a new sub path, replacing the old value.

func (*RelativePath) AtRoot

func (r *RelativePath) AtRoot() bool

AtRoot signals whether the path has any sub path.

func (*RelativePath) Depth

func (r *RelativePath) Depth() int

Depth is the amount of elements contained in the full path.

func (*RelativePath) FullPath

func (r *RelativePath) FullPath() string

FullPath returns the full path of the path.

func (*RelativePath) LastElement

func (r *RelativePath) LastElement() string

LastElement returns the last element of the complete path.

func (*RelativePath) RenameLastElement

func (r *RelativePath) RenameLastElement(value string) *RelativePath

RenameLastElement overwrites the last element completely with the given value and returns the corresponding new RelativePath.

func (*RelativePath) RootPath

func (r *RelativePath) RootPath() string

RootPath returns the root path.

func (*RelativePath) String

func (r *RelativePath) String() string

func (*RelativePath) SubPath

func (r *RelativePath) SubPath() string

SubPath returns the current sub path.

func (*RelativePath) Up

func (r *RelativePath) Up() *RelativePath

Up removes the last element from the path, up to the root path (and no further!).

type RequestMessage

type RequestMessage struct {
	Type           MsgType
	ObjType        ObjectType
	Identification string
}

RequestMessage is used to trigger the sending of messages or files from other peers.

func CreateRequestMessage

func CreateRequestMessage(ot ObjectType, identification string) RequestMessage

CreateRequestMessage is a convenience method for building an instance of the message.

func (*RequestMessage) JSON

func (rm *RequestMessage) JSON() string

JSON representation of this message.

func (*RequestMessage) String

func (rm *RequestMessage) String() string

type Sortable

type Sortable []*ObjectInfo

Sortable allows sorting Objectinfos by path.

func (Sortable) Len

func (s Sortable) Len() int

func (Sortable) Less

func (s Sortable) Less(i, j int) bool

func (Sortable) Swap

func (s Sortable) Swap(i, j int)

type SortableString

type SortableString []string

SortableString is a string slice that can be sorted by length.

func (SortableString) Len

func (s SortableString) Len() int

func (SortableString) Less

func (s SortableString) Less(i, j int) bool

func (SortableString) Swap

func (s SortableString) Swap(i, j int)

type SortableUpdateMessage

type SortableUpdateMessage []*UpdateMessage

SortableUpdateMessage allows the sorting of UpdateMessages

func (SortableUpdateMessage) Len

func (s SortableUpdateMessage) Len() int

func (SortableUpdateMessage) Less

func (s SortableUpdateMessage) Less(i, j int) bool

func (SortableUpdateMessage) Swap

func (s SortableUpdateMessage) Swap(i, j int)

type ToxPeerDump

type ToxPeerDump struct {
	SelfPeer *Peer
	ToxData  []byte
}

ToxPeerDump stores the self peer information along with the tox binary data required for it to work.

func LoadToxDumpFrom

func LoadToxDumpFrom(path string) (*ToxPeerDump, error)

LoadToxDumpFrom loads the toxPeerDump file from the given path.

func (*ToxPeerDump) StoreTo

func (t *ToxPeerDump) StoreTo(path string) error

StoreTo the toxPeerDump to the given path.

type UpdateMessage

type UpdateMessage struct {
	Type      MsgType
	Operation Operation
	Object    ObjectInfo
}

UpdateMessage contains the relevant information for notifiying peers of updates.

func CreateUpdateMessage

func CreateUpdateMessage(op Operation, obj ObjectInfo) UpdateMessage

CreateUpdateMessage is a convenience method for building an instance of the message.

func (*UpdateMessage) JSON

func (um *UpdateMessage) JSON() string

JSON representation of this message.

func (*UpdateMessage) String

func (um *UpdateMessage) String() string

type Version

type Version map[string]int

Version implements a vector clock. The value of zero should never turn up.

func CreateVersion

func CreateVersion() Version

CreateVersion returns a new Version object

func (Version) Equal

func (v Version) Equal(that Version) bool

Equal checks whether the version per id match perfectly between the two.

func (Version) Includes

func (v Version) Includes(that Version) bool

Includes returns true if the version contains all knowledge and version of the given version. Method is intended to allow ordering of versions.

func (Version) Increase

func (v Version) Increase(selfid string)

Increase the version for the given peer based on the already existing versions.

func (Version) IsEmpty

func (v Version) IsEmpty() bool

IsEmpty returns whether any entries have been made in this version.

func (Version) Max

func (v Version) Max() int

Max version number from all listed peers.

func (Version) String

func (v Version) String() string

String representation of version.

func (Version) Valid

func (v Version) Valid(that Version, selfid string) bool

Valid checks whether the version can be automerged or whether manual resolution is required.

NOTE: The selfid value is a special case and ignored, so long as its knowledge is correct. The value is however updated if the other version has a higher value.

Jump to

Keyboard shortcuts

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