| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | package restic | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							|  |  |  | 	"fmt" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 22:17:37 +02:00
										 |  |  | 	"restic/errors" | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 20:29:54 +02:00
										 |  |  | // FileType is the type of a file in the backend. | 
					
						
							|  |  |  | type FileType string | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // These are the different data types a backend can store. | 
					
						
							|  |  |  | const ( | 
					
						
							|  |  |  | 	DataFile     FileType = "data" | 
					
						
							|  |  |  | 	KeyFile               = "key" | 
					
						
							|  |  |  | 	LockFile              = "lock" | 
					
						
							|  |  |  | 	SnapshotFile          = "snapshot" | 
					
						
							|  |  |  | 	IndexFile             = "index" | 
					
						
							|  |  |  | 	ConfigFile            = "config" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | // Handle is used to store and access data in a backend. | 
					
						
							|  |  |  | type Handle struct { | 
					
						
							| 
									
										
										
										
											2016-09-01 21:19:30 +02:00
										 |  |  | 	Type FileType | 
					
						
							|  |  |  | 	Name string | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func (h Handle) String() string { | 
					
						
							|  |  |  | 	name := h.Name | 
					
						
							|  |  |  | 	if len(name) > 10 { | 
					
						
							|  |  |  | 		name = name[:10] | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2016-09-01 21:19:30 +02:00
										 |  |  | 	return fmt.Sprintf("<%s/%s>", h.Type, name) | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Valid returns an error if h is not valid. | 
					
						
							|  |  |  | func (h Handle) Valid() error { | 
					
						
							| 
									
										
										
										
											2016-09-01 21:19:30 +02:00
										 |  |  | 	if h.Type == "" { | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | 		return errors.New("type is empty") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 21:19:30 +02:00
										 |  |  | 	switch h.Type { | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | 	case DataFile: | 
					
						
							|  |  |  | 	case KeyFile: | 
					
						
							|  |  |  | 	case LockFile: | 
					
						
							|  |  |  | 	case SnapshotFile: | 
					
						
							|  |  |  | 	case IndexFile: | 
					
						
							|  |  |  | 	case ConfigFile: | 
					
						
							|  |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2016-09-01 21:19:30 +02:00
										 |  |  | 		return errors.Errorf("invalid Type %q", h.Type) | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-09-01 21:19:30 +02:00
										 |  |  | 	if h.Type == ConfigFile { | 
					
						
							| 
									
										
										
										
											2016-08-31 19:10:10 +02:00
										 |  |  | 		return nil | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if h.Name == "" { | 
					
						
							|  |  |  | 		return errors.New("invalid Name") | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil | 
					
						
							|  |  |  | } |