a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 3.8 kB view raw
1package sftp 2 3// ssh_FXP_ATTRS support 4// see https://filezilla-project.org/specs/draft-ietf-secsh-filexfer-02.txt#section-5 5 6import ( 7 "os" 8 "time" 9) 10 11const ( 12 sshFileXferAttrSize = 0x00000001 13 sshFileXferAttrUIDGID = 0x00000002 14 sshFileXferAttrPermissions = 0x00000004 15 sshFileXferAttrACmodTime = 0x00000008 16 sshFileXferAttrExtended = 0x80000000 17 18 sshFileXferAttrAll = sshFileXferAttrSize | sshFileXferAttrUIDGID | sshFileXferAttrPermissions | 19 sshFileXferAttrACmodTime | sshFileXferAttrExtended 20) 21 22// fileInfo is an artificial type designed to satisfy os.FileInfo. 23type fileInfo struct { 24 name string 25 stat *FileStat 26} 27 28// Name returns the base name of the file. 29func (fi *fileInfo) Name() string { return fi.name } 30 31// Size returns the length in bytes for regular files; system-dependent for others. 32func (fi *fileInfo) Size() int64 { return int64(fi.stat.Size) } 33 34// Mode returns file mode bits. 35func (fi *fileInfo) Mode() os.FileMode { return fi.stat.FileMode() } 36 37// ModTime returns the last modification time of the file. 38func (fi *fileInfo) ModTime() time.Time { return fi.stat.ModTime() } 39 40// IsDir returns true if the file is a directory. 41func (fi *fileInfo) IsDir() bool { return fi.Mode().IsDir() } 42 43func (fi *fileInfo) Sys() interface{} { return fi.stat } 44 45// FileStat holds the original unmarshalled values from a call to READDIR or 46// *STAT. It is exported for the purposes of accessing the raw values via 47// os.FileInfo.Sys(). It is also used server side to store the unmarshalled 48// values for SetStat. 49type FileStat struct { 50 Size uint64 51 Mode uint32 52 Mtime uint32 53 Atime uint32 54 UID uint32 55 GID uint32 56 Extended []StatExtended 57} 58 59// ModTime returns the Mtime SFTP file attribute converted to a time.Time 60func (fs *FileStat) ModTime() time.Time { 61 return time.Unix(int64(fs.Mtime), 0) 62} 63 64// AccessTime returns the Atime SFTP file attribute converted to a time.Time 65func (fs *FileStat) AccessTime() time.Time { 66 return time.Unix(int64(fs.Atime), 0) 67} 68 69// FileMode returns the Mode SFTP file attribute converted to an os.FileMode 70func (fs *FileStat) FileMode() os.FileMode { 71 return toFileMode(fs.Mode) 72} 73 74// StatExtended contains additional, extended information for a FileStat. 75type StatExtended struct { 76 ExtType string 77 ExtData string 78} 79 80func fileInfoFromStat(stat *FileStat, name string) os.FileInfo { 81 return &fileInfo{ 82 name: name, 83 stat: stat, 84 } 85} 86 87// FileInfoUidGid extends os.FileInfo and adds callbacks for Uid and Gid retrieval, 88// as an alternative to *syscall.Stat_t objects on unix systems. 89type FileInfoUidGid interface { 90 os.FileInfo 91 Uid() uint32 92 Gid() uint32 93} 94 95// FileInfoUidGid extends os.FileInfo and adds a callbacks for extended data retrieval. 96type FileInfoExtendedData interface { 97 os.FileInfo 98 Extended() []StatExtended 99} 100 101func fileStatFromInfo(fi os.FileInfo) (uint32, *FileStat) { 102 mtime := fi.ModTime().Unix() 103 atime := mtime 104 var flags uint32 = sshFileXferAttrSize | 105 sshFileXferAttrPermissions | 106 sshFileXferAttrACmodTime 107 108 fileStat := &FileStat{ 109 Size: uint64(fi.Size()), 110 Mode: fromFileMode(fi.Mode()), 111 Mtime: uint32(mtime), 112 Atime: uint32(atime), 113 } 114 115 // os specific file stat decoding 116 fileStatFromInfoOs(fi, &flags, fileStat) 117 118 // The call above will include the sshFileXferAttrUIDGID in case 119 // the os.FileInfo can be casted to *syscall.Stat_t on unix. 120 // If fi implements FileInfoUidGid, retrieve Uid, Gid from it instead. 121 if fiExt, ok := fi.(FileInfoUidGid); ok { 122 flags |= sshFileXferAttrUIDGID 123 fileStat.UID = fiExt.Uid() 124 fileStat.GID = fiExt.Gid() 125 } 126 127 // if fi implements FileInfoExtendedData, retrieve extended data from it 128 if fiExt, ok := fi.(FileInfoExtendedData); ok { 129 fileStat.Extended = fiExt.Extended() 130 if len(fileStat.Extended) > 0 { 131 flags |= sshFileXferAttrExtended 132 } 133 } 134 135 return flags, fileStat 136}