Documentation

Index

Constants

View Source
const (
	// ExportStateExporting is the state used when the export document is being
	// created.
	ExportStateExporting = "exporting"
	// ExportStateDone is used when the export document is finished, without
	// error.
	ExportStateDone = "done"
	// ExportStateError is used when the export document is finshed with error.
	ExportStateError = "error"
)
View Source
const (
	// ExportDataDir is the directory for storing the documents from CouchDB in
	// the export archive.
	ExportDataDir = "My Cozy/Data"
	// ExportFilesDir is the directory for storing the content of the files in
	// the export archive.
	ExportFilesDir = "My Cozy/Files"
	// ExportVersionsDir is the directory for storing the content of the old
	// versions of the files in the export archive.
	ExportVersionsDir = "My Cozy/Versions"
)
View Source
const (
	// MoveScope is the scope requested for a move (when we don't know yet if
	// the cozy will be the source or the target).
	MoveScope = consts.ExportsRequests + " " + consts.Imports
	// SourceClientID is the fake OAuth client ID used for some move endpoints.
	SourceClientID = "move"
)

Variables

View Source
var (
	// ErrExportNotFound is used when a export document could not be found
	ErrExportNotFound = echo.NewHTTPError(http.StatusNotFound, "exports: not found")
	// ErrExportExpired is used when the export document has expired along with
	// its associated data.
	ErrExportExpired = echo.NewHTTPError(http.StatusNotFound, "exports: has expired")
	// ErrMACInvalid is used when the given MAC is not valid.
	ErrMACInvalid = echo.NewHTTPError(http.StatusUnauthorized, "exports: invalid mac")
	// ErrExportConflict is used when an export is already being perfomed.
	ErrExportConflict = echo.NewHTTPError(http.StatusConflict, "export: an archive is already being created")
	// ErrExportDoesNotContainIndex is used when we could not find the index data
	// in the archive.
	ErrExportDoesNotContainIndex = echo.NewHTTPError(http.StatusBadRequest, "export: archive does not contain index data")
	// ErrExportInvalidCursor is used when the given index cursor is invalid
	ErrExportInvalidCursor = echo.NewHTTPError(http.StatusBadRequest, "export: cursor is invalid")
	// ErrNotEnoughSpace is used when the quota is too small to import the files
	ErrNotEnoughSpace = echo.NewHTTPError(http.StatusRequestEntityTooLarge, "import: not enough disk space")
)

Functions

func Abort

func Abort(inst *instance.Instance, otherURL, token string)

Abort will call the /move/abort endpoint on the other instance to unblock it after a failed export or import during a move.

func CallFinalize

func CallFinalize(inst *instance.Instance, otherURL, token string, vault bool)

CallFinalize will call the /move/finalize endpoint on the other instance to unblock it after a successful move.

func CheckImport

func CheckImport(inst *instance.Instance, settingsURL string) error

CheckImport returns an error if an exports cannot be found at the given URL, or if the instance has not enough disk space to import the files.

func CreateRequestClient

func CreateRequestClient(inst *instance.Instance) (*oauth.Client, error)

CreateRequestClient creates an OAuth client that can be used for move requests.

func ExportCopyData

func ExportCopyData(w io.Writer, inst *instance.Instance, exportDoc *ExportDoc, archiver Archiver, cursor Cursor) error

ExportCopyData does an HTTP copy of a part of the file indexes.

func Finalize

func Finalize(inst *instance.Instance, subdomainType string) error

Finalize makes the last steps on the source Cozy after the data has been successfully imported: - stop the konnectors - warn the OAuth clients - unblock the instance

func Import

func Import(inst *instance.Instance, options ImportOptions) ([]string, error)

Import downloads the documents and files from an export and add them to the local instance. It returns the list of slugs for apps/konnectors that have not been installed.

func ImportIsFinished

func ImportIsFinished(inst *instance.Instance) bool

ImportIsFinished returns true unless an import is running

func NotifySharings

func NotifySharings(inst *instance.Instance) error

NotifySharings will notify other instances with a common sharing that this instance has moved, and will tell them to the new URL to use for the sharing.

func ScheduleImport

func ScheduleImport(inst *instance.Instance, options ImportOptions) error

ScheduleImport blocks the instance and adds a job to import the data from the given URL.

func SendExportFailureMail

func SendExportFailureMail(inst *instance.Instance) error

SendExportFailureMail sends an email to the user when the export has failed.

func SendImportDoneMail

func SendImportDoneMail(inst *instance.Instance, status Status, notInstalled []string) error

SendImportDoneMail sends an email to the user when the import is done. The content will depend if the import has been successful or not, and if it was a move or just a import of a tarball.

Types

type Archiver

type Archiver interface {
	OpenArchive(inst *instance.Instance, exportDoc *ExportDoc) (io.ReadCloser, error)
	CreateArchive(exportDoc *ExportDoc) (io.WriteCloser, error)
	RemoveArchives(exportDocs []*ExportDoc) error
}

Archiver is an interface describing an abstraction for storing archived data.

func SystemArchiver

func SystemArchiver() Archiver

SystemArchiver returns the global system archiver, corresponding to the user's configuration.

type Cursor

type Cursor struct {
	Number  int
	Doctype string
	ID      string
}

Cursor can be used to know which files must be included in a part.

func ParseCursor

func ParseCursor(exportDoc *ExportDoc, cursorStr string) (Cursor, error)

ParseCursor checks that the given cursor is part of our pre-defined list of cursors.

func (Cursor) String

func (c Cursor) String() string

String returns a string representation of the cursor.

type ExportDoc

type ExportDoc struct {
	DocID     string `json:"_id,omitempty"`
	DocRev    string `json:"_rev,omitempty"`
	Domain    string `json:"domain"`
	PartsSize int64  `json:"parts_size,omitempty"`

	PartsCursors     []string      `json:"parts_cursors"`
	WithDoctypes     []string      `json:"with_doctypes,omitempty"`
	State            string        `json:"state"`
	CreatedAt        time.Time     `json:"created_at"`
	ExpiresAt        time.Time     `json:"expires_at"`
	TotalSize        int64         `json:"total_size,omitempty"`
	CreationDuration time.Duration `json:"creation_duration,omitempty"`
	Error            string        `json:"error,omitempty"`
}

ExportDoc is a documents storing the metadata of an export.

func CreateExport

func CreateExport(i *instance.Instance, opts ExportOptions, archiver Archiver) (*ExportDoc, error)

CreateExport is used to create a tarball with the data from an instance.

Note: the tarball is a .tar.gz and not a .zip to allow streaming from Swift to the stack, and from the stack to the client, as .tar.gz can be read sequentially and reading a .zip need to seek.

func GetExport

func GetExport(inst *instance.Instance, mac []byte) (*ExportDoc, error)

GetExport returns an Export document associated with the given instance and with the given MAC message.

func GetExports

func GetExports(domain string) ([]*ExportDoc, error)

GetExports returns the list of exported documents.

func (*ExportDoc) AcceptDoctype

func (e *ExportDoc) AcceptDoctype(doctype string) bool

AcceptDoctype returns true if the documents of the given doctype must be exported.

func (*ExportDoc) CleanPreviousExports

func (e *ExportDoc) CleanPreviousExports(archiver Archiver) error

CleanPreviousExports ensures that we have no old exports (or clean them).

func (*ExportDoc) Clone

func (e *ExportDoc) Clone() couchdb.Doc

Clone implements the couchdb.Doc interface

func (*ExportDoc) DocType

func (e *ExportDoc) DocType() string

DocType implements the couchdb.Doc interface

func (e *ExportDoc) GenerateLink(i *instance.Instance) string

GenerateLink generates a link to download the export with a MAC.

func (*ExportDoc) HasExpired

func (e *ExportDoc) HasExpired() bool

HasExpired returns whether or not the export document has expired.

func (*ExportDoc) ID

func (e *ExportDoc) ID() string

ID implements the couchdb.Doc interface

func (*ExportDoc) Included

func (e *ExportDoc) Included() []jsonapi.Object

Included implements the jsonapi.Object interface

func (e *ExportDoc) Links() *jsonapi.LinksList

Links implements the jsonapi.Object interface

func (*ExportDoc) MarksAsFinished

func (e *ExportDoc) MarksAsFinished(i *instance.Instance, size int64, err error) error

MarksAsFinished saves the document when the export is done.

func (*ExportDoc) NotifyTarget

func (e *ExportDoc) NotifyTarget(inst *instance.Instance, to *MoveToOptions, token string, ignoreVault bool) error

NotifyTarget sends an HTTP request to the target so that it can start importing the tarballs.

func (*ExportDoc) Relationships

func (e *ExportDoc) Relationships() jsonapi.RelationshipMap

Relationships implements the jsonapi.Object interface

func (*ExportDoc) Rev

func (e *ExportDoc) Rev() string

Rev implements the couchdb.Doc interface

func (*ExportDoc) SendExportMail

func (e *ExportDoc) SendExportMail(inst *instance.Instance) error

SendExportMail sends a mail to the user with a link where they can download the export tarballs.

func (*ExportDoc) SetID

func (e *ExportDoc) SetID(id string)

SetID implements the couchdb.Doc interface

func (*ExportDoc) SetRev

func (e *ExportDoc) SetRev(rev string)

SetRev implements the couchdb.Doc interface

type ExportOptions

type ExportOptions struct {
	PartsSize        int64          `json:"parts_size"`
	MaxAge           time.Duration  `json:"max_age"`
	WithDoctypes     []string       `json:"with_doctypes,omitempty"`
	ContextualDomain string         `json:"contextual_domain,omitempty"`
	TokenSource      string         `json:"token_source,omitempty"`
	IgnoreVault      bool           `json:"ignore_vault,omitempty"`
	MoveTo           *MoveToOptions `json:"move_to,omitempty"`
}

ExportOptions contains the options for launching the export worker.

type FromOptions

type FromOptions struct {
	URL   string `json:"url"`
	Token string `json:"token"`
}

FromOptions is used when the import finishes to notify the source Cozy.

type ImportOptions

type ImportOptions struct {
	SettingsURL string       `json:"url,omitempty"`
	ManifestURL string       `json:"manifest_url,omitempty"`
	Vault       bool         `json:"valut,omitempty"`
	MoveFrom    *FromOptions `json:"move_from,omitempty"`
}

ImportOptions contains the options for launching the import worker.

type MoveToOptions

type MoveToOptions struct {
	URL          string `json:"url"`
	Token        string `json:"token"`
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
}

MoveToOptions is used when the export must be sent to another Cozy.

func (*MoveToOptions) ImportsURL

func (m *MoveToOptions) ImportsURL() string

ImportsURL returns the URL on the target for sending the download link to the export tarballs.

type Request

type Request struct {
	IgnoreVault bool               `json:"ignore_vault,omitempty"`
	SourceCreds RequestCredentials `json:"source_credentials"`
	TargetCreds RequestCredentials `json:"target_credentials"`
	Target      string             `json:"target"`
	Link        string             `json:"-"`
}

Request is a struct for confirming a move to another Cozy.

func CreateRequest

func CreateRequest(inst *instance.Instance, params url.Values) (*Request, error)

CreateRequest checks if the parameters are OK for moving, and if yes, it will persist them and return a link that can be used to confirm the move.

func StartMove

func StartMove(inst *instance.Instance, secret string) (*Request, error)

StartMove checks that the secret is known, sends a request to the other Cozy to block it during the move, and pushs a job for the export.

func (*Request) ImportingURL

func (r *Request) ImportingURL() string

ImportingURL returns the URL on the target for the page to wait until the move is done.

func (*Request) TargetHost

func (r *Request) TargetHost() string

TargetHost returns the host part of the target instance address.

type RequestCredentials

type RequestCredentials struct {
	Token        string `json:"token"`
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
}

RequestCredentials is struct for OAuth credentials (access_token, client_id and client_secret).

type Status

type Status int

Status is a type for the status of an import.

const (
	// StatusMoveSuccess is the status when a move has been successful.
	StatusMoveSuccess Status = iota + 1
	// StatusImportSuccess is the status when a import of a tarball has been
	// successful.
	StatusImportSuccess
	// StatusFailure is the status when the import/move has failed.
	StatusFailure
)

type Store

type Store interface {
	GetRequest(db prefixer.Prefixer, key string) (*Request, error)
	SaveRequest(db prefixer.Prefixer, req *Request) (string, error)
	SetAllowDeleteAccounts(db prefixer.Prefixer) error
	ClearAllowDeleteAccounts(db prefixer.Prefixer) error
	AllowDeleteAccounts(db prefixer.Prefixer) bool
}

Store is essentially an object to store and retrieve move requests

func GetStore

func GetStore() Store

GetStore returns the store for temporary move objects.