a geicko-2 based round robin ranking system designed to test c++ battleship submissions battleship.dunkirk.sh
at main 4.0 kB view raw
1package sftp 2 3import ( 4 "fmt" 5 "io" 6 "io/fs" 7 "os" 8 "path" 9 "path/filepath" 10 "time" 11 12 "golang.org/x/sys/windows" 13) 14 15func (s *Server) toLocalPath(p string) string { 16 if s.workDir != "" && !path.IsAbs(p) { 17 p = path.Join(s.workDir, p) 18 } 19 20 lp := filepath.FromSlash(p) 21 22 if path.IsAbs(p) { // starts with '/' 23 if len(p) == 1 && s.winRoot { 24 return `\\.\` // for openfile 25 } 26 27 tmp := lp 28 for len(tmp) > 0 && tmp[0] == '\\' { 29 tmp = tmp[1:] 30 } 31 32 if filepath.IsAbs(tmp) { 33 // If the FromSlash without any starting slashes is absolute, 34 // then we have a filepath encoded with a prefix '/'. 35 // e.g. "/C:/Windows" to "C:\\Windows" 36 return tmp 37 } 38 39 tmp += "\\" 40 41 if filepath.IsAbs(tmp) { 42 // If the FromSlash without any starting slashes but with extra end slash is absolute, 43 // then we have a filepath encoded with a prefix '/' and a dropped '/' at the end. 44 // e.g. "/C:" to "C:\\" 45 return tmp 46 } 47 48 if s.winRoot { 49 // Make it so that "/Windows" is not found, and "/c:/Windows" has to be used 50 return `\\.\` + tmp 51 } 52 } 53 54 return lp 55} 56 57func bitsToDrives(bitmap uint32) []string { 58 var drive rune = 'a' 59 var drives []string 60 61 for bitmap != 0 && drive <= 'z' { 62 if bitmap&1 == 1 { 63 drives = append(drives, string(drive)+":") 64 } 65 drive++ 66 bitmap >>= 1 67 } 68 69 return drives 70} 71 72func getDrives() ([]string, error) { 73 mask, err := windows.GetLogicalDrives() 74 if err != nil { 75 return nil, fmt.Errorf("GetLogicalDrives: %w", err) 76 } 77 return bitsToDrives(mask), nil 78} 79 80type driveInfo struct { 81 fs.FileInfo 82 name string 83} 84 85func (i *driveInfo) Name() string { 86 return i.name // since the Name() returned from a os.Stat("C:\\") is "\\" 87} 88 89type winRoot struct { 90 drives []string 91} 92 93func newWinRoot() (*winRoot, error) { 94 drives, err := getDrives() 95 if err != nil { 96 return nil, err 97 } 98 return &winRoot{ 99 drives: drives, 100 }, nil 101} 102 103func (f *winRoot) Readdir(n int) ([]os.FileInfo, error) { 104 drives := f.drives 105 if n > 0 && len(drives) > n { 106 drives = drives[:n] 107 } 108 f.drives = f.drives[len(drives):] 109 if len(drives) == 0 { 110 return nil, io.EOF 111 } 112 113 var infos []os.FileInfo 114 for _, drive := range drives { 115 fi, err := os.Stat(drive + `\`) 116 if err != nil { 117 return nil, err 118 } 119 120 di := &driveInfo{ 121 FileInfo: fi, 122 name: drive, 123 } 124 infos = append(infos, di) 125 } 126 127 return infos, nil 128} 129 130func (f *winRoot) Stat() (os.FileInfo, error) { 131 return rootFileInfo, nil 132} 133func (f *winRoot) ReadAt(b []byte, off int64) (int, error) { 134 return 0, os.ErrPermission 135} 136func (f *winRoot) WriteAt(b []byte, off int64) (int, error) { 137 return 0, os.ErrPermission 138} 139func (f *winRoot) Name() string { 140 return "/" 141} 142func (f *winRoot) Truncate(int64) error { 143 return os.ErrPermission 144} 145func (f *winRoot) Chmod(mode fs.FileMode) error { 146 return os.ErrPermission 147} 148func (f *winRoot) Chown(uid, gid int) error { 149 return os.ErrPermission 150} 151func (f *winRoot) Close() error { 152 f.drives = nil 153 return nil 154} 155 156func (s *Server) openfile(path string, flag int, mode fs.FileMode) (file, error) { 157 if path == `\\.\` && s.winRoot { 158 return newWinRoot() 159 } 160 return os.OpenFile(path, flag, mode) 161} 162 163type winRootFileInfo struct { 164 name string 165 modTime time.Time 166} 167 168func (w *winRootFileInfo) Name() string { return w.name } 169func (w *winRootFileInfo) Size() int64 { return 0 } 170func (w *winRootFileInfo) Mode() fs.FileMode { return fs.ModeDir | 0555 } // read+execute for all 171func (w *winRootFileInfo) ModTime() time.Time { return w.modTime } 172func (w *winRootFileInfo) IsDir() bool { return true } 173func (w *winRootFileInfo) Sys() interface{} { return nil } 174 175// Create a new root FileInfo 176var rootFileInfo = &winRootFileInfo{ 177 name: "/", 178 modTime: time.Now(), 179} 180 181func (s *Server) lstat(name string) (os.FileInfo, error) { 182 if name == `\\.\` && s.winRoot { 183 return rootFileInfo, nil 184 } 185 return os.Lstat(name) 186} 187 188func (s *Server) stat(name string) (os.FileInfo, error) { 189 if name == `\\.\` && s.winRoot { 190 return rootFileInfo, nil 191 } 192 return os.Stat(name) 193}