| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | package sftp | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // Methods on the Request object to make working with the Flags bitmasks and
 | 
					
						
							|  |  |  | // Attr(ibutes) byte blob easier. Use Pflags() when working with an Open/Write
 | 
					
						
							|  |  |  | // request and AttrFlags() and Attributes() when working with SetStat requests.
 | 
					
						
							|  |  |  | import "os" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | // FileOpenFlags defines Open and Write Flags. Correlate directly with with os.OpenFile flags
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | // (https://golang.org/pkg/os/#pkg-constants).
 | 
					
						
							|  |  |  | type FileOpenFlags struct { | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | 	Read, Write, Append, Creat, Trunc, Excl bool | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | func newFileOpenFlags(flags uint32) FileOpenFlags { | 
					
						
							|  |  |  | 	return FileOpenFlags{ | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 		Read:   flags&sshFxfRead != 0, | 
					
						
							|  |  |  | 		Write:  flags&sshFxfWrite != 0, | 
					
						
							|  |  |  | 		Append: flags&sshFxfAppend != 0, | 
					
						
							|  |  |  | 		Creat:  flags&sshFxfCreat != 0, | 
					
						
							|  |  |  | 		Trunc:  flags&sshFxfTrunc != 0, | 
					
						
							|  |  |  | 		Excl:   flags&sshFxfExcl != 0, | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | // Pflags converts the bitmap/uint32 from SFTP Open packet pflag values,
 | 
					
						
							|  |  |  | // into a FileOpenFlags struct with booleans set for flags set in bitmap.
 | 
					
						
							|  |  |  | func (r *Request) Pflags() FileOpenFlags { | 
					
						
							|  |  |  | 	return newFileOpenFlags(r.Flags) | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | // FileAttrFlags that indicate whether SFTP file attributes were passed. When a flag is
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | // true the corresponding attribute should be available from the FileStat
 | 
					
						
							|  |  |  | // object returned by Attributes method. Used with SetStat.
 | 
					
						
							|  |  |  | type FileAttrFlags struct { | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | 	Size, UidGid, Permissions, Acmodtime bool | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | func newFileAttrFlags(flags uint32) FileAttrFlags { | 
					
						
							|  |  |  | 	return FileAttrFlags{ | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | 		Size:        (flags & sshFileXferAttrSize) != 0, | 
					
						
							|  |  |  | 		UidGid:      (flags & sshFileXferAttrUIDGID) != 0, | 
					
						
							|  |  |  | 		Permissions: (flags & sshFileXferAttrPermissions) != 0, | 
					
						
							|  |  |  | 		Acmodtime:   (flags & sshFileXferAttrACmodTime) != 0, | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | // AttrFlags returns a FileAttrFlags boolean struct based on the
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | // bitmap/uint32 file attribute flags from the SFTP packaet.
 | 
					
						
							|  |  |  | func (r *Request) AttrFlags() FileAttrFlags { | 
					
						
							|  |  |  | 	return newFileAttrFlags(r.Flags) | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | // FileMode returns the Mode SFTP file attributes wrapped as os.FileMode
 | 
					
						
							|  |  |  | func (a FileStat) FileMode() os.FileMode { | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | 	return os.FileMode(a.Mode) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-08-30 23:04:37 +08:00
										 |  |  | // Attributes parses file attributes byte blob and return them in a
 | 
					
						
							| 
									
										
										
										
											2018-05-12 05:15:09 +08:00
										 |  |  | // FileStat object.
 | 
					
						
							|  |  |  | func (r *Request) Attributes() *FileStat { | 
					
						
							|  |  |  | 	fs, _ := getFileStat(r.Flags, r.Attrs) | 
					
						
							|  |  |  | 	return fs | 
					
						
							| 
									
										
										
										
											2018-01-10 07:31:54 +08:00
										 |  |  | } |